forked from cloudfoundry/cf-deployment-concourse-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared-functions
293 lines (251 loc) · 8.12 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
#!/bin/bash -eux
check_bbl_input_params() {
set +x
# Checks that we have IaaS credentials for GCP or AWS or Azure
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}" ]; 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'
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" ]; then
echo "SYSTEM_DOMAIN has not been set"
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 commit_bbl_state_dir {
local root_dir
root_dir="${1}"
local commit_message
commit_message="${2}"
pushd "${root_dir}/bbl-state/${BBL_STATE_DIR}"
if [[ -n $(git status --porcelain) ]]; 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
pushd "bbl-state/${BBL_STATE_DIR}"
eval "$(bbl print-env)"
popd
set -x
}
function bosh_interpolate() {
set +u
local root_dir
root_dir="${1}"
local release_name
release_name="${2}"
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
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
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}
# Give bogus values for TCP_DOMAIN and AVAILABLE_PORT
# so that we don't have to do jq magic.
local tcp_domain
tcp_domain=${TCP_DOMAIN:-" "}
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 tcp_domain "${tcp_domain}" \
--arg available_port ${available_port} \
--arg app_pushability ${APP_PUSHABILITY_THRESHOLD} \
--arg http_availability ${HTTP_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} \
'{
"while": [{
"command":"bosh",
"command_args":["--tty", "-n", "deploy", $manifest, "-d", $deployment_name]
}],
"cf": {
"api": $cf_api,
"app_domain": $app_domain,
"admin_user": "admin",
"admin_password": $admin_password,
"tcp_domain": $tcp_domain,
"available_port": $available_port | tonumber,
"use_single_app_instance": $use_single_app_instance | ascii_downcase | contains("true")
},
"allowed_failures": {
"app_pushability": $app_pushability | tonumber,
"http_availability": $http_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")
}
}'
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 bosh_update_dns_runtime_config() {
bosh -n update-runtime-config \
bbl-state/${BBL_STATE_DIR}/bosh-deployment/runtime-configs/dns.yml \
--name dns
}
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:"
echo "${credentials_in_directory}"
for cred in ${credentials_in_directory}
do
credhub d -n $cred > /dev/null
done
set -x
}
function remove_credentials_from_credhub() {
local director_name
director_name=$(jq -r .bosh.directorName bbl-state/${BBL_STATE_DIR}/bbl-state.json)
local deployment_name
deployment_name=$(bosh interpolate "${INTERPOLATED_MANIFEST}" --path /name)
remove_credentials_from_credhub_in_directory "/${director_name}/${deployment_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
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
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
}