git.iko.soy/typeable/nixpkgs
on master help sign in
nixos: add etcd module
commitb3bc157f7ff84a7ed0dc34901830c589d17a51e9 authorJaka Hudoklin <jakahudoklin@gmail.com> dateSat 15 Nov 2014, 16:27 (12 years ago) parentbcf9da3
5 files changed+252
nixos/modules/misc/ids.nix+1 nixos/modules/module-list.nix+1 nixos/modules/services/misc/etcd.nix+141 nixos/release.nix+1 nixos/tests/etcd.nix+108
nixos/modules/misc/ids.nix+1
@@ -163,6 +163,7 @@
163163 systemd-resolve = 153;
164164 systemd-timesync = 154;
165165 liquidsoap = 155;
166+ etcd = 156;
166167
167168 # When adding a uid, make sure it doesn't match an existing gid. And don't use uids above 399!
168169
nixos/modules/module-list.nix+1
@@ -166,6 +166,7 @@
166166 ./services/misc/cgminer.nix
167167 ./services/misc/dictd.nix
168168 ./services/misc/disnix.nix
169+ ./services/misc/etcd.nix
169170 ./services/misc/felix.nix
170171 ./services/misc/folding-at-home.nix
171172 ./services/misc/gitolite.nix
nixos/modules/services/misc/etcd.nixnew file+141
@@ -0,0 +1,141 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.etcd;
7+
8+in {
9+
10+ options.services.etcd = {
11+ enable = mkOption {
12+ description = "Whether to enable etcd.";
13+ default = false;
14+ type = types.uniq types.bool;
15+ };
16+
17+ name = mkOption {
18+ description = "Etcd unique node name.";
19+ default = config.networking.hostName;
20+ type = types.str;
21+ };
22+
23+ advertiseClientUrls = mkOption {
24+ description = "Etcd list of this member's client URLs to advertise to the rest of the cluster.";
25+ default = cfg.listenClientUrls;
26+ type = types.listOf types.str;
27+ };
28+
29+ listenClientUrls = mkOption {
30+ description = "Etcd list of URLs to listen on for client traffic.";
31+ default = ["http://localhost:2379" "http://localhost:4001"];
32+ type = types.listOf types.str;
33+ };
34+
35+ listenPeerUrls = mkOption {
36+ description = "Etcd list of URLs to listen on for peer traffic.";
37+ default = ["http://localhost:2380" "http://localhost:7001"];
38+ type = types.listOf types.str;
39+ };
40+
41+ initialAdvertisePeerUrls = mkOption {
42+ description = "Etcd list of this member's peer URLs to advertise to rest of the cluster.";
43+ default = cfg.listenPeerUrls;
44+ type = types.listOf types.str;
45+ };
46+
47+ initialCluster = mkOption {
48+ description = "Etcd initial cluster configuration for bootstrapping.";
49+ default = ["${cfg.name}=http://localhost:2380" "${cfg.name}=http://localhost:7001"];
50+ type = types.listOf types.str;
51+ };
52+
53+ initialClusterState = mkOption {
54+ description = "Etcd initial cluster configuration for bootstrapping.";
55+ default = "new";
56+ type = types.enum ["new" "existing"];
57+ };
58+
59+ initialClusterToken = mkOption {
60+ description = "Etcd initial cluster token for etcd cluster during bootstrap.";
61+ default = "etcd-cluster";
62+ type = types.str;
63+ };
64+
65+ discovery = mkOption {
66+ description = "Etcd discovery url";
67+ default = "";
68+ type = types.str;
69+ };
70+
71+ extraConf = mkOption {
72+ description = ''
73+ Etcd extra configuration. See
74+ <link xlink:href='https://github.com/coreos/etcd/blob/master/Documentation/configuration.md#environment-variables' />
75+ '';
76+ type = types.attrsOf types.str;
77+ default = {};
78+ example = literalExample ''
79+ {
80+ "CORS": "*",
81+ "NAME": "default-name",
82+ "MAX_RESULT_BUFFER": "1024",
83+ "MAX_CLUSTER_SIZE": "9",
84+ "MAX_RETRY_ATTEMPTS": "3"
85+ }
86+ '';
87+ };
88+
89+ dataDir = mkOption {
90+ type = types.path;
91+ default = "/var/lib/etcd";
92+ description = "Etcd data directory.";
93+ };
94+ };
95+
96+ config = mkIf cfg.enable {
97+ systemd.services.etcd = {
98+ description = "Etcd Daemon";
99+ wantedBy = [ "multi-user.target" ];
100+ after = [ "network-interfaces.target" ];
101+
102+ environment = {
103+ ETCD_NAME = cfg.name;
104+ ETCD_DISCOVERY = cfg.discovery;
105+ ETCD_DATA_DIR = cfg.dataDir;
106+ ETCD_ADVERTISE_CLIENT_URLS = concatStringsSep "," cfg.advertiseClientUrls;
107+ ETCD_LISTEN_CLIENT_URLS = concatStringsSep "," cfg.listenClientUrls;
108+ ETCD_LISTEN_PEER_URLS = concatStringsSep "," cfg.listenPeerUrls;
109+ ETCD_INITIAL_ADVERTISE_PEER_URLS = concatStringsSep "," cfg.initialAdvertisePeerUrls;
110+ } // (optionalAttrs (cfg.discovery == ""){
111+ ETCD_INITIAL_CLUSTER = concatStringsSep "," cfg.initialCluster;
112+ ETCD_INITIAL_CLUSTER_STATE = cfg.initialClusterState;
113+ ETCD_INITIAL_CLUSTER_TOKEN = cfg.initialClusterToken;
114+ }) // (mapAttrs' (n: v: nameValuePair "ETCD_${n}" v) cfg.extraConf);
115+
116+ serviceConfig = {
117+ ExecStart = "${pkgs.etcd}/bin/etcd";
118+ User = "etcd";
119+ PermissionsStartOnly = true;
120+ };
121+ preStart = ''
122+ mkdir -m 0700 -p ${cfg.dataDir}
123+ if [ "$(id -u)" = 0 ]; then chown etcd ${cfg.dataDir}; fi
124+ '';
125+ postStart = ''
126+ until ${pkgs.curl}/bin/curl -s -o /dev/null '${head cfg.listenClientUrls}/version'; do
127+ sleep 1;
128+ done
129+ '';
130+ };
131+
132+ environment.systemPackages = [ pkgs.etcdctl ];
133+
134+ users.extraUsers = singleton {
135+ name = "etcd";
136+ uid = config.ids.uids.etcd;
137+ description = "Etcd daemon user";
138+ home = cfg.dataDir;
139+ };
140+ };
141+}
nixos/release.nix+1
@@ -239,6 +239,7 @@ in rec {
239239 tests.chromium = callTest tests/chromium.nix {};
240240 tests.cjdns = callTest tests/cjdns.nix {};
241241 tests.containers = callTest tests/containers.nix {};
242+ tests.etcd = callTest tests/etcd.nix {};
242243 tests.firefox = callTest tests/firefox.nix {};
243244 tests.firewall = callTest tests/firewall.nix {};
244245 tests.gnome3 = callTest tests/gnome3.nix {};
nixos/tests/etcd.nixnew file+108
@@ -0,0 +1,108 @@
1+# This test runs etcd as single node, multy node and using discovery
2+
3+import ./make-test.nix {
4+ name = "etcd";
5+
6+ nodes = {
7+ simple =
8+ { config, pkgs, nodes, ... }:
9+ {
10+ services.etcd.enable = true;
11+ services.etcd.listenClientUrls = ["http://0.0.0.0:4001"];
12+ environment.systemPackages = [ pkgs.curl ];
13+ networking.firewall.allowedTCPPorts = [ 4001 ];
14+ };
15+
16+
17+ node1 =
18+ { config, pkgs, nodes, ... }:
19+ {
20+ services = {
21+ etcd = {
22+ enable = true;
23+ listenPeerUrls = ["http://0.0.0.0:7001"];
24+ initialAdvertisePeerUrls = ["http://node1:7001"];
25+ initialCluster = ["node1=http://node1:7001" "node2=http://node2:7001"];
26+ };
27+ };
28+
29+ networking.firewall.allowedTCPPorts = [ 7001 ];
30+ };
31+
32+ node2 =
33+ { config, pkgs, ... }:
34+ {
35+ services = {
36+ etcd = {
37+ enable = true;
38+ listenPeerUrls = ["http://0.0.0.0:7001"];
39+ initialAdvertisePeerUrls = ["http://node2:7001"];
40+ initialCluster = ["node1=http://node1:7001" "node2=http://node2:7001"];
41+ };
42+ };
43+
44+ networking.firewall.allowedTCPPorts = [ 7001 ];
45+ };
46+
47+ discovery1 =
48+ { config, pkgs, nodes, ... }:
49+ {
50+ services = {
51+ etcd = {
52+ enable = true;
53+ listenPeerUrls = ["http://0.0.0.0:7001"];
54+ initialAdvertisePeerUrls = ["http://discovery1:7001"];
55+ discovery = "http://simple:4001/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/";
56+ };
57+ };
58+
59+ networking.firewall.allowedTCPPorts = [ 7001 ];
60+ };
61+
62+ discovery2 =
63+ { config, pkgs, ... }:
64+ {
65+ services = {
66+ etcd = {
67+ enable = true;
68+ listenPeerUrls = ["http://0.0.0.0:7001"];
69+ initialAdvertisePeerUrls = ["http://discovery2:7001"];
70+ discovery = "http://simple:4001/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/";
71+ };
72+ };
73+
74+ networking.firewall.allowedTCPPorts = [ 7001 ];
75+ };
76+ };
77+
78+ testScript = ''
79+ subtest "single node", sub {
80+ $simple->start();
81+ $simple->waitForUnit("etcd.service");
82+ $simple->succeed("etcdctl set /foo/bar 'Hello world'");
83+ $simple->succeed("etcdctl get /foo/bar | grep 'Hello world'");
84+ };
85+
86+ subtest "multy node", sub {
87+ $node1->start();
88+ $node2->start();
89+ $node1->waitForUnit("etcd.service");
90+ $node2->waitForUnit("etcd.service");
91+ $node1->succeed("etcdctl set /foo/bar 'Hello world'");
92+ $node2->succeed("etcdctl get /foo/bar | grep 'Hello world'");
93+ $node1->shutdown();
94+ $node2->shutdown();
95+ };
96+
97+ subtest "discovery", sub {
98+ $simple->succeed("curl -X PUT http://localhost:4001/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size -d value=2");
99+
100+ $discovery1->start();
101+ $discovery2->start();
102+ $discovery1->waitForUnit("etcd.service");
103+ $discovery2->waitForUnit("etcd.service");
104+ $discovery1->succeed("etcdctl set /foo/bar 'Hello world'");
105+ $discovery2->succeed("etcdctl get /foo/bar | grep 'Hello world'");
106+ };
107+ '';
108+}