From 2a8bd4ce9bb3a279f940225aae6c2d253dbfbe25 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Thu, 7 Mar 2024 12:47:19 +0200 Subject: [PATCH] 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. --- test/cases/installers.sh | 27 +++++++++++++-------------- test/cases/ostree-ng.sh | 27 +++++++++++++++------------ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/test/cases/installers.sh b/test/cases/installers.sh index a942a3bf1..bd3ab109c 100755 --- a/test/cases/installers.sh +++ b/test/cases/installers.sh @@ -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. diff --git a/test/cases/ostree-ng.sh b/test/cases/ostree-ng.sh index 305d58b75..e0e67af71 100755 --- a/test/cases/ostree-ng.sh +++ b/test/cases/ostree-ng.sh @@ -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. 😢"