forked from cloudfoundry/cf-deployment-concourse-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared-functions
493 lines (426 loc) · 14.7 KB
/
shared-functions
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
#!/bin/bash -eux
check_bbl_input_params() {
set +x
# Checks that we have IaaS credentials for GCP or AWS or Azure or OpenStack or vSphere
if [ ! -z "${BBL_GCP_SERVICE_ACCOUNT_KEY}" ] || \
[ ! -z "${BBL_AWS_ACCESS_KEY_ID}" -a ! -z "${BBL_AWS_SECRET_ACCESS_KEY}" ] || \
[ ! -z "${BBL_AZURE_CLIENT_ID}" -a ! -z "${BBL_AZURE_CLIENT_SECRET}" -a ! -z "${BBL_AZURE_TENANT_ID}" -a ! -z "${BBL_AZURE_SUBSCRIPTION_ID}" ] || \
[ ! -z "${BBL_OPENSTACK_USERNAME}" -a ! -z "${BBL_OPENSTACK_PASSWORD}" -a ! -z "${BBL_OPENSTACK_DOMAIN}" -a ! -z "${BBL_OPENSTACK_PROJECT}" ] || \
[ ! -z "${BBL_VSPHERE_VCENTER_USER}" -a ! -z "${BBL_VSPHERE_VCENTER_PASSWORD}" -a ! -z "${BBL_VSPHERE_VCENTER_IP}" ]; then
return 0
else
echo 'The BBL task must be configured with IaaS credentials. Please configure either `BBL_GCP_SERVICE_ACCOUNT_KEY`, or the `BBL_AWS_ACCESS_KEY_ID` `BBL_AWS_SECRET_ACCESS_KEY` pair, or `BBL_AZURE_CLIENT_ID` `BBL_AZURE_CLIENT_SECRET` `BBL_AZURE_TENANT_ID` `BBL_AZURE_SUBSCRIPTION_ID` tetrad, or `BBL_OPENSTACK_USERNAME` `BBL_OPENSTACK_PASSWORD` `BBL_OPENSTACK_DOMAIN` `BBL_OPENSTACK_PROJECT` tetrad'
exit 1
fi
set -x
}
function check_input_params() {
set +x
if [ -z "$MANIFEST_FILE" ]; then
echo "MANIFEST_FILE has not been set"
exit 1
fi
if [ -z "$SYSTEM_DOMAIN" -a ! -d toolsmiths-env ]; then
echo "SYSTEM_DOMAIN or toolsmiths-env needs to be passed in"
exit 1
fi
if ${FAIL_ON_DOWNTIME}; then
if [ -z "${DEPLOY_WITH_UPTIME_MEASUREMENTS}" ]; then
echo "FAIL_ON_DOWNTIME requires that DEPLOY_WITH_UPTIME_MEASUREMENTS be true. Exiting."
exit 1
fi
fi
set -x
}
function load_from_json_config() {
set +ux
if [ -z "${BBL_JSON_CONFIG}" ]; then
set -ux
return
fi
keys=$( jq -r 'keys[]' "${BBL_JSON_CONFIG}" )
for key in $keys; do
if [[ -v $key ]]; then
echo "Getting $key from BBL_JSON_CONFIG"
export $key=$( jq -r ".$key" "${BBL_JSON_CONFIG}" )
fi
done
set -ux
}
function commit_bbl_state_dir {
local root_dir
root_dir="${1}"
local commit_message
commit_message="${2}"
pushd "${root_dir}/bbl-state/${BBL_STATE_DIR}"
status="$(git status --porcelain)"
if [[ -n "$status" ]]; then
set_git_config
git add --all .
git commit -m "${commit_message}"
fi
popd
pushd "${root_dir}"
shopt -s dotglob
cp -R "bbl-state/." "updated-bbl-state/"
popd
}
function set_git_config() {
git config user.name "${GIT_COMMIT_USERNAME}"
git config user.email "${GIT_COMMIT_EMAIL}"
}
function setup_bosh_env_vars() {
set +x
if [ -d toolsmiths-env ]; then
eval "$(bbl print-env --metadata-file toolsmiths-env/metadata)"
export SYSTEM_DOMAIN="$(cat toolsmiths-env/metadata | jq -r '.cf.api_url | sub("api."; "")')"
export TCP_DOMAIN="tcp.${SYSTEM_DOMAIN}"
else
if [ -d bbl-state ]; then
pushd "bbl-state/${BBL_STATE_DIR}"
eval "$(bbl print-env)"
popd
else
echo "Must provide either toolsmiths-env or bbl-state as an input"
exit 1
fi
fi
set -x
}
function bosh_interpolate() {
set +u
local root_dir
root_dir="${1}"
local release_name
release_name="${2}"
local release_tarball_name
release_tarball_name="${3}"
set -u
local bosh_manifest
bosh_manifest="cf-deployment/${MANIFEST_FILE}"
local arguments
arguments="-v system_domain=${SYSTEM_DOMAIN}"
for op in ${OPS_FILES}
do
arguments="${arguments} -o ops-files/${op}"
done
for vf in ${VARS_FILES}
do
arguments="${arguments} -l vars-files/${vf}"
done
if [ -n "${release_name}" ]; then
if [ -n "${release_tarball_name}" ]; then
tarball_path="${root_dir}/release/${release_tarball_name}"
version=$(tar xzf "${tarball_path}" -O release.MF | yq -r '.version // "latest"')
cat << EOF > create-provided-release.yml
---
- type: replace
path: /releases/name=${release_name}
value:
name: ${release_name}
url: file://${tarball_path}
version: ${version}
EOF
else
cat << EOF > create-provided-release.yml
---
- type: replace
path: /releases/name=${release_name}
value:
name: ${release_name}
version: create
url: file://${root_dir}/release
EOF
fi
arguments="${arguments} -o create-provided-release.yml"
fi
INTERPOLATED_MANIFEST=$(mktemp)
export INTERPOLATED_MANIFEST
# We are intentionally passing a series of arguments here:
# shellcheck disable=SC2086
local bosh_arguments
bosh_arguments=""
bosh \
-n \
interpolate ${arguments} \
${bosh_arguments} \
"${bosh_manifest}" \
> "${INTERPOLATED_MANIFEST}"
}
write_uptimer_deploy_config() {
local deployment_name
deployment_name=${1}
local manifest
manifest=${2}
# The remaining positional parameters are consumed by
# jq as bosh deploy args
shift 2
# Give bogus values for TCP_DOMAIN, TCP_PORT, and
# AVAILABLE_PORT so that we don't have to do jq magic.
local tcp_domain
tcp_domain=${TCP_DOMAIN:-" "}
local tcp_port
tcp_port=${TCP_PORT:-"-1"}
local available_port
available_port=${AVAILABLE_PORT:-"-1"}
set +x
local cf_admin_password
cf_admin_password=$(get_password_from_credhub cf_admin_password)
echo '{}' | jq --arg cf_api api.${SYSTEM_DOMAIN} \
--arg admin_password ${cf_admin_password} \
--arg app_domain ${SYSTEM_DOMAIN} \
--arg manifest ${manifest} \
--arg deployment_name ${deployment_name} \
--arg run_app_syslog_availability ${MEASURE_SYSLOG_AVAILABILITY} \
--arg run_tcp_availability ${MEASURE_TCP_AVAILABILITY} \
--arg tcp_domain "${tcp_domain}" \
--arg tcp_port ${tcp_port} \
--arg available_port ${available_port} \
--arg app_pushability ${APP_PUSHABILITY_THRESHOLD} \
--arg app_stats ${APP_STATS_THRESHOLD} \
--arg http_availability ${HTTP_AVAILABILITY_THRESHOLD} \
--arg tcp_availability ${TCP_AVAILABILITY_THRESHOLD} \
--arg recent_logs ${RECENT_LOGS_THRESHOLD} \
--arg streaming_logs ${STREAMING_LOGS_THRESHOLD} \
--arg use_single_app_instance ${USE_SINGLE_APP_INSTANCE} \
--arg app_syslog_availability ${APP_SYSLOG_AVAILABILITY_THRESHOLD} \
--args \
'{
"while": [{
"command":"bosh",
"command_args":["--tty", "-n", "deploy", $manifest, "-d", $deployment_name, $ARGS.positional[]]
}],
"cf": {
"api": $cf_api,
"app_domain": $app_domain,
"admin_user": "admin",
"admin_password": $admin_password,
"tcp_domain": $tcp_domain,
"available_port": $available_port | tonumber,
"tcp_port": $tcp_port | tonumber,
"use_single_app_instance": $use_single_app_instance | ascii_downcase | contains("true")
},
"allowed_failures": {
"app_pushability": $app_pushability | tonumber,
"app_stats": $app_stats | tonumber,
"http_availability": $http_availability | tonumber,
"tcp_availability": $tcp_availability | tonumber,
"recent_logs": $recent_logs | tonumber,
"streaming_logs": $streaming_logs | tonumber,
"app_syslog_availability": $app_syslog_availability | tonumber
},
"optional_tests": {
"run_app_syslog_availability": $run_app_syslog_availability | ascii_downcase | contains("true"),
"run_tcp_availability": $run_tcp_availability | ascii_downcase | contains("true")
}
}' \
-- ${@}
set -x
}
function uptimer_bosh_deploy() {
if ${MEASURE_SYSLOG_AVAILABILITY}; then
if [ -z "${TCP_DOMAIN}" ] || [ -z "${AVAILABLE_PORT}" ]; then
echo "Both TCP_DOMAIN and AVAILABLE_PORT are required to run syslog availability measurement."
exit 1
fi
fi
local deployment_name
deployment_name=$(bosh interpolate "${INTERPOLATED_MANIFEST}" --path /name)
uptimer_config=$(mktemp)
write_uptimer_deploy_config "${deployment_name}" "${INTERPOLATED_MANIFEST}" ${@} > ${uptimer_config}
pushd cf-deployment > /dev/null
set +e
local exitStatus
uptimer -configFile ${uptimer_config}
exitStatus=$?
set -e
popd
if [ "${FAIL_ON_DOWNTIME}" = "false" ]; then
# exitStatus 64 means that there was downtime, but the deployment was successful
# exitStatus 70 means that there was a measurement setup failure, but the deployment was successful
if [ $exitStatus == 64 ] || [ $exitStatus == 70 ]; then
exit 0
fi
fi
exit $exitStatus
}
function bosh_deploy() {
local deployment_name
deployment_name=$(bosh interpolate "${INTERPOLATED_MANIFEST}" --path /name)
if ${DEPLOY_WITH_UPTIME_MEASUREMENTS}; then
uptimer_bosh_deploy ${@}
else
pushd cf-deployment > /dev/null
bosh \
-n \
-d "${deployment_name}" \
deploy \
"${INTERPOLATED_MANIFEST}" \
${@}
popd
fi
}
function remove_credentials_from_credhub_in_directory() {
local directory_name
directory_name=$1
set +x
credentials_in_directory=$(credhub f -p ${directory_name} -j | jq -r .credentials[].name)
echo "Deleting credentials in the ${directory_name} directory from CredHub:"
for cred in ${credentials_in_directory}; do
echo "Deleting ${cred} from CredHub"
credhub d -n $cred > /dev/null
done
set -x
}
function remove_credentials_from_credhub() {
local directory_name
local deployment_name
if [ -d toolsmiths-env ]; then
deployment_name=$(jq -r .name toolsmiths-env/metadata)
directory_name="/bosh-${deployment_name}"
else
local director_name
director_name=$(jq -r .bosh.directorName bbl-state/${BBL_STATE_DIR}/bbl-state.json)
deployment_name=$(bosh interpolate "${INTERPOLATED_MANIFEST}" --path /name)
directory_name="/${director_name}/${deployment_name}"
set +x
credentials=$(credhub f -n /dns -j | jq -r .credentials[].name)
echo "Deleting DNS credentials from CredHub:"
for cred in ${credentials}; do
echo "Deleting ${cred} from CredHub"
credhub d -n $cred > /dev/null
done
set -x
fi
remove_credentials_from_credhub_in_directory ${directory_name}
}
write_gcp_service_account_key() {
set +x
if [ -f "${BBL_GCP_SERVICE_ACCOUNT_KEY}" ]; then
cp "${BBL_GCP_SERVICE_ACCOUNT_KEY}" /tmp/google_service_account.json
else
echo "${BBL_GCP_SERVICE_ACCOUNT_KEY}" > /tmp/google_service_account.json
fi
export BBL_GCP_SERVICE_ACCOUNT_KEY="/tmp/google_service_account.json"
set -x
}
get_password_from_credhub() {
set +x
local bosh_manifest_password_variable_name=$1
local credential_path=$(credhub find -j -n ${bosh_manifest_password_variable_name} | jq -r .credentials[].name )
local credential_paths_len=$(echo ${credential_path} | tr ' ' '\n' | wc -l)
if [ "${credential_paths_len}" -gt 1 ]; then
echo "ambiguous ${bosh_manifest_password_variable_name} variable name; expected one got ${credential_paths_len}" >&2
echo "${credential_path}" | tr ' ' '\n' >&2
return
elif [ "${credential_paths_len}" -eq 0 ]; then
echo "${bosh_manifest_password_variable_name} variable not found" >&2
return
fi
echo $(credhub find -j -n ${bosh_manifest_password_variable_name} | jq -r .credentials[].name | xargs credhub get -j -n | jq -r .value)
set -x
}
setup_password_from_credhub() {
set +x
local bosh_manifest_password_variable_name=$1
local environment_variable_name=$1
if [ "$#" -gt 1 ]; then
environment_variable_name=$2
fi
local credential_path=$(credhub find -j -n ${bosh_manifest_password_variable_name} | jq -r .credentials[].name )
local credential_paths_len=$(echo ${credential_path} | tr ' ' '\n' | wc -l)
if [ "${credential_paths_len}" -gt 1 ]; then
echo "ambiguous password variable name; expected one got ${credential_paths_len}"
echo "${credential_path}" | tr ' ' '\n'
exit 1
fi
export "${environment_variable_name}"=$(credhub find -j -n ${bosh_manifest_password_variable_name} | jq -r .credentials[].name | xargs credhub get -j -n | jq -r .value)
set -x
}
upload_stemcells() {
local arguments=''
for op in ${OPS_FILES}
do
arguments="${arguments} -o ops-files/${op}"
done
bosh interpolate ${arguments} cf-deployment/${MANIFEST_FILE} > /tmp/cf.yml
local stemcells_json=$(ruby -rjson -ryaml -e "puts YAML.load_file('/tmp/cf.yml').to_json" | jq .stemcells)
local size=$(echo ${stemcells_json} | jq 'length')
if [ ${size} -eq 0 ]; then
echo "Error: Did not find any stemcells to upload."
exit 1
fi
for i in `seq 0 $((size - 1))`
do
local os=$(echo ${stemcells_json} | jq -r .[$i].os)
local version=$(echo ${stemcells_json} | jq -r .[$i].version)
upload_stemcell ${os} ${version}
done
}
upload_stemcell() {
# Read potentially variable stemcell paramaters out of cf-deployment with bosh
local os
os=$1
local version
version=$2
# Hardcode a couple of stable stemcell paramaters
local stemcells_url
stemcells_url="https://bosh.io/d/stemcells"
# Ask bosh if it already has our OS / version stemcell combination
# As of this writing, the new bosh cli doesn't have --skip-if-exists
set +e
local existing_stemcell
existing_stemcell=$(bosh stemcells | grep "${os}" | awk '{print $2}' | tr -d "\*" | grep ^"${version}"$ )
set -e
local stemcell_name
local infrastructure
if [ -d toolsmiths-env ]; then
infrastructure="gcp"
stemcell_name="bosh-google-kvm"
else
infrastructure="$(jq -r .iaas bbl-state/${BBL_STATE_DIR}/bbl-state.json)"
if [ "${BOSH_LITE}" == "true" ]; then
infrastructure="bosh-lite"
fi
if [ "$infrastructure" = "bosh-lite" ]; then
stemcell_name="bosh-warden-boshlite"
elif [ "$infrastructure" = "aws" ]; then
stemcell_name="bosh-aws-xen-hvm"
elif [ "$infrastructure" = "gcp" ]; then
stemcell_name="bosh-google-kvm"
elif [ "$infrastructure" = "vsphere" ]; then
stemcell_name="bosh-vsphere-esxi"
elif [ "$infrastructure" = "azure" ]; then
stemcell_name="bosh-azure-hyperv"
elif [ "$infrastructure" = "openstack" ]; then
stemcell_name="bosh-openstack-kvm"
fi
fi
stemcell_name="${stemcell_name}-${os}-go_agent"
if [ "$version" = "latest" ]; then
full_stemcell_url="${stemcells_url}/${stemcell_name}"
else
full_stemcell_url="${stemcells_url}/${stemcell_name}?v=${version}"
fi
# If bosh already has our stemcell, exit 0
if [ "${existing_stemcell}" ]; then
echo "Task bosh-upload-stemcell-from-cf-deployment:"
echo "Stemcell '${stemcell_name}/${version}' already exists. Skipping..."
return
fi
# If bosh.io doesn't have our stemcell, exit 2
set +e
local stemcell_exists
wget -S --spider "${full_stemcell_url}" > /dev/null 2>&1
stemcell_exists=$?
if [ ${stemcell_exists} -ne 0 ]; then
echo "Error: Could not find a '$os' stemcell for IaaS '$infrastructure' on bosh.io. Please double-check that the IaaS/OS combination is supported."
exit 2 #POSIX 'No such file or directory'
fi
set -e
# ... otherwise, begin the upload process
bosh \
-n \
upload-stemcell \
"${full_stemcell_url}"
}