aboutsummaryrefslogtreecommitdiff
path: root/infra/libkookie/nixpkgs/nixos/modules/services/databases/stanchion.nix
blob: 97e55bc70c470166d6a0e28ff584790c92c03ca2 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.stanchion;

in

{

  ###### interface

  options = {

    services.stanchion = {

      enable = mkEnableOption "stanchion";

      package = mkOption {
        type = types.package;
        default = pkgs.stanchion;
        defaultText = "pkgs.stanchion";
        example = literalExample "pkgs.stanchion";
        description = ''
          Stanchion package to use.
        '';
      };

      nodeName = mkOption {
        type = types.str;
        default = "stanchion@127.0.0.1";
        description = ''
          Name of the Erlang node.
        '';
      };

      adminKey = mkOption {
        type = types.str;
        default = "";
        description = ''
          Name of admin user.
        '';
      };

      adminSecret = mkOption {
        type = types.str;
        default = "";
        description = ''
          Name of admin secret
        '';
      };

      riakHost = mkOption {
        type = types.str;
        default = "127.0.0.1:8087";
        description = ''
          Name of riak hosting service.
        '';
      };

      listener = mkOption {
        type = types.str;
        default = "127.0.0.1:8085";
        description = ''
          Name of Riak CS listening service.
        '';
      };

      stanchionHost = mkOption {
        type = types.str;
        default = "127.0.0.1:8085";
        description = ''
          Name of stanchion hosting service.
        '';
      };

      distributedCookie = mkOption {
        type = types.str;
        default = "riak";
        description = ''
          Cookie for distributed node communication.  All nodes in the
          same cluster should use the same cookie or they will not be able to
          communicate.
        '';
      };

      dataDir = mkOption {
        type = types.path;
        default = "/var/db/stanchion";
        description = ''
          Data directory for Stanchion.
        '';
      };

      logDir = mkOption {
        type = types.path;
        default = "/var/log/stanchion";
        description = ''
          Log directory for Stanchion.
        '';
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = ''
          Additional text to be appended to <filename>stanchion.conf</filename>.
        '';
      };
    };
  };

  ###### implementation

  config = mkIf cfg.enable {

    environment.systemPackages = [ cfg.package ];

    environment.etc."stanchion/advanced.config".text = ''
      [{stanchion, []}].
    '';

    environment.etc."stanchion/stanchion.conf".text = ''
      listener = ${cfg.listener}

      riak_host = ${cfg.riakHost}

      ${optionalString (cfg.adminKey == "") "#"} admin.key=${optionalString (cfg.adminKey != "") cfg.adminKey}
      ${optionalString (cfg.adminSecret == "") "#"} admin.secret=${optionalString (cfg.adminSecret != "") cfg.adminSecret}

      platform_bin_dir = ${pkgs.stanchion}/bin
      platform_data_dir = ${cfg.dataDir}
      platform_etc_dir = /etc/stanchion
      platform_lib_dir = ${pkgs.stanchion}/lib
      platform_log_dir = ${cfg.logDir}

      nodename = ${cfg.nodeName}

      distributed_cookie = ${cfg.distributedCookie}

      ${cfg.extraConfig}
    '';

    users.users.stanchion = {
      name = "stanchion";
      uid = config.ids.uids.stanchion;
      group = "stanchion";
      description = "Stanchion server user";
    };

    users.groups.stanchion.gid = config.ids.gids.stanchion;

    systemd.tmpfiles.rules = [
      "d '${cfg.logDir}' - stanchion stanchion --"
      "d '${cfg.dataDir}' 0700 stanchion stanchion --"
    ];

    systemd.services.stanchion = {
      description = "Stanchion Server";

      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      path = [
        pkgs.utillinux # for `logger`
        pkgs.bash
      ];

      environment.HOME = "${cfg.dataDir}";
      environment.STANCHION_DATA_DIR = "${cfg.dataDir}";
      environment.STANCHION_LOG_DIR = "${cfg.logDir}";
      environment.STANCHION_ETC_DIR = "/etc/stanchion";

      serviceConfig = {
        ExecStart = "${cfg.package}/bin/stanchion console";
        ExecStop = "${cfg.package}/bin/stanchion stop";
        StandardInput = "tty";
        User = "stanchion";
        Group = "stanchion";
        # Give Stanchion a decent amount of time to clean up.
        TimeoutStopSec = 120;
        LimitNOFILE = 65536;
      };

      unitConfig.RequiresMountsFor = [
        "${cfg.dataDir}"
        "${cfg.logDir}"
        "/etc/stanchion"
      ];
    };
  };
}