2023-07-30 05:49:54 +03:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
2023-09-18 19:50:24 +03:00
|
|
|
}: let
|
|
|
|
mkOptional = {
|
|
|
|
derivationTarget,
|
2023-09-20 14:43:04 +03:00
|
|
|
pingTarget,
|
2023-09-18 19:50:24 +03:00
|
|
|
}: ''
|
2023-09-20 14:43:04 +03:00
|
|
|
if ${pkgs.inetutils}/bin/ping -c 1 ${pingTarget}; then
|
2023-09-18 19:50:24 +03:00
|
|
|
${pkgs.deploy-rs}/bin/deploy \
|
|
|
|
--ssh-opts="-i ''${CREDENTIALS_DIRECTORY}/ssh-key" \
|
|
|
|
--ssh-user=deployerbot-follower \
|
|
|
|
--confirm-timeout 60 \
|
|
|
|
--targets ${derivationTarget} -- \
|
|
|
|
--accept-flake-config
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
in {
|
2023-07-30 05:49:54 +03:00
|
|
|
options.mj.services.deployerbot.main = with lib.types; {
|
|
|
|
enable = lib.mkEnableOption "Enable system updater orchestrator";
|
|
|
|
deployDerivations = lib.mkOption {type = listOf str;};
|
2023-09-18 19:50:24 +03:00
|
|
|
deployIfPresent = lib.mkOption {
|
|
|
|
type = listOf (submodule (
|
|
|
|
{...}: {
|
|
|
|
options = {
|
|
|
|
derivationTarget = lib.mkOption {type = str;};
|
2023-09-20 14:43:04 +03:00
|
|
|
pingTarget = lib.mkOption {type = str;};
|
2023-09-18 19:50:24 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
));
|
|
|
|
default = [];
|
|
|
|
};
|
2023-07-30 05:49:54 +03:00
|
|
|
uidgid = lib.mkOption {type = int;};
|
|
|
|
repo = lib.mkOption {type = str;};
|
|
|
|
};
|
|
|
|
|
|
|
|
options.mj.services.deployerbot.follower = with lib.types; {
|
|
|
|
enable = lib.mkEnableOption "Allow system to be deployed with deployerbot";
|
2023-09-18 20:49:17 +03:00
|
|
|
sshAllowSubnets = lib.mkOption {type = listOf str;};
|
2023-07-30 05:49:54 +03:00
|
|
|
publicKey = lib.mkOption {type = str;};
|
|
|
|
uidgid = lib.mkOption {type = int;};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkMerge [
|
2023-09-23 22:25:58 +03:00
|
|
|
(let
|
|
|
|
cfg = config.mj.services.deployerbot.main;
|
|
|
|
in
|
|
|
|
lib.mkIf cfg.enable {
|
|
|
|
# TODO: git config --global user.email bot@jakstys.lt
|
|
|
|
users.users.deployerbot-main = {
|
|
|
|
description = "Deployerbot Main";
|
|
|
|
home = "/var/lib/deployerbot-main";
|
2023-09-23 22:46:14 +03:00
|
|
|
shell = "/bin/sh";
|
2023-09-23 22:25:58 +03:00
|
|
|
group = "deployerbot-main";
|
|
|
|
isSystemUser = true;
|
|
|
|
createHome = true;
|
|
|
|
uid = cfg.uidgid;
|
2023-07-30 05:49:54 +03:00
|
|
|
};
|
2023-09-23 22:25:58 +03:00
|
|
|
users.groups.deployerbot-main.gid = cfg.uidgid;
|
2023-07-30 05:49:54 +03:00
|
|
|
|
2023-09-23 22:25:58 +03:00
|
|
|
systemd.services.deployerbot = {
|
|
|
|
description = "Update all known systems";
|
|
|
|
environment = {TZ = "UTC";};
|
|
|
|
path = [pkgs.git pkgs.openssh pkgs.nix];
|
|
|
|
restartIfChanged = false;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = "deployerbot-main";
|
|
|
|
WorkingDirectory = config.users.users.deployerbot-main.home;
|
|
|
|
LoadCredential = ["ssh-key:/etc/ssh/ssh_host_ed25519_key"];
|
|
|
|
};
|
|
|
|
script = let
|
|
|
|
deployDerivationsStr = builtins.concatStringsSep " " cfg.deployDerivations;
|
|
|
|
in ''
|
|
|
|
set -x
|
|
|
|
export GIT_SSH_COMMAND="ssh -i ''${CREDENTIALS_DIRECTORY}/ssh-key"
|
|
|
|
if [[ ! -d config ]]; then
|
|
|
|
git clone ${cfg.repo} config
|
|
|
|
cd config
|
|
|
|
else
|
|
|
|
cd config
|
|
|
|
git fetch origin
|
|
|
|
git reset --hard origin/main
|
|
|
|
fi
|
2023-07-30 05:49:54 +03:00
|
|
|
|
2023-09-23 22:25:58 +03:00
|
|
|
nix flake update --accept-flake-config --commit-lock-file
|
2023-07-30 05:49:54 +03:00
|
|
|
|
2023-09-23 22:25:58 +03:00
|
|
|
${pkgs.deploy-rs}/bin/deploy \
|
|
|
|
--ssh-opts="-i ''${CREDENTIALS_DIRECTORY}/ssh-key" \
|
|
|
|
--ssh-user=deployerbot-follower \
|
|
|
|
--confirm-timeout 60 \
|
|
|
|
--targets ${deployDerivationsStr} -- \
|
|
|
|
--accept-flake-config
|
2023-07-30 08:53:19 +03:00
|
|
|
|
2023-09-23 22:25:58 +03:00
|
|
|
# Optional deployments
|
|
|
|
${lib.concatLines (map mkOptional cfg.deployIfPresent)}
|
2023-07-30 05:49:54 +03:00
|
|
|
|
2023-09-23 22:25:58 +03:00
|
|
|
# done
|
|
|
|
git push origin main
|
|
|
|
'';
|
|
|
|
};
|
2023-07-30 05:49:54 +03:00
|
|
|
|
2023-09-23 22:25:58 +03:00
|
|
|
systemd.timers.deployerbot = {
|
|
|
|
description = "deployerbot-main timer";
|
|
|
|
wantedBy = ["timers.target"];
|
|
|
|
timerConfig.OnCalendar = "*-*-* 22:00:00 UTC";
|
|
|
|
};
|
2023-07-30 05:49:54 +03:00
|
|
|
|
2023-09-23 22:25:58 +03:00
|
|
|
mj.base.unitstatus.units = ["deployerbot"];
|
|
|
|
|
|
|
|
nix.settings.trusted-users = ["deployerbot-main"];
|
|
|
|
})
|
|
|
|
|
|
|
|
(let
|
|
|
|
cfg = config.mj.services.deployerbot.follower;
|
|
|
|
in
|
|
|
|
lib.mkIf cfg.enable {
|
2023-09-23 22:56:23 +03:00
|
|
|
users.users.deployerbot-follower = {
|
|
|
|
description = "Deployerbot Follower";
|
|
|
|
home = "/var/lib/deployerbot-follower";
|
|
|
|
shell = "/bin/sh";
|
|
|
|
group = "deployerbot-follower";
|
|
|
|
extraGroups = ["wheel"];
|
|
|
|
isSystemUser = true;
|
|
|
|
createHome = true;
|
|
|
|
uid = cfg.uidgid;
|
|
|
|
openssh.authorizedKeys.keys = let
|
|
|
|
restrictedPubKey = "from=\"${builtins.concatStringsSep "," cfg.sshAllowSubnets}\" " + cfg.publicKey;
|
|
|
|
in [restrictedPubKey];
|
2023-07-30 05:49:54 +03:00
|
|
|
};
|
2023-09-23 22:25:58 +03:00
|
|
|
users.groups.deployerbot-follower.gid = cfg.uidgid;
|
|
|
|
nix.settings.trusted-users = ["deployerbot-follower"];
|
|
|
|
})
|
2023-07-30 05:49:54 +03:00
|
|
|
];
|
|
|
|
}
|