modules/consul-distributed-services: init

This commit is contained in:
Max Headroom 2023-08-27 16:25:44 +02:00
parent e81345d4a0
commit 6635ea516d
2 changed files with 45 additions and 0 deletions

View file

@ -0,0 +1,43 @@
{ config, lib, pkgs, ... }:
with lib;
let
consul = config.services.consul.package;
in
{
options.systemd.services = mkOption {
type = with types; attrsOf (submodule ({ config, ... }: let
cfg = config.distributed;
in {
config = mkIf cfg.enable {
};
}));
};
config.systemd.packages = pipe config.systemd.services [
(filterAttrs (_: v: v.distributed.enable))
(mapAttrsToList (n: v: let
inherit (v.serviceConfig) ExecStart;
cfg = v.distributed;
svc = config.consul.services.${cfg.registerService};
runWithRegistration = pkgs.writeShellScript "run-with-registration" ''
trap '${svc.commands.deregister}' EXIT
${svc.commands.register}
''${@}
'';
hasSpecialPrefix = elem (substring 0 1 ExecStart) [ "@" "-" ":" "+" "!" ];
in assert !hasSpecialPrefix; pkgs.writeTextDir "etc/systemd/system/${n}.service.d/distributed.conf" ''
[Service]
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}
${optionalString (v.serviceConfig ? RestrictAddressFamilies) "RestrictAddressFamilies=AF_NETLINK"}
${optionalString (cfg.registerService != null) "ExecStopPost=${svc.commands.deregister}"}
''))
];
}

View file

@ -7,6 +7,7 @@ in
{
flake.nixosModules = with config.flake.nixosModules; {
autopatch = ./autopatch;
consul-distributed-services = ./consul-distributed-services;
consul-service-registry = ./consul-service-registry;
deploy-rs-receiver = ./deploy-rs-receiver;
effect-receiver = ./effect-receiver;
@ -44,6 +45,7 @@ in
serverBase = group [
machineBase
consul-distributed-services
consul-service-registry
deploy-rs-receiver
effect-receiver