From 1ca0e7f4542fa619137e80e5f908770ccea0038e Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 3 Jun 2023 12:45:45 +0200 Subject: [PATCH] modules/consul-service-registry: implement manual registration --- modules/consul-service-registry/default.nix | 35 +++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/modules/consul-service-registry/default.nix b/modules/consul-service-registry/default.nix index 93ce290..b2ec47c 100644 --- a/modules/consul-service-registry/default.nix +++ b/modules/consul-service-registry/default.nix @@ -21,7 +21,9 @@ let deregister = servicesJson: "${consulDeregisterScript} ${servicesJson}"; - consulServiceDefinition = types.submodule ({ name, ... }: { + writeServicesJson = name: services: pkgs.writeText "consul-services-${name}.json" (builtins.toJSON { inherit services; }); + + consulServiceDefinition = types.submodule ({ config, name, ... }: { options = { unit = mkOption { description = "Which systemd service to attach to."; @@ -30,19 +32,37 @@ let }; mode = mkOption { description = "How to attach command executions to the service."; - type = types.enum [ "direct" "external" ]; + type = types.enum [ "direct" "external" "manual" ]; default = "direct"; }; definition = mkOption { description = "Consul service definition."; type = types.attrs; }; + 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; }; }); attachToService = unit: servicesRaw: let services = map (getAttr "definition") servicesRaw; - servicesJson = pkgs.writeText "consul-services-${unit}.json" (builtins.toJSON { inherit services; }); + servicesJson = writeServicesJson unit services; mode = if any (x: x.mode == "external") servicesRaw then "external" else "direct"; in { name = { @@ -81,8 +101,11 @@ in }; }; - config = lib.mkIf (cfg.services != {}) { - systemd.services = mapAttrs' attachToService (groupBy (getAttr "unit") (attrValues cfg.services)); + config = lib.mkIf (cfg.services != {}) (let + servicesRaw = filter (x: x.mode != "manual") (attrValues cfg.services); + in { + systemd.services = mapAttrs' attachToService (groupBy (getAttr "unit") servicesRaw); + warnings = optional (!config.services.consul.enable) "Consul service registrations found, but Consul agent is not enabled on this machine."; - }; + }); }