From 4d6c88ce97583e1a2bca3a2fbdeb8cee0c0e3f6b Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 6 Mar 2023 16:42:14 +0100 Subject: [PATCH] modules/consul-service-registry: allow binding multiple services to one systemd unit --- cluster/services/dns/authoritative.nix | 2 +- cluster/services/irc/irc-host.nix | 2 +- cluster/services/patroni/worker.nix | 2 +- cluster/services/websites/host.nix | 2 +- modules/consul-service-registry/default.nix | 30 +++++++++++---------- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/cluster/services/dns/authoritative.nix b/cluster/services/dns/authoritative.nix index d821a26..70fccc3 100644 --- a/cluster/services/dns/authoritative.nix +++ b/cluster/services/dns/authoritative.nix @@ -65,7 +65,7 @@ in { consul.services.pdns = { mode = "external"; - definition.service = { + definition = { name = "authoritative-dns-backend"; address = config.links.localAuthoritativeDNS.ipv4; port = config.links.localAuthoritativeDNS.port; diff --git a/cluster/services/irc/irc-host.nix b/cluster/services/irc/irc-host.nix index 6c8b411..97edd45 100644 --- a/cluster/services/irc/irc-host.nix +++ b/cluster/services/irc/irc-host.nix @@ -108,7 +108,7 @@ in { }; consul.services.ngircd = { - definition.service = { + definition = { name = "irc"; address = linkSecure.ipv4; port = linkSecure.port; diff --git a/cluster/services/patroni/worker.nix b/cluster/services/patroni/worker.nix index 72b5a9e..f2ec44f 100644 --- a/cluster/services/patroni/worker.nix +++ b/cluster/services/patroni/worker.nix @@ -75,7 +75,7 @@ in consul.services.patroni = { mode = "external"; - definition.service = rec { + definition = rec { name = "patroni"; address = getMeshIp vars.hostName; port = cluster.config.links.patroni-pg-internal.port; diff --git a/cluster/services/websites/host.nix b/cluster/services/websites/host.nix index e925065..c26d1c1 100644 --- a/cluster/services/websites/host.nix +++ b/cluster/services/websites/host.nix @@ -23,7 +23,7 @@ in { consul.services.nginx = { mode = "external"; - definition.service = { + definition = { name = "static-lb"; address = lib.toLower "${config.networking.hostName}.${config.networking.domain}"; port = 443; diff --git a/modules/consul-service-registry/default.nix b/modules/consul-service-registry/default.nix index 5165ec2..2088d66 100644 --- a/modules/consul-service-registry/default.nix +++ b/modules/consul-service-registry/default.nix @@ -26,32 +26,34 @@ let }; }); - attachToService = name: conf: let - serviceJson = pkgs.writeText "consul-service-${name}.json" (builtins.toJSON conf.definition); + attachToService = unit: servicesRaw: let + services = map (getAttr "definition") servicesRaw; + servicesJson = pkgs.writeText "consul-services-${unit}.json" (builtins.toJSON { inherit services; }); + mode = if any (x: x.mode == "external") servicesRaw then "external" else "direct"; in { name = { - direct = conf.unit; - external = "register-consul-svc-${conf.unit}"; - }.${conf.mode}; + direct = unit; + external = "register-consul-svc-${unit}"; + }.${mode}; value = { direct = { serviceConfig = { - ExecStartPost = "${consul} services register ${serviceJson}"; - ExecStopPost = "${consul} services deregister ${serviceJson}"; + ExecStartPost = "${consul} services register ${servicesJson}"; + ExecStopPost = "${consul} services deregister ${servicesJson}"; }; }; external = { - after = [ "${conf.unit}.service" ]; - wantedBy = [ "${conf.unit}.service" ]; - unitConfig.BindsTo = "${conf.unit}.service"; + after = [ "${unit}.service" ]; + wantedBy = [ "${unit}.service" ]; + unitConfig.BindsTo = "${unit}.service"; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; - ExecStart = "${consul} services register ${serviceJson}"; - ExecStop = "${consul} services deregister ${serviceJson}"; + ExecStart = "${consul} services register ${servicesJson}"; + ExecStop = "${consul} services deregister ${servicesJson}"; }; }; - }.${conf.mode}; + }.${mode}; }; in @@ -64,7 +66,7 @@ in }; config = lib.mkIf (cfg.services != {}) { - systemd.services = mapAttrs' attachToService cfg.services; + systemd.services = mapAttrs' attachToService (groupBy (getAttr "unit") (attrValues cfg.services)); warnings = optional (!config.services.consul.enable) "Consul service registrations found, but Consul agent is not enabled on this machine."; }; }