2023-11-05 17:24:27 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
2023-11-05 18:24:37 +01:00
|
|
|
ALLOW_REBOOT=
|
2023-12-01 18:14:25 +01:00
|
|
|
NO_CONFIRM=
|
2023-12-10 19:51:21 +01:00
|
|
|
NODE_EXPORTER_METRICS_PATH=
|
2023-11-05 18:24:37 +01:00
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case $1 in
|
|
|
|
--allow-reboot)
|
|
|
|
ALLOW_REBOOT=1
|
|
|
|
shift
|
2023-12-01 18:14:25 +01:00
|
|
|
;;
|
|
|
|
--no-confirm)
|
|
|
|
NO_CONFIRM=1
|
|
|
|
shift
|
|
|
|
;;
|
2023-12-10 19:51:21 +01:00
|
|
|
--node-exporter-metrics-path)
|
|
|
|
NODE_EXPORTER_METRICS_PATH=$2
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2023-11-05 18:24:37 +01:00
|
|
|
*)
|
|
|
|
echo "Unknown option $1"
|
|
|
|
exit 1
|
2023-12-01 18:14:25 +01:00
|
|
|
;;
|
2023-11-05 18:24:37 +01:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2023-11-05 22:26:48 +01:00
|
|
|
HYDRA_JOB_URL="https://hydra.clerie.de/job/nixfiles/nixfiles/nixosConfigurations.${HOSTNAME}/latest-finished"
|
2023-11-05 17:24:27 +01:00
|
|
|
|
|
|
|
echo "Fetching job output from ${HYDRA_JOB_URL}"
|
|
|
|
STORE_PATH="$(curl --fail -s -L -H "Accept: application/json" "${HYDRA_JOB_URL}" | jq -r ".buildoutputs.out.path")"
|
|
|
|
|
2023-12-01 18:14:25 +01:00
|
|
|
if [[ -z $NO_CONFIRM ]]; then
|
|
|
|
echo ""
|
|
|
|
echo " ! WARNING !"
|
|
|
|
echo ""
|
|
|
|
echo " You are about to upgrade ${HOSTNAME} to ${STORE_PATH}."
|
|
|
|
echo " This can be an older version than currently running on this system."
|
|
|
|
echo ""
|
|
|
|
read -e -r -p "Continue?" confirm
|
|
|
|
echo "$confirm" > /dev/null
|
|
|
|
fi
|
|
|
|
|
2023-11-05 17:24:27 +01:00
|
|
|
echo "Download ${STORE_PATH}"
|
|
|
|
nix copy --from "https://nix-cache.clerie.de" "${STORE_PATH}"
|
|
|
|
|
|
|
|
echo "Add to system profile"
|
|
|
|
nix-env -p "/nix/var/nix/profiles/system" --set "${STORE_PATH}"
|
|
|
|
|
2023-11-05 18:24:37 +01:00
|
|
|
echo "Set as boot target"
|
|
|
|
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
|
|
|
|
|
2023-12-10 19:51:21 +01:00
|
|
|
if [[ -n "$NODE_EXPORTER_METRICS_PATH" ]]; then
|
|
|
|
echo "Write monitoring check data"
|
2024-03-17 15:50:49 +01:00
|
|
|
echo "clerie_system_upgrade_last_check $(date +%s)" > "$NODE_EXPORTER_METRICS_PATH"
|
2023-12-10 19:51:21 +01:00
|
|
|
fi
|
|
|
|
|
2023-11-05 18:24:37 +01:00
|
|
|
BOOTED_SYSTEM_KERNEL="$(readlink /run/booted-system/{initrd,kernel,kernel-modules})"
|
|
|
|
ACTIVATING_SYSTEM_KERNEL="$(readlink /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"
|
|
|
|
|
|
|
|
if [[ "$BOOTED_SYSTEM_KERNEL" != "$ACTIVATING_SYSTEM_KERNEL" ]]; then
|
|
|
|
echo "Reboot is required"
|
|
|
|
if [[ -n "$ALLOW_REBOOT" ]]; then
|
|
|
|
echo "Rebooting system now"
|
|
|
|
shutdown -r +1 "System update requires reboot"
|
|
|
|
else
|
|
|
|
echo "Automatic reboot not allowed (maybe use --allow-reboot next time)"
|
|
|
|
echo "The system upgrade is staged, please reboot manually soon"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "No reboot is required"
|
|
|
|
echo "Activating system now"
|
|
|
|
/nix/var/nix/profiles/system/bin/switch-to-configuration switch
|
|
|
|
fi
|
2023-11-05 17:24:27 +01:00
|
|
|
|
|
|
|
echo "Finished system upgrade"
|