From b304f7b1c52550fef0d0a116e0155f247c16141c Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 31 Mar 2017 15:51:48 +0200 Subject: Establish build machine layout --- buildos | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 124 insertions(+), 1 deletion(-) (limited to 'buildos') diff --git a/buildos b/buildos index 7aa9a0e..ec2be0b 100755 --- a/buildos +++ b/buildos @@ -14,7 +14,8 @@ # owd="$(pwd)" trap "{ cd '$owd'; exit 1; }" ERR -set -o errtrace # Trap in functions. +set -o errtrace # Trap in functions. +shopt -s nullglob # Expand patterns than don't match to empty. # Note: diagnostics goes to stdout. # @@ -320,6 +321,128 @@ if [ -z "$tc" ]; then info "no buildos.toolchain_url specified, not bootstrapping" fi +# Cleanup /build/machines/ of any stray snapshots or deleted machines. +# +diag=() +fail= +for v in /build/machines/*; do + if [ ! -d "$v" ]; then + diag+=("$v: error: invalid volume") + fail="true" + continue + fi + + cd "$v" + + for m in *; do + if [ ! -d "$m" ]; then + diag+=("$v/$m: error: invalid machine") + fail="true" + continue + fi + + cd "$m" + + # If there is no current machine symlink, then delete the whole thing. + # + d= + if [ ! -L "$m" ]; then + d="true" + fi + + # Examine each machine subvolume. + # + for s in "$m"-*; do + if [ ! -d "$s" ]; then + diag+=("$v/$m/$s: error: invalid machine subvolume") + fail="true" + continue + fi + + # Unless we are deleting the whole thing, keep initial and bootstrapped + # (for known toolchains) subvolumes. + # + if [ -z "$d" ]; then + # - + # + if [[ "$s" =~ ^"$m"-[0-9]+$ ]]; then + continue + fi + + # - + # + f= + for tn in "${!toolchains[@]}"; do + if [[ "$s" =~ ^"$m"-"$tn"$ ]]; then + f="true" + break + fi + done + + if [ -n "$f" ]; then + continue + fi + fi + + # This is either a stray working submodule or a bootsrapped subvolume + # for a toolchain that was deleted (or we are deleting everything). + # + if ! btrfs property set -ts "$s" ro false; then + diag+=("$v/$m/$s: error: unable to change subvolume property") + fail="true" + continue + fi + + if ! btrfs subvolume delete "$s"; then + diag+=("$v/$m/$s: error: unable to delete subvolume") + fail="true" + continue + fi + + diag+=("$v/$m/$s: info: deleted subvolume") + done + + cd "$v" + + # Delete the machine directory (which we expect to be now empty). + # + if [ -n "$d" ]; then + if ! rmdir "$m"; then + diag+=("$v/$m: error: unable to delete machine directory") + fail="true" + continue + fi + + diag+=("$v/$m: info: deleted machine directory") + fi + done + cd "$owd" +done + +function print_diag () +{ + local p + for p in "${diag[@]}"; do + echo " $p" + done +} + +if [ "${#diag[@]}" -gt 0 ]; then + if [ -z "$fail" ]; then + s="cleaned up entries in /build/machines/" + else + s="invalid entries in /build/machines/, halting" + fi + + print_diag | email "$s" + info "$s" && print_diag 2>&1 + + if [ -n "$fail" ]; then + info "correct and restart the monitor (systemctl restart buildos)" + exit 1 + fi +fi + # Monitoring loop. # while true; do -- cgit v1.1