modules/{consul-distributed-services,consul-service-registry}: set CONSUL_HTTP_ADDR

This commit is contained in:
Max Headroom 2023-09-03 21:20:40 +02:00
parent 0025a4bb2a
commit 5267d14b48
2 changed files with 9 additions and 0 deletions

View file

@ -4,6 +4,9 @@ with lib;
let let
consul = config.services.consul.package; consul = config.services.consul.package;
consulCfg = config.services.consul.extraConfig;
consulHttpAddr = "${consulCfg.addresses.http or "127.0.0.1"}:${toString (consulCfg.ports.http or 8500)}";
in in
{ {
options.systemd.services = mkOption { options.systemd.services = mkOption {
@ -36,6 +39,7 @@ in
[Service] [Service]
ExecStart= ExecStart=
ExecStart=${consul}/bin/consul lock --name=${n} --n=${toString cfg.replicas} --shell=false --child-exit-code 'services/${n}%i' ${optionalString (cfg.registerService != null) runWithRegistration} ${ExecStart} ExecStart=${consul}/bin/consul lock --name=${n} --n=${toString cfg.replicas} --shell=false --child-exit-code 'services/${n}%i' ${optionalString (cfg.registerService != null) runWithRegistration} ${ExecStart}
Environment="CONSUL_HTTP_ADDR=${consulHttpAddr}"
${optionalString (v.serviceConfig ? RestrictAddressFamilies) "RestrictAddressFamilies=AF_NETLINK"} ${optionalString (v.serviceConfig ? RestrictAddressFamilies) "RestrictAddressFamilies=AF_NETLINK"}
${optionalString (cfg.registerService != null) "ExecStopPost=${svc.commands.deregister}"} ${optionalString (cfg.registerService != null) "ExecStopPost=${svc.commands.deregister}"}
'')) ''))

View file

@ -7,13 +7,18 @@ let
consul = "${config.services.consul.package}/bin/consul"; consul = "${config.services.consul.package}/bin/consul";
consulCfg = config.services.consul.extraConfig;
consulHttpAddr = "${consulCfg.addresses.http or "127.0.0.1"}:${toString (consulCfg.ports.http or 8500)}";
consulRegisterScript = pkgs.writeShellScript "consul-register" '' consulRegisterScript = pkgs.writeShellScript "consul-register" ''
export CONSUL_HTTP_ADDR='${consulHttpAddr}'
while ! ${consul} services register "$1"; do while ! ${consul} services register "$1"; do
sleep 1 sleep 1
done done
''; '';
consulDeregisterScript = pkgs.writeShellScript "consul-deregister" '' consulDeregisterScript = pkgs.writeShellScript "consul-deregister" ''
export CONSUL_HTTP_ADDR='${consulHttpAddr}'
for i in {1..5}; do for i in {1..5}; do
if ${consul} services deregister "$1"; then if ${consul} services deregister "$1"; then
break break