diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2019-02-11 09:13:48 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2019-02-11 09:13:48 +0200 |
commit | 191e64af54b5dffce9f9288045dca3317af19f8e (patch) | |
tree | ce27bc90b1523a116856e65266753158b333753b /remove-machine | |
parent | 4f96bb013c4ee1ffce17883136308b9d4a67d62b (diff) |
Add remove-machine script
Diffstat (limited to 'remove-machine')
-rwxr-xr-x | remove-machine | 95 |
1 files changed, 95 insertions, 0 deletions
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. +# +# <host> - build host to upload to (as user 'build') +# <machine> - machine name including version +# +usage="usage: $0 <host> <machine>" + +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 (<name>-<P>) 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 <name>-<toolchain>-<xxx> 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" |