git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/consul: fix consul alerts enable
commitc9da002a07a835bef8b6e7e3eeeb5f4a85b39ba3 authorJaka Hudoklin <jakahudoklin@gmail.com> dateMon 8 Jun 2015, 13:41 (11 years ago) parentcc96e47
1 file changed+96−85
nixos/modules/services/networking/consul.nix+96−85
nixos/modules/services/networking/consul.nix+96−85
@@ -106,6 +106,12 @@ in
106106 alerts = {
107107 enable = mkEnableOption "Whether to enable consul-alerts";
108108
109+ package = mkOption {
110+ description = "Package to use for consul-alerts.";
111+ default = pkgs.consul-alerts;
112+ type = types.package;
113+ };
114+
109115 listenAddr = mkOption {
110116 description = "Api listening address.";
111117 default = "localhost:9000";
@@ -135,96 +141,101 @@ in
135141
136142 };
137143
138− config = mkIf cfg.enable {
144+ config = mkIf cfg.enable (
145+ mkMerge [{
139146
140− users.extraUsers."consul" = {
141− description = "Consul agent daemon user";
142− uid = config.ids.uids.consul;
143− # The shell is needed for health checks
144− shell = "/run/current-system/sw/bin/bash";
145− };
147+ users.extraUsers."consul" = {
148+ description = "Consul agent daemon user";
149+ uid = config.ids.uids.consul;
150+ # The shell is needed for health checks
151+ shell = "/run/current-system/sw/bin/bash";
152+ };
146153
147− environment = {
148− etc."consul.json".text = builtins.toJSON configOptions;
149− # We need consul.d to exist for consul to start
150− etc."consul.d/dummy.json".text = "{ }";
151− systemPackages = with pkgs; [ consul ];
152− };
154+ environment = {
155+ etc."consul.json".text = builtins.toJSON configOptions;
156+ # We need consul.d to exist for consul to start
157+ etc."consul.d/dummy.json".text = "{ }";
158+ systemPackages = with pkgs; [ consul ];
159+ };
153160
154− systemd.services.consul = {
155− wantedBy = [ "multi-user.target" ];
156− after = [ "network.target" ] ++ systemdDevices;
157− bindsTo = systemdDevices;
158− restartTriggers = [ config.environment.etc."consul.json".source ]
159− ++ mapAttrsToList (_: d: d.source)
160− (filterAttrs (n: _: hasPrefix "consul.d/" n) config.environment.etc);
161−
162− serviceConfig = {
163− ExecStart = "@${pkgs.consul}/bin/consul consul agent -config-dir /etc/consul.d"
164− + concatMapStrings (n: " -config-file ${n}") configFiles;
165− ExecReload = "${pkgs.consul}/bin/consul reload";
166− PermissionsStartOnly = true;
167− User = if cfg.dropPrivileges then "consul" else null;
168− TimeoutStartSec = "0";
169− } // (optionalAttrs (cfg.leaveOnStop) {
170− ExecStop = "${pkgs.consul}/bin/consul leave";
171− });
172−
173− path = with pkgs; [ iproute gnugrep gawk consul ];
174− preStart = ''
175− mkdir -m 0700 -p ${dataDir}
176− chown -R consul ${dataDir}
177−
178− # Determine interface addresses
179− getAddrOnce () {
180− ip addr show dev "$1" \
181− | grep 'inet${optionalString (cfg.forceIpv4) " "}.*scope global' \
182− | awk -F '[ /\t]*' '{print $3}' | head -n 1
183− }
184− getAddr () {
185− ADDR="$(getAddrOnce $1)"
186− LEFT=60 # Die after 1 minute
187− while [ -z "$ADDR" ]; do
188− sleep 1
189− LEFT=$(expr $LEFT - 1)
190− if [ "$LEFT" -eq "0" ]; then
191− echo "Address lookup timed out"
192− exit 1
193− fi
161+ systemd.services.consul = {
162+ wantedBy = [ "multi-user.target" ];
163+ after = [ "network.target" ] ++ systemdDevices;
164+ bindsTo = systemdDevices;
165+ restartTriggers = [ config.environment.etc."consul.json".source ]
166+ ++ mapAttrsToList (_: d: d.source)
167+ (filterAttrs (n: _: hasPrefix "consul.d/" n) config.environment.etc);
168+
169+ serviceConfig = {
170+ ExecStart = "@${pkgs.consul}/bin/consul consul agent -config-dir /etc/consul.d"
171+ + concatMapStrings (n: " -config-file ${n}") configFiles;
172+ ExecReload = "${pkgs.consul}/bin/consul reload";
173+ PermissionsStartOnly = true;
174+ User = if cfg.dropPrivileges then "consul" else null;
175+ TimeoutStartSec = "0";
176+ } // (optionalAttrs (cfg.leaveOnStop) {
177+ ExecStop = "${pkgs.consul}/bin/consul leave";
178+ });
179+
180+ path = with pkgs; [ iproute gnugrep gawk consul ];
181+ preStart = ''
182+ mkdir -m 0700 -p ${dataDir}
183+ chown -R consul ${dataDir}
184+
185+ # Determine interface addresses
186+ getAddrOnce () {
187+ ip addr show dev "$1" \
188+ | grep 'inet${optionalString (cfg.forceIpv4) " "}.*scope global' \
189+ | awk -F '[ /\t]*' '{print $3}' | head -n 1
190+ }
191+ getAddr () {
194192 ADDR="$(getAddrOnce $1)"
195− done
196− echo "$ADDR"
197− }
198− echo "{" > /etc/consul-addrs.json
199− delim=" "
200− ''
201− + concatStrings (flip mapAttrsToList cfg.interface (name: i:
202− optionalString (i != null) ''
203− echo "$delim \"${name}_addr\": \"$(getAddr "${i}")\"" >> /etc/consul-addrs.json
204− delim=","
205− ''))
206− + ''
207− echo "}" >> /etc/consul-addrs.json
208− '';
209− };
210−
211− systemd.services.consul-alerts = mkIf (cfg.alerts.enable) {
212− wantedBy = [ "multi-user.target" ];
213− after = [ "consul.service" ];
214−
215− path = [ pkgs.consul ];
216−
217− serviceConfig = {
218− ExecStart = ''
219− ${pkgs.consul-alerts}/bin/consul-alerts start \
220− --alert-addr=${cfg.alerts.listenAddr} \
221− --consul-addr=${cfg.alerts.consulAddr} \
222− ${optionalString cfg.alerts.watchChecks "--watch-checks"} \
223− ${optionalString cfg.alerts.watchEvents "--watch-events"}
193+ LEFT=60 # Die after 1 minute
194+ while [ -z "$ADDR" ]; do
195+ sleep 1
196+ LEFT=$(expr $LEFT - 1)
197+ if [ "$LEFT" -eq "0" ]; then
198+ echo "Address lookup timed out"
199+ exit 1
200+ fi
201+ ADDR="$(getAddrOnce $1)"
202+ done
203+ echo "$ADDR"
204+ }
205+ echo "{" > /etc/consul-addrs.json
206+ delim=" "
207+ ''
208+ + concatStrings (flip mapAttrsToList cfg.interface (name: i:
209+ optionalString (i != null) ''
210+ echo "$delim \"${name}_addr\": \"$(getAddr "${i}")\"" >> /etc/consul-addrs.json
211+ delim=","
212+ ''))
213+ + ''
214+ echo "}" >> /etc/consul-addrs.json
224215 '';
225− User = if cfg.dropPrivileges then "consul" else null;
226216 };
227− };
217+ }
218+
219+ (mkIf (cfg.alerts.enable) {
220+ systemd.services.consul-alerts = {
221+ wantedBy = [ "multi-user.target" ];
222+ after = [ "consul.service" ];
223+
224+ path = [ pkgs.consul ];
225+
226+ serviceConfig = {
227+ ExecStart = ''
228+ ${cfg.alerts.package}/bin/consul-alerts start \
229+ --alert-addr=${cfg.alerts.listenAddr} \
230+ --consul-addr=${cfg.alerts.consulAddr} \
231+ ${optionalString cfg.alerts.watchChecks "--watch-checks"} \
232+ ${optionalString cfg.alerts.watchEvents "--watch-events"}
233+ '';
234+ User = if cfg.dropPrivileges then "consul" else null;
235+ Restart = "on-failure";
236+ };
237+ };
238+ })
228239
229− };
240+ ]);
230241}