diff --git a/arkdep b/arkdep index 4822511..1e2ab2c 100755 --- a/arkdep +++ b/arkdep @@ -15,6 +15,7 @@ if [[ ! -n $1 ]]; then deploy Deploy a new or update an existing deployment init Initialize arkdep on a new system teardown Remove all arkdep-deploy related files and folders + cleanup Remove any hanging images and unfished downloads healthcheck Check for and report any config issues or untracked deployments get-available List available packages in repo remove Remove a specified deployment @@ -120,9 +121,9 @@ cleanup_and_quit () { ## Healthcheck # -# Check for and report on any issues such as untracked deployments or hanging files in cache -healthcheck () { - +# Set common variables for healthcheck and cleanup, +# only set all these vars if they will actually be used +if [[ always_healthcheck -eq 1 ]] || [[ $1 =~ ^(healthcheck|cleanup) ]]; then # Gather tracked deployments declare -r tracker=($(cat $arkdep_dir/tracker)) declare -r deployed=($(ls $arkdep_dir/deployments/)) @@ -147,6 +148,10 @@ healthcheck () { # Clean whitespaces untracked=$(echo $untracked | xargs) +fi + +# Check for and report on any issues such as untracked deployments or hanging files in cache +healthcheck () { if [[ -n $untracked ]]; then printf '\e[1;33m\e[0m\e[1m The following deployments were found but are untracked\n\e[0m' @@ -172,6 +177,48 @@ healthcheck () { } +cleanup () { + + echo UN: ${untracked[@]} + echo A: ${hanging_cache[@]} + + if [[ -n $untracked ]]; then + printf '\e[1;34m-->\e[0m\e[1m Cleaning up untracked deployments\e[0m\n' + for target in ${untracked[@]}; do + if [[ $target == 'recovery' ]]; then + printf '\e[1;33m\e[0m\e[1m Detected untracked recovery entry, ignoring\n\e[0m' + break + fi + + # Ensure deployment is not currently active + if grep -q "$arkdep_dir/deployments/$target/rootfs" /proc/cmdline; then + printf '\e[1;33m\e[0m\e[1m Target is currently active deployment\n\e[0m' + break + fi + + # Remove bootloader entry + rm -rfv $arkdep_boot/loader/entries/$target.conf + rm -rfv $arkdep_boot/arkdep/$target + + # 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 $ARKDEP_ROOT/$volume) ro false || printf "failed to make subvol $volume writable\n" + done + + # Remove the deployment + rm -rfv $arkdep_dir/deployments/$target + done + fi + + if [[ -n $hanging_cache ]]; then + printf '\e[1;34m-->\e[0m\e[1m Cleaning up hanging cache\e[0m\n' + for target in ${hanging_cache[@]}; do + rm -v $arkdep_dir/cache/$target + done + fi + +} + # Always healthcheck on run if requested in config, unless the user explicitely called it [[ $always_healthcheck -eq 1 ]] && [[ ! $1 == 'healthcheck' ]] && healthcheck @@ -801,6 +848,7 @@ deploy () { [[ $1 == 'deploy' ]] && deploy $2 $3 [[ $1 == 'remove' ]] && remove_deployment $2 $3 [[ $1 == 'healthcheck' ]] && healthcheck $1 +[[ $1 == 'cleanup' ]] && cleanup # No valid params were provided exit 3