From 8de208ac762012c2ebf7c2be0c78e28d9993e4a9 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 4 Jan 2021 20:52:02 +0100 Subject: [PATCH] Misc fixes for cleanup/cancel handling --- lib/common.sh | 12 +----------- lib/lxc.sh | 2 +- package_check.sh | 45 ++++++++++++++++++++++++++++++++------------- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/lib/common.sh b/lib/common.sh index e1c18ab..cc0fcaa 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -19,16 +19,6 @@ LXC_NAME="ynh-appci-$DIST-$ARCH-$YNH_BRANCH-test" readonly lock_file="./pcheck.lock" -clean_exit () { - - LXC_RESET - - [ -n "$TEST_CONTEXT" ] && rm -rf "$TEST_CONTEXT" - rm -f "$lock_file" - - exit $1 -} - #================================================= # LXC helpers #================================================= @@ -124,7 +114,7 @@ function log_error() function log_critical() { echo "${BOLD}${RED}Critical: ${1}${NORMAL}" | tee -a /proc/self/fd/3 - clean_exit 1 + exit 1 } function log_report_test_success () { diff --git a/lib/lxc.sh b/lib/lxc.sh index f1ca517..3f3283c 100644 --- a/lib/lxc.sh +++ b/lib/lxc.sh @@ -12,7 +12,7 @@ LXC_CREATE () { -c limits.cpu.allowance=80% \ | grep -v -E "^\s*Remapping container filesystem\s*$\|^\s*Retrieving image" - [[ "${PIPESTATUS[0]}" -eq 0 ]] || clean_exit 1 + [[ "${PIPESTATUS[0]}" -eq 0 ]] || exit 1 _LXC_START_AND_WAIT $LXC_NAME set_witness_files diff --git a/package_check.sh b/package_check.sh index e5a8b38..31ee708 100755 --- a/package_check.sh +++ b/package_check.sh @@ -117,10 +117,17 @@ function parse_args() { arguments=("$@") parse_args +#================================================= +# Cleanup / force-stop +#================================================= -#================================================= -# Force-stop -#================================================= +function cleanup() +{ + LXC_RESET + + [ -n "$TEST_CONTEXT" ] && rm -rf "$TEST_CONTEXT" + [ -n "$lock_file" ] && rm -f "$lock_file" +} if [[ $force_stop == 1 ]] then @@ -128,36 +135,48 @@ then if [ -n "$package_check_pid" ]; then kill --signal 15 $package_check_pid fi - clean_exit 0 + cleanup + exit 0 fi #================================================= # Check if the lock file exist #================================================= -if test -e "$lock_file" +# If the lock file exist and corresponding process still exists +if test -e "$lock_file" && ps --pid "$(cat pcheck.lock | cut -d: -f3)" | grep --quiet "$(cat pcheck.lock | cut -d: -f3)" then - # If the lock file exist - echo "The lock file $lock_file is present. Package check would not continue." if [ $interactive -eq 1 ]; then + echo "The lock file $lock_file already exists." echo -n "Do you want to continue anyway? (y/n) :" read answer + else + log_critical "The lock file $lock_file already exists. Package check won't continue." fi # Set the answer at lowercase only answer=${answer,,} if [ "${answer:0:1}" != "y" ] then - echo "Cancel Package check execution" - exit 0 + log_critical "Package check cancelled" fi fi # Create the lock file # $$ is the PID of package_check itself. echo "start:$(date +%s):$$" > "$lock_file" -################################### +#========================== +# Cleanup +# N.B. the traps are added AFTER the lock is taken +# because we don't want to mess with with the lock and LXC +# it we ain't the process with the lock... +#========================== + +trap cleanup EXIT +trap 'exit 2' TERM KILL + +#========================== # Main code -################################### +#========================== assert_we_are_connected_to_the_internets assert_we_have_all_dependencies @@ -165,7 +184,7 @@ assert_we_have_all_dependencies if [[ $rebuild == 1 ]] then rebuild_base_lxc 2>&1 | tee -a "./build_base_lxc.log" - clean_exit 0 + exit 0 fi #self_upgrade # FIXME renenable this later @@ -176,4 +195,4 @@ readonly TEST_CONTEXT=$(mktemp -d /tmp/package_check.XXXXXX) fetch_package_to_test "$path_to_package_to_test" run_all_tests -clean_exit 0 +exit 0