2017-02-16 10:49:58 -08:00
Param (
2017-05-16 12:13:49 +02:00
[ parameter ( Mandatory = $false ) ] [ string ] $registry ,
[ parameter ( Mandatory = $false ) ] [ string ] $dockerUser ,
[ parameter ( Mandatory = $false ) ] [ string ] $dockerPassword ,
[ parameter ( Mandatory = $false ) ] [ bool ] $deployCI ,
[ parameter ( Mandatory = $false ) ] [ bool ] $useDockerHub ,
[ parameter ( Mandatory = $false ) ] [ string ] $execPath ,
2017-06-16 17:40:27 +02:00
[ parameter ( Mandatory = $false ) ] [ string ] $kubeconfigPath ,
2017-06-16 20:44:11 +02:00
[ parameter ( Mandatory = $true ) ] [ string ] $configFile ,
2017-06-19 19:01:00 +02:00
[ parameter ( Mandatory = $false ) ] [ string ] $imageTag ,
2017-06-16 20:44:11 +02:00
[ parameter ( Mandatory = $false ) ] [ bool ] $deployInfrastructure = $true
2017-02-16 10:49:58 -08:00
)
2017-06-16 17:40:27 +02:00
$debugMode = $PSCmdlet . MyInvocation . BoundParameters [ " Debug " ] . IsPresent
2017-06-19 19:01:00 +02:00
if ( [ string ] :: IsNullOrEmpty ( $imageTag ) ) {
$imageTag = $ ( git rev-parse - -abbrev -ref HEAD )
}
Write-Host " Docker image Tag: $imageTag " -ForegroundColor Yellow
2017-05-16 12:13:49 +02:00
function ExecKube($cmd ) {
if ( $deployCI ) {
$kubeconfig = $kubeconfigPath + 'config' ;
$exp = $execPath + 'kubectl ' + $cmd + ' --kubeconfig=' + $kubeconfig
Invoke-Expression $exp
}
else {
$exp = $execPath + 'kubectl ' + $cmd
Invoke-Expression $exp
2017-02-16 10:49:58 -08:00
}
}
2017-06-16 17:40:27 +02:00
$config = Get-Content -Raw -Path $configFile | ConvertFrom-Json
if ( $debugMode ) {
2017-06-19 19:01:00 +02:00
Write-Host " Using following JSON config: " -ForegroundColor Yellow
2017-06-16 17:40:27 +02:00
$json = ConvertTo-Json $config -Depth 5
2017-06-19 19:01:00 +02:00
Write-Host $json
Write-Host " Press a key " -ForegroundColor Yellow
2017-06-16 17:40:27 +02:00
[ System.Console ] :: Read ( )
}
2017-05-16 12:13:49 +02:00
# Not used when deploying through CI VSTS
if ( -not $deployCI ) {
$requiredCommands = ( " docker " , " docker-compose " , " kubectl " )
foreach ( $command in $requiredCommands ) {
if ( ( Get-Command $command -ErrorAction SilentlyContinue ) -eq $null ) {
Write-Host " $command must be on path " -ForegroundColor Red
exit
}
}
2017-02-16 10:49:58 -08:00
}
2017-05-16 12:13:49 +02:00
# Use ACR instead of DockerHub as image repository
if ( -not $useDockerHub ) {
Write-Host " Logging in to $registry " -ForegroundColor Yellow
docker login -u $dockerUser -p $dockerPassword $registry
if ( -not $LastExitCode -eq 0 ) {
Write-Host " Login failed " -ForegroundColor Red
exit
}
# create registry key secret
ExecKube -cmd ' create secret docker-registry registry-key `
2017-02-16 10:49:58 -08:00
- -docker -server = $registry `
- -docker -username = $dockerUser `
- -docker -password = $dockerPassword `
2017-05-16 12:13:49 +02:00
- -docker -email = not @used . com '
}
2017-02-16 10:49:58 -08:00
2017-05-16 12:13:49 +02:00
# Removing previous services & deployments
Write-Host " Removing existing services & deployments.. " -ForegroundColor Yellow
2017-05-25 09:24:37 +02:00
ExecKube -cmd 'delete deployments --all'
ExecKube -cmd 'delete services --all'
2017-05-16 12:13:49 +02:00
ExecKube -cmd 'delete configmap config-files'
ExecKube -cmd 'delete configmap urls'
2017-06-16 17:40:27 +02:00
ExecKube -cmd 'delete configmap externalcfg'
2017-02-16 10:49:58 -08:00
2017-05-16 12:13:49 +02:00
# start sql, rabbitmq, frontend deploymentsExecKube -cmd 'delete configmap config-files'
ExecKube -cmd 'create configmap config-files --from-file=nginx-conf=nginx.conf'
ExecKube -cmd 'label configmap config-files app=eshop'
2017-06-16 20:44:11 +02:00
if ( $deployInfrastructure ) {
Write-Host 'Deploying infrastructure deployments (databases, redis, ...)' -ForegroundColor Yellow
ExecKube -cmd 'create -f sql-data.yaml -f basket-data.yaml -f keystore-data.yaml -f rabbitmq.yaml -f nosql-data.yaml'
}
Write-Host 'Deploying code deployments (databases, redis, ...)' -ForegroundColor Yellow
2017-06-16 17:40:27 +02:00
ExecKube -cmd 'create -f services.yaml -f frontend.yaml'
2017-02-16 10:49:58 -08:00
2017-05-16 12:13:49 +02:00
# building and publishing docker images not necessary when deploying through CI VSTS
if ( -not $deployCI ) {
Write-Host " Building and publishing eShopOnContainers... " -ForegroundColor Yellow
dotnet restore . . / eShopOnContainers-ServicesAndWebApps . sln
dotnet publish -c Release -o obj / Docker / publish . . / eShopOnContainers-ServicesAndWebApps . sln
2017-02-16 10:49:58 -08:00
2017-06-19 19:01:00 +02:00
Write-Host " Building Docker images. " -ForegroundColor Yellow
$env:TAG = $imageTag
2017-05-24 17:32:39 +02:00
docker-compose -p . . -f . . / docker-compose . yml build
2017-05-16 12:13:49 +02:00
Write-Host " Pushing images to $registry ... " -ForegroundColor Yellow
2017-06-19 19:01:00 +02:00
$services = ( " basket.api " , " catalog.api " , " identity.api " , " ordering.api " , " marketing.api " , " payment.api " , " locations.api " , " webmvc " , " webspa " , " webstatus " )
2017-05-16 12:13:49 +02:00
foreach ( $service in $services ) {
2017-06-19 19:01:00 +02:00
docker tag eshop / $ { service } : $imageTag $registry / eshop / $ { service } : $imageTag
docker push $registry / eshop / $ { service } : $imageTag
2017-05-16 12:13:49 +02:00
}
2017-02-16 10:49:58 -08:00
}
Write-Host " Waiting for frontend's external ip... " -ForegroundColor Yellow
while ( $true ) {
2017-05-16 12:13:49 +02:00
$frontendUrl = & ExecKube -cmd 'get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"'
2017-02-16 10:49:58 -08:00
if ( [ bool ] ( $frontendUrl -as [ ipaddress ] ) ) {
break
}
Start-Sleep -s 15
}
2017-05-16 12:13:49 +02:00
ExecKube -cmd ' create configmap urls `
2017-05-24 17:32:39 +02:00
- -from -literal = BasketUrl = http : / / basket `
- -from -literal = BasketHealthCheckUrl = http : / / basket / hc `
2017-02-16 10:49:58 -08:00
- -from -literal = CatalogUrl = http : / / $ ( $frontendUrl ) / catalog-api `
2017-05-24 17:32:39 +02:00
- -from -literal = CatalogHealthCheckUrl = http : / / catalog / hc `
2017-02-16 10:49:58 -08:00
- -from -literal = IdentityUrl = http : / / $ ( $frontendUrl ) / identity `
2017-05-24 17:32:39 +02:00
- -from -literal = IdentityHealthCheckUrl = http : / / identity / hc `
- -from -literal = OrderingUrl = http : / / ordering `
- -from -literal = OrderingHealthCheckUrl = http : / / ordering / hc `
- -from -literal = MvcClientExternalUrl = http : / / $ ( $frontendUrl ) / webmvc `
- -from -literal = WebMvcHealthCheckUrl = http : / / webmvc / hc `
- -from -literal = MvcClientOrderingUrl = http : / / ordering `
- -from -literal = MvcClientCatalogUrl = http : / / catalog `
- -from -literal = MvcClientBasketUrl = http : / / basket `
- -from -literal = WebSpaHealthCheckUrl = http : / / webspa / hc `
- -from -literal = SpaClientOrderingExternalUrl = http : / / $ ( $frontendUrl ) / ordering-api `
- -from -literal = SpaClientCatalogExternalUrl = http : / / $ ( $frontendUrl ) / catalog-api `
- -from -literal = SpaClientBasketExternalUrl = http : / / $ ( $frontendUrl ) / basket-api `
- -from -literal = SpaClientIdentityExternalUrl = http : / / $ ( $frontendUrl ) / identity `
- -from -literal = SpaClientExternalUrl = http : / / $ ( $frontendUrl ) '
2017-05-16 12:13:49 +02:00
ExecKube -cmd 'label configmap urls app=eshop'
2017-02-16 10:49:58 -08:00
2017-06-16 17:40:27 +02:00
Write-Host " Applying external configuration from json " -ForegroundColor Yellow
ExecKube -cmd ' create configmap externalcfg `
- -from -literal = CatalogSqlDb = $ ( $config . sql . catalog ) `
- -from -literal = IdentitySqlDb = $ ( $config . sql . identity ) `
- -from -literal = OrderingSqlDb = $ ( $config . sql . ordering ) `
- -from -literal = MarketingSqlDb = $ ( $config . sql . marketing ) `
- -from -literal = LocationsNoSqlDb = $ ( $config . nosql . locations . constr ) `
- -from -literal = LocationsNoSqlDbName = $ ( $config . nosql . locations . db ) `
2017-06-19 19:01:00 +02:00
- -from -literal = MarketingNoSqlDb = $ ( $config . nosql . marketing . constr ) `
2017-06-16 17:40:27 +02:00
- -from -literal = MarketingNoSqlDbName = $ ( $config . nosql . marketing . db ) `
- -from -literal = BasketRedisConStr = $ ( $config . redis . basket ) `
- -from -literal = LocationsBus = $ ( $config . servicebus . locations ) `
- -from -literal = MarketingBus = $ ( $config . servicebus . marketing ) `
- -from -literal = BasketBus = $ ( $config . servicebus . basket ) `
- -from -literal = OrderingBus = $ ( $config . servicebus . ordering ) `
2017-06-16 20:44:11 +02:00
- -from -literal = CatalogBus = $ ( $config . servicebus . catalog ) `
2017-06-19 19:01:00 +02:00
- -from -literal = PaymentBus = $ ( $config . servicebus . payment ) `
- -from -literal = UseAzureServiceBus = $ ( $config . servicebus . use_azure ) `
- -from -literal = keystore = $ ( $config . redis . keystore ) '
2017-06-16 17:40:27 +02:00
ExecKube -cmd 'label configmap externalcfg app=eshop'
2017-05-24 17:32:39 +02:00
Write-Host " Creating deployments... " -ForegroundColor Yellow
2017-05-16 12:13:49 +02:00
ExecKube -cmd 'create -f deployments.yaml'
# not using ACR for pulling images when deploying through CI VSTS
if ( -not $deployCI ) {
# update deployments with the private registry before k8s tries to pull images
# (deployment templating, or Helm, would obviate this)
2017-05-25 09:24:37 +02:00
Write-Host " Update Image containers... " -ForegroundColor Yellow
2017-06-19 19:01:00 +02:00
ExecKube -cmd 'set image deployments/basket basket=$registry/eshop/basket.api:$imageTag'
ExecKube -cmd 'set image deployments/catalog catalog=$registry/eshop/catalog.api:$imageTag'
ExecKube -cmd 'set image deployments/identity identity=$registry/eshop/identity.api:$imageTag'
ExecKube -cmd 'set image deployments/ordering ordering=$registry/eshop/ordering.api:$imageTag'
ExecKube -cmd 'set image deployments/marketing marketing=$registry/eshop/marketing.api:$imageTag'
ExecKube -cmd 'set image deployments/locations locations=$registry/eshop/locations.api:$imageTag'
ExecKube -cmd 'set image deployments/payment payment=$registry/eshop/payment.api:$imageTag'
ExecKube -cmd 'set image deployments/webmvc webmvc=$registry/eshop/webmvc:$imageTag'
ExecKube -cmd 'set image deployments/webstatus webstatus=$registry/eshop/webstatus:$imageTag'
ExecKube -cmd 'set image deployments/webspa webspa=$registry/eshop/webspa:$imageTag'
2017-05-16 12:13:49 +02:00
}
2017-05-25 09:24:37 +02:00
Write-Host " Execute rollout... " -ForegroundColor Yellow
ExecKube -cmd 'rollout resume deployments/basket'
ExecKube -cmd 'rollout resume deployments/catalog'
ExecKube -cmd 'rollout resume deployments/identity'
ExecKube -cmd 'rollout resume deployments/ordering'
2017-06-19 19:01:00 +02:00
ExecKube -cmd 'rollout resume deployments/marketing'
ExecKube -cmd 'rollout resume deployments/locations'
ExecKube -cmd 'rollout resume deployments/payment'
2017-05-25 09:24:37 +02:00
ExecKube -cmd 'rollout resume deployments/webmvc'
ExecKube -cmd 'rollout resume deployments/webstatus'
ExecKube -cmd 'rollout resume deployments/webspa'
2017-02-16 10:49:58 -08:00
2017-05-19 17:19:29 -07:00
Write-Host " WebSPA is exposed at http:// $frontendUrl , WebMVC at http:// $frontendUrl /webmvc, WebStatus at http:// $frontendUrl /webstatus " -ForegroundColor Yellow