nix-config/pkgs/deployment.nix

119 lines
3.8 KiB
Nix
Raw Permalink Normal View History

2023-06-08 07:53:57 +02:00
{ self, pkgs, lib }:
2023-06-08 15:27:13 +02:00
# This generates deployement scripts **ONLY** for non-microvm (e.g. bare-metal
# or conventional vm) hosts
2022-05-14 20:47:17 +02:00
let
2023-06-08 07:53:57 +02:00
# filter out deployable (aka not microvm or container) systems
filterHosts = k: v: !(builtins.hasAttr "microvm" v.config);
nonVmHosts = lib.filterAttrs filterHosts self.nixosConfigurations;
2022-05-14 20:47:17 +02:00
2023-06-08 07:53:57 +02:00
# the deployment script
deployScriptTemplate = (system: command:
let
2023-11-26 02:38:52 +01:00
ip = system._module.specialArgs.registry.wgAddr4;
2023-06-08 07:53:57 +02:00
host = system.config.networking.hostName;
in
2023-11-29 15:03:45 +01:00
(pkgs.writeScript "deploy-${command}-${host}" ''
#!${pkgs.runtimeShell}
2023-06-08 13:45:23 +02:00
set -e
2023-06-08 07:53:57 +02:00
echo -e "\033[0;33mChecking if ${host} is up (ip: ${ip})\033[0m"
if ping -c 1 ${ip} > /dev/null
then
2023-06-08 07:53:57 +02:00
echo -e "\033[0;32mRedeploying ${host} with \"${command}\"\033[0m"
nixos-rebuild --flake ${self}\#${system.config.networking.hostName} --target-host root@${ip} --use-substitutes ${command} -L
else
2023-06-08 07:53:57 +02:00
echo -e "\033[0;31m${ip} seems to be down!\033[0m"
exit 1
fi
''));
2022-05-14 20:47:17 +02:00
2023-06-08 15:27:13 +02:00
# garbage collect everything
garbageCollect = (system:
let
2023-11-26 02:38:52 +01:00
ip = system._module.specialArgs.registry.wgAddr4;
2023-06-08 15:27:13 +02:00
host = system.config.networking.hostName;
in
2023-11-29 15:03:45 +01:00
(pkgs.writeScript "collect-garbage-${host}" ''
2023-06-08 15:27:13 +02:00
#!${pkgs.runtimeShell}
set -e
echo -e "\033[0;33mChecking if ${host} is up (ip: ${ip})\033[0m"
if ping -c 1 ${ip} > /dev/null
then
echo -e "\033[0;32mCollecting garbage on ${host} with \"nix-collect-garbage -d\"\033[0m"
ssh root@${ip} -- nix-collect-garbage -d
else
echo -e "\033[0;31m${ip} seems to be down!\033[0m"
exit 1
fi
''));
# reboot everything
reboot = (system:
let
2023-11-26 02:38:52 +01:00
ip = system._module.specialArgs.registry.wgAddr4;
2023-06-08 15:27:13 +02:00
host = system.config.networking.hostName;
in
2023-11-29 15:03:45 +01:00
(pkgs.writeScript "reboot-${host}" ''
2023-06-08 15:27:13 +02:00
#!${pkgs.runtimeShell}
set -e
echo -e "\033[0;33mChecking if ${host} is up (ip: ${ip})\033[0m"
if ping -c 1 ${ip} > /dev/null
then
echo -e "\033[0;32mRebooting ${host}\033[0m"
ssh root@${ip} -- shutdown -r 1
echo -e "\033[0;31m${host} IS SCHEDULED FOR REBOOT IN 1 MINUTE\033[0m"
else
echo -e "\033[0;31m${ip} seems to be down!\033[0m"
exit 1
fi
''));
# individual script generation
deployScriptWriter = (command: lib.mapAttrs' (name: system: lib.nameValuePair ("rebuild-" + command + "-" + name) (deployScriptTemplate system command)) nonVmHosts);
2022-05-14 20:47:17 +02:00
2023-06-08 15:27:13 +02:00
switchInstallScripts = deployScriptWriter "switch";
bootInstallScripts = deployScriptWriter "boot";
installScripts = bootInstallScripts // switchInstallScripts;
garbageCollectScripts = lib.mapAttrs' (name: system: lib.nameValuePair ("collect-garbage-" + name) (garbageCollect system)) nonVmHosts;
rebootScripts = lib.mapAttrs' (name: system: lib.nameValuePair ("reboot-" + name) (reboot system)) nonVmHosts;
## all at once
2023-11-29 15:03:45 +01:00
switchAll = lib.strings.concatStringsSep "\n" (builtins.attrValues switchInstallScripts);
bootAll = lib.strings.concatStringsSep "\n" (builtins.attrValues bootInstallScripts);
rebootAll = lib.strings.concatStringsSep "\n" (builtins.attrValues rebootScripts);
garbageAll = lib.strings.concatStringsSep "\n" (builtins.attrValues garbageCollectScripts);
2023-06-08 15:27:13 +02:00
nukeAll = lib.mapAttrs'
(name: scripts:
2023-11-29 15:03:45 +01:00
lib.nameValuePair (name) (pkgs.writeScript "${name}" ''
#!${pkgs.runtimeShell}
set -x
2023-06-08 15:27:13 +02:00
${scripts}
''))
2023-06-08 15:27:13 +02:00
{
rebuild-boot-all = bootAll;
rebuild-switch-all = switchAll;
reboot-all = rebootAll;
garbage-collect-all = garbageAll;
};
allPackages = installScripts // garbageCollectScripts // rebootScripts // nukeAll;
# rewrite to app definitions
2022-06-11 01:19:12 +02:00
in
builtins.mapAttrs
(name: value: {
type = "app";
2023-11-29 15:03:45 +01:00
program = "${value}";
})
allPackages