build-inventory: move inventory and inventoryClass into explizitly different folders
This commit is contained in:
55
lib/modules/inventory/constraints/default.nix
Normal file
55
lib/modules/inventory/constraints/default.nix
Normal file
@@ -0,0 +1,55 @@
|
||||
{
|
||||
resolvedRoles,
|
||||
instanceName,
|
||||
moduleName,
|
||||
allRoles,
|
||||
}:
|
||||
{
|
||||
lib,
|
||||
config,
|
||||
...
|
||||
}:
|
||||
let
|
||||
inherit (config) roles;
|
||||
in
|
||||
{
|
||||
imports = [
|
||||
(lib.modules.importApply ./interface.nix { inherit allRoles; })
|
||||
# Role assertions
|
||||
{
|
||||
config.assertions = lib.foldlAttrs (
|
||||
ass: roleName: roleConstraints:
|
||||
let
|
||||
members = resolvedRoles.${roleName}.machines;
|
||||
memberCount = builtins.length members;
|
||||
# Checks
|
||||
minCheck = lib.optionalAttrs (roleConstraints.min > 0) {
|
||||
"${moduleName}.${instanceName}.roles.${roleName}.min" = {
|
||||
assertion = memberCount >= roleConstraints.min;
|
||||
message = ''
|
||||
The '${moduleName}' module requires at least ${builtins.toString roleConstraints.min} members of the '${roleName}' role
|
||||
but found '${builtins.toString memberCount}' members within instance '${instanceName}':
|
||||
|
||||
${lib.concatLines members}
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
maxCheck = lib.optionalAttrs (roleConstraints.max != null) {
|
||||
"${moduleName}.${instanceName}.roles.${roleName}.max" = {
|
||||
assertion = memberCount <= roleConstraints.max;
|
||||
message = ''
|
||||
The ${moduleName} module allows at most for ${builtins.toString roleConstraints.max} members of the '${roleName}' role
|
||||
but found '${builtins.toString memberCount}' members within instance '${instanceName}':
|
||||
|
||||
${lib.concatLines members}
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
in
|
||||
ass // maxCheck // minCheck
|
||||
) { } roles;
|
||||
}
|
||||
];
|
||||
}
|
||||
66
lib/modules/inventory/constraints/interface.nix
Normal file
66
lib/modules/inventory/constraints/interface.nix
Normal file
@@ -0,0 +1,66 @@
|
||||
{
|
||||
allRoles,
|
||||
}:
|
||||
{
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
let
|
||||
inherit (lib) mkOption types;
|
||||
rolesAttrs = builtins.groupBy lib.id allRoles;
|
||||
in
|
||||
{
|
||||
options.roles = lib.mapAttrs (
|
||||
_name: _:
|
||||
mkOption {
|
||||
description = ''
|
||||
Sub-attributes of `${_name}` are constraints for the role.
|
||||
'';
|
||||
default = { };
|
||||
type = types.submoduleWith {
|
||||
modules = [
|
||||
{
|
||||
options = {
|
||||
max = mkOption {
|
||||
type = types.nullOr types.int;
|
||||
default = null;
|
||||
description = ''
|
||||
Maximum number of instances of this role that can be assigned to a module of this type.
|
||||
'';
|
||||
};
|
||||
min = mkOption {
|
||||
type = types.int;
|
||||
default = 0;
|
||||
description = ''
|
||||
Minimum number of instances of this role that must at least be assigned to a module of this type.
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
) rolesAttrs;
|
||||
|
||||
# The resulting assertions
|
||||
options.assertions = mkOption {
|
||||
visible = false;
|
||||
default = { };
|
||||
type = types.attrsOf (
|
||||
types.submoduleWith {
|
||||
modules = [
|
||||
{
|
||||
options = {
|
||||
assertion = mkOption {
|
||||
type = types.bool;
|
||||
};
|
||||
message = mkOption {
|
||||
type = types.str;
|
||||
};
|
||||
};
|
||||
}
|
||||
];
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user