borgbackup-static: only print warning if it is actually used

This commit is contained in:
Jörg Thalheim
2024-09-15 17:26:14 +02:00
parent 9ca86f07ec
commit bb8ffc46ae

View File

@@ -3,101 +3,102 @@ let
clanDir = config.clan.core.clanDir; clanDir = config.clan.core.clanDir;
machineDir = clanDir + "/machines/"; machineDir = clanDir + "/machines/";
in in
lib.warn {
"The borgbackup-static module is deprecated use the service via the inventory interface instead." imports = [ ../borgbackup ];
{
imports = [ ../borgbackup ];
options.clan.borgbackup-static = { options.clan.borgbackup-static = {
excludeMachines = lib.mkOption { excludeMachines = lib.mkOption {
type = lib.types.listOf lib.types.str; type = lib.types.listOf lib.types.str;
example = [ config.clan.core.machineName ]; example = [ config.clan.core.machineName ];
default = [ ]; default = [ ];
description = '' description = ''
Machines that should not be backuped. Machines that should not be backuped.
Mutually exclusive with includeMachines. Mutually exclusive with includeMachines.
If this is not empty, every other machine except the targets in the clan will be backuped by this module. If this is not empty, every other machine except the targets in the clan will be backuped by this module.
If includeMachines is set, only the included machines will be backuped. If includeMachines is set, only the included machines will be backuped.
''; '';
};
includeMachines = lib.mkOption {
type = lib.types.listOf lib.types.str;
example = [ config.clan.core.machineName ];
default = [ ];
description = ''
Machines that should be backuped.
Mutually exclusive with excludeMachines.
'';
};
targets = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = ''
Machines that should act as target machines for backups.
'';
};
}; };
includeMachines = lib.mkOption {
type = lib.types.listOf lib.types.str;
example = [ config.clan.core.machineName ];
default = [ ];
description = ''
Machines that should be backuped.
Mutually exclusive with excludeMachines.
'';
};
targets = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = ''
Machines that should act as target machines for backups.
'';
};
};
config.services.borgbackup.repos = config.services.borgbackup.repos =
let let
machines = builtins.readDir machineDir; machines = builtins.readDir machineDir;
borgbackupIpMachinePath = machines: machineDir + machines + "/facts/borgbackup.ssh.pub"; borgbackupIpMachinePath = machines: machineDir + machines + "/facts/borgbackup.ssh.pub";
filteredMachines = filteredMachines =
if ((builtins.length config.clan.borgbackup-static.includeMachines) != 0) then if ((builtins.length config.clan.borgbackup-static.includeMachines) != 0) then
lib.filterAttrs (name: _: (lib.elem name config.clan.borgbackup-static.includeMachines)) machines lib.filterAttrs (name: _: (lib.elem name config.clan.borgbackup-static.includeMachines)) machines
else else
lib.filterAttrs (name: _: !(lib.elem name config.clan.borgbackup-static.excludeMachines)) machines; lib.filterAttrs (name: _: !(lib.elem name config.clan.borgbackup-static.excludeMachines)) machines;
machinesMaybeKey = lib.mapAttrsToList ( machinesMaybeKey = lib.mapAttrsToList (
machine: _: machine: _:
let let
fullPath = borgbackupIpMachinePath machine; fullPath = borgbackupIpMachinePath machine;
in in
if builtins.pathExists fullPath then machine else null if builtins.pathExists fullPath then machine else null
) filteredMachines; ) filteredMachines;
machinesWithKey = lib.filter (x: x != null) machinesMaybeKey; machinesWithKey = lib.filter (x: x != null) machinesMaybeKey;
hosts = builtins.map (machine: { hosts = builtins.map (machine: {
name = machine; name = machine;
value = { value = {
path = "/var/lib/borgbackup/${machine}"; path = "/var/lib/borgbackup/${machine}";
authorizedKeys = [ (builtins.readFile (borgbackupIpMachinePath machine)) ]; authorizedKeys = [ (builtins.readFile (borgbackupIpMachinePath machine)) ];
}; };
}) machinesWithKey; }) machinesWithKey;
in in
lib.mkIf lib.mkIf
(builtins.any ( (builtins.any (
target: target == config.clan.core.machineName
) config.clan.borgbackup-static.targets)
(if (builtins.listToAttrs hosts) != null then builtins.listToAttrs hosts else { });
config.clan.borgbackup.destinations =
let
destinations = builtins.map (d: {
name = d;
value = {
repo = "borg@${d}:/var/lib/borgbackup/${config.clan.core.machineName}";
};
}) config.clan.borgbackup-static.targets;
in
lib.mkIf (builtins.any (
target: target == config.clan.core.machineName target: target == config.clan.core.machineName
) config.clan.borgbackup-static.includeMachines) (builtins.listToAttrs destinations); ) config.clan.borgbackup-static.targets)
(if (builtins.listToAttrs hosts) != null then builtins.listToAttrs hosts else { });
config.assertions = [ config.clan.borgbackup.destinations =
{ let
assertion = destinations = builtins.map (d: {
!( name = d;
((builtins.length config.clan.borgbackup-static.excludeMachines) != 0) value = {
&& ((builtins.length config.clan.borgbackup-static.includeMachines) != 0) repo = "borg@${d}:/var/lib/borgbackup/${config.clan.core.machineName}";
); };
message = '' }) config.clan.borgbackup-static.targets;
The options: in
config.clan.borgbackup-static.excludeMachines = [${builtins.toString config.clan.borgbackup-static.excludeMachines}] lib.mkIf (builtins.any (
and target: target == config.clan.core.machineName
config.clan.borgbackup-static.includeMachines = [${builtins.toString config.clan.borgbackup-static.includeMachines}] ) config.clan.borgbackup-static.includeMachines) (builtins.listToAttrs destinations);
are mutually exclusive.
Use excludeMachines to exclude certain machines and backup the other clan machines. config.assertions = [
Use include machines to only backup certain machines. {
''; assertion =
} !(
]; ((builtins.length config.clan.borgbackup-static.excludeMachines) != 0)
} && ((builtins.length config.clan.borgbackup-static.includeMachines) != 0)
);
message = ''
The options:
config.clan.borgbackup-static.excludeMachines = [${builtins.toString config.clan.borgbackup-static.excludeMachines}]
and
config.clan.borgbackup-static.includeMachines = [${builtins.toString config.clan.borgbackup-static.includeMachines}]
are mutually exclusive.
Use excludeMachines to exclude certain machines and backup the other clan machines.
Use include machines to only backup certain machines.
'';
}
];
config.warnings = lib.optional (
builtins.length config.clan.borgbackup-static.targets > 0
) "The borgbackup-static module is deprecated use the service via the inventory interface instead.";
}