summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorJaka Hudoklin <jakahudoklin@gmail.com>2014-03-10 10:57:51 +0100
committerJaka Hudoklin <jakahudoklin@gmail.com>2014-03-10 10:59:26 +0100
commitb21d95e1f8d455c06ecc9bbf5d618a64f3e4935f (patch)
treedde366112be9a283de226fe564430824e625e08a /nixos
parent086afe9161b0763075f8f4e4793c5490648f0ec9 (diff)
downloadnixpkgs-b21d95e1f8d455c06ecc9bbf5d618a64f3e4935f.tar
nixpkgs-b21d95e1f8d455c06ecc9bbf5d618a64f3e4935f.tar.gz
nixpkgs-b21d95e1f8d455c06ecc9bbf5d618a64f3e4935f.tar.bz2
nixpkgs-b21d95e1f8d455c06ecc9bbf5d618a64f3e4935f.tar.lz
nixpkgs-b21d95e1f8d455c06ecc9bbf5d618a64f3e4935f.tar.xz
nixpkgs-b21d95e1f8d455c06ecc9bbf5d618a64f3e4935f.tar.zst
nixpkgs-b21d95e1f8d455c06ecc9bbf5d618a64f3e4935f.zip
nixos/graphite: Make pure, fix several bugs, add dataDir option
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/services/monitoring/graphite.nix94
1 files changed, 57 insertions, 37 deletions
diff --git a/nixos/modules/services/monitoring/graphite.nix b/nixos/modules/services/monitoring/graphite.nix
index be57b8c5c03..3e2e7e9df6e 100644
--- a/nixos/modules/services/monitoring/graphite.nix
+++ b/nixos/modules/services/monitoring/graphite.nix
@@ -4,17 +4,31 @@ with pkgs.lib;
 
 let
   cfg = config.services.graphite;
-  writeTextOrNull = f: t: if t == null then null else pkgs.writeText f t;
+  writeTextOrNull = f: t: if t == null then null else pkgs.writeTextDir f t;
+
+  dataDir = cfg.dataDir;
+
+  configDir = pkgs.buildEnv {
+    name = "graphite-config";
+    paths = lists.filter (el: el != null) [
+      (writeTextOrNull "carbon.conf" cfg.carbon.config)
+      (writeTextOrNull "storage-agregation.conf" cfg.carbon.storageAggregation)
+      (writeTextOrNull "storage-schemas.conf" cfg.carbon.storageSchemas)
+      (writeTextOrNull "blacklist.conf" cfg.carbon.blacklist)
+      (writeTextOrNull "whitelist.conf" cfg.carbon.whitelist)
+      (writeTextOrNull "rewrite-rules.conf" cfg.carbon.rewriteRules)
+      (writeTextOrNull "relay-rules.conf" cfg.carbon.relayRules)
+      (writeTextOrNull "aggregation-rules.conf" cfg.carbon.aggregationRules)
+    ];
+  };
 
-  dataDir = "/var/db/graphite";
   carbonOpts = name: with config.ids; ''
-    --nodaemon --syslog --prefix=${name} --pidfile /var/run/${name}.pid \
-    --uid ${toString uids.graphite} --gid ${toString uids.graphite} ${name}
+    --nodaemon --syslog --prefix=${name} --pidfile ${dataDir}/${name}.pid ${name}
   '';
   carbonEnv = {
     PYTHONPATH = "${pkgs.python27Packages.carbon}/lib/python2.7/site-packages";
     GRAPHITE_ROOT = dataDir;
-    GRAPHITE_CONF_DIR = "/etc/graphite/";
+    GRAPHITE_CONF_DIR = configDir;
     GRAPHITE_STORAGE_DIR = dataDir;
   };
 
