Implement trap for remove_deployment

This commit is contained in:
Dennis ten Hoove 2023-12-19 03:47:23 +01:00
parent 087d3be61f
commit 0c12a59ea2

52
arkdep
View file

@ -53,6 +53,45 @@ source $(readlink -m $arkdep_dir/config)
[[ -z ${deploy_keep+x} ]] && deploy_keep=3 && printf '\e[1;33m<!>\e[0m\e[1m deploy_config not defined in config, using default\e[0m\n'
[[ -z ${clean_cache_on_remove+x} ]] && clean_cache_on_remove=1 && printf '\e[1;33m<!>\e[0m\e[1m clean_cache_on_remove not defined in config, using default\e[0m\n'
## Setup and process trap
#
# If called perform a safe shutdown of the script and clean up current task
trapped () {
# Logic for remove_deployment
if [[ -v trap_remove_deployment ]] && [[ ! -v trap_remove_deployment_done ]]; then
printf '\e[1;31m<#>\e[0m\e[1m remove_deployment was cancelled\n\e[0m'
# If the bootloader was not yet changed, no changes should have been made to the system if true
if [[ -v trap_remove_deployment_bootloader ]]; then
printf '\e[1;33m<!>\e[0m\e[1m Bootloader entires of the related deployment have already been removed\n\e[0m'
else
printf '\e[1;33m<!>\e[0m\e[1m The bootloader configuration was not yet removed, this likely means no changes have been made to the system\n\e[0m'
fi
# Disks have been unlocked and may still be writable
if [[ -v trap_remove_deployment_unlock ]] && [[ ! -v trap_remove_deployment_remove ]]; then
printf '\e[1;33m<!>\e[0m\e[1m A deployment was unlocked but not (fully) removed, it may currently still remain and be in a writable state\n\e[0m'
fi
# Target was not yet removed from tracker
if [[ ! -v trap_remove_deployment_tracker ]]; then
printf '\e[1;33m<!>\e[0m\e[1m Deployment was not removed from tracker, future runs of arkdep will still assume it to be installed\n\e[0m'
fi
# Cache cleanup did not finish
if [[ -v trap_remove_deployment_cache ]]; then
printf '\e[1;33m<!>\e[0m\e[1m A cache cleanup was started but not finished, data may remain in cache and possibly be corrupted\n\e[0m'
fi
fi
exit 2
}
# Trap any attempt at canceling to ensure a clean shutdown
trap trapped SIGINT SIGTERM
## Common functions
#
# Cleanup and quit if error
@ -212,6 +251,8 @@ teardown () {
remove_deployment () {
trap_remove_deployment=1
# Ensure required vars are set
[[ -z $1 ]] && \
printf 'No deployment defined\n' && exit 1
@ -243,26 +284,37 @@ remove_deployment () {
rm -rfv /boot/loader/entries/$target.conf
rm -rfv /boot/arkdep/$target
trap_remove_deployment_bootloader=1
# Ensure the deployment and all sub-volumes are writable
for volume in $(btrfs subvolume list / | grep -oE '[^ ]+$' | grep $target); do
btrfs property set -f -ts $(readlink -m /$volume) ro false || printf "failed to make subvol $volume writable\n"
done
trap_remove_deployment_unlock=1
# Remove the deployment
rm -rf $(readlink -m $arkdep_dir/deployments/$target)
trap_remove_deployment_remove=1
# Remove from tracker
grep -v $1 $arkdep_dir/tracker > $arkdep_dir/tracker_tmp || cleanup_and_quit 'Failed to update tracker file'
mv $arkdep_dir/tracker_tmp $arkdep_dir/tracker || cleanup_and_quit 'Failed to move tracker_tmp file to tracker'
trap_remove_deployment_tracker=1
# Remove images from cache if requested
if [[ $clean_cache_on_remove -eq 1 ]]; then
trap_remove_deployment_cache=1
# Only attempt remove if file exists
if ls $arkdep_dir/cache/ | grep $1; then
rm -v $arkdep_dir/cache/$1.tar.*
fi
fi
trap_remove_deployment_done=1
exit 0
}