Inventory/constraints: add id's to inventory constraints to make them more observable
This commit is contained in:
@@ -2,53 +2,51 @@
|
|||||||
lib,
|
lib,
|
||||||
config,
|
config,
|
||||||
resolvedRoles,
|
resolvedRoles,
|
||||||
|
instanceName,
|
||||||
moduleName,
|
moduleName,
|
||||||
...
|
...
|
||||||
}:
|
}:
|
||||||
|
let
|
||||||
|
inherit (config) roles;
|
||||||
|
in
|
||||||
{
|
{
|
||||||
imports = [
|
imports = [
|
||||||
./interface.nix
|
./interface.nix
|
||||||
];
|
# Role assertions
|
||||||
config.assertions = lib.foldl' (
|
{
|
||||||
ass: roleName:
|
config.assertions = lib.foldlAttrs (
|
||||||
|
ass: roleName: roleConstraints:
|
||||||
let
|
let
|
||||||
roleConstraints = config.roles.${roleName};
|
|
||||||
members = resolvedRoles.${roleName}.machines;
|
members = resolvedRoles.${roleName}.machines;
|
||||||
memberCount = builtins.length members;
|
memberCount = builtins.length members;
|
||||||
# Checks
|
# Checks
|
||||||
eqCheck =
|
minCheck = lib.optionalAttrs (roleConstraints.min > 0) {
|
||||||
if roleConstraints.eq != null then
|
"${moduleName}.${instanceName}.roles.${roleName}.min" = {
|
||||||
[
|
|
||||||
{
|
|
||||||
assertion = memberCount == roleConstraints.eq;
|
|
||||||
message = "The ${moduleName} module requires exactly ${builtins.toString roleConstraints.eq} '${roleName}', but found ${builtins.toString memberCount}: ${builtins.toString members}";
|
|
||||||
}
|
|
||||||
]
|
|
||||||
else
|
|
||||||
[ ];
|
|
||||||
|
|
||||||
minCheck =
|
|
||||||
if roleConstraints.min > 0 then
|
|
||||||
[
|
|
||||||
{
|
|
||||||
assertion = memberCount >= roleConstraints.min;
|
assertion = memberCount >= roleConstraints.min;
|
||||||
message = "The ${moduleName} module requires at least ${builtins.toString roleConstraints.min} '${roleName}'s, but found ${builtins.toString memberCount}: ${builtins.toString members}";
|
message = ''
|
||||||
}
|
The ${moduleName} module requires at least ${builtins.toString roleConstraints.min} '${roleName}'s
|
||||||
]
|
but found '${builtins.toString memberCount}' within instance '${instanceName}':
|
||||||
else
|
|
||||||
[ ];
|
|
||||||
|
|
||||||
maxCheck =
|
${lib.concatLines members}
|
||||||
if roleConstraints.max != null then
|
'';
|
||||||
[
|
};
|
||||||
{
|
};
|
||||||
|
|
||||||
|
maxCheck = lib.optionalAttrs (roleConstraints.max != null) {
|
||||||
|
"${moduleName}.${instanceName}.roles.${roleName}.max" = {
|
||||||
assertion = memberCount <= roleConstraints.max;
|
assertion = memberCount <= roleConstraints.max;
|
||||||
message = "The ${moduleName} module allows at most for ${builtins.toString roleConstraints.max} '${roleName}'s, but found ${builtins.toString memberCount}: ${builtins.toString members}";
|
message = ''
|
||||||
}
|
The ${moduleName} module allows at most for ${builtins.toString roleConstraints.max} '${roleName}'s
|
||||||
]
|
but found '${builtins.toString memberCount}' within instance '${instanceName}':
|
||||||
else
|
|
||||||
[ ];
|
${lib.concatLines members}
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
in
|
in
|
||||||
eqCheck ++ minCheck ++ maxCheck ++ ass
|
ass // maxCheck // minCheck
|
||||||
) [ ] (lib.attrNames config.roles);
|
) { } roles;
|
||||||
|
}
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,19 @@
|
|||||||
{ lib, allRoles, ... }:
|
{
|
||||||
|
lib,
|
||||||
|
allRoles,
|
||||||
|
moduleName,
|
||||||
|
...
|
||||||
|
}:
|
||||||
let
|
let
|
||||||
inherit (lib) mkOption types;
|
inherit (lib) mkOption types;
|
||||||
rolesAttrs = builtins.groupBy lib.id allRoles;
|
rolesAttrs = builtins.groupBy lib.id allRoles;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
|
options.serviceName = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = moduleName;
|
||||||
|
readOnly = true;
|
||||||
|
};
|
||||||
options.roles = lib.mapAttrs (
|
options.roles = lib.mapAttrs (
|
||||||
_name: _:
|
_name: _:
|
||||||
mkOption {
|
mkOption {
|
||||||
@@ -20,10 +30,6 @@ in
|
|||||||
type = types.int;
|
type = types.int;
|
||||||
default = 0;
|
default = 0;
|
||||||
};
|
};
|
||||||
eq = mkOption {
|
|
||||||
type = types.nullOr types.int;
|
|
||||||
default = null;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
@@ -31,10 +37,26 @@ in
|
|||||||
}
|
}
|
||||||
) rolesAttrs;
|
) rolesAttrs;
|
||||||
|
|
||||||
|
options.instances = mkOption {
|
||||||
|
default = { };
|
||||||
|
type = types.submoduleWith {
|
||||||
|
modules = [
|
||||||
|
{
|
||||||
|
options = {
|
||||||
|
max = mkOption {
|
||||||
|
type = types.nullOr types.int;
|
||||||
|
default = null;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
# The resulting assertions
|
# The resulting assertions
|
||||||
options.assertions = mkOption {
|
options.assertions = mkOption {
|
||||||
default = [ ];
|
default = { };
|
||||||
type = types.listOf (
|
type = types.attrsOf (
|
||||||
types.submoduleWith {
|
types.submoduleWith {
|
||||||
modules = [
|
modules = [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,27 +1,26 @@
|
|||||||
{ clan-core, lib }:
|
{ clan-core, lib }:
|
||||||
let
|
let
|
||||||
getRoles =
|
trimExtension = name: builtins.substring 0 (builtins.stringLength name - 4) name;
|
||||||
modulePath:
|
|
||||||
let
|
getRoles' =
|
||||||
rolesDir = modulePath + "/roles";
|
serviceName:
|
||||||
in
|
lib.mapAttrsToList (name: _value: trimExtension name) (
|
||||||
if builtins.pathExists rolesDir then
|
lib.filterAttrs (name: type: type == "regular" && lib.hasSuffix ".nix" name) (
|
||||||
lib.pipe rolesDir [
|
builtins.readDir (
|
||||||
builtins.readDir
|
if clan-core.clanModules ? ${serviceName} then
|
||||||
(lib.filterAttrs (_n: v: v == "regular"))
|
clan-core.clanModules.${serviceName} + "/roles"
|
||||||
lib.attrNames
|
|
||||||
(lib.filter (fileName: lib.hasSuffix ".nix" fileName))
|
|
||||||
(map (fileName: lib.removeSuffix ".nix" fileName))
|
|
||||||
]
|
|
||||||
else
|
else
|
||||||
[ ];
|
throw "ClanModule not found: '${serviceName}'. Make sure the module is added in the 'clanModules' attribute of clan-core."
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
getConstraints =
|
getConstraints =
|
||||||
modulename:
|
modulename:
|
||||||
let
|
let
|
||||||
eval = lib.evalModules {
|
eval = lib.evalModules {
|
||||||
specialArgs = {
|
specialArgs = {
|
||||||
allRoles = getRoles clan-core.clanModules.${modulename};
|
allRoles = getRoles' modulename;
|
||||||
};
|
};
|
||||||
modules = [
|
modules = [
|
||||||
./constraints/interface.nix
|
./constraints/interface.nix
|
||||||
@@ -32,23 +31,22 @@ let
|
|||||||
eval.config.roles;
|
eval.config.roles;
|
||||||
|
|
||||||
checkConstraints =
|
checkConstraints =
|
||||||
{ moduleName, resolvedRoles }:
|
{
|
||||||
|
moduleName,
|
||||||
|
resolvedRoles,
|
||||||
|
instanceNames,
|
||||||
|
instanceName,
|
||||||
|
}:
|
||||||
let
|
let
|
||||||
eval = lib.evalModules {
|
eval = lib.evalModules {
|
||||||
specialArgs = {
|
specialArgs = {
|
||||||
inherit moduleName;
|
inherit
|
||||||
allRoles = getRoles clan-core.clanModules.${moduleName};
|
moduleName
|
||||||
resolvedRoles = {
|
instanceNames
|
||||||
controller = {
|
instanceName
|
||||||
machines = [ "test-inventory-machine" ];
|
resolvedRoles
|
||||||
};
|
;
|
||||||
moon = {
|
allRoles = getRoles' moduleName;
|
||||||
machines = [ ];
|
|
||||||
};
|
|
||||||
peer = {
|
|
||||||
machines = [ ];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
modules = [
|
modules = [
|
||||||
./constraints/default.nix
|
./constraints/default.nix
|
||||||
@@ -101,7 +99,7 @@ in
|
|||||||
inherit
|
inherit
|
||||||
getFrontmatter
|
getFrontmatter
|
||||||
getReadme
|
getReadme
|
||||||
getRoles
|
getRoles'
|
||||||
getConstraints
|
getConstraints
|
||||||
checkConstraints
|
checkConstraints
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -55,15 +55,12 @@ let
|
|||||||
evalClanModulesWithRoles =
|
evalClanModulesWithRoles =
|
||||||
clanModules:
|
clanModules:
|
||||||
let
|
let
|
||||||
getRoles = clan-core.lib.modules.getRoles;
|
|
||||||
res = builtins.mapAttrs (
|
res = builtins.mapAttrs (
|
||||||
moduleName: module:
|
moduleName: module:
|
||||||
let
|
let
|
||||||
# module must be a path to the clanModule root by convention
|
|
||||||
# See: clanModules/flake-module.nix
|
|
||||||
roles =
|
roles =
|
||||||
assert lib.isPath module;
|
assert lib.isPath module;
|
||||||
getRoles module;
|
clan-core.lib.modules.getRoles' moduleName;
|
||||||
in
|
in
|
||||||
lib.listToAttrs (
|
lib.listToAttrs (
|
||||||
lib.map (role: {
|
lib.map (role: {
|
||||||
|
|||||||
@@ -41,39 +41,60 @@ let
|
|||||||
serviceName:
|
serviceName:
|
||||||
builtins.elem "inventory" (clan-core.lib.modules.getFrontmatter serviceName).features or [ ];
|
builtins.elem "inventory" (clan-core.lib.modules.getFrontmatter serviceName).features or [ ];
|
||||||
|
|
||||||
trimExtension = name: builtins.substring 0 (builtins.stringLength name - 4) name;
|
extendMachine =
|
||||||
|
{ machineConfig, inventory }:
|
||||||
|
[
|
||||||
|
(lib.optionalAttrs (machineConfig.deploy.targetHost or null != null) {
|
||||||
|
config.clan.core.networking.targetHost = machineConfig.deploy.targetHost;
|
||||||
|
})
|
||||||
|
{
|
||||||
|
assertions = lib.foldlAttrs (
|
||||||
|
acc: serviceName: _serviceConfigs:
|
||||||
|
acc
|
||||||
|
++ [
|
||||||
|
{
|
||||||
|
assertion = checkService serviceName;
|
||||||
|
message = ''
|
||||||
|
Service ${serviceName} cannot be used in inventory. It does not declare the 'inventory' feature.
|
||||||
|
|
||||||
/*
|
|
||||||
Returns a NixOS configuration for every machine in the inventory.
|
|
||||||
|
|
||||||
machinesFromInventory :: Inventory -> { ${machine_name} :: NixOSConfiguration }
|
To allow it add the following to the beginning of the README.md of the module:
|
||||||
*/
|
|
||||||
buildInventory =
|
---
|
||||||
{ inventory, directory }:
|
...
|
||||||
# For every machine in the inventory, build a NixOS configuration
|
|
||||||
# For each machine generate config, forEach service, if the machine is used.
|
features = [ "inventory" ]
|
||||||
builtins.mapAttrs (
|
---
|
||||||
machineName: machineConfig:
|
|
||||||
|
Also make sure to test the module with the 'inventory' feature enabled.
|
||||||
|
|
||||||
|
'';
|
||||||
|
}
|
||||||
|
]
|
||||||
|
) [ ] inventory.services;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
mapMachineConfigToNixOSConfig =
|
||||||
|
# Returns a NixOS configuration for the machine 'machineName'.
|
||||||
|
# Return Format: { imports = [ ... ]; config = { ... }; options = { ... } }
|
||||||
|
{
|
||||||
|
machineName,
|
||||||
|
machineConfig,
|
||||||
|
inventory,
|
||||||
|
directory,
|
||||||
|
}:
|
||||||
lib.foldlAttrs (
|
lib.foldlAttrs (
|
||||||
# [ Modules ], String, { ${instance_name} :: ServiceConfig }
|
# [ Modules ], String, { ${instance_name} :: ServiceConfig }
|
||||||
acc: serviceName: serviceConfigs:
|
initialServiceModules: serviceName: serviceConfigs:
|
||||||
acc
|
initialServiceModules
|
||||||
# Collect service config
|
# Collect service config
|
||||||
++ (lib.foldlAttrs (
|
++ (lib.foldlAttrs (
|
||||||
# [ Modules ], String, ServiceConfig
|
# [ Modules ], String, ServiceConfig
|
||||||
acc2: instanceName: serviceConfig:
|
acc2: instanceName: serviceConfig:
|
||||||
|
|
||||||
let
|
let
|
||||||
roles = lib.mapAttrsToList (name: _value: trimExtension name) (
|
roles = clan-core.lib.modules.getRoles' serviceName;
|
||||||
lib.filterAttrs (name: type: type == "regular" && lib.hasSuffix ".nix" name) (
|
|
||||||
builtins.readDir (
|
|
||||||
if clan-core.clanModules ? ${serviceName} then
|
|
||||||
clan-core.clanModules.${serviceName} + "/roles"
|
|
||||||
else
|
|
||||||
throw "ClanModule not found: '${serviceName}'. Make sure the module is added in the 'clanModules' attribute of clan-core."
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
resolvedRoles = lib.genAttrs roles (
|
resolvedRoles = lib.genAttrs roles (
|
||||||
roleName:
|
roleName:
|
||||||
@@ -127,9 +148,11 @@ let
|
|||||||
nonExistingRoles = builtins.filter (role: !(builtins.elem role roles)) (
|
nonExistingRoles = builtins.filter (role: !(builtins.elem role roles)) (
|
||||||
builtins.attrNames (serviceConfig.roles or { })
|
builtins.attrNames (serviceConfig.roles or { })
|
||||||
);
|
);
|
||||||
|
|
||||||
constraintAssertions = clan-core.lib.modules.checkConstraints {
|
constraintAssertions = clan-core.lib.modules.checkConstraints {
|
||||||
moduleName = serviceName;
|
moduleName = serviceName;
|
||||||
inherit resolvedRoles;
|
inherit resolvedRoles instanceName;
|
||||||
|
instanceNames = builtins.attrNames serviceConfigs;
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
if (nonExistingRoles != [ ]) then
|
if (nonExistingRoles != [ ]) then
|
||||||
@@ -141,10 +164,17 @@ let
|
|||||||
++ [
|
++ [
|
||||||
{
|
{
|
||||||
imports = roleModules ++ extraModules;
|
imports = roleModules ++ extraModules;
|
||||||
|
|
||||||
|
clan.inventory.assertions = constraintAssertions;
|
||||||
|
clan.inventory.services.${serviceName}.${instanceName} = {
|
||||||
|
roles = resolvedRoles;
|
||||||
|
# TODO: Add inverseRoles to the service config if needed
|
||||||
|
# inherit inverseRoles;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
(lib.optionalAttrs (globalConfig != { } || machineServiceConfig != { } || roleServiceConfigs != [ ])
|
(lib.optionalAttrs (globalConfig != { } || machineServiceConfig != { } || roleServiceConfigs != [ ])
|
||||||
{
|
{
|
||||||
config.clan.${serviceName} = lib.mkMerge (
|
clan.${serviceName} = lib.mkMerge (
|
||||||
[
|
[
|
||||||
globalConfig
|
globalConfig
|
||||||
machineServiceConfig
|
machineServiceConfig
|
||||||
@@ -153,51 +183,32 @@ let
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
({
|
|
||||||
assertions = constraintAssertions;
|
|
||||||
clan.inventory.services.${serviceName}.${instanceName} = {
|
|
||||||
roles = resolvedRoles;
|
|
||||||
# TODO: Add inverseRoles to the service config if needed
|
|
||||||
# inherit inverseRoles;
|
|
||||||
};
|
|
||||||
})
|
|
||||||
]
|
]
|
||||||
else
|
else
|
||||||
acc2
|
acc2
|
||||||
) [ ] (serviceConfigs))
|
) [ ] (serviceConfigs))
|
||||||
) [ ] inventory.services
|
) [ ] inventory.services
|
||||||
# Append each machine config
|
# Global extension for each machine
|
||||||
++ [
|
++ (extendMachine { inherit machineConfig inventory; });
|
||||||
(lib.optionalAttrs (machineConfig.deploy.targetHost or null != null) {
|
/*
|
||||||
config.clan.core.networking.targetHost = machineConfig.deploy.targetHost;
|
Returns a NixOS configuration for every machine in the inventory.
|
||||||
})
|
|
||||||
{
|
|
||||||
assertions = lib.foldlAttrs (
|
|
||||||
acc: serviceName: _:
|
|
||||||
acc
|
|
||||||
++ [
|
|
||||||
{
|
|
||||||
assertion = checkService serviceName;
|
|
||||||
message = ''
|
|
||||||
Service ${serviceName} cannot be used in inventory. It does not declare the 'inventory' feature.
|
|
||||||
|
|
||||||
|
machinesFromInventory :: Inventory -> { ${machine_name} :: NixOSConfiguration }
|
||||||
To allow it add the following to the beginning of the README.md of the module:
|
*/
|
||||||
|
buildInventory =
|
||||||
---
|
{ inventory, directory }:
|
||||||
...
|
# For every machine in the inventory, build a NixOS configuration
|
||||||
|
# For each machine generate config, forEach service, if the machine is used.
|
||||||
features = [ "inventory" ]
|
builtins.mapAttrs (
|
||||||
---
|
machineName: machineConfig:
|
||||||
|
mapMachineConfigToNixOSConfig {
|
||||||
Also make sure to test the module with the 'inventory' feature enabled.
|
inherit
|
||||||
|
machineName
|
||||||
'';
|
machineConfig
|
||||||
|
inventory
|
||||||
|
directory
|
||||||
|
;
|
||||||
}
|
}
|
||||||
]
|
|
||||||
) [ ] inventory.services;
|
|
||||||
}
|
|
||||||
]
|
|
||||||
) (inventory.machines or { });
|
) (inventory.machines or { });
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -92,8 +92,8 @@ in
|
|||||||
not_used_machine = builtins.length configs.not_used_machine;
|
not_used_machine = builtins.length configs.not_used_machine;
|
||||||
};
|
};
|
||||||
expected = {
|
expected = {
|
||||||
client_1_machine = 5;
|
client_1_machine = 4;
|
||||||
client_2_machine = 5;
|
client_2_machine = 4;
|
||||||
not_used_machine = 2;
|
not_used_machine = 2;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user