Merge pull request 'chore(inventory/services): dont check _class in our logics. Let the error eccour in the final evaluation.' (#3236) from hsjobeki/clan-core:role-settings into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3236
This commit is contained in:
@@ -56,7 +56,7 @@ let
|
||||
assertions = { };
|
||||
};
|
||||
|
||||
legacyResolveImports =
|
||||
resolveImports =
|
||||
{
|
||||
supportedRoles,
|
||||
resolvedRolesPerInstance,
|
||||
@@ -168,27 +168,7 @@ in
|
||||
./roles.nix
|
||||
];
|
||||
|
||||
isClanModule =
|
||||
let
|
||||
firstRole = import (getRoleFile (builtins.head config.supportedRoles));
|
||||
loadModuleForClassCheck =
|
||||
m:
|
||||
if lib.isFunction m then
|
||||
let
|
||||
args = lib.functionArgs m;
|
||||
in
|
||||
m args
|
||||
else
|
||||
m;
|
||||
module = loadModuleForClassCheck (firstRole);
|
||||
in
|
||||
if (module) ? _class then module._class == "clan" else false;
|
||||
# The actual result
|
||||
machineImports =
|
||||
if config.isClanModule then
|
||||
throw "Clan modules are not supported yet."
|
||||
else
|
||||
legacyResolveImports {
|
||||
machineImports = resolveImports {
|
||||
supportedRoles = config.supportedRoles;
|
||||
resolvedRolesPerInstance = config.resolvedRolesPerInstance;
|
||||
inherit
|
||||
|
||||
@@ -54,9 +54,6 @@ in
|
||||
matchedRoles = mkOption {
|
||||
type = types.listOf types.str;
|
||||
};
|
||||
isClanModule = mkOption {
|
||||
type = types.bool;
|
||||
};
|
||||
machinesRoles = mkOption {
|
||||
type = types.attrsOf (types.listOf types.str);
|
||||
};
|
||||
|
||||
@@ -14,7 +14,7 @@ in
|
||||
{
|
||||
# Roles resolution
|
||||
# : List String
|
||||
supportedRoles = clanLib.modules.getRoles inventory.modules serviceName;
|
||||
supportedRoles = clanLib.modules.getRoles "inventory.modules" inventory.modules serviceName;
|
||||
matchedRoles = builtins.attrNames (
|
||||
lib.filterAttrs (_: ms: builtins.elem machineName ms) config.machinesRoles
|
||||
);
|
||||
|
||||
@@ -120,22 +120,13 @@ let
|
||||
# TODO: Eagerly check the _class of the resolved module
|
||||
importedModulesEvaluated = lib.mapAttrs (
|
||||
_module_ident: instances:
|
||||
let
|
||||
matchedClass = "clan.service";
|
||||
instance = (builtins.head instances).instance;
|
||||
classCheckedModule =
|
||||
if instance.moduleClass == matchedClass then
|
||||
instance.resolvedModule
|
||||
else
|
||||
(throw ''Module '${instance.module.name}' is not a valid '${matchedClass}' module. Got module with class:${builtins.toJSON instance.moduleClass}'');
|
||||
in
|
||||
(lib.evalModules {
|
||||
class = matchedClass;
|
||||
class = "clan.service";
|
||||
modules =
|
||||
[
|
||||
./service-module.nix
|
||||
# Import the resolved module
|
||||
classCheckedModule
|
||||
# Import the resolved module.
|
||||
(builtins.head instances).instance.resolvedModule
|
||||
]
|
||||
# Include all the instances that correlate to the resolved module
|
||||
++ (builtins.map (v: {
|
||||
|
||||
@@ -53,13 +53,4 @@ in
|
||||
};
|
||||
};
|
||||
};
|
||||
# Currently this should fail
|
||||
# TODO: Can we implement a default wrapper to make migration easy?
|
||||
test_import_local_legacy_module = {
|
||||
expr = (resolve { name = "B"; }).allMachines;
|
||||
expectedError = {
|
||||
type = "ThrownError";
|
||||
msg = "Module 'B' is not a valid 'clan.service' module.*";
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ let
|
||||
roles =
|
||||
if builtins.elem "inventory" frontmatter.features or [ ] then
|
||||
assert lib.isPath module;
|
||||
clanLib.modules.getRoles allModules moduleName
|
||||
clan-core.lib.modules.getRoles "Documentation: inventory.modules" allModules moduleName
|
||||
else
|
||||
[ ];
|
||||
in
|
||||
|
||||
@@ -36,7 +36,7 @@ let
|
||||
lib.evalModules {
|
||||
specialArgs = {
|
||||
inherit moduleName resolvedRoles instanceName;
|
||||
allRoles = getRoles allModules moduleName;
|
||||
allRoles = getRoles "inventory.modules" allModules moduleName;
|
||||
};
|
||||
modules = [
|
||||
(getFrontmatter allModules.${moduleName} moduleName)
|
||||
@@ -56,16 +56,42 @@ let
|
||||
];
|
||||
}).options;
|
||||
|
||||
# This is a legacy function
|
||||
# Old modules needed to define their roles by directory
|
||||
# This means if this function gets anything other than a string/path it will throw
|
||||
getRoles =
|
||||
allModules: serviceName:
|
||||
lib.mapAttrsToList (name: _value: trimExtension name) (
|
||||
lib.filterAttrs (name: type: type == "regular" && lib.hasSuffix ".nix" name) (
|
||||
builtins.readDir (
|
||||
if allModules ? ${serviceName} then
|
||||
allModules.${serviceName} + "/roles"
|
||||
scope: allModules: serviceName:
|
||||
let
|
||||
module =
|
||||
allModules.${serviceName}
|
||||
or (throw "(Legacy) ClanModule not found: '${serviceName}'. Make sure the module is added to ${scope}");
|
||||
moduleType = (lib.typeOf module);
|
||||
checked =
|
||||
if
|
||||
builtins.elem moduleType [
|
||||
"string"
|
||||
"path"
|
||||
]
|
||||
then
|
||||
true
|
||||
else
|
||||
throw "ClanModule not found: '${serviceName}'. Make sure the module is added in the 'clanModules' attribute of clan-core."
|
||||
)
|
||||
throw "(Legacy) ClanModule must be a 'path' or 'string' pointing to a directory: Got 'typeOf inventory.modules.${serviceName}' => ${moduleType} ";
|
||||
modulePath = lib.seq checked module + "/roles";
|
||||
checkedPath =
|
||||
if builtins.pathExists modulePath then
|
||||
modulePath
|
||||
else
|
||||
throw ''
|
||||
(Legacy) ClanModule must have a 'roles' directory'
|
||||
|
||||
Fixes:
|
||||
- Provide a 'roles' subdirectory
|
||||
- Use the newer 'clan.service' modules. (Recommended)
|
||||
'';
|
||||
in
|
||||
lib.seq checkedPath lib.mapAttrsToList (name: _value: trimExtension name) (
|
||||
lib.filterAttrs (name: type: type == "regular" && lib.hasSuffix ".nix" name) (
|
||||
builtins.readDir (checkedPath)
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@ in
|
||||
};
|
||||
expected = {
|
||||
legacyModule = {
|
||||
isClanModule = false;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user