-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request 'inventory: refactor role resolution into submodul…
…e' (#2826) from hsjobeki/clan-core:hsjobeki-main into main Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/2826
- Loading branch information
Showing
5 changed files
with
122 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,7 @@ | |
"lib/flake-module.nix" | ||
"lib/frontmatter" | ||
"lib/inventory" | ||
"lib/constraints" | ||
"nixosModules" | ||
]; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{ | ||
lib, | ||
config, | ||
resolveTags, | ||
inventory, | ||
clan-core, | ||
machineName, | ||
serviceConfigs, | ||
... | ||
}: | ||
let | ||
serviceName = config.serviceName; | ||
in | ||
{ | ||
# Roles resolution | ||
# : List String | ||
supportedRoles = clan-core.lib.modules.getRoles inventory.modules serviceName; | ||
matchedRoles = builtins.attrNames ( | ||
lib.filterAttrs (_: ms: builtins.elem machineName ms) config.machinesRoles | ||
); | ||
resolvedRolesPerInstance = lib.mapAttrs ( | ||
instanceName: instanceConfig: | ||
let | ||
resolvedRoles = lib.genAttrs config.supportedRoles ( | ||
roleName: | ||
resolveTags { | ||
members = instanceConfig.roles.${roleName} or { }; | ||
inherit | ||
instanceName | ||
serviceName | ||
roleName | ||
inventory | ||
; | ||
} | ||
); | ||
usedRoles = builtins.attrNames instanceConfig.roles; | ||
unmatchedRoles = builtins.filter (role: !builtins.elem role config.supportedRoles) usedRoles; | ||
in | ||
if unmatchedRoles != [ ] then | ||
throw '' | ||
Roles ${builtins.toJSON unmatchedRoles} are not defined in the service ${serviceName}. | ||
Instance: '${instanceName}' | ||
Please use one of available roles: ${builtins.toJSON config.supportedRoles} | ||
'' | ||
else | ||
resolvedRoles | ||
) serviceConfigs; | ||
|
||
machinesRoles = builtins.zipAttrsWith ( | ||
_n: vs: | ||
let | ||
flat = builtins.foldl' (acc: s: acc ++ s.machines) [ ] vs; | ||
in | ||
lib.unique flat | ||
) (builtins.attrValues config.resolvedRolesPerInstance); | ||
|
||
assertions = lib.concatMapAttrs ( | ||
instanceName: resolvedRoles: | ||
clan-core.lib.modules.checkConstraints { | ||
moduleName = serviceName; | ||
allModules = inventory.modules; | ||
inherit resolvedRoles instanceName; | ||
} | ||
) config.resolvedRolesPerInstance; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters