aboutsummaryrefslogtreecommitdiff
path: root/upload-machine
blob: 19544fa6a4d8319c4cd0469232503c9b30240ef2 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /usr/bin/env bash

# Upload new or upgrade existing machine subvolume on a Build OS build host.
#
# -k            - keep the old subvolume on the build host
#
# <host>        - build host to upload to (as user 'build')
# <new-subvol>  - machine subvolume to upload
# <old-subvol>  - previous machine subvolume (btrfs send -p)
#
usage="usage: $0 [<options>] <host> <new-subvol> [<old-subvol>]"

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; }

keep=false

while [ "$#" -gt 0 ]; do
  case "$1" in
    -k)
      keep=true
      shift
      ;;
    -*)
      error "unknown option: $1"
      ;;
    *)
      break
      ;;
  esac
done

host="$1"
newsv="${2%/}"
oldsv="${3%/}"

if [ -z "$host" -o -z "$newsv" ]; then
  error "$usage"
fi

host="build@$host"

newsv_name="$(sed -n -re 's%^(.*/)?([^/]+)$%\2%p' <<<"$newsv")"
oldsv_name="$(sed -n -re 's%^(.*/)?([^/]+)$%\2%p' <<<"$oldsv")"

if [ -z "$newsv_name" ]; then
  error "unable to extract subvolume name from '$newsv'"
fi

if [ -n "$oldsv" -a -z "$oldsv_name" ]; then
  error "unable to extract subvolume name from '$oldsv'"
fi

# Get the machine link (<name>-<P>) and name.
#
mlink="$(sed -n -re 's/^(.+-[0-9]+)\.[0-9]+$/\1/p' <<<"$newsv_name")"
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 '$newsv'"
fi

# Subvolume paths on build host.
#
newsv_host="$machines/$mname/$newsv_name"
oldsv_host="$machines/$mname/$oldsv_name"

# Make sure subvolumes are read-only.
#
function check_ro () # <subvol>
{
  local r;
  r="$(btrfs property get -ts "$1" ro)"
  if [ "$r" != "ro=true" ]; then
    info "subvolume '$1' is not read-only"
    info "to change, run: btrfs property set -ts $1 ro true"
    exit 1
  fi
}

check_ro "$newsv"
if [ -n "$oldsv" ]; then
  check_ro "$oldsv"
fi

# btrfs send command
#
send=(sudo btrfs send)
if [ -n "$oldsv" ]; then
  send+=(-p "$oldsv")
fi
send+=("$newsv")

set -x

# Make sure the machine directory exists.
#
ssh "$host" mkdir -p "$machines/$mname"

# Send the snapshot over.
#
sudo "${send[@]}" | ssh "$host" sudo btrfs receive "$machines/$mname/"

# Adjust machine ownership.
#
ssh "$host" sudo btrfs property set -ts "$newsv_host" ro false
ssh "$host" sudo chown build:build "$newsv_host"
ssh "$host" sudo chown build:build "$newsv_host/*"
ssh "$host" btrfs property set -ts "$newsv_host" ro true

# Atomically switch the current machine.
#
ssh "$host" "cd $machines/$mname && ln -s $newsv_name new-$mlink"
ssh "$host" "cd $machines/$mname && mv -T new-$mlink $mlink"

# Remove the old machine subvolume.
#
{ set +x; } 2>/dev/null
if [ -z "$oldsv" -o "$keep" = true ]; then
  exit 0
fi

set -x
ssh "$host" btrfs property set -ts "$oldsv_host" ro false
ssh "$host" btrfs subvolume delete "$oldsv_host"