Define cleanup() function in outer scope and execute on EXIT

for these 2 scripts we make cleanup() defined in the outer scope and
make sure that it executes on EXIT rather than RETURN which makes it
possible to stick the journalctl killing functionality inside of the
same function.
This commit is contained in:
Alexander Todorov 2024-03-07 12:47:19 +02:00 committed by Alexander Todorov
parent 85ce42570d
commit 2a8bd4ce9b
2 changed files with 28 additions and 26 deletions

View file

@ -15,11 +15,21 @@ set -euo pipefail
source /usr/libexec/osbuild-composer-test/set-env-variables.sh
source /usr/libexec/tests/osbuild-composer/shared_lib.sh
isomount=$(mktemp -d)
kspath=$(mktemp -d)
cleanup() {
# kill dangling journalctl processes to prevent GitLab CI from hanging
sudo pkill journalctl || echo "Nothing killed"
sudo umount -v "${isomount}" || echo
rmdir -v "${isomount}"
rm -rv "${kspath}"
}
trap cleanup EXIT
# modify existing kickstart by prepending and appending commands
function modksiso {
sudo dnf install -y lorax # for mkksiso
isomount=$(mktemp -d)
kspath=$(mktemp -d)
iso="$1"
newiso="$2"
@ -27,14 +37,6 @@ function modksiso {
echo "Mounting ${iso} -> ${isomount}"
sudo mount -v -o ro "${iso}" "${isomount}"
cleanup() {
sudo umount -v "${isomount}"
rmdir -v "${isomount}"
rm -rv "${kspath}"
}
trap cleanup RETURN
ksfiles=("${isomount}"/*.ks)
ksfile="${ksfiles[0]}" # there shouldn't be more than one anyway
echo "Found kickstart file ${ksfile}"
@ -189,8 +191,6 @@ build_image() {
WORKER_UNIT=$(sudo systemctl list-units | grep -o -E "osbuild.*worker.*\.service")
sudo journalctl -af -n 1 -u "${WORKER_UNIT}" &
WORKER_JOURNAL_PID=$!
# Stop watching the worker journal when exiting.
trap 'sudo pkill -P ${WORKER_JOURNAL_PID}' EXIT
# Start the compose.
greenprint "🚀 Starting compose"
@ -223,9 +223,8 @@ build_image() {
exit 1
fi
# Kill the journal monitor immediately and remove the trap
# Kill the journal monitor
sudo pkill -P ${WORKER_JOURNAL_PID}
trap - EXIT
}
# Wait for the ssh server up to be.

View file

@ -157,11 +157,21 @@ case "${ID}-${VERSION_ID}" in
exit 1;;
esac
isomount=$(mktemp -d --tmpdir=/var/tmp/)
kspath=$(mktemp -d --tmpdir=/var/tmp/)
cleanup() {
# kill dangling journalctl processes to prevent GitLab CI from hanging
sudo pkill journalctl || echo "Nothing killed"
sudo umount -v "${isomount}" || echo
rmdir -v "${isomount}"
rm -rv "${kspath}"
}
trap cleanup EXIT
# modify existing kickstart by prepending and appending commands
function modksiso {
sudo dnf install -y lorax # for mkksiso
isomount=$(mktemp -d --tmpdir=/var/tmp/)
kspath=$(mktemp -d --tmpdir=/var/tmp/)
iso="$1"
newiso="$2"
@ -169,14 +179,6 @@ function modksiso {
echo "Mounting ${iso} -> ${isomount}"
sudo mount -v -o ro "${iso}" "${isomount}"
cleanup() {
sudo umount -v "${isomount}"
rmdir -v "${isomount}"
rm -rv "${kspath}"
}
trap cleanup RETURN
# When sudo-nopasswd is specified, a second kickstart file is added which
# includes the %post section for creating sudoers drop-in files. This
# kickstart file is called osbuild.ks and it %includes osbuild-base.ks at
@ -246,8 +248,6 @@ build_image() {
WORKER_UNIT=$(sudo systemctl list-units | grep -o -E "osbuild.*worker.*\.service")
sudo journalctl -af -n 1 -u "${WORKER_UNIT}" &
WORKER_JOURNAL_PID=$!
# Stop watching the worker journal when exiting.
trap 'sudo pkill -P ${WORKER_JOURNAL_PID}' EXIT
# Start the compose.
greenprint "🚀 Starting compose"
@ -285,6 +285,9 @@ build_image() {
get_compose_log "$COMPOSE_ID"
get_compose_metadata "$COMPOSE_ID"
# Kill the journal monitor
sudo pkill -P ${WORKER_JOURNAL_PID}
# Did the compose finish with success?
if [[ $COMPOSE_STATUS != FINISHED ]]; then
redprint "Something went wrong with the compose. 😢"