summaryrefslogtreecommitdiffstats
path: root/3modules/tv/consul.nix
blob: db0cd7a9e2c4fa8a83263f0b9bde4037ab8ca4e6 (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
{ config, lib, pkgs, ... }:

# if quorum gets lost, then start any node with a config that doesn't contain bootstrap_expect
# but -bootstrap
# TODO consul-bootstrap HOST  that actually does is
# TODO tools to inspect state of a cluster in outage state

with builtins;
with lib;
let
  cfg = config.tv.consul;

  out = {
    imports = [ ../../3modules/tv/iptables.nix ];
    options.tv.consul = api;
    config = mkIf cfg.enable (mkMerge [
      imp
      { tv.iptables.input-retiolum-accept-new-tcp = [ "8300" "8301" ]; }
      # TODO udp for 8301
    ]);
  };

  api = {
    enable = mkEnableOption "tv.consul";

    dc = mkOption {
      type = types.unspecified;
    };
    hosts = mkOption {
      type = with types; listOf unspecified;
    };
    encrypt-file = mkOption {
      type = types.str; # TODO path (but not just into store)
      default = "/root/src/secrets/consul-encrypt.json";
    };
    data-dir = mkOption {
      type = types.str; # TODO path (but not just into store)
      default = "/var/lib/consul";
    };
    self = mkOption {
      type = types.unspecified;
    };
    server = mkOption {
      type = types.bool;
      default = false;
    };
    GOMAXPROCS = mkOption {
      type = types.int;
      default = cfg.self.cores;
    };
  };

  consul-config = {
    datacenter = cfg.dc;
    data_dir = cfg.data-dir;
    log_level = "INFO";
    #node_name =
    server = cfg.server;
    bind_addr = cfg.self.addr; # TODO cfg.addr
    enable_syslog = true;
    retry_join = map (getAttr "addr") (filter (host: host.fqdn != cfg.self.fqdn) cfg.hosts);
    leave_on_terminate = true;
  } // optionalAttrs cfg.server {
    bootstrap_expect = length cfg.hosts;
    leave_on_terminate = false;
  };

  imp = {
    environment.systemPackages = with pkgs; [
      consul
    ];

    systemd.services.consul = {
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      path = with pkgs; [
        consul
      ];
      environment = {
        GOMAXPROCS = toString cfg.GOMAXPROCS;
      };
      serviceConfig = {
        PermissionsStartOnly = "true";
        SyslogIdentifier = "consul";
        User = user.name;
        PrivateTmp = "true";
        Restart = "always";
        ExecStartPre = pkgs.writeScript "consul-init" ''
          #! /bin/sh
          mkdir -p ${cfg.data-dir}
          chown ${user.name}: ${cfg.data-dir}
          install -o ${user.name} -m 0400 ${cfg.encrypt-file} /tmp/encrypt.json
        '';
        ExecStart = pkgs.writeScript "consul-service" ''
          #! /bin/sh
          set -euf
          exec >/dev/null
          exec consul agent \
            -config-file=${toFile "consul.json" (toJSON consul-config)} \
            -config-file=/tmp/encrypt.json
        '';
        #-node=${cfg.self.fqdn} \
        #ExecStart = "${tinc}/sbin/tincd -c ${confDir} -d 0 -U ${user} -D";
      };
    };

    users.extraUsers = singleton {
      inherit (user) name uid;
    };
  };

  user = {
    name = "consul";
    uid = 2999951406; # genid consul
  };

in
out