#! /usr/bin/env bash # Remove machine from a Build OS build host. # # Note that this removes the entire machine directory with all the subvolumes, # etc., rather than a specific machine subvolume. # # - build host to upload to (as user 'build') # - machine name including version # usage="usage: $0 " machines=/build/machines/default owd="$(pwd)" trap "{ cd '$owd'; exit 1; }" ERR set -o errtrace # Trap in functions. function info () { echo "$*" 1>&2; } function error () { info "$*"; exit 1; } while [ "$#" -gt 0 ]; do case "$1" in -*) error "unknown option: $1" ;; *) break ;; esac done host="$1" machine="${2%/}" if [ -z "$host" -o -z "$machine" ]; then error "$usage" fi # Get the machine link (-

) and name. # mlink="$(sed -n -re 's/^(.+-[0-9]+)\.[0-9]+$/\1/p' <<<"$machine")" mname="$(sed -n -re 's/^(.+)-[0-9]+$/\1/p' <<<"$mlink")" if [ -z "$mlink" -o -z "$mname" ]; then error "unable to extract machine link/name from '$machine'" fi host="build@$host" # Make sure the machine exists. # if ! ssh "$host" test -d "$machines/$mname"; then error "$machines/$mname does not exist on $host" fi set -x # Remove the current machine symlink, if any. # ssh "$host" rm -f "$machines/$mname/$mlink" # Wait for all the -- subvolumes to disappear. # { set +x; } 2>/dev/null sv=($(ssh "$host" "shopt -s nullglob; echo $machines/$mname/$mname-*-*/")) for d in "${sv[@]}"; do while ssh "$host" test -d "$d"; do echo "waiting for $d to disappear..." sleep 10 done done set -x # Remove the initial and bootstrapped machine subvolume(s). # { set +x; } 2>/dev/null sv=($(ssh "$host" "shopt -s nullglob; echo $machines/$mname/$mname-*/")) for d in "${sv[@]}"; do set -x ssh "$host" btrfs property set -ts "$d" ro false ssh "$host" btrfs subvolume delete "$d" { set +x; } 2>/dev/null done set -x # Finally remove the machine directory (which should be empty). # ssh "$host" rmdir "$machines/$mname"