forked from zachChilders/just-add-water
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.ps1
295 lines (274 loc) · 10.8 KB
/
deploy.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
param(
[string]$EnclaveName = "sbd",
[string]$HostName = "wp.mics-sbd.dev"
)
$ErrorActionPreference = "Stop"
# Bootstrap Dependencies
Import-Module -Name "./modules/jaw"
"Requirements" | % {
Install-Module -Name $_ -Force
Import-Module -Name $_
}
# Set up some directories
$RepoRoot = $PSScriptRoot
$OutputDir = "$PSScriptRoot/out"
$TemplateDir = "$RepoRoot/templates"
$SecurityDir = "$RepoRoot/security"
# Common infrastructure
$tf_share = "sbdtfstorage"
$kv_name = "sbdvault"
$acr_name = "sbdacrglobal.azurecr.io"
# Detect if we're running in a github action
$RunningInCI = ([boolean] $env:GITHUB_CLIENT_SECRET -and [boolean] $env:GITHUB_TENANT)
# Auth Azure and gather subscription secrets
Push-Namespace "Dev Environment" {
@{
Describe = "Authenticate Azure Session"
Test = { [boolean] (az account show) }
Set = {
if ($RunningInCI) {
az login --service-principal -u "http://sbdsp" -p $env:GITHUB_CLIENT_SECRET --tenant $env:GITHUB_TENANT
# Set up SP variables for terraform to detect headless mode
$account = (az account list | ConvertFrom-Json)
$env:ARM_SUBSCRIPTION_ID = $account.id
$env:ARM_TENANT_ID = $account.tenantId
}
else {
az login
}
}
}
@{
Describe = "Inject Secrets into Session"
Set = {
# Parse all variables
$KEYVAULTNAME = $kv_name
$SECRETS = ( $(az keyvault secret list --vault-name $KEYVAULTNAME | jq '.[].id' -r | sed 's/.*\/\([^/]\+\)$/\1/') )
$SECRETS | % {
$SECRET = $(az keyvault secret show --name $_ --vault-name $KEYVAULTNAME | jq '.value' -r)
$NAME = $_.Replace("-", "_")
[Environment]::SetEnvironmentVariable($NAME, $SECRET)
}
if ($RunningInCI) {
# Set the last variables for terraform to detect headless mode
$env:ARM_CLIENT_ID = $env:TF_VAR_client_id
$env:ARM_CLIENT_SECRET = $env:TF_VAR_client_secret
}
}
}
} `
| Invoke-Requirement `
| Format-Checklist
# Provision Infra
Push-Namespace "Terraform" {
@{
Describe = "Enter Terraform Context"
Test = { (Get-Location).Path -eq "$RepoRoot/tf/enclave" }
Set = { Set-Location "$RepoRoot/tf/enclave" }
}
@{
Describe = "Initialize Terraform Environment"
Test = { Test-Path "$PSScriptRoot/tf/enclave/.terraform/terraform.tfstate" }
Set = {
# Prep Terraform with a few last minute variables
$env:TF_IN_AUTOMATION = "true"
$env:TF_VAR_name_prefix = $EnclaveName
terraform init -backend-config="storage_account_name=$($tf_share)" `
-backend-config="container_name=tfstate" `
-backend-config="access_key=$($env:tf_storage_key)" `
-backend-config="key=$EnclaveName.tfstate"
# Ensure state is synchronized across deployments with production
terraform refresh
}
}
@{
Describe = "Plan terraform environment"
Test = { Test-Path "$OutputDir/$EnclaveName.plan" }
Set = {
New-Item -Path "$OutputDir" -ItemType Directory -Force
terraform plan -out "$OutputDir/$EnclaveName.plan" -refresh=true
}
}
@{
Describe = "Apply Terraform plan"
Test = { [boolean] (terraform output host) }
Set = { terraform apply "$OutputDir/$EnclaveName.plan" }
}
@{
Describe = "Generate k8s File"
Test = { Test-Path "$OutputDir/$EnclaveName" }
Set = { terraform output kube_config | Out-File "$OutputDir/$EnclaveName" }
}
@{
Describe = "Restore Location"
Test = { (Get-Location).Path -eq $RepoRoot }
Set = { Set-Location $RepoRoot }
}
} `
| Invoke-Requirement `
| Format-Checklist
# Docker cooking
Push-Namespace "Docker" {
@{
Describe = "Generate Application Manifest"
Set = {
# Try to pull images to save time
$DockerImages = az acr repository list -n sbdacrglobal -o json | ConvertFrom-Json
Get-ContainerNames | % {
$ImageName = $_.ImageName
if ($ImageName -in $DockerImages) { docker pull $env:acr_login_server/$ImageName }
}
# Generate config JSON for remaining steps to read
Set-k8sConfig -AppPath "./app" -OutPath $OutputDir
}
}
} `
| Invoke-Requirement `
| Format-Checklist
# Application Deployment
Push-Namespace "Application" {
Push-Namespace "Kubernetes Deployment" {
@{
Describe = "Load k8s config"
Set = {
# Kubectl reads this variable to authenticate
$env:KUBECONFIG = "$OutputDir/$EnclaveName"
}
}
@{
Describe = "Generate kustomization.yaml"
Set = {
# Read manifest and templates
$list = Get-Content $OutputDir/k8s.json | ConvertFrom-Json
# Populate templates from manifest info
$list | % {
$deploy_data = @{
"sql_password" = $env:TF_VAR_sql_password
}
$deploy_template = (Get-Content $_.Name | Join-String -Separator "`n" )
Expand-Template -Template $deploy_template -Data $deploy_data | Out-File "$($_.Path)/kustomization.yaml"
}
}
}
@{
Describe = "Application deployment"
Set = {
# Read manifest and templates
$list = Get-Content $OutputDir/k8s.json | ConvertFrom-Json
$list | % {
kubectl apply -k $_.Path
}
}
}
}
Push-Namespace "Reverse Proxy" {
@{
Describe = "Upload TLS Cert"
Set = {
$tls_key = (az keyvault secret show --vault-name $kv_name --name tls-key | ConvertFrom-Json).value
$tls_cert = (az keyvault secret show --vault-name $kv_name --name tls-cert | ConvertFrom-Json).value
$tls_key | Out-File $OutputDir/tls-key.pem
$tls_cert | Out-File $OutputDir/tls-cert.pem
kubectl create secret tls $HostName --key "$OutputDir/tls-key.pem" --cert "$OutputDir/tls-cert.pem"
"$OutputDir/tls-key.pem", "$OutputDir/tls-cert.pem" | % { Remove-Item $_ }
}
}
@{
Describe = "Deploy Nginx"
Set = {
kubectl apply -f $TemplateDir/nginx-mandatory.yaml
kubectl apply -f $TemplateDir/nginx-azure.yaml
}
}
@{
Describe = "Configure Nginx"
Set = {
$k8s_config = Get-Content $OutputDir/k8s.json | ConvertFrom-Json
$nginx_config = @{
"image_name" = $k8s_config.ImageName
"host_name" = $HostName
}
$nginx_template = (Get-Content $TemplateDir/nginx.yaml | Join-String -Separator "`n" )
Expand-Template -Template $nginx_template -Data $nginx_config | Out-File $OutputDir/nginx.yaml
kubectl apply -f $OutputDir/nginx.yaml
}
}
}
Push-Namespace "Resiliency" {
@{
Describe = "Configure Autoscale"
Test = { kubectl get hpa }
Set = {
# These values are pretty arbitrary and not measured at all.
(kubectl get deployment -o json | ConvertFrom-Json).items | % {
kubectl autoscale deployment $_.metadata.name --min=2 --max=5 --cpu-percent=80
}
}
}
@{
Describe = "Deploy kured"
Test = { [boolean] (kubectl describe nodes | grep kured) }
Set = {
# TODO: Cache kured fork
kubectl apply -f $TemplateDir/kured.yaml
}
}
@{
Describe = "Create DNS Name"
Test = {
# By default, there's no domain label on the public ip resource
$rg = "MC_$($EnclaveName)_$($EnclaveName)_southcentralus"
$name = (az network public-ip list -g $rg | ConvertFrom-Json).name
(az network public-ip show -g $rg -n $name | ConvertFrom-Json).dnsSettings.domainNameLabel -eq "$EnclaveName"
}
Set = {
# Set the domain label - it will be $EnclaveName.$Region.cloudapp.azure.com
$rg = "MC_$($EnclaveName)_$($EnclaveName)_southcentralus"
$name = (az network public-ip list -g $rg | ConvertFrom-Json).name
az network public-ip update -g $rg -n $name --dns-name "$EnclaveName"
}
}
@{
Describe = "Update Traffic Manager"
Test = {
# Check for existence of our endpoint
$rg = "sbd-global"
(az network traffic-manager endpoint list -g $rg --profile-name "sbd-atm" | ConvertFrom-Json).name -eq $EnclaveName
}
Set = {
# Add our endpoint with a weight of 1
$rg = "sbd-global"
$iprg = "MC_$($EnclaveName)_$($EnclaveName)_southcentralus"
$id = (az network public-ip list -g $iprg | ConvertFrom-Json).id
az network traffic-manager endpoint create -g $rg --profile-name "sbd-atm" -n $EnclaveName --type azureEndpoints --target-resource-id $id --endpoint-status enabled --weight 1
}
}
}
Push-Namespace "Hardening" {
@{
Describe = "Generate Security Templates"
Test = { Test-Path "$SecurityDir/firewall.yml" }
Set = {
# Read block-egress template
$firewall_template = (Get-Content $TemplateDir/block-egress.yml | Join-String -Separator "`n")
# Populate block-egress template with service name
$firewall_data = @{
"service_name" = "wordpress"
}
Expand-Template -Template $firewall_template -Data $firewall_data | Out-File $SecurityDir/firewall.yml -Append
}
}
@{
Describe = "Security Policy Deployment"
# TODO: Set a test here to ensure proper application
Set = {
# Apply all templates in the security path
Get-ChildItem $SecurityDir | % {
kubectl apply -f $_
}
}
}
}
} `
| Invoke-Requirement `
| Format-Checklist