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.

212 lines
10 KiB

5 years ago
  1. Param(
  2. [parameter(Mandatory=$false)][string]$registry,
  3. [parameter(Mandatory=$false)][string]$dockerUser,
  4. [parameter(Mandatory=$false)][string]$dockerPassword,
  5. [parameter(Mandatory=$false)][string]$execPath,
  6. [parameter(Mandatory=$false)][string]$kubeconfigPath,
  7. [parameter(Mandatory=$true)][string]$configFile,
  8. [parameter(Mandatory=$false)][string]$imageTag,
  9. [parameter(Mandatory=$false)][bool]$deployCI=$false,
  10. [parameter(Mandatory=$false)][bool]$buildImages=$true,
  11. [parameter(Mandatory=$false)][bool]$pushImages=$true,
  12. [parameter(Mandatory=$false)][bool]$deployInfrastructure=$true,
  13. [parameter(Mandatory=$false)][string]$dockerOrg="eshop"
  14. )
  15. function ExecKube($cmd) {
  16. if($deployCI) {
  17. $kubeconfig = $kubeconfigPath + 'config';
  18. $exp = $execPath + 'kubectl ' + $cmd + ' --kubeconfig=' + $kubeconfig
  19. Invoke-Expression $exp
  20. }
  21. else{
  22. $exp = $execPath + 'kubectl ' + $cmd
  23. Invoke-Expression $exp
  24. }
  25. }
  26. # Initialization
  27. $debugMode = $PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent
  28. $useDockerHub = [string]::IsNullOrEmpty($registry)
  29. $externalDns = & ExecKube -cmd 'get svc ingress-nginx -n ingress-nginx -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"'
  30. Write-Host "Ingress ip detected: $externalDns" -ForegroundColor Yellow
  31. if (-not [bool]($externalDns -as [ipaddress])) {
  32. Write-Host "Must install ingress first" -ForegroundColor Red
  33. Write-Host "Run deploy-ingress.ps1 and deploy-ingress-azure.ps1" -ForegroundColor Red
  34. exit
  35. }
  36. # Check required commands (only if not in CI environment)
  37. if(-not $deployCI) {
  38. $requiredCommands = ("docker", "docker-compose", "kubectl")
  39. foreach ($command in $requiredCommands) {
  40. if ((Get-Command $command -ErrorAction SilentlyContinue) -eq $null) {
  41. Write-Host "$command must be on path" -ForegroundColor Red
  42. exit
  43. }
  44. }
  45. }
  46. else {
  47. $buildImages = false; # Never build images through CI, as they previously built
  48. }
  49. # Get tag to use from current branch if no tag is passed
  50. if ([string]::IsNullOrEmpty($imageTag)) {
  51. $imageTag = $(git rev-parse --abbrev-ref HEAD)
  52. }
  53. Write-Host "Docker image Tag: $imageTag" -ForegroundColor Yellow
  54. # building docker images if needed
  55. if ($buildImages) {
  56. Write-Host "Building Docker images tagged with '$imageTag'" -ForegroundColor Yellow
  57. $env:TAG=$imageTag
  58. docker-compose -p .. -f ../docker-compose.yml build
  59. }
  60. if ($pushImages) {
  61. Write-Host "Pushing images to $registry/$dockerOrg..." -ForegroundColor Yellow
  62. $services = ("basket.api", "catalog.api", "identity.api", "ordering.api", "ordering.backgroundtasks", "marketing.api","payment.api","locations.api", "webmvc", "webspa", "webstatus", "ocelotapigw", "mobileshoppingagg", "webshoppingagg", "ordering.signalrhub")
  63. foreach ($service in $services) {
  64. $imageFqdn = if ($useDockerHub) {"$dockerOrg/${service}"} else {"$registry/$dockerOrg/${service}"}
  65. docker tag eshop/${service}:$imageTag ${imageFqdn}:$imageTag
  66. docker push ${imageFqdn}:$imageTag
  67. }
  68. }
  69. # if we have login/pwd add the secret to k8s
  70. if (-not [string]::IsNullOrEmpty($dockerUser)) {
  71. $registryFDQN = if (-not $useDockerHub) {$registry} else {"index.docker.io/v1/"}
  72. Write-Host "Logging in to $registryFDQN as user $dockerUser" -ForegroundColor Yellow
  73. if ($useDockerHub) {
  74. docker login -u $dockerUser -p $dockerPassword
  75. }
  76. else {
  77. docker login -u $dockerUser -p $dockerPassword $registryFDQN
  78. }
  79. if (-not $LastExitCode -eq 0) {
  80. Write-Host "Login failed" -ForegroundColor Red
  81. exit
  82. }
  83. # Try to delete the Docker registry key secret
  84. ExecKube -cmd 'delete secret docker-registry registry-key'
  85. # Create the Docker registry key secret
  86. ExecKube -cmd 'create secret docker-registry registry-key `
  87. --docker-server=$registryFDQN `
  88. --docker-username=$dockerUser `
  89. --docker-password=$dockerPassword `
  90. --docker-email=not@used.com'
  91. }
  92. # Removing previous services & deployments
  93. Write-Host "Removing existing services & deployments.." -ForegroundColor Yellow
  94. ExecKube -cmd 'delete deployments --all'
  95. ExecKube -cmd 'delete services --all'
  96. ExecKube -cmd 'delete configmap internalurls'
  97. ExecKube -cmd 'delete configmap urls'
  98. ExecKube -cmd 'delete configmap externalcfg'
  99. ExecKube -cmd 'delete configmap ocelot'
  100. ExecKube -cmd 'delete -f ingress.yaml'
  101. # start sql, rabbitmq, frontend deployments
  102. if ($deployInfrastructure) {
  103. Write-Host 'Deploying infrastructure deployments (databases, redis, RabbitMQ...)' -ForegroundColor Yellow
  104. ExecKube -cmd 'create -f sql-data.yaml -f basket-data.yaml -f keystore-data.yaml -f rabbitmq.yaml -f nosql-data.yaml'
  105. }
  106. Write-Host 'Deploying ocelot APIGW' -ForegroundColor Yellow
  107. ExecKube "create configmap ocelot --from-file=mm=ocelot/configuration-mobile-marketing.json --from-file=ms=ocelot/configuration-mobile-shopping.json --from-file=wm=ocelot/configuration-web-marketing.json --from-file=ws=ocelot/configuration-web-shopping.json "
  108. ExecKube -cmd "apply -f ocelot/deployment.yaml"
  109. ExecKube -cmd "apply -f ocelot/service.yaml"
  110. Write-Host 'Deploying code deployments (Web APIs, Web apps, ...)' -ForegroundColor Yellow
  111. ExecKube -cmd 'create -f services.yaml'
  112. ExecKube -cmd 'create -f internalurls.yaml'
  113. ExecKube -cmd 'create configmap urls `
  114. --from-literal=PicBaseUrl=http://$($externalDns)/webshoppingapigw/c/api/v1/catalog/items/[0]/pic/ `
  115. --from-literal=Marketing_PicBaseUrl=http://$($externalDns)/webmarketingapigw/m/api/v1/campaigns/[0]/pic/ `
  116. --from-literal=mvc_e=http://$($externalDns)/webmvc `
  117. --from-literal=marketingapigw_e=http://$($externalDns)/webmarketingapigw `
  118. --from-literal=webshoppingapigw_e=http://$($externalDns)/webshoppingapigw `
  119. --from-literal=mobileshoppingagg_e=http://$($externalDns)/mobileshoppingagg `
  120. --from-literal=webshoppingagg_e=http://$($externalDns)/webshoppingagg `
  121. --from-literal=identity_e=http://$($externalDns)/identity `
  122. --from-literal=spa_e=http://$($externalDns) `
  123. --from-literal=locations_e=http://$($externalDns)/locations-api `
  124. --from-literal=marketing_e=http://$($externalDns)/marketing-api `
  125. --from-literal=basket_e=http://$($externalDns)/basket-api `
  126. --from-literal=ordering_e=http://$($externalDns)/ordering-api `
  127. --from-literal=xamarin_callback_e=http://$($externalDns)/xamarincallback'
  128. ExecKube -cmd 'label configmap urls app=eshop'
  129. Write-Host "Deploying configuration from $configFile" -ForegroundColor Yellow
  130. ExecKube -cmd "create -f $configFile"
  131. Write-Host "Creating deployments..." -ForegroundColor Yellow
  132. ExecKube -cmd 'create -f deployments.yaml'
  133. # update deployments with the correct image (with tag and/or registry)
  134. $registryPath = ""
  135. if (-not [string]::IsNullOrEmpty($registry)) {
  136. $registryPath = "$registry/"
  137. }
  138. Write-Host "Update Image containers to use prefix '$registry$dockerOrg' and tag '$imageTag'" -ForegroundColor Yellow
  139. ExecKube -cmd 'set image deployments/basket basket=${registryPath}${dockerOrg}/basket.api:$imageTag'
  140. ExecKube -cmd 'set image deployments/catalog catalog=${registryPath}${dockerOrg}/catalog.api:$imageTag'
  141. ExecKube -cmd 'set image deployments/identity identity=${registryPath}${dockerOrg}/identity.api:$imageTag'
  142. ExecKube -cmd 'set image deployments/ordering ordering=${registryPath}${dockerOrg}/ordering.api:$imageTag'
  143. ExecKube -cmd 'set image deployments/ordering-backgroundtasks ordering-backgroundtasks=${registryPath}${dockerOrg}/ordering.backgroundtasks:$imageTag'
  144. ExecKube -cmd 'set image deployments/marketing marketing=${registryPath}${dockerOrg}/marketing.api:$imageTag'
  145. ExecKube -cmd 'set image deployments/locations locations=${registryPath}${dockerOrg}/locations.api:$imageTag'
  146. ExecKube -cmd 'set image deployments/payment payment=${registryPath}${dockerOrg}/payment.api:$imageTag'
  147. ExecKube -cmd 'set image deployments/webmvc webmvc=${registryPath}${dockerOrg}/webmvc:$imageTag'
  148. ExecKube -cmd 'set image deployments/webstatus webstatus=${registryPath}${dockerOrg}/webstatus:$imageTag'
  149. ExecKube -cmd 'set image deployments/webspa webspa=${registryPath}${dockerOrg}/webspa:$imageTag'
  150. ExecKube -cmd 'set image deployments/ordering-signalrhub ordering-signalrhub=${registryPath}${dockerOrg}/ordering.signalrhub:$imageTag'
  151. ExecKube -cmd 'set image deployments/mobileshoppingagg mobileshoppingagg=${registryPath}${dockerOrg}/mobileshoppingagg:$imageTag'
  152. ExecKube -cmd 'set image deployments/webshoppingagg webshoppingagg=${registryPath}${dockerOrg}/webshoppingagg:$imageTag'
  153. ExecKube -cmd 'set image deployments/apigwmm apigwmm=${registryPath}${dockerOrg}/ocelotapigw:$imageTag'
  154. ExecKube -cmd 'set image deployments/apigwms apigwms=${registryPath}${dockerOrg}/ocelotapigw:$imageTag'
  155. ExecKube -cmd 'set image deployments/apigwwm apigwwm=${registryPath}${dockerOrg}/ocelotapigw:$imageTag'
  156. ExecKube -cmd 'set image deployments/apigwws apigwws=${registryPath}${dockerOrg}/ocelotapigw:$imageTag'
  157. Write-Host "Execute rollout..." -ForegroundColor Yellow
  158. ExecKube -cmd 'rollout resume deployments/basket'
  159. ExecKube -cmd 'rollout resume deployments/catalog'
  160. ExecKube -cmd 'rollout resume deployments/identity'
  161. ExecKube -cmd 'rollout resume deployments/ordering'
  162. ExecKube -cmd 'rollout resume deployments/ordering-backgroundtasks'
  163. ExecKube -cmd 'rollout resume deployments/marketing'
  164. ExecKube -cmd 'rollout resume deployments/locations'
  165. ExecKube -cmd 'rollout resume deployments/payment'
  166. ExecKube -cmd 'rollout resume deployments/webmvc'
  167. ExecKube -cmd 'rollout resume deployments/webstatus'
  168. ExecKube -cmd 'rollout resume deployments/webspa'
  169. ExecKube -cmd 'rollout resume deployments/mobileshoppingagg'
  170. ExecKube -cmd 'rollout resume deployments/webshoppingagg'
  171. ExecKube -cmd 'rollout resume deployments/apigwmm'
  172. ExecKube -cmd 'rollout resume deployments/apigwms'
  173. ExecKube -cmd 'rollout resume deployments/apigwwm'
  174. ExecKube -cmd 'rollout resume deployments/apigwws'
  175. ExecKube -cmd 'rollout resume deployments/ordering-signalrhub'
  176. Write-Host "Adding/Updating ingress resource..." -ForegroundColor Yellow
  177. ExecKube -cmd 'apply -f ingress.yaml'
  178. Write-Host "WebSPA is exposed at http://$externalDns, WebMVC at http://$externalDns/webmvc, WebStatus at http://$externalDns/webstatus" -ForegroundColor Yellow