@@ -23,6 +37,14 @@ in {
   ###### interface
 
   options.services.graphite = {
+    dataDir = mkOption {
+      type = types.path;
+      default = "/var/db/graphite";
+      description = ''
+        Data directory for graphite.
+      '';
+    };
+
     web = {
       enable = mkOption {
         description = "Whether to enable graphite web frontend";
@@ -38,8 +60,8 @@ in {
 
       port = mkOption {
         description = "Graphite web frontend port";
-        default = "8080";
-        type = types.str;
+        default = 8080;
+        type = types.int;
       };
     };
 
@@ -152,31 +174,17 @@ in {
   ###### implementation
 
   config = mkIf (cfg.carbon.enableAggregator || cfg.carbon.enableCache || cfg.carbon.enableRelay || cfg.web.enable) {
-    environment.etc = lists.filter (el: el.source != null) [
-      { source = writeTextOrNull "carbon.conf" cfg.carbon.config;
-        target = "graphite/carbon.conf"; }
-      { source = writeTextOrNull "storage-agregation.conf" cfg.carbon.storageAggregation;
-        target = "graphite/storage-agregation.conf"; }
-      { source = writeTextOrNull "storage-schemas.conf" cfg.carbon.storageSchemas;
-        target = "graphite/storage-schemas.conf"; }
-      { source = writeTextOrNull "blacklist.conf" cfg.carbon.blacklist;
-        target = "graphite/blacklist.conf"; }
-      { source = writeTextOrNull "whitelist.conf" cfg.carbon.whitelist;
-        target = "graphite/whitelist.conf"; }
-      { source = writeTextOrNull "rewrite-rules.conf" cfg.carbon.rewriteRules;
-        target = "graphite/rewrite-rules.conf"; }
-      { source = writeTextOrNull "relay-rules.conf" cfg.carbon.relayRules;
-        target = "graphite/relay-rules.conf"; }
-      { source = writeTextOrNull "aggregation-rules.conf" cfg.carbon.aggregationRules;
-        target = "graphite/aggregation-rules.conf"; }
-    ];
-
-    systemd.services.carbonCache = mkIf cfg.carbon.enableCache {
+    systemd.services.carbonCache = {
+      enable = cfg.carbon.enableCache;
       description = "Graphite Data Storage Backend";
       wantedBy = [ "multi-user.target" ];
       after = [ "network-interfaces.target" ];
       environment = carbonEnv;
-      serviceConfig.ExecStart = "${pkgs.twisted}/bin/twistd ${carbonOpts "carbon-cache"}";
+      serviceConfig = {
+        ExecStart = "${pkgs.twisted}/bin/twistd ${carbonOpts "carbon-cache"}";
+        User = "graphite";
+        Group = "graphite";
+      };
       restartTriggers = [
         pkgs.pythonPackages.carbon
         cfg.carbon.config
@@ -185,33 +193,45 @@ in {
         cfg.carbon.rewriteRules
       ];
       preStart = ''
-        mkdir -p ${dataDir}/whisper
+        mkdir -m 0700 -p ${cfg.dataDir}/whisper
+        if [ "$(id -u)" = 0 ]; then chown -R graphite:graphite ${cfg.dataDir}; fi
       '';
     };
 
-    systemd.services.carbonAggregator = mkIf cfg.carbon.enableAggregator {
+    systemd.services.carbonAggregator = {
+      enable = cfg.carbon.enableAggregator;
       description = "Carbon Data Aggregator";
       wantedBy = [ "multi-user.target" ];
       after = [ "network-interfaces.target" ];
       environment = carbonEnv;
-      serviceConfig.ExecStart = "${pkgs.twisted}/bin/twistd ${carbonOpts "carbon-aggregator"}";
+      serviceConfig = {
+        ExecStart = "${pkgs.twisted}/bin/twistd ${carbonOpts "carbon-aggregator"}";
+        User = "graphite";
+        Group = "graphite";
+      };
       restartTriggers = [
         pkgs.pythonPackages.carbon cfg.carbon.config cfg.carbon.aggregationRules
       ];
     };
 
-    systemd.services.carbonRelay = mkIf cfg.carbon.enableRelay {
+    systemd.services.carbonRelay = {
+      enable = cfg.carbon.enableRelay;
       description = "Carbon Data Relay";
       wantedBy = [ "multi-user.target" ];
       after = [ "network-interfaces.target" ];
       environment = carbonEnv;
-      serviceConfig.ExecStart = "${pkgs.twisted}/bin/twistd ${carbonOpts "carbon-relay"}";
+      serviceConfig = {
+        ExecStart = "${pkgs.twisted}/bin/twistd ${carbonOpts "carbon-relay"}";
+        User = "graphite";
+        Group = "graphite";
+      };
       restartTriggers = [
         pkgs.pythonPackages.carbon cfg.carbon.config cfg.carbon.relayRules
       ];
     };
 
-    systemd.services.graphiteWeb = mkIf cfg.web.enable {
+    systemd.services.graphiteWeb = {
+      enable = cfg.web.enable;
       description = "Graphite Web Interface";
       wantedBy = [ "multi-user.target" ];
       after = [ "network-interfaces.target" ];
@@ -224,14 +244,15 @@ in {
       serviceConfig = {
         ExecStart = ''
           ${pkgs.python27Packages.waitress}/bin/waitress-serve \
-          --host=${cfg.web.host} --port=${cfg.web.port} \
+          --host=${cfg.web.host} --port=${toString cfg.web.port} \
           --call django.core.handlers.wsgi:WSGIHandler'';
         User = "graphite";
         Group = "graphite";
       };
       preStart = ''
         if ! test -e ${dataDir}/db-created; then
-          mkdir -p ${dataDir}/{whisper/,log/webapp/}
+          mkdir -m 0700 -p ${dataDir}/{whisper/,log/webapp/}
+          if [ "$(id -u)" = 0 ]; then chown -R graphite:graphite ${cfg.dataDir}; fi
 
           # populate database
           ${pkgs.python27Packages.graphite_web}/bin/manage-graphite.py syncdb --noinput
@@ -259,7 +280,6 @@ in {
       uid = config.ids.uids.graphite;
       description = "Graphite daemon user";
       home = dataDir;
-      createHome = true;
     };
     users.extraGroups.graphite.gid = config.ids.gids.graphite;
   };