65 lines
1.6 KiB
Nix
65 lines
1.6 KiB
Nix
{ 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.writers.writeDash "on-failure.${plan.name}" ''
|
|
${pkgs.curl}/bin/curl \
|
|
--include \
|
|
--request POST \
|
|
--data-urlencode \
|
|
'payload={"text": "<!channel> :fire: 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);
|
|
};
|
|
}
|