Add fail trigger to backup healthchecks

This commit is contained in:
SebastianStork 2024-09-09 23:10:57 +02:00
parent 8ab39c79ab
commit b14db2b903

View file

@ -33,13 +33,13 @@ in
name: value: "d /var/cache/restic-backups-${name} 700 ${value.user} ${value.user} -" name: value: "d /var/cache/restic-backups-${name} 700 ${value.user} ${value.user} -"
) cfg; ) cfg;
users.groups.restic.members = lib.mapAttrsToList (_: value: value.user) cfg; users.groups.backup.members = lib.mapAttrsToList (_: value: value.user) cfg;
sops.secrets = sops.secrets =
let let
resticPermissions = { resticPermissions = {
mode = "440"; mode = "440";
group = config.users.groups.restic.name; group = config.users.groups.backup.name;
}; };
in in
{ {
@ -69,21 +69,28 @@ in
// value.extraConfig // value.extraConfig
) cfg; ) cfg;
systemd.services = lib.mapAttrs' ( systemd.services = lib.mkMerge [
name: _: (lib.mapAttrs' (
lib.nameValuePair "restic-backups-${name}" ( name: _:
let lib.nameValuePair "restic-backups-${name}" {
ping = signal: '' wants = [ "healthcheck-ping@${name}-backup_start.service" ];
onSuccess = [ "healthcheck-ping@${name}-backup.service" ];
onFailure = [ "healthcheck-ping@${name}-backup_fail.service" ];
}
) (lib.filterAttrs (_: value: value.healthchecks.enable) cfg))
(lib.mkIf ((lib.filterAttrs (_: value: value.healthchecks.enable) cfg) != { }) {
"healthcheck-ping@" = {
description = "Pings healthcheck (%i)";
serviceConfig.Type = "oneshot";
scriptArgs = "%i";
script = ''
${lib.getExe pkgs.curl} -fsS -m 10 --retry 5 https://hc-ping.com/$(cat ${ ${lib.getExe pkgs.curl} -fsS -m 10 --retry 5 https://hc-ping.com/$(cat ${
config.sops.secrets."healthchecks-ping-key".path config.sops.secrets."healthchecks-ping-key".path
})/${name}-backup/${signal} })/$(echo $1 | tr _ /)
''; '';
in };
{ })
preStart = lib.mkBefore (ping "start"); ];
postStop = lib.mkAfter (ping "0");
}
)
) (lib.filterAttrs (_: value: value.healthchecks.enable) cfg);
}; };
} }