-
Notifications
You must be signed in to change notification settings - Fork 18
/
bt-iso
executable file
·440 lines (398 loc) · 15.4 KB
/
bt-iso
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
#!/bin/bash -e
# Copyright (c) 2011-2021 TurnKey GNU/Linux - http://www.turnkeylinux.org
#
# This file is part of buildtasks.
#
# Buildtasks is free software; you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
[[ -z "$BT_DEBUG" ]] || set -x
fatal() { echo "FATAL [$(basename "$0")]: $*" 1>&2; exit 1; }
warning() { echo "WARNING [$(basename "$0")]: $*"; }
info() { echo "INFO [$(basename "$0")]: $*"; }
usage() {
cat<<EOF
Syntax: $(basename "$0") [--publish | -u] [-n] [-s] appname
Builds appliance appname (e.g., core) ISO
Arguments::
appname - e.g., core
Options::
-n|--no-screens - skip taking screenshots
-u|--use-existing - use an existing iso, rather than rebuilding, will
fail if iso does not exist
-s|--skip-setup - skip the inital setup step
--publish - publish iso, release files and tklbam profile,
incompatible with -u|--use-existing and
-s|--skip-setup
Environment::
WEBDRIVER_CONNECT_ATTEMPTS
- number of times to (re)try connection to webdriver
container (for screenshots)
DEFAULT: $WEBDRIVER_CONNECT_ATTEMPTS
WEBDRIVER_CONNECT_TIMEOUT
- time (in seconds) to wait between webdriver
connection attempts (for screenshots)
DEFAULT: $WEBDRIVER_CONNECT_TIMEOUT
BT_DEBUG - turn on debugging
EOF
if [[ "$#" -ne 0 ]]; then
echo "Error: $*"
exit 1
fi
exit
}
clone_or_pull() {
# If $dir doesn't exist, clone $repo. If it does, update from origin master
dir=$1
repo=$2
if [[ -z "$dir" ]] || [[ -z "$repo" ]]; then
fatal "One or more empty values passed to function: ${FUNCNAME[*]}."
fi
if [ ! -e "$dir" ]; then
info "Attempting to clone repo $repo to $dir."
cd "$(dirname "$dir")"
git clone "https://github.com/$repo" "$dir"
cd "$dir"
else
info "Repo $repo found, attempting to update"
cd "$dir"
git pull origin master
fi
}
get_version() {
pkg=$1
sp="[[:space:]]"
pkg_info=$(dpkg -l \
| sed -En "s|^ii.*($pkg)$sp*([0-9a-z\.-:]*)$sp*amd64$sp*.*|\1 \2|p")
echo "$pkg_info" | cut -d' ' -f2
}
git_unshallow() {
local dir=$1
local branch=$2
# find remote name for turnkey repo
remote=$(\
sed -En '\|github\.com.turnkeylinux.*fetch\)$| s|([a-z]*)\s.*|\1|p' \
<<<"$(git remote -v)")
local "remote"
cd "$dir"
if [[ "$(git symbolic-ref --short HEAD)" != "$branch" ]]; then
git remote set-branches "$remote" '*'
git fetch "$remote" --unshallow 2>/dev/null || git fetch "$remote"
git checkout "$branch" || git checkout "$branch-dev"
fi
cd -
}
get_codename() {
local tkl_ver=$1
case $tkl_ver in
16*)
TKL_CODENAME=buster;;
17*)
TKL_CODENAME=bullseye;;
18*)
TKL_CODENAME=bookworm;;
19*)
TKL_CODENAME=trixie;;
*)
fatal "Unrecognised TKL version: $tkl_ver";;
esac
}
container_status() {
podman container inspect "$1" --format "{{.State.Status}}"
}
check_debug() {
local msg=$1
if [[ -z "$DEBUG" ]]; then
fatal "$msg - set DEBUG to force"
else
warning "$msg - build may fail"
fi
}
export WEBDRIVER_CONNECT_ATTEMPTS=${WEBDRIVER_CONNECT_ATTEMPTS:-200}
export WEBDRIVER_CONNECT_TIMEOUT=${WEBDRIVER_CONNECT_TIMEOUT:-5}
ARGS="$*"
unset no_screens use_existing appname skip_setup TKL_CODENAME
while [[ "$1" != "" ]]; do
case $1 in
--help|-h) usage;;
--publish) publish="yes";;
-n|--no-screens) no_screens="true";;
-s|--skip-setup) skip_setup="true";;
-u|--use-existing) use_existing="true"
shift
iso="$1";;
*) if [[ -n "$appname" ]]; then
usage "Accepts only one appliance name"
else
appname=$1
fi;;
esac
shift
done
[[ -n "$appname" ]] || usage "Must give one app name"
# don't try screens for tkldev & extend MAX_COUNT for gitlab & canvas
if [[ -z "$no_screens" ]]; then
case $appname in
tkldev|core)
info "$appname build - disabling screenshots as no web UI"
no_screens="true";;
gitlab|canvas)
# GitLab & Canvas take ages to start...
export MAX_COUNT=70 # possibly overkill
;;
esac
fi
link=$(readlink -f "$0")
BT=$(dirname "$link")
export BT
export BT_CONFIG="$BT/config"
. "$BT_CONFIG/common.cfg"
INIT_CONF=/turnkey/public/tkldev-docker/inithooks.conf
if [[ ! -f "$INIT_CONF" ]]; then
check_debug "Inithooks preseed file ($INIT_CONF) not found"
else
. "$INIT_CONF"
fi
if [[ -z "$APP_DOMAIN" ]]; then
check_debug "APP_DOMAIN not set"
fi
HOST_IP=$(hostname -I | awk '{print $1}')
export HOST_IP
if [[ -z "$HOST_IP" ]]; then
check_debug "HOST_IP not set"
fi
if [[ "$publish" == "yes" ]]; then
unset msg
[[ -n "$BT_PUBLISH_IMGS" ]] || fatal "BT_PUBLISH_IMGS not set"
[[ -n "$BT_PUBLISH_META" ]] || fatal "BT_PUBLISH_META not set"
[[ -n "$BT_PUBLISH_SCREENS" ]] || fatal "BT_PUBLISH_SCREENS not set"
[[ -n "$BT_PUBLISH_PROFILES" ]] || fatal "BT_PUBLISH_PROFILES not set"
[[ -z "$use_existing" ]] || usage "conflicting options: --publish / -u|--use-existing"
[[ -z "$skip_setup" ]] || usage "conflicting options: --publish / -s|--skip-setup"
which aws >/dev/null || "$BT"/bin/aws-setup --awscli
else
warning "--publish was not specified"
fi
cd "$BT"
BASE_DIR=/turnkey/public
COMMIT_ID=$(git rev-parse --short HEAD)
if [[ -z "$skip_setup" ]]; then
# unless -n|--no-screens set up clicksnap and check for clicksnap file
if [[ -z "$no_screens" ]]; then
if [[ ! -f /root/clicksnap-setup.done ]]; then
info "Setting up clicksnap and dockerize"
"$BT"/bin/clicksnap-setup "$appname"
touch /root/clicksnap-setup.done
else
setup_time=$(date -r /root/clicksnap-setup.done +%s)
allowed_age=$(( setup_time + 86400 )) # 24 hours
if [[ $allowed_age -lt $(date +%s) ]]; then
info "Attempting clicksnap update"
clone_or_pull $BASE_DIR/clicksnap turnkeylinux/clicksnap
cd $BASE_DIR/clicksnap
cargo build
cd -
touch /root/clicksnap-setup.done
fi
fi
if ! grep -q $appname <<<$(clicksnap list); then
fatal "Clicksnap code for $appname not found"
fi
fi
# Leverage tkldev-setup to ensure important repos are cloned and at latest
# commit; also ensures RELEASE & ARCH are set.
tkldev-setup "$appname" \
|| warning "tkldev-setup failed. Attempting to continue anyway."
# if TKL version doesn't match host, check out the relevant branches and
# attempt to download the right bootstrap; otherwise build it
TKL_HOST_VER=$(turnkey-version -t | cut -d. -f1)
TKL_BUILD_VER=$(sed -nE "1s|turnkey-[a-z0-9-]*-([0-9]+).*|\1|p" \
"$BT_PRODUCTS/$appname/changelog")
if [[ "$TKL_HOST_VER" != "$TKL_BUILD_VER" ]]; then
warning "Host TKL version ($TKL_HOST_VER) does not match guest version" \
" ($TKL_BUILD_VER)"
unset TKL_CODENAME
get_codename "$TKL_BUILD_VER"
export RELEASE=debian/$TKL_CODENAME
git_unshallow "/turnkey/buildtasks" "$TKL_BUILD_VER.x"
# if buildtasks has changed, restart bt-iso
cd "$BT"
if [[ "$(git rev-parse --short HEAD)" != "$COMMIT_ID" ]]; then
exec ./"$(basename "$0")" "$ARGS"
exit
fi
cd -
git_unshallow "$FAB_PATH/common" "$TKL_BUILD_VER.x"
# TODO KEY unused!?
KEY=$FAB_PATH/common/keys/tkl-$TKL_CODENAME-images.asc
IMAGES="http://mirror.turnkeylinux.org/turnkeylinux/images"
BOOTSTRAP_NAME="bootstrap-$TKL_CODENAME-$(dpkg --print-architecture)"
BOOTSTRAP_PATH="$FAB_PATH/bootstraps/$TKL_CODENAME"
if [[ ! -d "$BOOTSTRAP_PATH" ]]; then
info "Attempting to download $BOOTSTRAP_NAME"
mkdir -p "$(dirname "$BOOTSTRAP_PATH")"
cd "$(dirname "$BOOTSTRAP_PATH")"
exit_code=0
wget -nc $IMAGES/bootstrap/"$BOOTSTRAP_NAME.tar.gz" || exit_code=$?
wget -nc $IMAGES/bootstrap/"$BOOTSTRAP_NAME.tar.gz.hash" || exit_code=$?
if [[ "$exit_code" -eq 0 ]]; then
info "verifying $BOOTSTRAP_NAME"
TMP_KEYRING="$(mktemp -d)/tmp.gpg"
GPG="gpg --no-default-keyring --keyring $TMP_KEYRING"
$GPG --import "$FAB_PATH/common/keys/tkl-$TKL_CODENAME-images.asc"
$GPG --verify "$BOOTSTRAP_NAME.tar.gz.hash"
info "unpacking $BOOTSTRAP_NAME"
mkdir "$BOOTSTRAP_PATH"
tar -zxf "$BOOTSTRAP_NAME.tar.gz" -C "$BOOTSTRAP_PATH"
cd -
else
warning "Downloading bootstrap failed."
info "Attempting to build bootstrap."
unset PUBLISH
BOOTSTRAP_SRC=$(dirname "$FAB_PATH")/bootstrap
[[ "$publish" != "yes" ]] || PUBLISH="--publish"
"$BT"/bt-bootstrap $PUBLISH --no-clean --force \
|| fatal "Building bootstrap failed... :("
rsync --delete -Hac "$BOOTSTRAP_SRC"/build/bootstrap/ \
"$FAB_PATH"/bootstraps/$TKL_CODENAME/
# create symlinks so bin/generate-buildenv completes successfully
ln -s "$BT_BUILDS/bootstrap/$BOOTSTRAP_NAME.tar.gz" \
"$(dirname "$BOOTSTRAP_PATH")/$BOOTSTRAP_NAME.tar.gz"
ln -s "$BT_BUILDS/bootstrap/$BOOTSTRAP_NAME.tar.gz.hash" \
"$(dirname "$BOOTSTRAP_PATH")"/"$BOOTSTRAP_NAME.tar.gz.hash"
fi
fi
fi
fi
cd "$BT_PRODUCTS/$appname"
if [[ -n "$use_existing" ]]; then
ISO=build/product.iso
[[ -f "$ISO" ]] || fatal "iso $PWD/$ISO not found"
else
info "Preperation done. Building appliance $appname."
cd "$BT_PRODUCTS/$appname"
deck -D build/root.sandbox || true
make clean || true
mkdir -p build
make 2>&1 | tee build/log || true
if [[ ! -e build/product.iso ]]; then
if [[ -z "$BT_DEBUG" ]]; then
deck -D build/root.sandbox >/dev/null 2>&1 || true
make clean >/dev/null 2>&1 || true
fi
fatal "Build failed..."
fi
fi
if [[ -z "$no_screens" ]]; then
if [[ -n "$use_existing" ]]; then
info "Product iso being loaded as docker container"
# TODO make dockerize output more quiet when --quiet used - workaround via tail
image_name=$(dockerize --quiet --iso build/product.iso --name "$appname" | tail -1)
else
info "Product rootfs being loaded as docker container"
# TODO make dockerize output more quiet when --quiet used - workaround via tail
image_name=$(dockerize --quiet --rootfs build/root.sandbox --deck | tail -1)
fi
info "Creating new product docker container from image and starting"
app_cont_id=$(podman run -dt -p 80:80 -p 443:443 -p 12321:12321 \
-p 12322:12322 --tmpfs /tmp --tmpfs /run \
--tmpfs /run/lock "$image_name")
#app_ip=$(podman inspect $app_cont_id --format "{{.NetworkSettings.IPAddress}}")
export TKL_SCREENSHOT_PATH=$BT_PRODUCTS/$appname/build/screens
mkdir -p "$TKL_SCREENSHOT_PATH"
SELENIUM_CONT=docker.io/selenium/standalone-chrome
SELENIUM_TAG=4.18.1-20240224
info "Starting selenium container"
unset add_hosts
if [[ -n "$APP_DOMAIN" ]]; then
info "Adding FQDN:${APP_DOMAIN} pointing to IP:${HOST_IP} to selenium hosts file"
add_hosts="--add-host=${APP_DOMAIN}:${HOST_IP}"
fi
# check if selenium container already exists
existing_instance=$(podman ps -a --format "{{.ID}}:{{.Image}}" \
| grep "$SELENIUM_CONT:$SELENIUM_TAG" || [[ $? == 1 ]])
if [[ $(wc -l <<<"$existing_instance") -gt 1 ]]; then
fatal "More than one selenium instance found"
fi
existing_id=$(sed -En "s|(^[a-f0-9]+):.*|\1|p" <<<"$existing_instance")
if [[ -z "$existing_id" ]]; then
info "no existing selenium container found - starting one"
#podman container stop $existing_id >/dev/null 2>&1 || [[ $? -eq 1 ]]
#podman container rm $existing_id >/dev/null 2>&1 || [[ $? -eq 1 ]]A
sel_cont_id=$(podman run -d -p 4444:4444 -p 7900:7900 "$add_hosts" \
--shm-size="2g" $SELENIUM_CONT:$SELENIUM_TAG)
else
info "Local container found (id: $existing_id)"
info "Assuming it has proper hosts added already (may fail if not)"
sel_cont_id="$existing_id"
fi
#info "starting new selenium webdriver container (may require download)"
#sel_cont_id=$(podman run -d -p 4444:4444 -p 7900:7900 $add_hosts \
# --shm-size="2g" $SELENIUM_CONT:$SELENIUM_TAG)
info "waiting until container status is 'running'"
while [[ $(container_status "$sel_cont_id") != "running" ]]; do
sleep 1
done
info "container running - waiting for connection"
for ping in {1..20}; do
if ! curl localhost:7900 >/dev/null 2>&1; then
sleep 1
fi
if [[ "$appname" != 'tkldev' ]]; then
if ! curl --insecure "https://$HOST_IP:12321" >/dev/null 2>&1; then
sleep 1
fi
else
# TODO need to handle tkldev here; just sleeping for now...
sleep 1
fi
done
tkl-docker-wait-ready "$app_cont_id"
# set schema - most should be https, but there is at least one exception
case $appname in
gitlab)
schema=http;;
*)
schema=https;;
esac
info "taking screenshots"
count=0
max_tries=60
while [[ "$count" -lt $max_tries ]]; do
count=$((count + 1))
exit_code=0
info "attempt $count"
# note TKL_SCREENSHOT_PATH exported above
clicksnap test "$appname" "${schema}://${APP_DOMAIN}/" || exit_code=$?
if [[ $exit_code -eq 0 ]]; then
info "Success"
break
else
info "Clicksnap failed - guessing container not yet ready - waiting"
sleep 2
fi
done
if [[ ! $exit_code -eq 0 ]]; then
warning "Screenshots failed, stopping $appname container: $app_cont_id"
podman stop "$app_cont_id"
fatal "Screenshots failed after $max_tries attempts - container stopped"
fi
fi
mkdir -p "$BT_ISOS"
unset arg
[[ -z "$no_screens" ]] || arg="--no-screens"
"$BT/bin/iso-release" --force $arg "$BT_ISOS"
if [[ "$publish" == "yes" ]]; then
name=$(cat build/root.sandbox/etc/turnkey_version)
"$BT/bin/iso-publish" $arg "$BT_ISOS/$name.iso"
fi
if [[ -z "$BT_DEBUG" ]]; then
deck -D build/root.sandbox
make clean
if [[ -z "$no_screens" ]]; then
info "stopping $appname container: $(podman stop "$app_cont_id")"
info "removing $appname container: $(podman rm "$app_cont_id")"
info "removing $appname image: $(podman rmi "$image_name")"
fi
fi