nix/safe-remote-upgrade.bash

50 lines
873 B
Bash
Raw Permalink Normal View History

2024-07-16 22:54:36 -05:00
#!/usr/bin/env bash
usage() {
2024-07-17 14:18:35 -05:00
echo 'usage'
echo ' safe-remote-upgrade.bash $FLAKE_REF $TARGET_HOST'
2024-07-16 22:54:36 -05:00
}
error() {
echo "error: $1"
usage
}
if [[ -z $1 ]]; then
echo "error: no flake specified"
usage
exit 1
fi
flake="$1"; shift
if [[ -z $1 ]]; then
echo "error: no target host specified"
usage
exit 1
fi
target_host="$1"; shift
2024-07-17 14:18:35 -05:00
set -eu
2024-07-16 22:54:36 -05:00
git add -A
ssh "root@$target_host" "bash -c '
set -m
2024-08-06 10:33:09 -05:00
# sleep 15 mins
2024-07-17 14:18:35 -05:00
echo \"Starting background reboot job...\"
2024-08-06 10:33:09 -05:00
(sleep 900; reboot;) &
2024-07-16 22:54:36 -05:00
jobs -p
disown
2024-07-17 14:18:35 -05:00
'" &
2024-07-16 22:54:36 -05:00
nix run nixpkgs#nixos-rebuild -- --flake "$flake" \
--target-host "root@$target_host" test --show-trace
echo "Upgrade ready for verification. If you still have SSH access you can bail out without waiting with the following command:"
echo " ssh 'root@$target_host' nixos-rebuild --rollback switch"
2024-07-17 14:18:35 -05:00
echo
echo "Waiting..."
wait
2024-07-16 22:54:36 -05:00
echo 'Done!'