nixos-config/modules/system/on-failure.nix

66 lines
1.6 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.on-failure;
api = {
enable = mkEnableOption "krebs.on-failure" // {
default = cfg.plans != { };
};
url = mkOption {
type = types.str;
description = "url on where to send the message to";
};
plans = mkOption {
default = { };
type = with types;
attrsOf (submodule ({ config, ... }: {
options = {
enable = mkEnableOption "on-failure.${config.name}" // {
default = true;
};
name = mkOption {
type = types.str;
default = config._module.args.name;
description = "Name of the to-be-monitored service.";
};
};
}));
};
};
enabled-plans = filter (getAttr "enable") (attrValues cfg.plans);
to-services = plan: {
"${plan.name}".unitConfig.OnFailure = "on-failure.${plan.name}.service";
"on-failure.${plan.name}".serviceConfig = rec {
ExecStart = mattermostStart plan;
SyslogIdentifier = ExecStart.name;
Type = "oneshot";
};
};
# todo this output must be better
mattermostStart = plan:
pkgs.writeDash "on-failure.${plan.name}" ''
${pkgs.curl}/bin/curl \
--include \
--request POST \
--data-urlencode \
2020-03-11 03:57:21 +01:00
'payload={"text": "Service Failed ${plan.name} on ${config.networking.hostName}"}' \
${cfg.url}
'';
in {
options.on-failure = api;
config = lib.mkIf cfg.enable {
systemd.services = foldl (a: b: a // b) { } (map to-services enabled-plans);
};
}