aboutsummaryrefslogtreecommitdiff
path: root/modules/apps/cassandra/default.nix
blob: 953acb352edd6b155e01b4a126fe0d2856b1538b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{ config, lib, pkgs, ... }:

let

  inherit (builtins)
    match ;

  inherit (lib)
    concatMapStringsSep concatStringsSep filterAttrs flatten foldAttrs foldl
    mapAttrsToList mkOption ;

  inherit (lib.types)
    attrsOf submodule ;

  explicit = filterAttrs (n: v: n != "_module" && v != null);
  concatMapAttrsSep = s: f: attrs: concatStringsSep s (mapAttrsToList f attrs);

  instances = explicit config.nixsap.apps.cassandra;
  users = mapAttrsToList (_: i: i.user) instances;

  keyrings =
    let
      ik = mapAttrsToList (n: i: { "${i.user}" = []; } ) instances;
    in foldAttrs (l: r: l ++ r) [] ik;

  mkService = name: cfg:
    let

      tmpdir = "${cfg.home}/tmp";
      cp = concatStringsSep ":" cfg.classpath;

      isDir = d: _: match ".*_director(y|ies)$" d != null;
      directories = [ cfg.home ]
        ++ flatten (mapAttrsToList (_: d: d) (filterAttrs isDir (explicit cfg.parameters)));

      directories_sh = concatMapStringsSep " " (d: "'${d}'") directories;


      start = pkgs.writeBashScriptBin "cassandra-${name}" ''
        set -euo pipefail
        umask 0027
        export HOME='${cfg.home}'

        rm   -rf -- '${cfg.jre.properties.java.io.tmpdir}'
        mkdir -p -- '${cfg.jre.properties.java.io.tmpdir}'

        exec ${cfg.jre.package}/bin/java \
        -Dcassandra.config='${cfg.jre.properties.cassandra.config}' \
        -Djava.io.tmpdir='${cfg.jre.properties.java.io.tmpdir}' \
        -Djava.library.path='${concatStringsSep ":" cfg.jre.properties.java.library.path}' \
        -cp '${cp}' \
        org.apache.cassandra.service.CassandraDaemon

      '';

    in {
      "cassandra-${name}" = {
        description = "Cassandra (${name}) distributed NoSQL database";
        wantedBy = [ "multi-user.target" ];
        after = [ "keys.target" "network.target" "local-fs.target" ];
        preStart = ''
          mkdir -p -- ${directories_sh}

          find ${directories_sh} -not -type l \( \
              -not -user '${cfg.user}' \
          -or -not -group '${cfg.user}' \
          -or \( -type d -not -perm -u=wrx,g=rx \) \
          -or \( -type f -not -perm -u=rw,g=r \) \
          \) \
          -exec chown -c -- '${cfg.user}:${cfg.user}' {} + \
          -exec chmod -c -- u=rwX,g=rX,o= {} +

        '';

        unitConfig = {
          # XXX It can be running long before fail:
          StartLimitBurst = 3;
          StartLimitIntervalSec = 60;
        };

        serviceConfig = {
          ExecStart = "${start}/bin/cassandra-${name}";
          KillMode = "mixed";
          PermissionsStartOnly = true;
          Restart = "always";
          TimeoutSec = 0;
          User = cfg.user;
          LimitAS = "infinity";
          LimitMEMLOCK = "infinity";
          LimitNOFILE = 10000;
          LimitNPROC = 32768;
        };
      };
    };

in {

  options.nixsap.apps.cassandra = mkOption {
    description = "Cassandra instances";
    default = {};
    type = attrsOf (submodule (import ./instance.nix pkgs));
  };

  config = {
    systemd.services = foldl (a: b: a//b) {} (mapAttrsToList mkService instances);
    nixsap.deployment.keyrings = keyrings;
    nixsap.system.users.daemons = users;
  };

}