2023-11-07 18:15:25 +02:00
|
|
|
{ config, lib, self, withSystem, ... }:
|
2023-02-24 16:16:15 +02:00
|
|
|
|
|
|
|
let
|
2023-11-07 18:15:25 +02:00
|
|
|
inherit (lib) const elem flip genAttrs mapAttrs';
|
2023-07-23 04:27:24 +03:00
|
|
|
inherit (config) gods defaultEffectSystem systems;
|
2023-02-24 16:16:15 +02:00
|
|
|
inherit (self) nixosConfigurations;
|
|
|
|
|
|
|
|
chosenHours = gods.fromLight;
|
|
|
|
|
|
|
|
withEffectSystem = withSystem defaultEffectSystem;
|
|
|
|
|
2023-11-07 18:15:25 +02:00
|
|
|
callUpon = hour: { ${hour} = nixosConfigurations.${hour}.config.system.build.toplevel; };
|
2023-02-24 16:16:15 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2023-07-23 04:27:24 +03:00
|
|
|
herculesCI = { config, ... }: {
|
2023-11-07 18:15:25 +02:00
|
|
|
onPush.default.outputs.effects = mapAttrs' (hour: const {
|
|
|
|
name = "deploy-${hour}";
|
|
|
|
value = withEffectSystem ({ hci-effects, ... }: let
|
|
|
|
inherit (hci-effects) runIf runCachixDeploy;
|
|
|
|
in runIf (elem config.repo.branch [ "master" "staging" ]) (
|
|
|
|
runCachixDeploy {
|
|
|
|
async = true;
|
|
|
|
deploy = {
|
|
|
|
agents = callUpon hour;
|
2024-06-04 14:57:23 +03:00
|
|
|
rollbackScript = genAttrs systems (flip withSystem ({ config, pkgs, ... }:
|
2023-11-07 18:15:25 +02:00
|
|
|
let
|
|
|
|
scheduleReboot = pkgs.writeShellScript "schedule-reboot.sh" ''
|
2024-06-04 14:57:23 +03:00
|
|
|
export PATH="${config.packages.consul}/bin:${pkgs.systemd}/bin:${pkgs.coreutils}/bin"
|
2023-11-07 18:15:25 +02:00
|
|
|
currentTime=$(date +%s)
|
|
|
|
lastScheduledTime=$(consul kv get system/coordinated-reboot/last)
|
|
|
|
if [[ $? -ne 0 ]]; then
|
2024-06-07 00:06:02 +03:00
|
|
|
lastScheduledTime=$((currentTime - 900))
|
2023-11-07 18:15:25 +02:00
|
|
|
fi
|
|
|
|
nextScheduledTime=$((lastScheduledTime + 3600))
|
2024-06-07 00:06:02 +03:00
|
|
|
if [[ $nextScheduledTime -lt $((currentTime + 900)) ]]; then
|
|
|
|
nextScheduledTime=$((currentTime + 900))
|
2023-11-07 18:15:25 +02:00
|
|
|
fi
|
|
|
|
consul kv put system/coordinated-reboot/last $nextScheduledTime
|
|
|
|
echo "Scheduling reboot for $nextScheduledTime"
|
|
|
|
systemd-analyze timestamp @$nextScheduledTime
|
|
|
|
busctl call \
|
|
|
|
org.freedesktop.login1 \
|
|
|
|
/org/freedesktop/login1 \
|
|
|
|
org.freedesktop.login1.Manager \
|
|
|
|
ScheduleShutdown st reboot ''${nextScheduledTime}000000
|
|
|
|
'';
|
|
|
|
in pkgs.writeShellScript "post-effect.sh" ''
|
2024-07-17 23:14:48 +03:00
|
|
|
if [[ -e /etc/consul.json ]]; then
|
|
|
|
export CONSUL_HTTP_ADDR="$(${pkgs.jq}/bin/jq -r < /etc/consul.json '"\(.addresses.http // "127.0.0.1"):\(.ports.http // 8500)"')"
|
|
|
|
fi
|
2024-06-04 14:57:23 +03:00
|
|
|
export PATH="${config.packages.consul}/bin:${pkgs.coreutils}/bin"
|
2023-12-05 02:13:15 +02:00
|
|
|
if [[ "$(realpath /run/booted-system/kernel)" != "$(realpath /nix/var/nix/profiles/system/kernel)" ]]; then
|
2023-11-07 18:15:25 +02:00
|
|
|
echo "Scheduling reboot for kernel upgrade"
|
|
|
|
if ! consul members >/dev/null; then
|
|
|
|
echo "Consul not active, skipping reboot"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
consul lock --timeout=3m system/coordinated-reboot ${scheduleReboot}
|
2023-07-23 04:27:24 +03:00
|
|
|
fi
|
2023-11-07 18:15:25 +02:00
|
|
|
''
|
|
|
|
));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}) chosenHours;
|
2023-03-21 18:10:27 +02:00
|
|
|
};
|
2023-02-24 16:16:15 +02:00
|
|
|
}
|