summary refs log tree commit diff
path: root/nixos/modules/services/misc/exhibitor.nix
blob: 4c935efbd844024ca03dd196a91ed8a4a1afb22f (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
{ config, lib, options, pkgs, ... }:

with lib;

let
  cfg = config.services.exhibitor;
  opt = options.services.exhibitor;
  exhibitorConfig = ''
    zookeeper-install-directory=${cfg.baseDir}/zookeeper
    zookeeper-data-directory=${cfg.zkDataDir}
    zookeeper-log-directory=${cfg.zkLogDir}
    zoo-cfg-extra=${cfg.zkExtraCfg}
    client-port=${toString cfg.zkClientPort}
    connect-port=${toString cfg.zkConnectPort}
    election-port=${toString cfg.zkElectionPort}
    cleanup-period-ms=${toString cfg.zkCleanupPeriod}
    servers-spec=${concatStringsSep "," cfg.zkServersSpec}
    auto-manage-instances=${toString cfg.autoManageInstances}
    ${cfg.extraConf}
  '';
  # NB: toString rather than lib.boolToString on cfg.autoManageInstances is intended.
  # Exhibitor tests if it's an integer not equal to 0, so the empty string (toString false)
  # will operate in the same fashion as a 0.
  configDir = pkgs.writeTextDir "exhibitor.properties" exhibitorConfig;
  cliOptionsCommon = {
    configtype = cfg.configType;
    defaultconfig = "${configDir}/exhibitor.properties";
    port = toString cfg.port;
    hostname = cfg.hostname;
    headingtext = if (cfg.headingText != null) then (lib.escapeShellArg cfg.headingText) else null;
    nodemodification = lib.boolToString cfg.nodeModification;
    configcheckms = toString cfg.configCheckMs;
    jquerystyle = cfg.jqueryStyle;
    loglines = toString cfg.logLines;
    servo = lib.boolToString cfg.servo;
    timeout = toString cfg.timeout;
  };
  s3CommonOptions = { s3region = cfg.s3Region; s3credentials = cfg.s3Credentials; };
  cliOptionsPerConfig = {
    s3 = {
      s3config = "${cfg.s3Config.bucketName}:${cfg.s3Config.objectKey}";
      s3configprefix = cfg.s3Config.configPrefix;
    };
    zookeeper = {
      zkconfigconnect = concatStringsSep "," cfg.zkConfigConnect;
      zkconfigexhibitorpath = cfg.zkConfigExhibitorPath;
      zkconfigpollms = toString cfg.zkConfigPollMs;
      zkconfigretry = "${toString cfg.zkConfigRetry.sleepMs}:${toString cfg.zkConfigRetry.retryQuantity}";
      zkconfigzpath = cfg.zkConfigZPath;
      zkconfigexhibitorport = toString cfg.zkConfigExhibitorPort; # NB: This might be null
    };
    file = {
      fsconfigdir = cfg.fsConfigDir;
      fsconfiglockprefix = cfg.fsConfigLockPrefix;
      fsConfigName = fsConfigName;
    };
    none = {
      noneconfigdir = configDir;
    };
  };
  cliOptions = concatStringsSep " " (mapAttrsToList (k: v: "--${k} ${v}") (filterAttrs (k: v: v != null && v != "") (cliOptionsCommon //
               cliOptionsPerConfig.${cfg.configType} //
               s3CommonOptions //
               optionalAttrs cfg.s3Backup { s3backup = "true"; } //
               optionalAttrs cfg.fileSystemBackup { filesystembackup = "true"; }
               )));
in
{
  options = {
    services.exhibitor = {
      enable = mkOption {
        type = types.bool;
        default = false;
        description = "
          Whether to enable the exhibitor server.
        ";
      };
      # See https://github.com/soabase/exhibitor/wiki/Running-Exhibitor for what these mean
      # General options for any type of config
      port = mkOption {
        type = types.int;
        default = 8080;
        description = ''
          The port for exhibitor to listen on and communicate with other exhibitors.
        '';
      };
      baseDir = mkOption {
        type = types.str;
        default = "/var/exhibitor";
        description = ''
          Baseline directory for exhibitor runtime config.
        '';
      };
      configType = mkOption {
        type = types.enum [ "file" "s3" "zookeeper" "none" ];
        description = ''
          Which configuration type you want to use. Additional config will be
          required depending on which type you are using.
        '';
      };
      hostname = mkOption {
        type = types.nullOr types.str;
        description = ''
          Hostname to use and advertise
        '';
        default = null;
      };
      nodeModification = mkOption {
        type = types.bool;
        description = ''
          Whether the Explorer UI will allow nodes to be modified (use with caution).
        '';
        default = true;
      };
      configCheckMs = mkOption {
        type = types.int;
        description = ''
          Period (ms) to check for shared config updates.
        '';
        default = 30000;
      };
      headingText = mkOption {
        type = types.nullOr types.str;
        description = ''
          Extra text to display in UI header
        '';
        default = null;
      };
      jqueryStyle = mkOption {
        type = types.enum [ "red" "black" "custom" ];
        description = ''
          Styling used for the JQuery-based UI.
        '';
        default = "red";
      };
      logLines = mkOption {
        type = types.int;
        description = ''
        Max lines of logging to keep in memory for display.
        '';
        default = 1000;
      };
      servo = mkOption {
        type = types.bool;
        description = ''
          ZooKeeper will be queried once a minute for its state via the 'mntr' four
          letter word (this requires ZooKeeper 3.4.x+). Servo will be used to publish
          this data via JMX.
        '';
        default = false;
      };
      timeout = mkOption {
        type = types.int;
        description = ''
          Connection timeout (ms) for ZK connections.
        '';
        default = 30000;
      };
      autoManageInstances = mkOption {
        type = types.bool;
        description = ''
          Automatically manage ZooKeeper instances in the ensemble
        '';
        default = false;
      };
      zkDataDir = mkOption {
        type = types.str;
        default = "${cfg.baseDir}/zkData";
        defaultText = literalExpression ''"''${config.${opt.baseDir}}/zkData"'';
        description = ''
          The Zookeeper data directory
        '';
      };
      zkLogDir = mkOption {
        type = types.path;
        default = "${cfg.baseDir}/zkLogs";
        defaultText = literalExpression ''"''${config.${opt.baseDir}}/zkLogs"'';
        description = ''
          The Zookeeper logs directory
        '';
      };
      extraConf = mkOption {
        type = types.str;
        default = "";
        description = ''
          Extra Exhibitor configuration to put in the ZooKeeper config file.
        '';
      };
      zkExtraCfg = mkOption {
        type = types.str;
        default = "initLimit=5&syncLimit=2&tickTime=2000";
        description = ''
          Extra options to pass into Zookeeper
        '';
      };
      zkClientPort = mkOption {
        type = types.int;
        default = 2181;
        description = ''
          Zookeeper client port
        '';
      };
      zkConnectPort = mkOption {
        type = types.int;
        default = 2888;
        description = ''
          The port to use for followers to talk to each other.
        '';
      };
      zkElectionPort = mkOption {
        type = types.int;
        default = 3888;
        description = ''
          The port for Zookeepers to use for leader election.
        '';
      };
      zkCleanupPeriod = mkOption {
        type = types.int;
        default = 0;
        description = ''
          How often (in milliseconds) to run the Zookeeper log cleanup task.
        '';
      };
      zkServersSpec = mkOption {
        type = types.listOf types.str;
        default = [];
        description = ''
          Zookeeper server spec for all servers in the ensemble.
        '';
        example = [ "S:1:zk1.example.com" "S:2:zk2.example.com" "S:3:zk3.example.com" "O:4:zk-observer.example.com" ];
      };

      # Backup options
      s3Backup = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to enable backups to S3
        '';
      };
      fileSystemBackup = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Enables file system backup of ZooKeeper log files
        '';
      };

      # Options for using zookeeper configType
      zkConfigConnect = mkOption {
        type = types.listOf types.str;
        description = ''
          The initial connection string for ZooKeeper shared config storage
        '';
        example = ["host1:2181" "host2:2181"];
      };
      zkConfigExhibitorPath = mkOption {
        type = types.str;
        description = ''
          If the ZooKeeper shared config is also running Exhibitor, the URI path for the REST call
        '';
        default = "/";
      };
      zkConfigExhibitorPort = mkOption {
        type = types.nullOr types.int;
        description = ''
          If the ZooKeeper shared config is also running Exhibitor, the port that
          Exhibitor is listening on. IMPORTANT: if this value is not set it implies
          that Exhibitor is not being used on the ZooKeeper shared config.
        '';
      };
      zkConfigPollMs = mkOption {
        type = types.int;
        description = ''
          The period in ms to check for changes in the config ensemble
        '';
        default = 10000;
      };
      zkConfigRetry = {
        sleepMs = mkOption {
          type = types.int;
          default = 1000;
          description = ''
            Retry sleep time connecting to the ZooKeeper config
          '';
        };
        retryQuantity = mkOption {
          type = types.int;
          default = 3;
          description = ''
            Retries connecting to the ZooKeeper config
          '';
        };
      };
      zkConfigZPath = mkOption {
        type = types.str;
        description = ''
          The base ZPath that Exhibitor should use
        '';
        example = "/exhibitor/config";
      };

      # Config options for s3 configType
      s3Config = {
        bucketName = mkOption {
          type = types.str;
          description = ''
            Bucket name to store config
          '';
        };
        objectKey = mkOption {
          type = types.str;
          description = ''
            S3 key name to store the config
          '';
        };
        configPrefix = mkOption {
          type = types.str;
          description = ''
            When using AWS S3 shared config files, the prefix to use for values such as locks
          '';
          default = "exhibitor-";
        };
      };

      # The next two are used for either s3backup or s3 configType
      s3Credentials = mkOption {
        type = types.nullOr types.path;
        description = ''
          Optional credentials to use for s3backup or s3config. Argument is the path
          to an AWS credential properties file with two properties:
          com.netflix.exhibitor.s3.access-key-id and com.netflix.exhibitor.s3.access-secret-key
        '';
        default = null;
      };
      s3Region = mkOption {
        type = types.nullOr types.str;
        description = ''
          Optional region for S3 calls
        '';
        default = null;
      };

      # Config options for file config type
      fsConfigDir = mkOption {
        type = types.path;
        description = ''
          Directory to store Exhibitor properties (cannot be used with s3config).
          Exhibitor uses file system locks so you can specify a shared location
          so as to enable complete ensemble management.
        '';
      };
      fsConfigLockPrefix = mkOption {
        type = types.str;
        description = ''
          A prefix for a locking mechanism used in conjunction with fsconfigdir
        '';
        default = "exhibitor-lock-";
      };
      fsConfigName = mkOption {
        type = types.str;
        description = ''
          The name of the file to store config in
        '';
        default = "exhibitor.properties";
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.exhibitor = {
      description = "Exhibitor Daemon";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      environment = {
        ZOO_LOG_DIR = cfg.baseDir;
      };
      serviceConfig = {
        /***
          Exhibitor is a bit un-nixy. It wants to present to you a user interface in order to
          mutate the configuration of both itself and ZooKeeper, and to coordinate changes
          among the members of the Zookeeper ensemble. I'm going for a different approach here,
          which is to manage all the configuration via nix and have it write out the configuration
          files that exhibitor will use, and to reduce the amount of inter-exhibitor orchestration.
        ***/
        ExecStart = ''
          ${pkgs.exhibitor}/bin/startExhibitor.sh ${cliOptions}
        '';
        User = "zookeeper";
        PermissionsStartOnly = true;
      };
      # This is a bit wonky, but the reason for this is that Exhibitor tries to write to
      # ${cfg.baseDir}/zookeeper/bin/../conf/zoo.cfg
      # I want everything but the conf directory to be in the immutable nix store, and I want defaults
      # from the nix store
      # If I symlink the bin directory in, then bin/../ will resolve to the parent of the symlink in the
      # immutable nix store. Bind mounting a writable conf over the existing conf might work, but it gets very
      # messy with trying to copy the existing out into a mutable store.
      # Another option is to try to patch upstream exhibitor, but the current package just pulls down the
      # prebuild JARs off of Maven, rather than building them ourselves, as Maven support in Nix isn't
      # very mature. So, it seems like a reasonable compromise is to just copy out of the immutable store
      # just before starting the service, so we're running binaries from the immutable store, but we work around
      # Exhibitor's desire to mutate its current installation.
      preStart = ''
        mkdir -m 0700 -p ${cfg.baseDir}/zookeeper
        # Not doing a chown -R to keep the base ZK files owned by root
        chown zookeeper ${cfg.baseDir} ${cfg.baseDir}/zookeeper
        cp -Rf ${pkgs.zookeeper}/* ${cfg.baseDir}/zookeeper
        chown -R zookeeper ${cfg.baseDir}/zookeeper/conf
        chmod -R u+w ${cfg.baseDir}/zookeeper/conf
        replace_what=$(echo ${pkgs.zookeeper} | sed 's/[\/&]/\\&/g')
        replace_with=$(echo ${cfg.baseDir}/zookeeper | sed 's/[\/&]/\\&/g')
        sed -i 's/'"$replace_what"'/'"$replace_with"'/g' ${cfg.baseDir}/zookeeper/bin/zk*.sh
      '';
    };
    users.users.zookeeper = {
      uid = config.ids.uids.zookeeper;
      description = "Zookeeper daemon user";
      home = cfg.baseDir;
    };
  };
}