From 191e64af54b5dffce9f9288045dca3317af19f8e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 11 Feb 2019 09:13:48 +0200 Subject: Add remove-machine script --- remove-machine | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100755 remove-machine (limited to 'remove-machine') diff --git a/remove-machine b/remove-machine new file mode 100755 index 0000000..71c1fc1 --- /dev/null +++ b/remove-machine @@ -0,0 +1,95 @@ +#! /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" -- cgit v1.1