2024-03-02 22:05:30 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}: let
|
2023-09-03 18:01:40 +02:00
|
|
|
cfg = config.services.influxdb2.provision;
|
|
|
|
|
2024-03-02 22:05:30 +01:00
|
|
|
inherit
|
|
|
|
(lib)
|
2023-09-03 18:01:40 +02:00
|
|
|
mkEnableOption
|
|
|
|
mkOption
|
|
|
|
types
|
|
|
|
mdDoc
|
|
|
|
flip
|
|
|
|
mapAttrsToList
|
|
|
|
getExe
|
2024-03-02 22:05:30 +01:00
|
|
|
mkIf
|
|
|
|
;
|
2023-09-03 18:01:40 +02:00
|
|
|
|
2024-03-02 22:05:30 +01:00
|
|
|
taskOptions = {...}: {
|
|
|
|
options = {
|
|
|
|
cron = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
description =
|
|
|
|
mdDoc ''
|
2023-09-03 18:01:40 +02:00
|
|
|
'';
|
2024-03-02 22:05:30 +01:00
|
|
|
};
|
2023-09-03 18:01:40 +02:00
|
|
|
|
2024-03-02 22:05:30 +01:00
|
|
|
every = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
description =
|
|
|
|
mdDoc ''
|
2023-09-03 18:01:40 +02:00
|
|
|
'';
|
2024-03-02 22:05:30 +01:00
|
|
|
};
|
2023-09-03 18:01:40 +02:00
|
|
|
|
2024-03-02 22:05:30 +01:00
|
|
|
fluxFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description =
|
|
|
|
mdDoc ''
|
2023-09-03 18:01:40 +02:00
|
|
|
'';
|
2024-03-02 22:05:30 +01:00
|
|
|
};
|
2023-09-03 18:01:40 +02:00
|
|
|
|
2024-03-02 22:05:30 +01:00
|
|
|
offset = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "0m";
|
|
|
|
description =
|
|
|
|
mdDoc ''
|
2023-09-03 18:01:40 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2024-03-02 22:05:30 +01:00
|
|
|
};
|
2023-09-03 18:01:40 +02:00
|
|
|
|
|
|
|
tasksFile =
|
|
|
|
(pkgs.formats.json {}).generate "tasks.json"
|
2024-03-02 22:05:30 +01:00
|
|
|
(flip mapAttrsToList cfg.tasks (
|
|
|
|
name: value: {
|
|
|
|
inherit name;
|
|
|
|
flux_file = value.fluxFile;
|
|
|
|
inherit
|
|
|
|
(value)
|
|
|
|
every
|
|
|
|
cron
|
|
|
|
offset
|
|
|
|
;
|
|
|
|
}
|
|
|
|
));
|
|
|
|
in {
|
2023-09-03 18:01:40 +02:00
|
|
|
options = {
|
2023-10-07 15:45:26 +02:00
|
|
|
services.influxdb2.provision-magic = {
|
2023-09-03 18:01:40 +02:00
|
|
|
enable = mkEnableOption "Enable InfluxDB2 provisioning";
|
|
|
|
|
|
|
|
itpPackage = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.itp;
|
2024-03-02 22:05:30 +01:00
|
|
|
description =
|
|
|
|
mdDoc ''
|
|
|
|
'';
|
2023-09-03 18:01:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
stateFile = mkOption {
|
|
|
|
type = types.str;
|
2024-03-02 22:05:30 +01:00
|
|
|
description =
|
|
|
|
mdDoc ''
|
|
|
|
'';
|
2023-09-03 18:01:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
organization = mkOption {
|
|
|
|
type = types.str;
|
2024-03-02 22:05:30 +01:00
|
|
|
description =
|
|
|
|
mdDoc ''
|
|
|
|
'';
|
2023-09-03 18:01:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
tasks = mkOption {
|
|
|
|
type = with types; attrsOf (submodule taskOptions);
|
|
|
|
default = {};
|
2024-03-02 22:05:30 +01:00
|
|
|
description =
|
|
|
|
mdDoc ''
|
|
|
|
'';
|
2023-09-03 18:01:40 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.influxdb2-provision = {
|
2024-03-02 22:05:30 +01:00
|
|
|
after = ["influxdb2.service"];
|
|
|
|
wants = ["influxdb2.service"];
|
|
|
|
wantedBy = ["multi-user.target"];
|
2023-09-03 18:01:40 +02:00
|
|
|
|
|
|
|
restartIfChanged = true;
|
|
|
|
|
|
|
|
script = ''
|
|
|
|
${getExe cfg.itpPackage} -s ${cfg.stateFile} -f ${tasksFile} -o ${cfg.organization}
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = 3;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-03-02 22:05:30 +01:00
|
|
|
assertions =
|
|
|
|
flip mapAttrsToList cfg.tasks
|
2023-09-03 18:01:40 +02:00
|
|
|
(n: v: {
|
|
|
|
assertion = (v.cron != null && v.every == null) || (v.cron == null && v.every != null);
|
|
|
|
message = "Exactly one of `services.influxdb2.provision.tasks.${n}.{cron, every}` must be non `null`";
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|