aboutsummaryrefslogtreecommitdiff
path: root/remove-machine
blob: 13ce31fe7d9810335bbc51b479f3903ed1741389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /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 1
  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
  # See upload-machine on the -f (force) flags.
  #
  ssh "$host" btrfs property set -f -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"