hosts: deploy in individual effects
This commit is contained in:
parent
f88749fd95
commit
7fdafae1c2
1 changed files with 49 additions and 46 deletions
|
@ -1,7 +1,7 @@
|
||||||
{ config, inputs, lib, self, withSystem, ... }:
|
{ config, lib, self, withSystem, ... }:
|
||||||
|
|
||||||
let
|
let
|
||||||
inherit (lib) const elem flip genAttrs mapAttrs;
|
inherit (lib) const elem flip genAttrs mapAttrs';
|
||||||
inherit (config) gods defaultEffectSystem systems;
|
inherit (config) gods defaultEffectSystem systems;
|
||||||
inherit (self) nixosConfigurations;
|
inherit (self) nixosConfigurations;
|
||||||
|
|
||||||
|
@ -9,54 +9,57 @@ let
|
||||||
|
|
||||||
withEffectSystem = withSystem defaultEffectSystem;
|
withEffectSystem = withSystem defaultEffectSystem;
|
||||||
|
|
||||||
callUpon = hours: mapAttrs (hour: const nixosConfigurations.${hour}.config.system.build.toplevel) hours;
|
callUpon = hour: { ${hour} = nixosConfigurations.${hour}.config.system.build.toplevel; };
|
||||||
in
|
in
|
||||||
|
|
||||||
{
|
{
|
||||||
herculesCI = { config, ... }: {
|
herculesCI = { config, ... }: {
|
||||||
onPush.default.outputs.effects.callUponTheHours = withEffectSystem ({ hci-effects, ... }: let
|
onPush.default.outputs.effects = mapAttrs' (hour: const {
|
||||||
inherit (hci-effects) runIf runCachixDeploy;
|
name = "deploy-${hour}";
|
||||||
in runIf (elem config.repo.branch [ "master" "staging" ]) (
|
value = withEffectSystem ({ hci-effects, ... }: let
|
||||||
runCachixDeploy {
|
inherit (hci-effects) runIf runCachixDeploy;
|
||||||
async = true;
|
in runIf (elem config.repo.branch [ "master" "staging" ]) (
|
||||||
deploy = {
|
runCachixDeploy {
|
||||||
agents = callUpon chosenHours;
|
async = true;
|
||||||
rollbackScript = genAttrs systems (flip withSystem ({ pkgs, ... }:
|
deploy = {
|
||||||
let
|
agents = callUpon hour;
|
||||||
scheduleReboot = pkgs.writeShellScript "schedule-reboot.sh" ''
|
rollbackScript = genAttrs systems (flip withSystem ({ pkgs, ... }:
|
||||||
export PATH="${pkgs.consul}/bin:${pkgs.systemd}/bin:${pkgs.coreutils}/bin"
|
let
|
||||||
currentTime=$(date +%s)
|
scheduleReboot = pkgs.writeShellScript "schedule-reboot.sh" ''
|
||||||
lastScheduledTime=$(consul kv get system/coordinated-reboot/last)
|
export PATH="${pkgs.consul}/bin:${pkgs.systemd}/bin:${pkgs.coreutils}/bin"
|
||||||
if [[ $? -ne 0 ]]; then
|
currentTime=$(date +%s)
|
||||||
lastScheduledTime=$((currentTime - 300))
|
lastScheduledTime=$(consul kv get system/coordinated-reboot/last)
|
||||||
|
if [[ $? -ne 0 ]]; then
|
||||||
|
lastScheduledTime=$((currentTime - 300))
|
||||||
|
fi
|
||||||
|
nextScheduledTime=$((lastScheduledTime + 3600))
|
||||||
|
if [[ $nextScheduledTime -lt $((currentTime + 300)) ]]; then
|
||||||
|
nextScheduledTime=$((currentTime + 300))
|
||||||
|
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" ''
|
||||||
|
export PATH="${pkgs.consul}/bin:${pkgs.coreutils}/bin"
|
||||||
|
if [[ "$(realpath /run/booted-system/kernel)" != "$(realpath /run/current-system/kernel)" ]]; then
|
||||||
|
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}
|
||||||
fi
|
fi
|
||||||
nextScheduledTime=$((lastScheduledTime + 3600))
|
''
|
||||||
if [[ $nextScheduledTime -lt $((currentTime + 300)) ]]; then
|
));
|
||||||
nextScheduledTime=$((currentTime + 300))
|
};
|
||||||
fi
|
}
|
||||||
consul kv put system/coordinated-reboot/last $nextScheduledTime
|
));
|
||||||
echo "Scheduling reboot for $nextScheduledTime"
|
}) chosenHours;
|
||||||
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" ''
|
|
||||||
export PATH="${pkgs.consul}/bin:${pkgs.coreutils}/bin"
|
|
||||||
if [[ "$(realpath /run/booted-system/kernel)" != "$(realpath /run/current-system/kernel)" ]]; then
|
|
||||||
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}
|
|
||||||
fi
|
|
||||||
''
|
|
||||||
));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
));
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue