-
Notifications
You must be signed in to change notification settings - Fork 24
/
ci.bash
292 lines (252 loc) · 7.68 KB
/
ci.bash
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
#!/bin/bash
# Make sure required environment variables are set and export them
function compile::env
{
: "${JUJU_CLOUD:?Must have a cloud defined}"
: "${JUJU_CONTROLLER:?Must have a controller defined}"
: "${JUJU_DEPLOY_BUNDLE:?Must have a bundle defined}"
: "${JUJU_DEPLOY_CHANNEL:?Must have a channel defined}"
: "${JUJU_MODEL:?Must have a model defined}"
: "${JUJU_UPDATE_STATUS_INTERVAL:=150s}"
: "${JUJU_OWNER:=k8sci}"
: "${JOB_STAGE:-}"
: "${JOB_REPORTING:-yes}"
: "${SERIES:?Must have a release series defined}"
: "${SNAP_VERSION:?Must have a snap version defined}"
: "${JOB_NAME_CUSTOM:?Must have a job name defined}"
: "${JOB_ID:?Must have a job id defined}"
: "${KV_DB:=metadata.db}"
export ARCH
export JOB_NAME_CUSTOM
export JOB_ID
export JUJU_CLOUD
export JUJU_CONTROLLER
export JUJU_DEPLOY_BUNDLE
export JUJU_DEPLOY_CHANNEL
export JUJU_MODEL
export SERIES
export SNAP_VERSION
echo "Storing initial meta information"
local job_name_format
local snap_version_format
test -d "meta" && rm -rf "meta"
mkdir -p "meta"
job_name_format=$(echo "$JOB_NAME_CUSTOM" | tr '/' '-')
juju_channel_format=$(echo "$JUJU_DEPLOY_CHANNEL" | tr '/' '-')
snap_version_format=$(echo "$SNAP_VERSION" | tr '/' '-')
touch "meta/name-$job_name_format"
touch "meta/channel-$juju_channel_format"
touch "meta/series-$SERIES"
touch "meta/snap_version-$snap_version_format"
for i in meta/*; do
python bin/s3 cp "$i" "$i"
done
kv::set "job_id" "$JOB_ID"
kv::set "job_name" "$JOB_NAME_CUSTOM"
kv::set "job_name_custom" "$JOB_NAME_CUSTOM"
kv::set "job_stage" "$JOB_STAGE"
kv::set "series" "$SERIES"
kv::set "arch" "$ARCH"
kv::set "snap_version" "$SNAP_VERSION"
kv::set "channel" "$JUJU_DEPLOY_CHANNEL"
kv::set "juju_channel" "$(juju::version)"
}
function identifier
{
uuidgen | tr '[:upper:]' '[:lower:]'
}
function identifier::short
{
uuidgen | tr '[:upper:]' '[:lower:]' | cut -f1 -d-
}
# Generate a isoformat timetsamp
function timestamp
{
python -c "from datetime import datetime; print(datetime.utcnow().isoformat())"
}
# Retry a command up to a specific numer of times until it exits successfully,
# with exponential back off.
#
# $ retry 5 echo Hello
# Hello
#
# $ retry 5 false
# Retry 1/5 exited 1, retrying in 1 seconds...
# Retry 2/5 exited 1, retrying in 2 seconds...
# Retry 3/5 exited 1, retrying in 4 seconds...
# Retry 4/5 exited 1, retrying in 8 seconds...
# Retry 5/5 exited 1, no more retries left.
# ref: https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746
function retry {
local retries=$1
shift
local count=0
until "$@"; do
exit=$?
wait=$((2 ** count))
count=$((count + 1))
if [ "$count" -lt "$retries" ]; then
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
sleep $wait
else
echo "Retry $count/$retries exited $exit, no more retries left."
return $exit
fi
done
return 0
}
# sets a state key/value namespaced to the current spell
#
# Arguments:
# $1: KEY
# $2: VALUE
function kv::set
{
kv-cli "$KV_DB" set "$1" "$2"
}
# gets a state key/value namespaced by the current spell
#
# Arguments:
# $1: KEY
function kv::get
{
kv-cli "$KV_DB" get "$1" || echo "None"
}
# Run pytest
#
# Returns str: True or False
function test::execute
{
juju::pip::2.9
extra_args=''
if [ -n "$TEST_UPGRADE_SNAPD_CHANNEL" ]; then
# Azure seems to have trouble with the daily image-stream
extra_args="${extra_args} \
--snapd-upgrade \
--snapd-channel ${TEST_UPGRADE_SNAPD_CHANNEL}"
fi
declare -n is_pass=$1
timeout -s INT 3h pytest \
--html="report.html" \
--json-report \
--json-report-summary \
--json-report-file="report.json" \
--full-trace \
jobs/integration/validation.py \
--cloud "$JUJU_CLOUD" \
--model "$JUJU_MODEL" \
--controller "$JUJU_CONTROLLER" \
${extra_args}
ret=$?
is_pass="True"
if (( ret == 124 )); then
is_pass="Timeout"
elif (( ret > 0 )); then
is_pass="False"
fi
}
# store test report
function test::report
{
result=$1
kv::set "result" "$result"
touch "meta/result-$result"
if [ "${JOB_REPORTING}" == "no" ]; then
echo "Job reporting disabled"
else
python bin/s3 cp "meta/result-$result" "meta/result-$result"
fi
}
function test::capture
{
if [ "${JOB_REPORTING}" == "no" ]; then
echo "Job reporting disabled"
return 0
fi
if which juju-crashdump; then
# -s small crashdump by skipping /var/lib/juju
# -a debug-layer included debug-layer addon
# -a config included config addon
# -j snap.kube* included logs from all kube* daemons
# -j snap.cdk-addons* included logs from cdk-addons*
juju-crashdump \
-s \
-a debug-layer \
-a config \
-j snap.kube-apiserver.daemon \
-j snap.kube-controller-manager.daemon \
-j snap.kube-proxy.daemon \
-j snap.kube-scheduler.daemon \
-j snap.kubelet.daemon \
-j snap.cdk-addons* \
-m "$JUJU_CONTROLLER:$JUJU_MODEL"
fi
tar -cvzf artifacts.tar.gz ci.log _out meta juju-crashdump* report.* failures* logs/ || true
/usr/local/bin/columbo -r columbo.yaml -o "_out" "artifacts.tar.gz" || true
python bin/s3 cp "columbo-report.json" columbo-report.json || true
python -c "import json; import kv; print(json.dumps(dict(kv.KV('metadata.db'))))" | tee "metadata.json"
python bin/s3 cp "metadata.json" metadata.json || true
python bin/s3 cp "report.html" report.html || true
python bin/s3 cp "report.json" report.json || true
python bin/s3 cp "metadata.db" metadata.db || true
python bin/s3 cp "artifacts.tar.gz" artifacts.tar.gz || true
# Generate job report
python bin/report job-result --job-id "$JOB_ID" --metadata-db metadata.db --columbo-json columbo-report.json
echo "@@@ CAPTURE RESULTS @@@"
echo "@"
echo "@ http://jenkaas.s3-website-us-east-1.amazonaws.com/$JOB_ID/artifacts.tar.gz"
echo "@"
echo "@@@"
}
# Entrypoint to start the deployment, testing, reporting
function ci::run
{
compile::env
local log_name_custom=$(echo "$JOB_NAME_CUSTOM" | tr '/' '-')
{
kv::set "build_starttime" "$(timestamp)"
juju::bootstrap::before
juju::bootstrap
juju::bootstrap::after
juju::model::speed-up
juju::deploy::before
juju::deploy::overlay
juju::deploy
juju::wait
juju::deploy::after
test::execute result
kv::set "build_endtime" "$(timestamp)"
test::report "$result"
} 2>&1 | sed -u -e "s/^/[$log_name_custom] /" | tee -a "ci.log"
}
# injects random sleep
function ci::sleep
{
sleep $(( ( RANDOM % 25 ) + 1 ))s
}
function ci::cleanup::before
{
echo "> skipping before tasks"
}
function ci::cleanup::model::addons
{
if ! timeout 10m juju destroy-model --no-prompt --destroy-storage "$JUJU_CONTROLLER:addons"; then
timeout 10m juju destroy-model --no-prompt --destroy-storage "$JUJU_CONTROLLER:addons" --force
fi
}
function ci::cleanup::after
{
echo "> skipping after tasks"
}
# cleanup function
function ci::cleanup
{
local log_name_custom=$(echo "$JOB_NAME_CUSTOM" | tr '/' '-')
{
ci::cleanup::before || true
test::capture || true
juju::destroy
ci::cleanup::after || true
} 2>&1 | sed -u -e "s/^/[$log_name_custom] /" | tee -a "ci.log"
}
trap ci::cleanup EXIT