2023-03-06 00:50:26 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2023-03-05 23:50:01 +02:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.consul;
|
|
|
|
|
|
|
|
consul = "${config.services.consul.package}/bin/consul";
|
|
|
|
|
2023-09-03 22:20:40 +03:00
|
|
|
consulCfg = config.services.consul.extraConfig;
|
|
|
|
consulHttpAddr = "${consulCfg.addresses.http or "127.0.0.1"}:${toString (consulCfg.ports.http or 8500)}";
|
|
|
|
|
2023-08-27 17:40:11 +03:00
|
|
|
consulRegisterScript = pkgs.writeShellScript "consul-register" ''
|
2023-09-03 22:20:40 +03:00
|
|
|
export CONSUL_HTTP_ADDR='${consulHttpAddr}'
|
2023-08-27 17:40:11 +03:00
|
|
|
while ! ${consul} services register "$1"; do
|
2023-04-15 01:47:57 +03:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
|
2023-08-27 17:40:11 +03:00
|
|
|
consulDeregisterScript = pkgs.writeShellScript "consul-deregister" ''
|
2023-09-03 22:20:40 +03:00
|
|
|
export CONSUL_HTTP_ADDR='${consulHttpAddr}'
|
2023-08-27 17:40:11 +03:00
|
|
|
for i in {1..5}; do
|
|
|
|
if ${consul} services deregister "$1"; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
'';
|
2023-04-15 01:47:57 +03:00
|
|
|
|
|
|
|
register = servicesJson: "${consulRegisterScript} ${servicesJson}";
|
|
|
|
|
|
|
|
deregister = servicesJson: "${consulDeregisterScript} ${servicesJson}";
|
|
|
|
|
2023-06-03 13:45:45 +03:00
|
|
|
writeServicesJson = name: services: pkgs.writeText "consul-services-${name}.json" (builtins.toJSON { inherit services; });
|
|
|
|
|
|
|
|
consulServiceDefinition = types.submodule ({ config, name, ... }: {
|
2023-03-06 00:50:26 +02:00
|
|
|
options = {
|
|
|
|
unit = mkOption {
|
|
|
|
description = "Which systemd service to attach to.";
|
|
|
|
default = name;
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
mode = mkOption {
|
|
|
|
description = "How to attach command executions to the service.";
|
2023-06-03 13:45:45 +03:00
|
|
|
type = types.enum [ "direct" "external" "manual" ];
|
2023-03-06 00:50:26 +02:00
|
|
|
default = "direct";
|
|
|
|
};
|
|
|
|
definition = mkOption {
|
|
|
|
description = "Consul service definition.";
|
|
|
|
type = types.attrs;
|
|
|
|
};
|
2023-06-03 13:45:45 +03:00
|
|
|
commands = {
|
|
|
|
register = mkOption {
|
|
|
|
description = "Command used to register this service.";
|
|
|
|
type = types.str;
|
|
|
|
readOnly = true;
|
|
|
|
};
|
|
|
|
deregister = mkOption {
|
|
|
|
description = "Command used to deregister this service.";
|
|
|
|
type = types.str;
|
|
|
|
readOnly = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config.commands = let
|
|
|
|
servicesJson = writeServicesJson name [ config.definition ];
|
|
|
|
in {
|
|
|
|
register = register servicesJson;
|
|
|
|
deregister = deregister servicesJson;
|
2023-03-05 23:50:01 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-03-06 17:42:14 +02:00
|
|
|
attachToService = unit: servicesRaw: let
|
|
|
|
services = map (getAttr "definition") servicesRaw;
|
2023-06-03 13:45:45 +03:00
|
|
|
servicesJson = writeServicesJson unit services;
|
2023-03-06 17:42:14 +02:00
|
|
|
mode = if any (x: x.mode == "external") servicesRaw then "external" else "direct";
|
2023-03-05 23:50:01 +02:00
|
|
|
in {
|
2023-03-06 00:50:26 +02:00
|
|
|
name = {
|
2023-03-06 17:42:14 +02:00
|
|
|
direct = unit;
|
|
|
|
external = "register-consul-svc-${unit}";
|
|
|
|
}.${mode};
|
2023-03-05 23:50:01 +02:00
|
|
|
value = {
|
2023-03-06 00:50:26 +02:00
|
|
|
direct = {
|
2023-12-02 00:14:50 +02:00
|
|
|
after = [ "consul.service" ];
|
2023-03-06 00:50:26 +02:00
|
|
|
serviceConfig = {
|
2023-04-15 01:47:57 +03:00
|
|
|
ExecStartPost = register servicesJson;
|
|
|
|
ExecStopPost = deregister servicesJson;
|
2023-03-06 00:50:26 +02:00
|
|
|
};
|
2023-03-05 23:50:01 +02:00
|
|
|
};
|
2023-03-06 00:50:26 +02:00
|
|
|
external = {
|
2023-12-02 00:14:50 +02:00
|
|
|
after = [ "consul.service" "${unit}.service" ];
|
2023-03-06 17:42:14 +02:00
|
|
|
wantedBy = [ "${unit}.service" ];
|
|
|
|
unitConfig.BindsTo = "${unit}.service";
|
2023-03-06 00:50:26 +02:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
2023-04-15 01:47:57 +03:00
|
|
|
ExecStart = register servicesJson;
|
|
|
|
ExecStop = deregister servicesJson;
|
2023-03-06 23:26:30 +02:00
|
|
|
Restart = "on-failure";
|
2023-04-15 01:47:57 +03:00
|
|
|
RestartSec = "30s";
|
2023-06-05 21:54:42 +03:00
|
|
|
TimeoutStartSec = "3m";
|
2023-03-06 00:50:26 +02:00
|
|
|
};
|
|
|
|
};
|
2023-03-06 17:42:14 +02:00
|
|
|
}.${mode};
|
2023-03-05 23:50:01 +02:00
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options.consul = {
|
|
|
|
services = mkOption {
|
|
|
|
type = with types; attrsOf consulServiceDefinition;
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-06-03 13:45:45 +03:00
|
|
|
config = lib.mkIf (cfg.services != {}) (let
|
|
|
|
servicesRaw = filter (x: x.mode != "manual") (attrValues cfg.services);
|
|
|
|
in {
|
|
|
|
systemd.services = mapAttrs' attachToService (groupBy (getAttr "unit") servicesRaw);
|
|
|
|
|
2023-03-05 23:50:01 +02:00
|
|
|
warnings = optional (!config.services.consul.enable) "Consul service registrations found, but Consul agent is not enabled on this machine.";
|
2023-06-03 13:45:45 +03:00
|
|
|
});
|
2023-03-05 23:50:01 +02:00
|
|
|
}
|