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.

232 lines
8.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> | --dns aks
  16. Specifies the external DNS/ IP address of the Kubernetes cluster.
  17. If 'aks' is set as value, the DNS value is retrieved from the AKS. --aks-name and --aks-rg are needed.
  18. When --use-local-k8s is specified the external DNS is automatically set to localhost.
  19. -h | --help
  20. Displays this help text and exits the script.
  21. --image-build
  22. Build images (default is to not build all images).
  23. --image-push
  24. Upload images to the container registry (default is not pushing to the custom registry)
  25. -n | --app-name <the name of the app>
  26. Specifies the name of the application (default: eshop).
  27. --namespace <namespace name>
  28. Specifies the namespace name to deploy the app. If it doesn't exists it will be created (default: eshop).
  29. -p | --docker-password <docker password>
  30. The Docker password used to logon to the custom registry, supplied using the -r parameter.
  31. -r | --registry <container registry>
  32. Specifies the container registry to use (required), e.g. myregistry.azurecr.io.
  33. --skip-clean
  34. Do not clean the Kubernetes cluster (default is to clean the cluster).
  35. --skip-infrastructure
  36. Do not deploy infrastructure resources (like sql-data, no-sql or redis).
  37. This is useful for production environments where infrastructure is hosted outside the Kubernetes cluster.
  38. -t | --tag <docker image tag>
  39. The tag used for the newly created docker images. Default: latest.
  40. -u | --docker-username <docker username>
  41. The Docker username used to logon to the custom registry, supplied using the -r parameter.
  42. --use-local-k8s
  43. Deploy to a locally installed Kubernetes (default: false).
  44. It is assumed that the Kubernetes cluster has been granted access to the container registry.
  45. If using AKS and ACR see link for more info:
  46. https://docs.microsoft.com/en-us/azure/container-registry/container-registry-auth-aks
  47. WARNING! THE SCRIPT WILL COMPLETELY DESTROY ALL DEPLOYMENTS AND SERVICES VISIBLE
  48. FROM THE CURRENT CONFIGURATION CONTEXT AND NAMESPACE.
  49. It is recommended that you check your selected namespace, 'eshop' by default, is already in use.
  50. Every deployment and service done in the namespace will be deleted.
  51. For more information see https://kubernetes.io/docs/tasks/administer-cluster/namespaces/
  52. END
  53. }
  54. app_name='eshop'
  55. aks_name=''
  56. aks_rg=''
  57. build_images=''
  58. clean='yes'
  59. build_solution=''
  60. container_registry=''
  61. docker_password=''
  62. docker_username=''
  63. dns=''
  64. image_tag='latest'
  65. push_images=''
  66. skip_infrastructure=''
  67. use_local_k8s=''
  68. namespace='eshop'
  69. while [[ $# -gt 0 ]]; do
  70. case "$1" in
  71. --aks-name )
  72. aks_name="$2"; shift 2;;
  73. --aks-rg )
  74. aks_rg="$2"; shift 2;;
  75. -b | --build-solution )
  76. build_solution='yes'; shift ;;
  77. -d | --dns )
  78. dns="$2"; shift 2;;
  79. -h | --help )
  80. usage; exit 1 ;;
  81. -n | --app-name )
  82. app_name="$2"; shift 2;;
  83. -p | --docker-password )
  84. docker_password="$2"; shift 2;;
  85. -r | --registry )
  86. container_registry="$2"; shift 2;;
  87. --skip-clean )
  88. clean=''; shift ;;
  89. --image-build )
  90. build_images='yes'; shift ;;
  91. --image-push )
  92. push_images='yes'; shift ;;
  93. --skip-infrastructure )
  94. skip_infrastructure='yes'; shift ;;
  95. -t | --tag )
  96. image_tag="$2"; shift 2;;
  97. -u | --docker-username )
  98. docker_username="$2"; shift 2;;
  99. --use-local-k8s )
  100. use_local_k8s='yes'; shift ;;
  101. --namespace )
  102. namespace="$2"; shift 2;;
  103. *)
  104. echo "Unknown option $1"
  105. usage; exit 2 ;;
  106. esac
  107. done
  108. if [[ $build_solution ]]; then
  109. echo "#################### Building $app_name solution ####################"
  110. dotnet publish -o obj/Docker/publish ../../eShopOnContainers-ServicesAndWebApps.sln
  111. fi
  112. export TAG=$image_tag
  113. if [[ $build_images ]]; then
  114. echo "#################### Building the $app_name Docker images ####################"
  115. docker-compose -p ../.. -f ../../docker-compose.yml build
  116. # Remove temporary images
  117. docker rmi $(docker images -qf "dangling=true")
  118. fi
  119. use_custom_registry=''
  120. if [[ -n $container_registry ]]; then
  121. echo "################ Log into custom registry $container_registry ##################"
  122. use_custom_registry='yes'
  123. if [[ -z $docker_username ]] || [[ -z $docker_password ]]; then
  124. echo "Error: Must use -u (--docker-username) AND -p (--docker-password) if specifying custom registry"
  125. exit 1
  126. fi
  127. docker login -u $docker_username -p $docker_password $container_registry
  128. fi
  129. if [[ $push_images ]]; then
  130. echo "#################### Pushing images to the container registry ####################"
  131. services=(basket.api catalog.api identity.api ordering.api payment.api webmvc webspa webstatus)
  132. if [[ -z "$(docker image ls -q --filter=reference=eshop/$service:$image_tag)" ]]; then
  133. image_tag=linux-$image_tag
  134. fi
  135. for service in "${services[@]}"
  136. do
  137. echo "Pushing image for service $service..."
  138. docker tag "eshop/$service:$image_tag" "$container_registry/$service:$image_tag"
  139. docker push "$container_registry/$service:$image_tag"
  140. done
  141. fi
  142. ingress_values_file="ingress_values.yaml"
  143. if [[ $use_local_k8s ]]; then
  144. ingress_values_file="ingress_values_dockerk8s.yaml"
  145. dns="localhost"
  146. fi
  147. if [[ $dns == "aks" ]]; then
  148. echo "#################### Begin AKS discovery based on the --dns aks setting. ####################"
  149. if [[ -z $aks_name ]] || [[ -z $aks_rg ]]; then
  150. echo "Error: When using -dns aks, MUST set -aksName and -aksRg too."
  151. echo ''
  152. usage
  153. exit 1
  154. fi
  155. echo "Getting AKS cluster $aks_name AKS (in resource group $aks_rg)"
  156. # JMESPath queries are case sensitive and httpapplicationrouting can be lowercase sometimes
  157. jmespath_dnsqueries=(\
  158. addonProfiles.httpApplicationRouting.config.HTTPApplicationRoutingZoneName \
  159. addonProfiles.httpapplicationrouting.config.HTTPApplicationRoutingZoneName \
  160. )
  161. for q in "${jmespath_dnsqueries[@]}"
  162. do
  163. dns="$(az aks show -n $aks_name -g $aks_rg --query $q -o tsv)"
  164. if [[ -n $dns ]]; then break; fi
  165. done
  166. if [[ -z $dns ]]; then
  167. 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."
  168. exit 1
  169. fi
  170. echo "DNS base found is $dns. Will use $aks_name.$dns for the app!"
  171. dns="$aks_name.$dns"
  172. fi
  173. # Initialization & check commands
  174. if [[ -z $dns ]]; then
  175. echo "No DNS specified. Ingress resources will be bound to public IP."
  176. fi
  177. if [[ $clean ]]; then
  178. echo "Cleaning previous helm releases..."
  179. if [[ -z $(helm ls -q --namespace $namespace) ]]; then
  180. echo "No previous releases found"
  181. else
  182. helm delete --purge $(helm ls -q --namespace $namespace)
  183. echo "Previous releases deleted"
  184. waitsecs=10; while [ $waitsecs -gt 0 ]; do echo -ne "$waitsecs\033[0K\r"; sleep 1; : $((waitsecs--)); done
  185. fi
  186. fi
  187. echo "#################### Begin $app_name installation using Helm ####################"
  188. infras=(sql-data nosql-data rabbitmq keystore-data basket-data)
  189. charts=(eshop-common apigwms apigwws basket-api catalog-api identity-api mobileshoppingagg ordering-api ordering-backgroundtasks ordering-signalrhub payment-api webmvc webshoppingagg webspa webstatus webhooks-api webhooks-web)
  190. if [[ !$skip_infrastructure ]]; then
  191. for infra in "${infras[@]}"
  192. do
  193. echo "Installing infrastructure: $infra"
  194. helm install --namespace $namespace --set "ingress.hosts={$dns}" --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
  195. done
  196. fi
  197. for chart in "${charts[@]}"
  198. do
  199. echo "Installing: $chart"
  200. if [[ $use_custom_registry ]]; then
  201. helm install --namespace $namespace --set "ingress.hosts={$dns}" --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
  202. elif [[ $chart != "eshop-common" ]]; then # eshop-common is ignored when no secret must be deployed
  203. helm install --namespace $namespace --set "ingress.hosts={$dns}" --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
  204. fi
  205. done
  206. echo "FINISHED: Helm charts installed."