on-failure: set up beta version of on-failure

This commit is contained in:
Ingolf Wagner 2020-03-10 11:26:53 +08:00
parent d065e151b6
commit ded316768d
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
3 changed files with 82 additions and 0 deletions

View file

@ -18,6 +18,22 @@
system.custom.wifi.interfaces = [ "wlp3s0" ]; system.custom.wifi.interfaces = [ "wlp3s0" ];
on-failure = {
url = lib.fileContents <common_secrets/mattermost_sink_url>;
enable = true;
plans = {
tinc_private.name = "tinc.private";
tinc_retiolum.name = "tinc.retiolum";
sshd.name = "sshd";
tor.name = "tor";
dnsmasq.name = "dnsmasq";
backup_on_workhorse.name = "backup.on-workhorse.private";
backup_on_workout.name = "backup.on-workout.private";
backup_on_porani.name = "backup.on-porani.private";
syncthing.name = "syncthing";
};
};
security.wrappers = { security.wrappers = {
pmount.source = "${pkgs.pmount}/bin/pmount"; pmount.source = "${pkgs.pmount}/bin/pmount";
pumount.source = "${pkgs.pmount}/bin/pumount"; pumount.source = "${pkgs.pmount}/bin/pumount";

View file

@ -39,6 +39,7 @@
./system/permown.nix ./system/permown.nix
./system/wifi.nix ./system/wifi.nix
./system/x11.nix ./system/x11.nix
./system/on-failure.nix
]; ];
} }

View file

@ -0,0 +1,65 @@
{ 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 \
'payload={"text": "Service Failed ${plan.name}"}' \
${cfg.url}
'';
in {
options.on-failure = api;
config = lib.mkIf cfg.enable {
systemd.services = foldl (a: b: a // b) { } (map to-services enabled-plans);
};
}