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.

202 lines
9.7 KiB

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