-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-deploy
executable file
·598 lines (506 loc) · 19.2 KB
/
auto-deploy
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#!/usr/bin/env bash
set -e
[[ "${TRACE}" ]] &&
set -x
export AUTO_DEPLOY_ENVIRONMENT_VALUES_FILE=/tmp/auto-deploy-environment-values.yaml
export RELEASE_NAME=${HELM_RELEASE_NAME:-${CI_ENVIRONMENT_SLUG}}
export POSTGRESQL_VERSION=${POSTGRESQL_VERSION:-"11.10.0"}
export REDIS_VERSION=${REDIS_VERSION:-"6.0.9"}
export MYSQL_VERSION=${MYSQL_VERSION:-"8.0.22"}
export MONGODB_VERSION=${MONGODB_VERSION:-"4.4.3"}
export MARIADB_VERSION=${MARIADB_VERSION:-"10.5.8"}
export POSTGRESQL_CHART_VERSION=${POSTGRESQL_CHART_VERSION:-"10.2.1"}
export REDIS_CHART_VERSION=${REDIS_CHART_VERSION:-"12.4.0"}
export MYSQL_CHART_VERSION=${MYSQL_CHART_VERSION:-"8.2.4"}
export MONGODB_CHART_VERSION=${MONGODB_CHART_VERSION:-"10.3.6"}
export MARIADB_CHART_VERSION=${MARIADB_CHART_VERSION:-"9.2.2"}
APPS=(mariadb mongodb mysql redis postgresql)
for i in "${APPS[@]}"; do
# shellcheck disable=SC1090
source <(
cat <<EOF
export ${i^^}_RELEASE_NAME="${RELEASE_NAME}-${i}";
export ${i^^}_DB="\${${i^^}_DB:-${RELEASE_NAME}}"
export ${i^^}_USER="\${${i^^}_USER:-${RELEASE_NAME}}"
export ${i^^}_PASSWORD="\${${i^^}_PASSWORD:-${RELEASE_NAME}}"
export ${i^^}_ROOT_PASSWORD="\${${i^^}_ROOT_PASSWORD:-${RELEASE_NAME}}"
EOF
)
done
export ROLLOUT_RESOURCE_TYPE=${ROLLOUT_RESOURCE_TYPE:-"deployment"}
export ASSETS_DIR='/assets'
export CHART='lambda'
export ASSETS_CHART_DIR="${ASSETS_DIR}/${CHART}"
function check_kube_domain() {
if [[ -z "$KUBE_INGRESS_BASE_DOMAIN" ]]; then
echo "In order to deploy or use Review Apps,"
echo "KUBE_INGRESS_BASE_DOMAIN variables must be set"
echo "From 11.8, you can set KUBE_INGRESS_BASE_DOMAIN in cluster settings"
echo "or by defining a variable at group or project level."
echo "You can also manually add it in .gitlab-ci.yml"
false
else
true
fi
}
function download_chart() {
add_chart_repositories
if [[ -d chart ]]; then
echo "Download skipped. Using the chart at local path 'chart/'..."
elif [[ -n "$AUTO_DEVOPS_CHART" ]]; then
# user specified a custom chart to use, but it can be a local directory or a remote chart
if [[ -d "$AUTO_DEVOPS_CHART" ]]; then
echo "Download skipped. Using the chart at local path ${AUTO_DEVOPS_CHART@Q} (moving to 'chart/' first)..."
mv "$AUTO_DEVOPS_CHART" chart/
else
echo "Downloading remote chart ${AUTO_DEVOPS_CHART@Q}..."
helm pull "$AUTO_DEVOPS_CHART" --untar
local auto_chart_name
auto_chart_name=$(basename "$AUTO_DEVOPS_CHART")
auto_chart_name=${auto_chart_name%.tgz}
auto_chart_name=${auto_chart_name%.tar.gz}
if [[ "$auto_chart_name" != "chart" ]]; then
mv "$auto_chart_name" chart
fi
fi
else
echo "Download skipped. Using the default chart included in auto-deploy-image..."
cp -R "$ASSETS_CHART_DIR" chart
fi
if [[ -f chart/Chart.lock ]]; then
helm dependency build chart/
else
helm dependency update chart/
fi
}
function add_chart_repositories() {
helm repo add stable https://charts.helm.sh/stable
helm repo add bitnami https://charts.bitnami.com/bitnami
# Additionaly, users can specify their custom chart repository.
add_custom_chart_repository
}
function add_custom_chart_repository() {
if [[ -z "$AUTO_DEVOPS_CHART_REPOSITORY" ]]; then
return
fi
echo "Adding Helm chart repository ${AUTO_DEVOPS_CHART_REPOSITORY_NAME@Q}"
# repo should always be added when present, because any chart can have external dependencies
local helm_repo_auth=()
if [[ -n "$AUTO_DEVOPS_CHART_REPOSITORY_USERNAME" ]]; then
helm_repo_auth+=('--username' "$AUTO_DEVOPS_CHART_REPOSITORY_USERNAME")
fi
if [[ -n "$AUTO_DEVOPS_CHART_REPOSITORY_PASSWORD" ]]; then
helm_repo_auth+=('--password' "$AUTO_DEVOPS_CHART_REPOSITORY_PASSWORD")
fi
helm repo add \
"${AUTO_DEVOPS_CHART_REPOSITORY_NAME}" \
"${AUTO_DEVOPS_CHART_REPOSITORY}" \
"${helm_repo_auth[@]}"
}
function ensure_namespace() {
kubectl get namespace "${KUBE_NAMESPACE}" ||
kubectl create namespace "${KUBE_NAMESPACE}"
}
function write_environment_values_file() {
export GITLAB_CI=${GITLAB_CI:-false}
if test "${GITLAB_CI}" == "true"; then
yq eval --null-input '.gitlab = true' >"${AUTO_DEPLOY_ENVIRONMENT_VALUES_FILE}"
fi
for i in "${APPS[@]}"; do
# shellcheck disable=SC1090
source <(
cat <<EOF
if [[ "\${${i^^}_ENABLED}" == "true" ]]; then
yq eval '.application.${i}_uri = env(${i}_uri)' -i "${AUTO_DEPLOY_ENVIRONMENT_VALUES_FILE}"
fi
EOF
)
done
# Helm 3 does not like `--set image.secrets[0]=""`
if [[ "$CI_PROJECT_VISIBILITY" != "public" ]]; then
yq eval '.image.secrets[0].name = "gitlab-registry-"+env(CI_PROJECT_PATH_SLUG)' -i "${AUTO_DEPLOY_ENVIRONMENT_VALUES_FILE}"
else
yq eval '.image.secrets = null' -i "${AUTO_DEPLOY_ENVIRONMENT_VALUES_FILE}"
fi
}
function create_secret() {
echo "Create secret..."
if [[ "${CI_PROJECT_VISIBILITY}" == "public" ]]; then
return
fi
kubectl create secret -n "${KUBE_NAMESPACE}" \
docker-registry "gitlab-registry-${CI_PROJECT_PATH_SLUG}" \
--docker-server="${CI_REGISTRY}" \
--docker-username="${CI_DEPLOY_USER:-$CI_REGISTRY_USER}" \
--docker-password="${CI_DEPLOY_PASSWORD:-$CI_REGISTRY_PASSWORD}" \
--docker-email="${GITLAB_USER_EMAIL}" \
-o yaml --dry-run | kubectl replace -n "${KUBE_NAMESPACE}" --force -f -
}
function persist_environment_url() {
echo "${CI_ENVIRONMENT_URL}" >environment_url.txt
}
function auto_database_uri() {
# this function gets called for a DB only if *DB_ENABLED is true
# if *DB_ENABLED is true & *DB_URI is set by user
# then we will provision the DB
# but *DB_URI will still contain the value set by user
db=${1}
case "${db}" in
mariadb)
uri="jdbc:mysql://${MARIADB_RELEASE_NAME}:3306/${MARIADB_DB}?username=${MARIADB_USER}&password=${MARIADB_PASSWORD}"
echo "${MARIADB_URI-$uri}"
;;
mongodb)
uri="mongodb://${MONGODB_USER}:${MONGODB_PASSWORD}@${MONGODB_RELEASE_NAME}:27017/${MONGODB_DB}"
echo "${MONGODB_URI-$uri}"
;;
mysql)
uri="jdbc:mysql://${MYSQL_RELEASE_NAME}:3306/${MYSQL_DB}?username=${MYSQL_USER}&password=${MYSQL_PASSWORD}"
echo "${MYSQL_URI-$uri}"
;;
redis)
uri="redis://${REDIS_RELEASE_NAME}:6379?password=${REDIS_PASSWORD}"
echo "${REDIS_URI-$uri}"
;;
postgresql)
uri="postgres://${POSTGRESQL_USER}:${POSTGRESQL_PASSWORD}@${POSTGRESQL_RELEASE_NAME}:5432/${POSTGRESQL_DB}"
echo "${POSTGRESQL_URI-$uri}"
;;
*)
echo ""
;;
esac
}
function install_app() {
local name="${!2}"
local debug_flag=()
if [[ -n "$AUTO_DEVOPS_DEPLOY_DEBUG" ]]; then
debug_flag=('--debug')
fi
if [[ "${MARIADB_ENABLED}" == "true" ]]; then
yq eval --null-input '.auth.database = env(MARIADB_DB)' >mariadb-specific-values.yaml
yq eval '.auth.username = env(MARIADB_USER)' -i mariadb-specific-values.yaml
yq eval '.auth.password = env(MARIADB_PASSWORD)' -i mariadb-specific-values.yaml
yq eval '.auth.rootPassword = env(MARIADB_ROOT_PASSWORD)' -i mariadb-specific-values.yaml
fi
if [[ "${MYSQL_ENABLED}" == "true" ]]; then
yq eval --null-input '.auth.database = env(MYSQL_DB)' >mysql-specific-values.yaml
yq eval '.auth.username = env(MYSQL_USER)' -i mysql-specific-values.yaml
yq eval '.auth.password = env(MYSQL_PASSWORD)' -i mysql-specific-values.yaml
yq eval '.auth.rootPassword = env(MYSQL_ROOT_PASSWORD)' -i mysql-specific-values.yaml
fi
if [[ "${REDIS_ENABLED}" == "true" ]]; then
yq eval --null-input '.password = env(REDIS_PASSWORD)' >redis-specific-values.yaml
fi
if [[ "${MONGODB_ENABLED}" == "true" ]]; then
yq eval --null-input '.auth.database = env(MONGODB_DB)' >mongodb-specific-values.yaml
yq eval '.auth.username = env(MONGODB_USER)' -i mongodb-specific-values.yaml
yq eval '.auth.password = env(MONGODB_PASSWORD)' -i mongodb-specific-values.yaml
yq eval '.auth.rootPassword = env(MONGODB_ROOT_PASSWORD)' -i mongodb-specific-values.yaml
fi
if [[ "${POSTGRESQL_ENABLED}" == "true" ]]; then
yq eval --null-input '.postgresqlDatabase = env(POSTGRESQL_DB)' >postgresql-specific-values.yaml
yq eval '.postgresqlUsername = env(POSTGRESQL_USER)' -i postgresql-specific-values.yaml
yq eval '.postgresqlPassword = env(POSTGRESQL_PASSWORD)' -i postgresql-specific-values.yaml
yq eval '.postgresqlPostgresPassword = env(POSTGRESQL_ROOT_PASSWORD)' -i postgresql-specific-values.yaml
fi
# shellcheck disable=SC1090
source <(
cat <<EOF
local ${1}_helm_values_args=()
local ${1}_helm_values_file=\${${1^^}_HELM_UPGRADE_VALUES_FILE:-.gitlab/auto-deploy-${1}-values.yaml}
if [[ -f "\${${1}_helm_values_file}" ]]; then
echo "Using ${1^^} helm values file \${${1}_helm_values_file@Q}"
${1}_helm_values_args=(--values "\${${1}_helm_values_file}")
else
echo "No ${1^^} helm values file found at \${${1}_helm_values_file@Q}"
fi
# shellcheck disable=SC2086 # POSTGRESQL_HELM_UPGRADE_EXTRA_ARGS -- double quote variables to prevent globbing
helm upgrade --install \
--atomic \
"\${debug_flag[@]}" \
--wait \
--version "\${${1^^}_CHART_VERSION}" \
--set fullnameOverride="$name" \
--set image.tag="\${${1^^}_VERSION}" \
--values "${1}-specific-values.yaml" \
"\${${1}_helm_values_args[@]}" \
\${${1^^}_HELM_UPGRADE_EXTRA_ARGS} \
--namespace="${KUBE_NAMESPACE}" \
"$name" \
"bitnami/${1}"
EOF
)
}
# shellcheck disable=SC2153 # warns that my_var vs MY_VAR is a possible misspelling
# shellcheck disable=SC2154 # env_ADDITIONAL_HOSTS eval assignment is not recognized
function deploy() {
local track="${1-stable}"
local percentage="${2:-100}"
local name
name=$(deploy_name "$track")
local stable_name
stable_name=$(deploy_name stable)
for i in "${APPS[@]}"; do
# shellcheck disable=SC1090
source <(
cat <<EOF
if [[ "\${${i^^}_ENABLED}" == "true" ]]; then
install_app "${i}" "${i^^}_RELEASE_NAME"
fi
EOF
)
done
validate-chart-version "$(helm list --namespace "${KUBE_NAMESPACE}" --output json)" "chart" "$name"
for i in "${APPS[@]}"; do
# shellcheck disable=SC1090
source <(
cat <<EOF
if [[ \${${i^^}_ENABLED} == "true" ]]; then
${i}_uri=\$(auto_database_uri "${i}")
export ${i}_uri;
elif [[ -v ${i^^}_URI ]]; then
# if *DB_URI is passed by user then set it in pod
${i}_uri=\${${i^^}_URI}
export ${i}_uri;
fi
# if *DB_ENABLED is true & *DB_URI is set by user
# then we will provision the DB
# but *DB_URI will still contain the value set by user
EOF
)
done
local image_repository
local image_tag
if [[ -z "$CI_COMMIT_TAG" ]]; then
image_repository=${CI_APPLICATION_REPOSITORY:-$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG}
image_tag=${CI_APPLICATION_TAG:-$CI_COMMIT_SHA}
else
image_repository=${CI_APPLICATION_REPOSITORY:-$CI_REGISTRY_IMAGE}
image_tag=${CI_APPLICATION_TAG:-$CI_COMMIT_TAG}
fi
local replicas
replicas=$(get_replicas "$track")
local modsecurity_set_args=()
if [[ -n "$AUTO_DEVOPS_MODSECURITY_SEC_RULE_ENGINE" ]]; then
modsecurity_set_args=("--set" "ingress.modSecurity.enabled=true,ingress.modSecurity.secRuleEngine=$AUTO_DEVOPS_MODSECURITY_SEC_RULE_ENGINE")
fi
create_application_secret "$track"
local env_slug
env_slug=$(echo "${CI_ENVIRONMENT_SLUG//-/_}" | tr '[:lower:]' '[:upper:]')
local additional_hosts
eval local env_ADDITIONAL_HOSTS="\$${env_slug}_ADDITIONAL_HOSTS"
if [ -n "$env_ADDITIONAL_HOSTS" ]; then
additional_hosts="{$env_ADDITIONAL_HOSTS}"
elif [ -n "$ADDITIONAL_HOSTS" ]; then
additional_hosts="{$ADDITIONAL_HOSTS}"
fi
local helm_values_args=()
local helm_values_file=${HELM_UPGRADE_VALUES_FILE:-.gitlab/auto-deploy-values.yaml}
if [[ -f "${helm_values_file}" ]]; then
echo "Using helm values file ${helm_values_file@Q}"
helm_values_args=(--values "${helm_values_file}")
else
echo "No helm values file found at ${helm_values_file@Q}"
fi
local atomic_flag=()
if [[ "$AUTO_DEVOPS_ATOMIC_RELEASE" != "false" ]]; then
atomic_flag=('--atomic')
fi
local debug_flag=()
if [[ -n "$AUTO_DEVOPS_DEPLOY_DEBUG" ]]; then
debug_flag=('--debug')
fi
# TODO: Over time, migrate all --set values to this file, see https://gitlab.com/gitlab-org/cluster-integration/auto-deploy-image/-/issues/31
write_environment_values_file
if [[ -n "$DB_INITIALIZE" && -z "$(helm ls --namespace "${KUBE_NAMESPACE}" -q -f "^$stable_name$")" ]]; then
echo "Initializing service URL and database. No deployment will be created"
# shellcheck disable=SC2086 # HELM_UPGRADE_EXTRA_ARGS -- double quote variables to prevent globbing
helm upgrade --install \
"${atomic_flag[@]}" \
"${debug_flag[@]}" \
--wait \
--set project.repo="$CI_PROJECT_PATH_SLUG" \
--set project.slug="$CI_ENVIRONMENT_SLUG" \
--set project.env="$CI_ENVIRONMENT_NAME" \
--set project.url="$CI_ENVIRONMENT_URL" \
--set releaseOverride="$RELEASE_NAME" \
--set image.repository="$image_repository" \
--set-string image.tag="$image_tag" \
--set application.track="stable" \
--set application.secretName="$APPLICATION_SECRET_NAME" \
--set application.secretChecksum="$APPLICATION_SECRET_CHECKSUM" \
--set service.commonName="le-$CI_PROJECT_ID.$KUBE_INGRESS_BASE_DOMAIN" \
--set service.url="$CI_ENVIRONMENT_URL" \
--set service.additionalHosts="$additional_hosts" \
--set replicaCount="$replicas" \
--set ingress.canary.weight="${percentage}" \
--set application.initializeCommand="$DB_INITIALIZE" \
"${modsecurity_set_args[@]}" \
--values "$AUTO_DEPLOY_ENVIRONMENT_VALUES_FILE" \
"${helm_values_args[@]}" \
$HELM_UPGRADE_EXTRA_ARGS \
--namespace="${KUBE_NAMESPACE}" \
"$stable_name" \
chart/
fi
echo "Deploying new ${track@Q} release..."
# shellcheck disable=SC2086 # HELM_UPGRADE_EXTRA_ARGS -- double quote variables to prevent globbing
helm upgrade --install \
"${atomic_flag[@]}" \
"${debug_flag[@]}" \
--wait \
--set project.repo="$CI_PROJECT_PATH_SLUG" \
--set project.slug="$CI_ENVIRONMENT_SLUG" \
--set project.env="$CI_ENVIRONMENT_NAME" \
--set project.url="$CI_ENVIRONMENT_URL" \
--set releaseOverride="$RELEASE_NAME" \
--set image.repository="$image_repository" \
--set-string image.tag="$image_tag" \
--set application.track="$track" \
--set application.secretName="$APPLICATION_SECRET_NAME" \
--set application.secretChecksum="$APPLICATION_SECRET_CHECKSUM" \
--set service.commonName="le-$CI_PROJECT_ID.$KUBE_INGRESS_BASE_DOMAIN" \
--set service.url="$CI_ENVIRONMENT_URL" \
--set service.additionalHosts="$additional_hosts" \
--set replicaCount="$replicas" \
--set ingress.canary.weight="${percentage}" \
--set application.initializeCommand="" \
--set application.migrateCommand="$DB_MIGRATE" \
"${modsecurity_set_args[@]}" \
--values "$AUTO_DEPLOY_ENVIRONMENT_VALUES_FILE" \
"${helm_values_args[@]}" \
$HELM_UPGRADE_EXTRA_ARGS \
--namespace="${KUBE_NAMESPACE}" \
"$name" \
chart/
if [[ -z "$ROLLOUT_STATUS_DISABLED" ]]; then
kubectl rollout status -n "${KUBE_NAMESPACE}" -w "$ROLLOUT_RESOURCE_TYPE/$name"
fi
}
function scale() {
local track="${1-stable}"
local percentage="${2-100}"
local name
name=$(deploy_name "$track")
local replicas
replicas=$(get_replicas "$track")
if [[ -n "$(helm ls --namespace "${KUBE_NAMESPACE}" -q -f "^$name$")" ]]; then
helm upgrade --reuse-values \
--wait \
--set replicaCount="$replicas" \
--set ingress.canary.weight="${percentage}" \
--namespace="${KUBE_NAMESPACE}" \
"$name" \
chart/
fi
}
function delete_app() {
local name="${!2}"
if [[ -n "$(helm ls --namespace "${KUBE_NAMESPACE}" -q -f "^${name}$")" ]]; then
helm delete "${name}" --namespace "${KUBE_NAMESPACE}"
kubectl delete pvc -n "${KUBE_NAMESPACE}" -l "release=${name}"
fi
}
# This deletes the PVC for the database if the track is stable
function delete() {
local track="${1-stable}"
local name
name=$(deploy_name "$track")
if [[ -n "$(helm ls --namespace "${KUBE_NAMESPACE}" -q -f "^$name$")" ]]; then
helm delete "$name" --namespace "${KUBE_NAMESPACE}"
fi
for i in "${APPS[@]}"; do
if [[ "$track" == "stable" ]]; then
delete_app "${i}" "${i^^}_RELEASE_NAME"
fi
done
local secret_name
secret_name=$(application_secret_name "$track")
kubectl delete secret --ignore-not-found -n "${KUBE_NAMESPACE}" "$secret_name"
}
## Helper functions
##
# Extracts variables prefixed with K8S_SECRET_
# and creates a Kubernetes secret.
#
# e.g. If we have the following environment variables:
# K8S_SECRET_A=value1
# K8S_SECRET_B=multi\ word\ value
#
# Then we will create a secret with the following key-value pairs:
# data:
# A: dmFsdWUxCg==
# B: bXVsdGkgd29yZCB2YWx1ZQo=
#
function create_application_secret() {
local track="${1-stable}"
local k8s_secrets_file
# shellcheck disable=SC2155 # declare and assign separately to avoid masking return values.
export APPLICATION_SECRET_NAME=$(application_secret_name "$track")
k8s_secrets_file=$(mktemp)
auto-deploy-application-secrets-yaml "$k8s_secrets_file"
kubectl replace -f "$k8s_secrets_file" -n "${KUBE_NAMESPACE}" --force
# shellcheck disable=SC2002 # useless cat, prefer cmd < file
# shellcheck disable=SC2155 # declare and assign separately to avoid masking return values.
export APPLICATION_SECRET_CHECKSUM=$(cat "$k8s_secrets_file" | sha256sum | cut -d ' ' -f 1)
rm "$k8s_secrets_file"
}
function application_secret_name() {
local track="${1-stable}"
local name
name=$(deploy_name "$track")
echo "${name}-secret"
}
# shellcheck disable=SC2086
function deploy_name() {
local name="$RELEASE_NAME"
local track="${1-stable}"
if [[ "$track" != "stable" ]]; then
name="$name-$track"
fi
echo $name
}
# shellcheck disable=SC2086 # double quote to prevent globbing
# shellcheck disable=SC2153 # incorrectly thinks replicas vs REPLICAS is a misspelling
function get_replicas() {
local track="${1:-stable}"
local env_track
env_track=$(echo $track | tr '[:lower:]' '[:upper:]')
local env_slug
env_slug=$(echo ${CI_ENVIRONMENT_SLUG//-/_} | tr '[:lower:]' '[:upper:]')
local environment_track_replicas
local environment_replicas
eval environment_track_replicas=\$${env_track}_${env_slug}_REPLICAS
eval environment_replicas=\$${env_slug}_REPLICAS
local new_replicas
new_replicas=${environment_track_replicas}
new_replicas=${new_replicas:-$environment_replicas}
new_replicas=${new_replicas:-$REPLICAS}
if [[ -n "$new_replicas" ]]; then
# If zero replicas requested, then return 0
echo "$new_replicas"
else
# Return one if replicas is not specified
echo 1
fi
}
##
## End Helper functions
option=$1
case $option in
check_kube_domain) check_kube_domain ;;
download_chart) download_chart ;;
ensure_namespace) ensure_namespace ;;
create_secret) create_secret ;;
persist_environment_url) persist_environment_url ;;
auto_database_uri) auto_database_uri ;;
install_app) install_app "${@:2}" ;;
deploy) deploy "${@:2}" ;;
scale) scale "${@:2}" ;;
delete) delete "${@:2}" ;;
create_application_secret) create_application_secret "${@:2}" ;;
deploy_name) deploy_name "${@:2}" ;;
get_replicas) get_replicas "${@:2}" ;;
*) exit 1 ;;
esac