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.

186 lines
8.0 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)][bool]$deployCI,
  6. [parameter(Mandatory=$false)][bool]$useDockerHub,
  7. [parameter(Mandatory=$false)][string]$execPath,
  8. [parameter(Mandatory=$false)][string]$kubeconfigPath,
  9. [parameter(Mandatory=$true)][string]$configFile,
  10. [parameter(Mandatory=$false)][bool]$deployInfrastructure=$true
  11. )
  12. $debugMode = $PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent
  13. function ExecKube($cmd) {
  14. if($deployCI) {
  15. $kubeconfig = $kubeconfigPath + 'config';
  16. $exp = $execPath + 'kubectl ' + $cmd + ' --kubeconfig=' + $kubeconfig
  17. Invoke-Expression $exp
  18. }
  19. else{
  20. $exp = $execPath + 'kubectl ' + $cmd
  21. Invoke-Expression $exp
  22. }
  23. }
  24. $config = Get-Content -Raw -Path $configFile | ConvertFrom-Json
  25. if ($debugMode) {
  26. Write-Host "Using following JSON config: "
  27. $json = ConvertTo-Json $config -Depth 5
  28. Write-Host $json
  29. Write-Host "Press a key "
  30. [System.Console]::Read()
  31. }
  32. # Not used when deploying through CI VSTS
  33. if(-not $deployCI) {
  34. $requiredCommands = ("docker", "docker-compose", "kubectl")
  35. foreach ($command in $requiredCommands) {
  36. if ((Get-Command $command -ErrorAction SilentlyContinue) -eq $null) {
  37. Write-Host "$command must be on path" -ForegroundColor Red
  38. exit
  39. }
  40. }
  41. }
  42. # Use ACR instead of DockerHub as image repository
  43. if(-not $useDockerHub) {
  44. Write-Host "Logging in to $registry" -ForegroundColor Yellow
  45. docker login -u $dockerUser -p $dockerPassword $registry
  46. if (-not $LastExitCode -eq 0) {
  47. Write-Host "Login failed" -ForegroundColor Red
  48. exit
  49. }
  50. # create registry key secret
  51. ExecKube -cmd 'create secret docker-registry registry-key `
  52. --docker-server=$registry `
  53. --docker-username=$dockerUser `
  54. --docker-password=$dockerPassword `
  55. --docker-email=not@used.com'
  56. }
  57. # Removing previous services & deployments
  58. Write-Host "Removing existing services & deployments.." -ForegroundColor Yellow
  59. ExecKube -cmd 'delete deployments --all'
  60. ExecKube -cmd 'delete services --all'
  61. ExecKube -cmd 'delete configmap config-files'
  62. ExecKube -cmd 'delete configmap urls'
  63. ExecKube -cmd 'delete configmap externalcfg'
  64. # start sql, rabbitmq, frontend deploymentsExecKube -cmd 'delete configmap config-files'
  65. ExecKube -cmd 'create configmap config-files --from-file=nginx-conf=nginx.conf'
  66. ExecKube -cmd 'label configmap config-files app=eshop'
  67. if ($deployInfrastructure) {
  68. Write-Host 'Deploying infrastructure deployments (databases, redis, ...)' -ForegroundColor Yellow
  69. ExecKube -cmd 'create -f sql-data.yaml -f basket-data.yaml -f keystore-data.yaml -f rabbitmq.yaml -f nosql-data.yaml'
  70. }
  71. Write-Host 'Deploying code deployments (databases, redis, ...)' -ForegroundColor Yellow
  72. ExecKube -cmd 'create -f services.yaml -f frontend.yaml'
  73. # building and publishing docker images not necessary when deploying through CI VSTS
  74. if(-not $deployCI) {
  75. Write-Host "Building and publishing eShopOnContainers..." -ForegroundColor Yellow
  76. dotnet restore ../eShopOnContainers-ServicesAndWebApps.sln
  77. dotnet publish -c Release -o obj/Docker/publish ../eShopOnContainers-ServicesAndWebApps.sln
  78. Write-Host "Building Docker images..." -ForegroundColor Yellow
  79. docker-compose -p .. -f ../docker-compose.yml build
  80. Write-Host "Pushing images to $registry..." -ForegroundColor Yellow
  81. $services = ("basket.api", "catalog.api", "identity.api", "ordering.api", "webmvc", "webspa", "webstatus")
  82. foreach ($service in $services) {
  83. docker tag eshop/$service $registry/eshop/$service
  84. docker push $registry/eshop/$service
  85. }
  86. }
  87. Write-Host "Waiting for frontend's external ip..." -ForegroundColor Yellow
  88. while ($true) {
  89. $frontendUrl = & ExecKube -cmd 'get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"'
  90. if ([bool]($frontendUrl -as [ipaddress])) {
  91. break
  92. }
  93. Start-Sleep -s 15
  94. }
  95. ExecKube -cmd 'create configmap urls `
  96. --from-literal=BasketUrl=http://basket `
  97. --from-literal=BasketHealthCheckUrl=http://basket/hc `
  98. --from-literal=CatalogUrl=http://$($frontendUrl)/catalog-api `
  99. --from-literal=CatalogHealthCheckUrl=http://catalog/hc `
  100. --from-literal=IdentityUrl=http://$($frontendUrl)/identity `
  101. --from-literal=IdentityHealthCheckUrl=http://identity/hc `
  102. --from-literal=OrderingUrl=http://ordering `
  103. --from-literal=OrderingHealthCheckUrl=http://ordering/hc `
  104. --from-literal=MvcClientExternalUrl=http://$($frontendUrl)/webmvc `
  105. --from-literal=WebMvcHealthCheckUrl=http://webmvc/hc `
  106. --from-literal=MvcClientOrderingUrl=http://ordering `
  107. --from-literal=MvcClientCatalogUrl=http://catalog `
  108. --from-literal=MvcClientBasketUrl=http://basket `
  109. --from-literal=WebSpaHealthCheckUrl=http://webspa/hc `
  110. --from-literal=SpaClientOrderingExternalUrl=http://$($frontendUrl)/ordering-api `
  111. --from-literal=SpaClientCatalogExternalUrl=http://$($frontendUrl)/catalog-api `
  112. --from-literal=SpaClientBasketExternalUrl=http://$($frontendUrl)/basket-api `
  113. --from-literal=SpaClientIdentityExternalUrl=http://$($frontendUrl)/identity `
  114. --from-literal=SpaClientExternalUrl=http://$($frontendUrl)'
  115. ExecKube -cmd 'label configmap urls app=eshop'
  116. Write-Host "Applying external configuration from json" -ForegroundColor Yellow
  117. ExecKube -cmd 'create configmap externalcfg `
  118. --from-literal=CatalogSqlDb=$($config.sql.catalog) `
  119. --from-literal=IdentitySqlDb=$($config.sql.identity) `
  120. --from-literal=OrderingSqlDb=$($config.sql.ordering) `
  121. --from-literal=MarketingSqlDb=$($config.sql.marketing) `
  122. --from-literal=LocationsNoSqlDb=$($config.nosql.locations.constr) `
  123. --from-literal=LocationsNoSqlDbName=$($config.nosql.locations.db) `
  124. --from-literal=MarketingsNoSqlDb=$($config.nosql.marketing.constr) `
  125. --from-literal=MarketingNoSqlDbName=$($config.nosql.marketing.db) `
  126. --from-literal=BasketRedisConStr=$($config.redis.basket) `
  127. --from-literal=LocationsBus=$($config.servicebus.locations) `
  128. --from-literal=MarketingBus=$($config.servicebus.marketing) `
  129. --from-literal=BasketBus=$($config.servicebus.basket) `
  130. --from-literal=OrderingBus=$($config.servicebus.ordering) `
  131. --from-literal=CatalogBus=$($config.servicebus.catalog) `
  132. --from-literal=PaymentBus=$($config.servicebus.payment) '
  133. ExecKube -cmd 'label configmap externalcfg app=eshop'
  134. Write-Host "Creating deployments..." -ForegroundColor Yellow
  135. ExecKube -cmd 'create -f deployments.yaml'
  136. # not using ACR for pulling images when deploying through CI VSTS
  137. if(-not $deployCI) {
  138. # update deployments with the private registry before k8s tries to pull images
  139. # (deployment templating, or Helm, would obviate this)
  140. Write-Host "Update Image containers..." -ForegroundColor Yellow
  141. ExecKube -cmd 'set image deployments/basket basket=$registry/eshop/basket.api'
  142. ExecKube -cmd 'set image deployments/catalog catalog=$registry/eshop/catalog.api'
  143. ExecKube -cmd 'set image deployments/identity identity=$registry/eshop/identity.api'
  144. ExecKube -cmd 'set image deployments/ordering ordering=$registry/eshop/ordering.api'
  145. ExecKube -cmd 'set image deployments/webmvc webmvc=$registry/eshop/webmvc'
  146. ExecKube -cmd 'set image deployments/webstatus webstatus=$registry/eshop/webstatus'
  147. ExecKube -cmd 'set image deployments/webspa webspa=$registry/eshop/webspa'
  148. }
  149. Write-Host "Execute rollout..." -ForegroundColor Yellow
  150. ExecKube -cmd 'rollout resume deployments/basket'
  151. ExecKube -cmd 'rollout resume deployments/catalog'
  152. ExecKube -cmd 'rollout resume deployments/identity'
  153. ExecKube -cmd 'rollout resume deployments/ordering'
  154. ExecKube -cmd 'rollout resume deployments/webmvc'
  155. ExecKube -cmd 'rollout resume deployments/webstatus'
  156. ExecKube -cmd 'rollout resume deployments/webspa'
  157. Write-Host "WebSPA is exposed at http://$frontendUrl, WebMVC at http://$frontendUrl/webmvc, WebStatus at http://$frontendUrl/webstatus" -ForegroundColor Yellow