You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
7.9 KiB

  1. #!/usr/bin/env bash
  2. # http://redsymbol.net/articles/unofficial-bash-strict-mode
  3. set -euo pipefail
  4. usage()
  5. {
  6. cat <<END
  7. deploy.sh: deploys the $app_name application to a Kubernetes cluster using Helm.
  8. Parameters:
  9. --aks-name <AKS cluster name>
  10. The name of the AKS cluster. Required when the registry (using the -r parameter) is set to "aks".
  11. --aks-rg <AKS resource group>
  12. The resource group for the AKS cluster. Required when the registry (using the -r parameter) is set to "aks".
  13. -b | --build-solution
  14. Force a solution build before deployment (default: false).
  15. -d | --dns <dns or ip address>
  16. Specifies the external DNS/ IP address of the Kubernetes cluster.
  17. When --use-local-k8s is specified the external DNS is automatically set to localhost.
  18. -h | --help
  19. Displays this help text and exits the script.
  20. -n | --app-name <the name of the app>
  21. Specifies the name of the application (default: eshop).
  22. -p | --docker-password <docker password>
  23. The Docker password used to logon to the custom registry, supplied using the -r parameter.
  24. -r | --registry <container registry>
  25. Specifies the container registry to use (required), e.g. myregistry.azurecr.io.
  26. --skip-clean
  27. Do not clean the Kubernetes cluster (default is to clean the cluster).
  28. --skip-image-build
  29. Do not build images (default is to build all images).
  30. --skip-image-push
  31. Do not upload images to the container registry (just run the Kubernetes deployment portion).
  32. Default is to push the images to the container registry.
  33. --skip-infrastructure
  34. Do not deploy infrastructure resources (like sql-data, no-sql or redis).
  35. This is useful for production environments where infrastructure is hosted outside the Kubernetes cluster.
  36. -t | --tag <docker image tag>
  37. The tag used for the newly created docker images. Default: newly created, date-based timestamp, with 1-minute resolution.
  38. -u | --docker-user <docker username>
  39. The Docker username used to logon to the custom registry, supplied using the -r parameter.
  40. --use-local-k8s
  41. Deploy to a locally installed Kubernetes (default: false).
  42. It is assumed that the Kubernetes cluster has been granted access to the container registry.
  43. If using AKS and ACR see link for more info:
  44. https://docs.microsoft.com/en-us/azure/container-registry/container-registry-auth-aks
  45. WARNING! THE SCRIPT WILL COMPLETELY DESTROY ALL DEPLOYMENTS AND SERVICES VISIBLE
  46. FROM THE CURRENT CONFIGURATION CONTEXT.
  47. It is recommended that you create a separate namespace and confguration context
  48. for the $app_name application, to isolate it from other applications on the cluster.
  49. For more information see https://kubernetes.io/docs/tasks/administer-cluster/namespaces/
  50. You can use namespace.yaml file (in the same directory) to create the namespace.
  51. END
  52. }
  53. app_name='eshop'
  54. aks_name=''
  55. aks_rg=''
  56. build_images='yes'
  57. clean='yes'
  58. build_solution=''
  59. container_registry=''
  60. docker_password=''
  61. docker_username=''
  62. dns=''
  63. image_tag=$(date '+%Y%m%d%H%M')
  64. push_images='yes'
  65. skip_infrastructure=''
  66. use_local_k8s=''
  67. while [[ $# -gt 0 ]]; do
  68. case "$1" in
  69. --aks-name )
  70. aks_name="$2"; shift 2;;
  71. --aks-rg )
  72. aks_rg="$2"; shift 2;;
  73. -b | --build-solution )
  74. build_solution='yes'; shift ;;
  75. -d | --dns )
  76. dns="$2"; shift 2;;
  77. -h | --help )
  78. usage; exit 1 ;;
  79. -n | --app-name )
  80. app_name="$2"; shift 2;;
  81. -p | --docker-password )
  82. docker_password="$2"; shift;;
  83. -r | --registry )
  84. container_registry="$2"; shift 2;;
  85. --skip-clean )
  86. clean=''; shift ;;
  87. --skip-image-build )
  88. build_images=''; shift ;;
  89. --skip-image-push )
  90. push_images=''; shift ;;
  91. --skip-infrastructure )
  92. skip_infrastructure='yes'; shift ;;
  93. -t | --tag )
  94. image_tag="$2"; shift 2;;
  95. -u | --docker-username )
  96. docker_username="$2"; shift 2;;
  97. --use-local-k8s )
  98. use_local_k8s='yes'; shift ;;
  99. *)
  100. echo "Unknown option $1"
  101. usage; exit 2 ;;
  102. esac
  103. done
  104. if [[ $build_solution ]]; then
  105. echo "#################### Building $app_name solution ####################"
  106. dotnet publish -o obj/Docker/publish ../../eShopOnContainers-ServicesAndWebApps.sln
  107. fi
  108. export TAG=$image_tag
  109. if [[ $build_images ]]; then
  110. echo "#################### Building the $app_name Docker images ####################"
  111. docker-compose -p ../.. -f ../../docker-compose.yml build
  112. # Remove temporary images
  113. docker rmi $(docker images -qf "dangling=true")
  114. fi
  115. if [[ $push_images ]]; then
  116. echo "#################### Pushing images to the container registry ####################"
  117. services=(basket.api catalog.api identity.api ordering.api marketing.api payment.api locations.api webmvc webspa webstatus)
  118. for service in "${services[@]}"
  119. do
  120. echo "Pushing image for service $service..."
  121. docker tag "eshop/$service:$image_tag" "$container_registry/$service:$image_tag"
  122. docker push "$container_registry/$service:$image_tag"
  123. done
  124. fi
  125. ingress_values_file="ingress_values.yaml"
  126. if [[ $use_local_k8s ]]; then
  127. ingress_values_file="ingress_values_dockerk8s.yaml"
  128. dns="localhost"
  129. fi
  130. if [[ $dns == "aks" ]]; then
  131. echo "#################### Begin AKS discovery based on the --dns aks setting. ####################"
  132. if [[ -z $aks_name ]] || [[ -z $aks_rg ]]; then
  133. echo "Error: When using -dns aks, MUST set -aksName and -aksRg too."
  134. echo ''
  135. usage
  136. exit 1
  137. fi
  138. echo "Getting DNS of AKS of AKS $aks_name (in resource group $aks_rg)"
  139. dns="$(az aks show -n $aks_name -g $aks_rg --query addonProfiles.httpApplicationRouting.config.HTTPApplicationRoutingZoneName)"
  140. if [[ -z dns ]]; then
  141. echo "Error: when getting DNS of AKS $aks_name (in resource group $aks_rg). Please ensure AKS has httpRouting enabled AND Azure CLI is logged in and is of version 2.0.37 or higher."
  142. exit 1
  143. fi
  144. $dns=${dns//[\"]/""}
  145. echo "DNS base found is $dns. Will use $aks_name.$dns for the app!"
  146. fi
  147. # Initialization & check commands
  148. if [[ -z $dns ]]; then
  149. echo "No DNS specified. Ingress resources will be bound to public IP."
  150. fi
  151. if [[ $clean ]]; then
  152. echo "Cleaning previous helm releases..."
  153. helm delete --purge $(helm ls -q)
  154. echo "Previous releases deleted"
  155. fi
  156. use_custom_registry=''
  157. if [[ -n $container_registry ]]; then
  158. use_custom_registry='yes'
  159. if [[ -z $docker_user ]] || [[ -z $docker_password ]]; then
  160. echo "Error: Must use -u (--docker-username) AND -p (--docker-password) if specifying custom registry"
  161. exit 1
  162. fi
  163. fi
  164. echo "#################### Begin $app_name installation using Helm ####################"
  165. infras=(sql-data nosql-data rabbitmq keystore-data basket-data)
  166. charts=(eshop-common apigwmm apigwms apigwwm apigwws basket-api catalog-api identity-api locations-api marketing-api mobileshoppingagg ordering-api ordering-signalrhub payment-api webmvc webshoppingagg webspa webstatus webhooks-api webhooks-web)
  167. if [[ !$skip_infrastructure ]]; then
  168. for infra in "${infras[@]}"
  169. do
  170. echo "Installing infrastructure: $infra"
  171. helm install --values app.yaml --values inf.yaml --values $ingress_values_file --set app.name=$app_name --set inf.k8s.dns=$dns --name="$app_name-$infra" $infra
  172. done
  173. fi
  174. for chart in "${charts[@]}"
  175. do
  176. echo "Installing: $chart"
  177. if [[ $use_custom_registry ]]; then
  178. helm install --set inf.registry.server=$container_registry --set inf.registry.login=$docker_username --set inf.registry.pwd=$docker_password --set inf.registry.secretName=eshop-docker-scret --values app.yaml --values inf.yaml --values $ingress_values_file --set app.name=$app_name --set inf.k8s.dns=$dns --set image.tag=$image_tag --set image.pullPolicy=Always --name="$app_name-$chart" $chart
  179. elif [[ $chart != "eshop-common" ]]; then # eshop-common is ignored when no secret must be deployed
  180. helm install --values app.yaml --values inf.yaml --values $ingress_values_file --set app.name=$app_name --set inf.k8s.dns=$dns --set image.tag=$image_tag --set image.pullPolicy=Always --name="$app_name-$chart" $chart
  181. fi
  182. done
  183. echo "FINISHED: Helm charts installed."