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.

205 lines
9.3 KiB

7 years ago
7 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)][string]$externalDns,
  10. [parameter(Mandatory=$false)][bool]$deployCI=$false,
  11. [parameter(Mandatory=$false)][bool]$buildImages=$true,
  12. [parameter(Mandatory=$false)][bool]$buildBits=$false,
  13. [parameter(Mandatory=$false)][bool]$deployInfrastructure=$true,
  14. [parameter(Mandatory=$false)][string]$dockerOrg="eshop"
  15. )
  16. function ExecKube($cmd) {
  17. if($deployCI) {
  18. $kubeconfig = $kubeconfigPath + 'config';
  19. $exp = $execPath + 'kubectl ' + $cmd + ' --kubeconfig=' + $kubeconfig
  20. Invoke-Expression $exp
  21. }
  22. else{
  23. $exp = $execPath + 'kubectl ' + $cmd
  24. Invoke-Expression $exp
  25. }
  26. }
  27. # Initialization
  28. $debugMode = $PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent
  29. $useDockerHub = [string]::IsNullOrEmpty($registry)
  30. # Check required commands (only if not in CI environment)
  31. if(-not $deployCI) {
  32. $requiredCommands = ("docker", "docker-compose", "kubectl")
  33. foreach ($command in $requiredCommands) {
  34. if ((Get-Command $command -ErrorAction SilentlyContinue) -eq $null) {
  35. Write-Host "$command must be on path" -ForegroundColor Red
  36. exit
  37. }
  38. }
  39. }
  40. else {
  41. $buildBits = false;
  42. $buildImages = false; # Never build images through CI, as they previously built
  43. }
  44. # Get tag to use from current branch if no tag is passed
  45. if ([string]::IsNullOrEmpty($imageTag)) {
  46. $imageTag = $(git rev-parse --abbrev-ref HEAD)
  47. }
  48. Write-Host "Docker image Tag: $imageTag" -ForegroundColor Yellow
  49. # building and publishing docker images if needed
  50. if($buildBits) {
  51. Write-Host "Building and publishing eShopOnContainers..." -ForegroundColor Yellow
  52. dotnet publish -c Release -o obj/Docker/publish ../eShopOnContainers-ServicesAndWebApps.sln
  53. }
  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", "marketing.api","payment.api","locations.api", "webmvc", "webspa", "webstatus")
  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 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://$($externalDns)/catalog-api `
  119. --from-literal=CatalogHealthCheckUrl=http://catalog/hc `
  120. --from-literal=PicBaseUrl=http://$($externalDns)/catalog-api/api/v1/catalog/items/[0]/pic/ `
  121. --from-literal=Marketing_PicBaseUrl=http://$($externalDns)/marketing-api/api/v1/campaigns/[0]/pic/ `
  122. --from-literal=IdentityUrl=http://$($externalDns)/identity `
  123. --from-literal=IdentityHealthCheckUrl=http://identity/hc `
  124. --from-literal=OrderingUrl=http://ordering `
  125. --from-literal=OrderingHealthCheckUrl=http://ordering/hc `
  126. --from-literal=MvcClientExternalUrl=http://$($externalDns)/webmvc `
  127. --from-literal=WebMvcHealthCheckUrl=http://webmvc/hc `
  128. --from-literal=MvcClientOrderingUrl=http://ordering `
  129. --from-literal=MvcClientCatalogUrl=http://catalog `
  130. --from-literal=MvcClientBasketUrl=http://basket `
  131. --from-literal=MvcClientMarketingUrl=http://marketing `
  132. --from-literal=MarketingHealthCheckUrl=http://marketing/hc `
  133. --from-literal=WebSpaHealthCheckUrl=http://webspa/hc `
  134. --from-literal=SpaClientMarketingExternalUrl=http://$($externalDns)/marketing-api `
  135. --from-literal=SpaClientOrderingExternalUrl=http://$($externalDns)/ordering-api `
  136. --from-literal=SpaClientCatalogExternalUrl=http://$($externalDns)/catalog-api `
  137. --from-literal=SpaClientBasketExternalUrl=http://$($externalDns)/basket-api `
  138. --from-literal=SpaClientIdentityExternalUrl=http://$($externalDns)/identity `
  139. --from-literal=LocationsHealthCheckUrl=http://locations/hc `
  140. --from-literal=SpaClientExternalUrl=http://$($externalDns) `
  141. --from-literal=LocationApiClient=http://$($externalDns)/locations-api `
  142. --from-literal=MarketingApiClient=http://$($externalDns)/marketing-api `
  143. --from-literal=BasketApiClient=http://$($externalDns)/basket-api `
  144. --from-literal=OrderingApiClient=http://$($externalDns)/ordering-api'
  145. ExecKube -cmd 'label configmap urls app=eshop'
  146. Write-Host "Deploying configuration from $configFile" -ForegroundColor Yellow
  147. ExecKube -cmd "create -f $configFile"
  148. Write-Host "Creating deployments..." -ForegroundColor Yellow
  149. ExecKube -cmd 'create -f deployments.yaml'
  150. # update deployments with the correct image (with tag and/or registry)
  151. $registryPath = ""
  152. if (-not [string]::IsNullOrEmpty($registry)) {
  153. $registryPath = "$registry/"
  154. }
  155. Write-Host "Update Image containers to use prefix '$registry$dockerOrg' and tag '$imageTag'" -ForegroundColor Yellow
  156. ExecKube -cmd 'set image deployments/basket basket=${registryPath}${dockerOrg}/basket.api:$imageTag'
  157. ExecKube -cmd 'set image deployments/catalog catalog=${registryPath}${dockerOrg}/catalog.api:$imageTag'
  158. ExecKube -cmd 'set image deployments/identity identity=${registryPath}${dockerOrg}/identity.api:$imageTag'
  159. ExecKube -cmd 'set image deployments/ordering ordering=${registryPath}${dockerOrg}/ordering.api:$imageTag'
  160. ExecKube -cmd 'set image deployments/marketing marketing=${registryPath}${dockerOrg}/marketing.api:$imageTag'
  161. ExecKube -cmd 'set image deployments/locations locations=${registryPath}${dockerOrg}/locations.api:$imageTag'
  162. ExecKube -cmd 'set image deployments/payment payment=${registryPath}${dockerOrg}/payment.api:$imageTag'
  163. ExecKube -cmd 'set image deployments/webmvc webmvc=${registryPath}${dockerOrg}/webmvc:$imageTag'
  164. ExecKube -cmd 'set image deployments/webstatus webstatus=${registryPath}${dockerOrg}/webstatus:$imageTag'
  165. ExecKube -cmd 'set image deployments/webspa webspa=${registryPath}${dockerOrg}/webspa:$imageTag'
  166. Write-Host "Execute rollout..." -ForegroundColor Yellow
  167. ExecKube -cmd 'rollout resume deployments/basket'
  168. ExecKube -cmd 'rollout resume deployments/catalog'
  169. ExecKube -cmd 'rollout resume deployments/identity'
  170. ExecKube -cmd 'rollout resume deployments/ordering'
  171. ExecKube -cmd 'rollout resume deployments/marketing'
  172. ExecKube -cmd 'rollout resume deployments/locations'
  173. ExecKube -cmd 'rollout resume deployments/payment'
  174. ExecKube -cmd 'rollout resume deployments/webmvc'
  175. ExecKube -cmd 'rollout resume deployments/webstatus'
  176. ExecKube -cmd 'rollout resume deployments/webspa'
  177. Write-Host "WebSPA is exposed at http://$externalDns, WebMVC at http://$externalDns/webmvc, WebStatus at http://$externalDns/webstatus" -ForegroundColor Yellow