2023-02-24 16:16:15 +02:00
|
|
|
{ config, inputs, lib, self, withSystem, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (lib) elem mapAttrs toLower;
|
2023-03-20 20:11:42 +02:00
|
|
|
inherit (config) gods defaultEffectSystem;
|
2023-02-24 16:16:15 +02:00
|
|
|
inherit (self) nixosConfigurations;
|
|
|
|
|
2023-03-20 20:11:42 +02:00
|
|
|
meta = import ../tools/meta.nix;
|
|
|
|
|
2023-02-24 16:16:15 +02:00
|
|
|
chosenHours = gods.fromLight;
|
|
|
|
|
|
|
|
withEffectSystem = withSystem defaultEffectSystem;
|
|
|
|
|
2023-03-21 18:10:27 +02:00
|
|
|
callUpon = name: host: withEffectSystem ({ config, hci-effects, ... }: let
|
2023-02-24 16:16:15 +02:00
|
|
|
inherit (hci-effects) runIf runNixOS;
|
|
|
|
inherit (host.enterprise) subdomain;
|
|
|
|
|
2023-03-20 20:11:42 +02:00
|
|
|
hostname = "${toLower name}.${subdomain}.${meta.domain}";
|
2023-02-24 16:16:15 +02:00
|
|
|
|
|
|
|
deploy-rs = inputs.deploy-rs.lib."${host.system}";
|
|
|
|
in {
|
2023-03-21 18:10:27 +02:00
|
|
|
effect = { branch, ... }: runIf (elem branch [ "master" "staging" ])
|
2023-05-06 19:17:09 +03:00
|
|
|
(runNixOS rec {
|
2023-02-24 16:16:15 +02:00
|
|
|
requiredSystemFeatures = [ "hci-deploy-agent-nixos" ];
|
|
|
|
|
|
|
|
inherit (nixosConfigurations.${name}) config;
|
|
|
|
|
|
|
|
secretsMap.ssh = "deploy-ssh";
|
|
|
|
|
|
|
|
userSetupScript = ''
|
|
|
|
writeSSHKey ssh
|
|
|
|
cat >>~/.ssh/known_hosts <<EOF
|
|
|
|
${hostname} ${host.ssh.id.publicKey}
|
|
|
|
EOF
|
|
|
|
'';
|
|
|
|
|
|
|
|
ssh.destination = "root@${hostname}";
|
2023-05-06 19:17:09 +03:00
|
|
|
|
|
|
|
postEffect = let
|
|
|
|
scheduleReboot = builtins.toFile "schedule-reboot.sh" /*bash*/ ''
|
|
|
|
currentTime=$(date +%s)
|
|
|
|
lastScheduledTime=$(consul kv get system/coordinated-reboot/last)
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
lastScheduledTime=$((currentTime - 300))
|
|
|
|
fi
|
|
|
|
nextScheduledTime=$((lastScheduledTime + 900))
|
|
|
|
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 hci-effects.ssh ssh /*bash*/ ''
|
|
|
|
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 bash ${scheduleReboot}
|
|
|
|
fi
|
|
|
|
'';
|
2023-02-24 16:16:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
deploy = {
|
|
|
|
inherit hostname;
|
|
|
|
profiles.system = {
|
|
|
|
user = "root";
|
|
|
|
sshUser = "deploy";
|
|
|
|
path = deploy-rs.activate.nixos self.nixosConfigurations.${name};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
calledUponHours = mapAttrs callUpon chosenHours;
|
|
|
|
|
|
|
|
pick = format: _: calledUponHour: calledUponHour.${format};
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2023-03-21 18:10:27 +02:00
|
|
|
herculesCI = { config, ... }: let
|
|
|
|
powers = mapAttrs (pick "effect") calledUponHours;
|
|
|
|
wield = mapAttrs (_: wieldPowerWith: wieldPowerWith config.repo);
|
|
|
|
in {
|
|
|
|
onPush.default.outputs.effects = wield powers;
|
|
|
|
};
|
2023-02-24 16:16:15 +02:00
|
|
|
|
|
|
|
flake.deploy.nodes = mapAttrs (pick "deploy") calledUponHours;
|
|
|
|
}
|