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:
|
|
|
|
pkgs.writeDash "on-failure.${plan.name}" ''
|
|
|
|
${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}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
options.on-failure = api;
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
systemd.services = foldl (a: b: a // b) { } (map to-services enabled-plans);
|
|
|
|
};
|
|
|
|
}
|