modules/consul-service-registry: init

This commit is contained in:
Max Headroom 2023-03-05 22:50:01 +01:00
parent 8be69d13ad
commit 674fde3617
2 changed files with 49 additions and 0 deletions

View file

@ -0,0 +1,47 @@
{ config, lib, ... }:
with lib;
let
cfg = config.consul;
consul = "${config.services.consul.package}/bin/consul";
consulServiceDefinition = submodule ({ name, ... }: {
unit = mkOption {
description = "Which systemd service to attach to.";
default = name;
type = types.str;
};
definition = mkOption {
description = "Consul service definition.";
type = types.attrs;
};
});
attachToService = name: conf: let
serviceJson = pkgs.writeText "consul-service-${name}.json" (builtins.toJSON conf.definition);
in {
name = conf.unit;
value = {
serviceConfig = {
ExecStartPost = "${consul} services register ${serviceJson}";
ExecStopPre = "${consul} services deregister ${serviceJson}";
};
};
};
in
{
options.consul = {
services = mkOption {
type = with types; attrsOf consulServiceDefinition;
default = {};
};
};
config = lib.mkIf (cfg.services != {}) {
systemd.services = mapAttrs' attachToService cfg.services;
warnings = optional (!config.services.consul.enable) "Consul service registrations found, but Consul agent is not enabled on this machine.";
};
}

View file

@ -3,6 +3,7 @@ with builtins;
let
aspects = {
autopatch = import ./autopatch;
consul-service-registry = import ./consul-service-registry;
deploy-rs-receiver = import ./deploy-rs-receiver;
enterprise = import ./enterprise;
fail2ban = import ./fail2ban;
@ -41,6 +42,7 @@ in rec {
];
server = [
consul-service-registry
deploy-rs-receiver
fail2ban
motd