From 8b3d4ee651cfaaa05225c541beaf6a35c887cb4e Mon Sep 17 00:00:00 2001 From: Xavier Moffett Date: Fri, 14 Jun 2024 17:04:29 -0400 Subject: [PATCH] Cleaned up error handling in dist scripting --- dist/tools/clean.sh | 1 + dist/tools/common.sh | 13 +++++++++++-- dist/tools/package.sh | 7 ++++--- dist/tools/prepare.sh | 11 ++++++----- dist/tools/runtime.sh | 21 ++++++++++++++++----- dist/tools/schema.sh | 3 ++- 6 files changed, 40 insertions(+), 16 deletions(-) diff --git a/dist/tools/clean.sh b/dist/tools/clean.sh index bfea717..aadb7d8 100755 --- a/dist/tools/clean.sh +++ b/dist/tools/clean.sh @@ -21,6 +21,7 @@ if [[ ! -d "$PWD/dist/tools/" ]]; then echo "This script may only be executed vi if [[ ! -f ./dist/tools/common.sh ]]; then echo "Common script is missing. Ensure the source tree is intact."; exit 2; fi source ./dist/tools/common.sh +ACTION_NOUN="Housekeeping" DIST_BIN="$PWD/dist/bin" DIST_RUNTIME="$PWD/dist/runtime" diff --git a/dist/tools/common.sh b/dist/tools/common.sh index d6bf589..f44586c 100755 --- a/dist/tools/common.sh +++ b/dist/tools/common.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -e # # pacwrap - common.sh # @@ -19,6 +19,11 @@ [[ ! -z $COMMON_SCRIPT ]] && return +set -e +trap 'LAST_CMD=$BASH_COMMAND' DEBUG +trap 'handle_failure "$ACTION_NOUN" $?' ERR TERM INT + +ACTION_NOUN="Common script" DIST_BIN="$PWD/dist/bin" DIST_SRC="$PWD/dist/src" @@ -27,7 +32,11 @@ if [[ -t 2 ]] && [[ ! -z $COLORTERM ]] && [[ $TERM != "dummy" ]]; then RED="" GREEN="" RESET="" -fi +fi + +handle_failure() { + error_fatal "$1 failure: $LAST_CMD exited with exit code $?." +} error_fatal() { echo $BOLD$RED"error:$RESET $@"; diff --git a/dist/tools/package.sh b/dist/tools/package.sh index 0c3431c..4d52371 100755 --- a/dist/tools/package.sh +++ b/dist/tools/package.sh @@ -23,10 +23,11 @@ if [[ ! -d "$PWD/dist/tools/" ]]; then echo "This script may only be executed vi if [[ ! -f ./dist/tools/common.sh ]]; then echo "Common script is missing. Ensure the source tree is intact."; exit 2; fi source ./dist/tools/common.sh +ACTION_NOUN="Packaging" DIST_MAN="$PWD/dist/man" validate_args $1 -package $DIST_MAN/pacwrap.1 $DIST_BIN/pacwrap.1 $1; if [[ $? != 0 ]]; then error_fatal "Packaging of pacwrap.1 manpage failed."; fi -package $DIST_MAN/pacwrap.yml.2 $DIST_BIN/pacwrap.yml.2 $1; if [[ $? != 0 ]]; then error_fatal "Packaging of pacwrap.yml.2 manpage failed."; fi -./dist/tools/runtime.sh $1; if [[ $? != 0 ]]; then error_fatal "Build of container runtime failed."; fi +package $DIST_MAN/pacwrap.1 $DIST_BIN/pacwrap.1 $1 +package $DIST_MAN/pacwrap.yml.2 $DIST_BIN/pacwrap.yml.2 $1 +./dist/tools/runtime.sh $1 diff --git a/dist/tools/prepare.sh b/dist/tools/prepare.sh index 8a3aa96..f7d47ab 100755 --- a/dist/tools/prepare.sh +++ b/dist/tools/prepare.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -e # # pacwrap - prepare.sh # @@ -23,8 +23,9 @@ if [[ ! -d "$PWD/dist/tools/" ]]; then echo "This script may only be executed vi if [[ ! -f ./dist/tools/common.sh ]]; then echo "Common script is missing. Ensure the source tree is intact."; exit 2; fi source ./dist/tools/common.sh +ACTION_NOUN="Preparation" -validate_args $1; if [[ $? != 0 ]]; then error_fatal "Argument validation failed."; fi -layout_dir; if [[ $? != 0 ]]; then error_fatal "Directory initialization failed."; fi -package $DIST_SRC/pacwrap-key $DIST_BIN/pacwrap-key $1 0; if [[ $? != 0 ]]; then error_fatal "Packaging of pacwrap-key failed."; fi -./dist/tools/schema.sh $1; if [[ $? != 0 ]]; then error_fatal "Build of container schema failed."; fi +validate_args $1 +layout_dir +package $DIST_SRC/pacwrap-key $DIST_BIN/pacwrap-key $1 0 +./dist/tools/schema.sh $1 diff --git a/dist/tools/runtime.sh b/dist/tools/runtime.sh index 71c8e4e..f69d5c3 100755 --- a/dist/tools/runtime.sh +++ b/dist/tools/runtime.sh @@ -24,6 +24,7 @@ if [[ ! -d "$PWD/dist/tools/" ]]; then echo "This script may only be executed vi if [[ ! -f ./dist/tools/common.sh ]]; then echo "Common script is missing. Ensure the source tree is intact."; exit 2; fi source ./dist/tools/common.sh +ACTION_NOUN="Runtime generation" # # Environment variables @@ -118,8 +119,8 @@ populate_lib() { ln -s .$FAKEROOT$FAKECHROOT/libfakechroot.so $DEST_DIR$LIB_DIR/libfakechroot.so # Remove debuglink section, to ensure the Arch Build System doesn't complain - for lib in $(find $DEST_DIR$LIB_DIR -maxdepth 3 -printf "%p "); do - objcopy --remove-section=.gnu_debuglink $lib 2>/dev/null + for lib in $(find $DEST_DIR$LIB_DIR -maxdepth 3 -type f -printf "%p "); do + objcopy --remove-section=.gnu_debuglink $lib done } @@ -148,8 +149,14 @@ populate_etc() { # Populate busybox links # busybox_links() { - for applet in $(busybox --list); do - ln -s busybox ./dist/runtime/bin/$applet 2>/dev/null + for applet in $(busybox --list); do + if [[ "${COREUTILS[@]}" == *$applet* ]] || + [[ "${BIN_UTILS[@]}" == *$applet* ]] || + [[ $applet == "busybox" ]]; then + continue + fi + + ln -s busybox ./dist/runtime/bin/$applet done } @@ -173,8 +180,12 @@ copy_bins() { for bin in ${@}; do cp $(type -P $bin) $DEST_DIR$BIN_DIR/$bin + if [[ $bin == "fakeroot" ]]; then + continue + fi + # Remove debuglink section, to ensure the Arch Build System doesn't complain - objcopy --remove-section=.gnu_debuglink $DEST_DIR$BIN_DIR/$bin 2>/dev/null + objcopy --remove-section=.gnu_debuglink $DEST_DIR$BIN_DIR/$bin done } diff --git a/dist/tools/schema.sh b/dist/tools/schema.sh index cf74977..29802d0 100755 --- a/dist/tools/schema.sh +++ b/dist/tools/schema.sh @@ -25,6 +25,7 @@ if [[ ! -d "$PWD/dist/tools/" ]]; then echo "This script may only be executed vi if [[ ! -f ./dist/tools/common.sh ]]; then echo "Common script is missing. Ensure the source tree is intact."; exit 2; fi source ./dist/tools/common.sh +ACTION_NOUN="Schema generation" # # Environment variables @@ -75,7 +76,7 @@ clean() { # Populate container skeleton archive # create_archive() { - cd $DEST_DIR + cd $DEST_DIR tar acf ../bin/filesystem.tar.zst * }