Make it pass a minimal test

This commit is contained in:
Alberto Berti 2019-02-21 16:32:46 +01:00 committed by Jean-Baptiste Giraudeau
parent 11b89720b7
commit 1b6ce80c2b
No known key found for this signature in database
GPG Key ID: E96EF57FD501B961
3 changed files with 69 additions and 37 deletions

View File

@ -137,44 +137,40 @@ let
''; '';
}; };
}; };
mkAMConfig = amCfg: amVersion: mkAMConfig = amCfg: amVersion: [
config = mkMerge [ (mkIf amCfg.enable {
(mkIf amCfg.enable { assertions = singleton {
assertions = singleton { assertion = amCfg.configuration != null || amCfg.configText != null;
assertion = amCfg.configuration != null || amCfg.configText != null; message = "Can not enable alertmanager without a configuration. "
message = "Can not enable alertmanager without a configuration. " + "Set either the `configuration` or `configText` attribute.";
+ "Set either the `configuration` or `configText` attribute."; };
}; })
}) (mkIf amCfg.enable {
(mkIf amCfg.enable { networking.firewall.allowedTCPPorts = optional amCfg.openFirewall amCfg.port;
networking.firewall.allowedTCPPorts = optional amCfg.openFirewall amCfg.port;
systemd.services."alertmanager${amVersion}" = { systemd.services."alertmanager${amVersion}" = {
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
after = [ "network.target" ]; after = [ "network.target" ];
script = '' script = ''
${amCfg.package}/bin/alertmanager \ ${amCfg.package}/bin/alertmanager \
${concatStringsSep " \\\n " cmdlineArgs} ${concatStringsSep " \\\n " cmdlineArgs}
''; '';
serviceConfig = { serviceConfig = {
User = amCfg.user; User = amCfg.user;
Group = amCfg.group; Group = amCfg.group;
Restart = "always"; Restart = "always";
PrivateTmp = true; PrivateTmp = true;
WorkingDirectory = "/tmp"; WorkingDirectory = "/tmp";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
};
}; };
}) };
]; })
];
in { in {
options = { options = {
services.prometheus.alertmanager = amOptions; services.prometheus.alertmanager = amOptions;
services.prometheus2.alertmanager = amOptions; services.prometheus2.alertmanager = amOptions;
}; };
config = mkMerge [ config = mkMerge ((mkAMConfig cfg "") ++ (mkAMConfig cfg2 "2"));
(mkAMConfig cfg "")
(mkAMConfig cfg2 "2")
];
} }

View File

@ -22,8 +22,10 @@ let
# a wrapper that verifies that the configuration is valid for # a wrapper that verifies that the configuration is valid for
# prometheus 2 # prometheus 2
prom2toolCheck = what: name: file: pkgs.runCommand "${name}-${what}-checked" prom2toolCheck = what: name: file:
{ buildInputs = [ cfg2.package ]; } '' pkgs.runCommand
"${name}-${replaceStrings [" "] [""] what}-checked"
{ buildInputs = [ cfg2.package ]; } ''
ln -s ${file} $out ln -s ${file} $out
promtool ${what} $out promtool ${what} $out
''; '';
@ -64,7 +66,7 @@ let
# This becomes the main config file for Prometheus 2 # This becomes the main config file for Prometheus 2
promConfig2 = { promConfig2 = {
global = cfg2.globalConfig; global = cfg2.globalConfig;
rule_files = map (prom2toolCheck "check-rules" "rules") (cfg2.ruleFiles ++ [ rule_files = map (prom2toolCheck "check rules" "rules") (cfg2.ruleFiles ++ [
(pkgs.writeText "prometheus.rules" (concatStringsSep "\n" cfg2.rules)) (pkgs.writeText "prometheus.rules" (concatStringsSep "\n" cfg2.rules))
]); ]);
scrape_configs = cfg2.scrapeConfigs; scrape_configs = cfg2.scrapeConfigs;
@ -83,7 +85,7 @@ let
yml = if cfg2.configText != null then yml = if cfg2.configText != null then
pkgs.writeText "prometheus.yml" cfg2.configText pkgs.writeText "prometheus.yml" cfg2.configText
else generatedPrometheus2Yml; else generatedPrometheus2Yml;
in promtoo2lCheck "check-config" "prometheus.yml" yml; in prom2toolCheck "check config" "prometheus.yml" yml;
cmdlineArgs2 = cfg2.extraFlags ++ [ cmdlineArgs2 = cfg2.extraFlags ++ [
"--storage.tsdb.path=${cfg2.dataDir}/data/" "--storage.tsdb.path=${cfg2.dataDir}/data/"
@ -704,7 +706,7 @@ in {
after = [ "network.target" ]; after = [ "network.target" ];
script = '' script = ''
#!/bin/sh #!/bin/sh
exec ${cfg.package}/bin/prometheus \ exec ${cfg2.package}/bin/prometheus \
${concatStringsSep " \\\n " cmdlineArgs2} ${concatStringsSep " \\\n " cmdlineArgs2}
''; '';
serviceConfig = { serviceConfig = {

View File

@ -0,0 +1,34 @@
import ./make-test.nix {
name = "prometheus-2";
nodes = {
one = { pkgs, ... }: {
services.prometheus2 = {
enable = true;
scrapeConfigs = [{
job_name = "prometheus";
static_configs = [{
targets = [ "127.0.0.1:9090" ];
labels = { instance = "localhost"; };
}];
}];
rules = [
''
groups:
- name: test
rules:
- record: testrule
expr: count(up{job="prometheus"})
''
];
};
};
};
testScript = ''
startAll;
$one->waitForUnit("prometheus2.service");
$one->waitForOpenPort(9090);
$one->succeed("curl -s http://127.0.0.1:9090/metrics");
'';
}