This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build-image.sh
executable file
·83 lines (66 loc) · 2.26 KB
/
build-image.sh
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
#!/bin/bash
set -euo pipefail
CONTAINER_NAME=imagebuilder
IMAGE_UUID=$(uuidgen)
SHIP_TO_AWS=yes
podman-exec() {
sudo podman exec -t $CONTAINER_NAME $@
}
composer-cli() {
podman-exec composer-cli ${@}
}
# Start the container.
echo "🚀 Launching the container"
mkdir -vp shared output
sudo podman run --rm --detach --privileged \
-v $(pwd)/shared:/repo \
-v $(pwd)/output:/output \
--name $CONTAINER_NAME \
$CONTAINER
# Wait for composer to be fully running.
echo "⏱ Waiting for composer to start"
for i in $(seq 1 10); do
sleep 1
composer-cli status show && break
done
echo "📥 Pushing the blueprint"
composer-cli blueprints push /repo/${BLUEPRINT_NAME}.toml
echo "🔎 Solving dependencies in the blueprint"
composer-cli blueprints depsolve ${BLUEPRINT_NAME}
if [[ $SHIP_TO_AWS == "yes" ]]; then
echo "🛠 Build the image and ship to AWS"
composer-cli --json \
compose start $BLUEPRINT_NAME ami $IMAGE_KEY /repo/aws-config.toml |
tee compose_start.json
else
echo "🛠 Build the image"
composer-cli --json compose start ${BLUEPRINT_NAME} ami | tee compose_start.json
fi
COMPOSE_ID=$(jq -r ".[].body.build_id" compose_start.json)
# Watch the logs while the build runs.
podman-exec journalctl -af &
# Sometimes osbuild-composer gets a bit grumpy if we check for status immediately and we
# end up with JSON that is partially built. We wait just a moment before checking it.
sleep 10
COUNTER=0
while true; do
composer-cli --json compose status | tee compose_info.json >/dev/null
COMPOSE_STATUS=$(jq -r --arg COMPOSE_ID "${COMPOSE_ID}" '.[].body[][] | select(.id==$COMPOSE_ID).queue_status' compose_info.json)
# Print a status line once per minute.
if [ $((COUNTER % 60)) -eq 0 ]; then
echo "💤 Waiting for the compose to finish at $(date +%H:%M:%S)"
fi
# Is the compose finished?
if [[ $COMPOSE_STATUS != RUNNING ]] && [[ $COMPOSE_STATUS != WAITING ]]; then
echo "🎉 Compose finished."
break
fi
sleep 15
let COUNTER=COUNTER+1
done
if [[ $COMPOSE_STATUS != FINISHED ]]; then
composer-cli compose logs ${COMPOSE_ID} >/dev/null
podman-exec tar -axf /${COMPOSE_ID}-logs.tar logs/osbuild.log -O
echo "😢 Something went wrong with the compose"
exit 1
fi