modules/consul-service-registry: allow binding multiple services to one systemd unit
This commit is contained in:
parent
027d681ede
commit
4d6c88ce97
5 changed files with 20 additions and 18 deletions
|
@ -65,7 +65,7 @@ in {
|
||||||
|
|
||||||
consul.services.pdns = {
|
consul.services.pdns = {
|
||||||
mode = "external";
|
mode = "external";
|
||||||
definition.service = {
|
definition = {
|
||||||
name = "authoritative-dns-backend";
|
name = "authoritative-dns-backend";
|
||||||
address = config.links.localAuthoritativeDNS.ipv4;
|
address = config.links.localAuthoritativeDNS.ipv4;
|
||||||
port = config.links.localAuthoritativeDNS.port;
|
port = config.links.localAuthoritativeDNS.port;
|
||||||
|
|
|
@ -108,7 +108,7 @@ in {
|
||||||
};
|
};
|
||||||
|
|
||||||
consul.services.ngircd = {
|
consul.services.ngircd = {
|
||||||
definition.service = {
|
definition = {
|
||||||
name = "irc";
|
name = "irc";
|
||||||
address = linkSecure.ipv4;
|
address = linkSecure.ipv4;
|
||||||
port = linkSecure.port;
|
port = linkSecure.port;
|
||||||
|
|
|
@ -75,7 +75,7 @@ in
|
||||||
|
|
||||||
consul.services.patroni = {
|
consul.services.patroni = {
|
||||||
mode = "external";
|
mode = "external";
|
||||||
definition.service = rec {
|
definition = rec {
|
||||||
name = "patroni";
|
name = "patroni";
|
||||||
address = getMeshIp vars.hostName;
|
address = getMeshIp vars.hostName;
|
||||||
port = cluster.config.links.patroni-pg-internal.port;
|
port = cluster.config.links.patroni-pg-internal.port;
|
||||||
|
|
|
@ -23,7 +23,7 @@ in {
|
||||||
|
|
||||||
consul.services.nginx = {
|
consul.services.nginx = {
|
||||||
mode = "external";
|
mode = "external";
|
||||||
definition.service = {
|
definition = {
|
||||||
name = "static-lb";
|
name = "static-lb";
|
||||||
address = lib.toLower "${config.networking.hostName}.${config.networking.domain}";
|
address = lib.toLower "${config.networking.hostName}.${config.networking.domain}";
|
||||||
port = 443;
|
port = 443;
|
||||||
|
|
|
@ -26,32 +26,34 @@ let
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
attachToService = name: conf: let
|
attachToService = unit: servicesRaw: let
|
||||||
serviceJson = pkgs.writeText "consul-service-${name}.json" (builtins.toJSON conf.definition);
|
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 {
|
in {
|
||||||
name = {
|
name = {
|
||||||
direct = conf.unit;
|
direct = unit;
|
||||||
external = "register-consul-svc-${conf.unit}";
|
external = "register-consul-svc-${unit}";
|
||||||
}.${conf.mode};
|
}.${mode};
|
||||||
value = {
|
value = {
|
||||||
direct = {
|
direct = {
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
ExecStartPost = "${consul} services register ${serviceJson}";
|
ExecStartPost = "${consul} services register ${servicesJson}";
|
||||||
ExecStopPost = "${consul} services deregister ${serviceJson}";
|
ExecStopPost = "${consul} services deregister ${servicesJson}";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
external = {
|
external = {
|
||||||
after = [ "${conf.unit}.service" ];
|
after = [ "${unit}.service" ];
|
||||||
wantedBy = [ "${conf.unit}.service" ];
|
wantedBy = [ "${unit}.service" ];
|
||||||
unitConfig.BindsTo = "${conf.unit}.service";
|
unitConfig.BindsTo = "${unit}.service";
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "oneshot";
|
Type = "oneshot";
|
||||||
RemainAfterExit = true;
|
RemainAfterExit = true;
|
||||||
ExecStart = "${consul} services register ${serviceJson}";
|
ExecStart = "${consul} services register ${servicesJson}";
|
||||||
ExecStop = "${consul} services deregister ${serviceJson}";
|
ExecStop = "${consul} services deregister ${servicesJson}";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}.${conf.mode};
|
}.${mode};
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
|
|
||||||
|
@ -64,7 +66,7 @@ in
|
||||||
};
|
};
|
||||||
|
|
||||||
config = lib.mkIf (cfg.services != {}) {
|
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.";
|
warnings = optional (!config.services.consul.enable) "Consul service registrations found, but Consul agent is not enabled on this machine.";
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue