diff --git a/devices/cluster/modules/pacemaker/default.nix b/devices/cluster/modules/pacemaker/default.nix index 4c30cb1..7d530a1 100644 --- a/devices/cluster/modules/pacemaker/default.nix +++ b/devices/cluster/modules/pacemaker/default.nix @@ -13,7 +13,13 @@ resources = { "caddy" = { enable = true; - virtualIp = "10.0.0.130"; + virtualIps = [ + { + id = "main"; + interface = "eno1"; + ip = "10.0.0.130"; + } + ]; }; }; }; diff --git a/devices/cluster/modules/pacemaker/options.nix b/devices/cluster/modules/pacemaker/options.nix index e6669e0..934346d 100644 --- a/devices/cluster/modules/pacemaker/options.nix +++ b/devices/cluster/modules/pacemaker/options.nix @@ -12,6 +12,7 @@ concatMapStringsSep elemAt filterAttrs + isAttrs mkIf mkOption types @@ -25,11 +26,6 @@ in { imports = ["${nixpkgs-pacemaker}/nixos/modules/${pacemakerPath}"]; options.services.pacemaker = { - networkInterface = mkOption { - default = "eno1"; - type = types.str; - }; - resources = mkOption { default = {}; type = with types; @@ -45,14 +41,36 @@ in { type = types.str; }; - virtualIp = mkOption { - default = null; - type = types.nullOr types.str; + # TODO: add assertion to not have same id + virtualIps = mkOption { + default = []; + type = with types; + listOf (submodule { + options = { + id = mkOption { + type = types.str; + }; + + interface = mkOption { + default = "eno1"; + type = types.str; + }; + + ip = mkOption { + type = types.str; + }; + + cidr = mkOption { + default = 24; + type = types.int; + }; + }; + }); }; + # TODO: add assertion, needs to be an existing systemdName dependsOn = mkOption { default = []; - # TODO: implement dependsOn type = types.listOf types.str; }; @@ -64,46 +82,48 @@ in { config = mkIf cfg.enable { systemd.services = let - mkVirtIp = res: '' - - - - - - - - - - - - - ''; + mkVirtIps = res: + concatMapStringsSep "\n" (vip: '' + + + + + + + + + + + + + '') + res.virtualIps; mkSystemdResource = res: '' @@ -115,25 +135,40 @@ in { ''; - mkConstraint = first: res: '' + mkConstraint = res: first: let + firstName = + if isAttrs first + then first.systemdName + else first; + in '' ''; + mkDependsOn = res: let + mkConstraint' = first: + mkConstraint res first; + in + concatMapStringsSep "\n" mkConstraint' res.dependsOn; + mkVipConstraint = res: - mkConstraint - (res.systemdName + "vip") - res; + concatMapStringsSep "\n" ( + vip: + mkConstraint + res + "${res.systemdName}-${vip.id}-vip" + ) + res.virtualIps; # If we're updating resources we have to kill constraints to add new resources constraintsEmpty = toFile "constraints.xml" '' @@ -143,11 +178,11 @@ in { resEnabled = filterAttrs (n: v: v.enable) cfg.resources; - resWithIp = filterAttrs (n: v: ! isNull v.virtualIp) resEnabled; + resWithIp = filterAttrs (n: v: ! isNull v.virtualIps) resEnabled; resources = toFile "resources.xml" '' - ${concatMapStringsSep "\n" mkVirtIp (attrValues resWithIp)} + ${concatMapStringsSep "\n" mkVirtIps (attrValues resWithIp)} ${concatMapStringsSep "\n" mkSystemdResource (attrValues resEnabled)} ''; @@ -155,6 +190,7 @@ in { constraints = toFile "constraints.xml" '' ${concatMapStringsSep "\n" mkVipConstraint (attrValues resWithIp)} + ${concatMapStringsSep "\n" mkDependsOn (attrValues resEnabled)} '';