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.

211 lines
9.5 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)][string]$externalDns,
  10. [parameter(Mandatory=$false)][bool]$deployCI=$false,
  11. [parameter(Mandatory=$false)][bool]$buildImages=$false,
  12. [parameter(Mandatory=$false)][bool]$deployInfrastructure=$true
  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. # Check required commands (only if not in CI environment)
  29. if(-not $deployCI) {
  30. $requiredCommands = ("docker", "docker-compose", "kubectl")
  31. foreach ($command in $requiredCommands) {
  32. if ((Get-Command $command -ErrorAction SilentlyContinue) -eq $null) {
  33. Write-Host "$command must be on path" -ForegroundColor Red
  34. exit
  35. }
  36. }
  37. }
  38. else {
  39. $buildImages = false; # Never build images through CI, as they previously built
  40. }
  41. # Get tag to use from current branch if no tag is passed
  42. if ([string]::IsNullOrEmpty($imageTag)) {
  43. $imageTag = $(git rev-parse --abbrev-ref HEAD)
  44. }
  45. Write-Host "Docker image Tag: $imageTag" -ForegroundColor Yellow
  46. # Read config to use
  47. $config = Get-Content -Raw -Path $configFile | ConvertFrom-Json
  48. if ($debugMode) {
  49. Write-Host "[DEBUG]: Using following JSON config: " -ForegroundColor Yellow
  50. $json = ConvertTo-Json $config -Depth 5
  51. Write-Host $json
  52. if (-not $deployCI) {
  53. Write-Host "[DEBUG]: Press a key " -ForegroundColor Yellow
  54. [System.Console]::Read()
  55. }
  56. }
  57. # building and publishing docker images if needed
  58. if($buildImages) {
  59. Write-Host "Building and publishing eShopOnContainers..." -ForegroundColor Yellow
  60. dotnet restore ../eShopOnContainers-ServicesAndWebApps.sln
  61. dotnet publish -c Release -o obj/Docker/publish ../eShopOnContainers-ServicesAndWebApps.sln
  62. Write-Host "Building Docker images tagged with '$imageTag'" -ForegroundColor Yellow
  63. $env:TAG=$imageTag
  64. docker-compose -p .. -f ../docker-compose.yml build
  65. Write-Host "Pushing images to $registry..." -ForegroundColor Yellow
  66. $services = ("basket.api", "catalog.api", "identity.api", "ordering.api", "marketing.api","payment.api","locations.api", "webmvc", "webspa", "webstatus")
  67. foreach ($service in $services) {
  68. docker tag eshop/${service}:$imageTag $registry/eshop/${service}:$imageTag
  69. docker push $registry/eshop/${service}:$imageTag
  70. }
  71. }
  72. # Use ACR instead of DockerHub as image repository
  73. if(-not $useDockerHub) {
  74. Write-Host "Logging in to $registry" -ForegroundColor Yellow
  75. docker login -u $dockerUser -p $dockerPassword $registry
  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=$registry `
  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 config-files'
  92. ExecKube -cmd 'delete configmap urls'
  93. ExecKube -cmd 'delete configmap externalcfg'
  94. # start sql, rabbitmq, frontend deploymentsExecKube -cmd 'delete configmap config-files'
  95. ExecKube -cmd 'create configmap config-files --from-file=nginx-conf=nginx.conf'
  96. ExecKube -cmd 'label configmap config-files app=eshop'
  97. if ($deployInfrastructure) {
  98. Write-Host 'Deploying infrastructure deployments (databases, redis, ...)' -ForegroundColor Yellow
  99. ExecKube -cmd 'create -f sql-data.yaml -f basket-data.yaml -f keystore-data.yaml -f rabbitmq.yaml -f nosql-data.yaml'
  100. }
  101. Write-Host 'Deploying code deployments (databases, redis, ...)' -ForegroundColor Yellow
  102. ExecKube -cmd 'create -f services.yaml -f frontend.yaml'
  103. if ([string]::IsNullOrEmpty($externalDns)) {
  104. Write-Host "Waiting for frontend's external ip..." -ForegroundColor Yellow
  105. while ($true) {
  106. $frontendUrl = & ExecKube -cmd 'get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"'
  107. if ([bool]($frontendUrl -as [ipaddress])) {
  108. break
  109. }
  110. Start-Sleep -s 15
  111. }
  112. $externalDns = $frontendUrl
  113. }
  114. Write-Host "Using $externalDns as the external DNS/IP of the k8s cluster"
  115. ExecKube -cmd 'create configmap urls `
  116. --from-literal=BasketUrl=http://basket `
  117. --from-literal=BasketHealthCheckUrl=http://basket/hc `
  118. --from-literal=CatalogUrl=http://$($frontendUrl)/catalog-api `
  119. --from-literal=CatalogHealthCheckUrl=http://catalog/hc `
  120. --from-literal=IdentityUrl=http://$($frontendUrl)/identity `
  121. --from-literal=IdentityHealthCheckUrl=http://identity/hc `
  122. --from-literal=OrderingUrl=http://ordering `
  123. --from-literal=OrderingHealthCheckUrl=http://ordering/hc `
  124. --from-literal=MvcClientExternalUrl=http://$($frontendUrl)/webmvc `
  125. --from-literal=WebMvcHealthCheckUrl=http://webmvc/hc `
  126. --from-literal=MvcClientOrderingUrl=http://ordering `
  127. --from-literal=MvcClientCatalogUrl=http://catalog `
  128. --from-literal=MvcClientBasketUrl=http://basket `
  129. --from-literal=WebSpaHealthCheckUrl=http://webspa/hc `
  130. --from-literal=SpaClientOrderingExternalUrl=http://$($externalDns)/ordering-api `
  131. --from-literal=SpaClientCatalogExternalUrl=http://$($externalDns)/catalog-api `
  132. --from-literal=SpaClientBasketExternalUrl=http://$($externalDns)/basket-api `
  133. --from-literal=SpaClientIdentityExternalUrl=http://$($externalDns)/identity `
  134. --from-literal=SpaClientExternalUrl=http://$($externalDns)'
  135. ExecKube -cmd 'label configmap urls app=eshop'
  136. Write-Host "Applying external configuration from json" -ForegroundColor Yellow
  137. ExecKube -cmd 'create configmap externalcfg `
  138. --from-literal=CatalogSqlDb=$($config.sql.catalog) `
  139. --from-literal=IdentitySqlDb=$($config.sql.identity) `
  140. --from-literal=OrderingSqlDb=$($config.sql.ordering) `
  141. --from-literal=MarketingSqlDb=$($config.sql.marketing) `
  142. --from-literal=LocationsNoSqlDb=$($config.nosql.locations.constr) `
  143. --from-literal=LocationsNoSqlDbName=$($config.nosql.locations.db) `
  144. --from-literal=MarketingNoSqlDb=$($config.nosql.marketing.constr) `
  145. --from-literal=MarketingNoSqlDbName=$($config.nosql.marketing.db) `
  146. --from-literal=BasketRedisConStr=$($config.redis.basket) `
  147. --from-literal=LocationsBus=$($config.servicebus.locations) `
  148. --from-literal=MarketingBus=$($config.servicebus.marketing) `
  149. --from-literal=BasketBus=$($config.servicebus.basket) `
  150. --from-literal=OrderingBus=$($config.servicebus.ordering) `
  151. --from-literal=CatalogBus=$($config.servicebus.catalog) `
  152. --from-literal=PaymentBus=$($config.servicebus.payment) `
  153. --from-literal=UseAzureServiceBus=$($config.servicebus.use_azure) `
  154. --from-literal=keystore=$($config.redis.keystore) '
  155. ExecKube -cmd 'label configmap externalcfg app=eshop'
  156. Write-Host "Creating deployments..." -ForegroundColor Yellow
  157. ExecKube -cmd 'create -f deployments.yaml'
  158. # update deployments with the correct image (with tag and/or registry)
  159. Write-Host "Update Image containers to use prefix '$registry' and tag '$imageTag'" -ForegroundColor Yellow
  160. $registryPath = ""
  161. if (-not [string]::IsNullOrEmpty($registry)) {
  162. $registryPath = "$registry/"
  163. }
  164. ExecKube -cmd 'set image deployments/basket basket=${registryPath}eshop/basket.api:$imageTag'
  165. ExecKube -cmd 'set image deployments/catalog catalog=${registryPath}eshop/catalog.api:$imageTag'
  166. ExecKube -cmd 'set image deployments/identity identity=${registryPath}eshop/identity.api:$imageTag'
  167. ExecKube -cmd 'set image deployments/ordering ordering=${registryPath}eshop/ordering.api:$imageTag'
  168. ExecKube -cmd 'set image deployments/marketing marketing=${registryPath}eshop/marketing.api:$imageTag'
  169. ExecKube -cmd 'set image deployments/locations locations=${registryPath}eshop/locations.api:$imageTag'
  170. ExecKube -cmd 'set image deployments/payment payment=${registryPath}eshop/payment.api:$imageTag'
  171. ExecKube -cmd 'set image deployments/webmvc webmvc=${registryPath}eshop/webmvc:$imageTag'
  172. ExecKube -cmd 'set image deployments/webstatus webstatus=${registryPath}eshop/webstatus:$imageTag'
  173. ExecKube -cmd 'set image deployments/webspa webspa=${registryPath}eshop/webspa:$imageTag'
  174. Write-Host "Execute rollout..." -ForegroundColor Yellow
  175. ExecKube -cmd 'rollout resume deployments/basket'
  176. ExecKube -cmd 'rollout resume deployments/catalog'
  177. ExecKube -cmd 'rollout resume deployments/identity'
  178. ExecKube -cmd 'rollout resume deployments/ordering'
  179. ExecKube -cmd 'rollout resume deployments/marketing'
  180. ExecKube -cmd 'rollout resume deployments/locations'
  181. ExecKube -cmd 'rollout resume deployments/payment'
  182. ExecKube -cmd 'rollout resume deployments/webmvc'
  183. ExecKube -cmd 'rollout resume deployments/webstatus'
  184. ExecKube -cmd 'rollout resume deployments/webspa'
  185. Write-Host "WebSPA is exposed at http://$frontendUrl, WebMVC at http://$frontendUrl/webmvc, WebStatus at http://$frontendUrl/webstatus" -ForegroundColor Yellow