2020-03-10 04:26:53 +01:00
|
|
|
{ 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:
|
2021-09-25 15:17:07 +02:00
|
|
|
pkgs.writers.writeDash "on-failure.${plan.name}" ''
|
2020-03-10 04:26:53 +01:00
|
|
|
${pkgs.curl}/bin/curl \
|
|
|
|
--include \
|
|
|
|
--request POST \
|
|
|
|
--data-urlencode \
|
2020-03-17 08:00:57 +01:00
|
|
|
'payload={"text": "<!channel> :fire: Service Failed ${plan.name} on ${config.networking.hostName}"}' \
|
2020-03-10 04:26:53 +01:00
|
|
|
${cfg.url}
|
|
|
|
'';
|
|
|
|
|
2021-11-01 09:20:42 +01:00
|
|
|
in
|
|
|
|
{
|
2020-03-10 04:26:53 +01:00
|
|
|
|
|
|
|
options.on-failure = api;
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
systemd.services = foldl (a: b: a // b) { } (map to-services enabled-plans);
|
|
|
|
};
|
|
|
|
}
|