summary refs log tree commit diff
path: root/nixos/modules/services/cluster/fleet.nix
blob: 78d4ea93c491a7557c2dac8e0797aa8a9aaff5b0 (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.fleet;

in {

  ##### Interface
  options.services.fleet = {
    enable = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Whether to enable fleet service.
      '';
    };

    listen = mkOption {
      type = types.listOf types.str;
      default = [ "/var/run/fleet.sock" ];
      example = [ "/var/run/fleet.sock" "127.0.0.1:49153" ];
      description = ''
        Fleet listening addresses.
      '';
    };

    etcdServers = mkOption {
      type = types.listOf types.str;
      default = [ "http://127.0.0.1:4001" ];
      description = ''
        Fleet list of etcd endpoints to use.
      '';
    };

    publicIp = mkOption {
      type = types.nullOr types.str;
      default = "";
      description = ''
        Fleet IP address that should be published with the local Machine's
        state and any socket information. If not set, fleetd will attempt
        to detect the IP it should publish based on the machine's IP
        routing information.
      '';
    };

    etcdCafile = mkOption {
      type = types.nullOr types.path;
      default = null;
      description = ''
        Fleet TLS ca file when SSL certificate authentication is enabled
        in etcd endpoints.
      '';
    };

    etcdKeyfile = mkOption {
      type = types.nullOr types.path;
      default = null;
      description = ''
        Fleet TLS key file when SSL certificate authentication is enabled
        in etcd endpoints.
      '';
    };

    etcdCertfile = mkOption {
      type = types.nullOr types.path;
      default = null;
      description = ''
        Fleet TLS cert file when SSL certificate authentication is enabled
        in etcd endpoints.
      '';
    };

    metadata = mkOption {
      type = types.attrsOf types.str;
      default = {};
      apply = attrs: concatMapStringsSep "," (n: "${n}=${attrs."${n}"}") (attrNames attrs);
      example = literalExample ''
        {
          region = "us-west";
          az = "us-west-1";
        }
      '';
      description = ''
        Key/value pairs that are published with the local to the fleet registry.
        This data can be used directly by a client of fleet to make scheduling decisions.
      '';
    };

    extraConfig = mkOption {
      type = types.attrsOf types.str;
      apply = mapAttrs' (n: v: nameValuePair ("FLEET_" + n) v);
      default = {};
      example = literalExample ''
        {
          VERBOSITY = 1;
          ETCD_REQUEST_TIMEOUT = "2.0";
          AGENT_TTL = "40s";
        }
      '';
      description = ''
        Fleet extra config. See
        <link xlink:href="https://github.com/coreos/fleet/blob/master/Documentation/deployment-and-configuration.md"/>
        for configuration options.
      '';
    };

  };

  ##### Implementation
  config = mkIf cfg.enable {
    systemd.services.fleet = {
      description = "Fleet Init System Daemon";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" "fleet.socket" "etcd.service" "docker.service" ];
      requires = [ "fleet.socket" ];
      environment = {
        FLEET_ETCD_SERVERS = concatStringsSep "," cfg.etcdServers;
        FLEET_PUBLIC_IP = cfg.publicIp;
        FLEET_ETCD_CAFILE = cfg.etcdCafile;
        FLEET_ETCD_KEYFILE = cfg.etcdKeyfile;
        FLEET_ETCD_CERTFILE = cfg.etcdCertfile;
        FLEET_METADATA = cfg.metadata;
      } // cfg.extraConfig;
      serviceConfig = {
        ExecStart = "${pkgs.fleet}/bin/fleetd";
        Group = "fleet";
      };
    };

    systemd.sockets.fleet = {
      description = "Fleet Socket for the API";
      wantedBy = [ "sockets.target" ];
      listenStreams = cfg.listen;
      socketConfig = {
        ListenStream = "/var/run/fleet.sock";
        SocketMode = "0660";
        SocketUser = "root";
        SocketGroup = "fleet";
      };
    };

    services.etcd.enable = mkDefault true;
    virtualisation.docker.enable = mkDefault true;

    environment.systemPackages = [ pkgs.fleet ];
    users.extraGroups.fleet.gid = config.ids.gids.fleet;
  };
}