git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #4991 from offlinehacker/nixos/etcd

nixos: add etcd module

commit444bda19362e6b3d725b91dbcd2f2963f3fc23e8 authorJaka Hudoklin <jakahudoklin@gmail.com> dateSat 22 Nov 2014, 12:05 (12 years ago) parenta4a235cb3bc157
7 files changed+258−32
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 pkgs/servers/etcd/default.nix+2−2 pkgs/servers/etcd/deps.nix+4−30
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+}
pkgs/servers/etcd/default.nix+2−2
@@ -1,11 +1,11 @@
11{ stdenv, lib, go, fetchurl, fetchgit, fetchhg, fetchbzr, fetchFromGitHub }:
22
33stdenv.mkDerivation rec {
4− version = "0.4.3";
4+ version = "0.5.0-alpha.3";
55 name = "etcd-${version}";
66
77 src = import ./deps.nix {
8− inherit stdenv lib fetchgit fetchhg fetchbzr fetchFromGitHub;
8+ inherit stdenv lib fetchFromGitHub;
99 };
1010
1111 buildInputs = [ go ];
pkgs/servers/etcd/deps.nix+4−30
@@ -1,4 +1,5 @@
1−{ stdenv, lib, fetchgit, fetchhg, fetchbzr, fetchFromGitHub }:
1+# This file was generated by go2nix.
2+{ stdenv, lib, fetchFromGitHub }:
23
34let
45 goDeps = [
@@ -7,35 +8,8 @@ let
78 src = fetchFromGitHub {
89 owner = "coreos";
910 repo = "etcd";
10− rev = "9970141f76241c909977af7bafe7b6f2c4923de8";
11− sha256 = "1bva46gfy4rkfw8k8pb3lsfzfg16csds01f0nvfrkh99pr7sp0sy";
12− };
13− }
14− {
15− root = "github.com/stathat/go";
16− src = fetchFromGitHub {
17− owner = "stathat";
18− repo = "go";
19− rev = "01d012b9ee2ecc107cb28b6dd32d9019ed5c1d77";
20− sha256 = "0mrn70wjfcs4rfkmga3hbfqmbjk33skcsc8pyqxp02bzpwdpc4bi";
21− };
22− }
23− {
24− root = "github.com/stretchr/objx";
25− src = fetchFromGitHub {
26− owner = "stretchr";
27− repo = "objx";
28− rev = "cbeaeb16a013161a98496fad62933b1d21786672";
29− sha256 = "1xn7iibjik77h6h0jilfvcjkkzaqz45baf44p3rb2i03hbmkqkp1";
30− };
31− }
32− {
33− root = "github.com/stretchr/testify";
34− src = fetchFromGitHub {
35− owner = "stretchr";
36− repo = "testify";
37− rev = "3e03dde72495487a4deb74152ac205d0619fbc8d";
38− sha256 = "1xd9sbi6y68cfwkxgybcz0dbfx4r6jmxq51wjj6six3wm9p7m8ls";
11+ rev = "a817ca705b5a06b3d8c008e1a59f08895971cb1e";
12+ sha256 = "1lksjrb7anh6z8ajnp8jpfz9mxmvm762acdczvc6h1h4997dz982";
3913 };
4014 }
4115 ];