Inventory: migrate import and config resolution into a module
This commit is contained in:
@@ -57,100 +57,16 @@ let
|
||||
assertions = { };
|
||||
};
|
||||
|
||||
compileServicesForMachine =
|
||||
# Returns a NixOS configuration for the machine 'machineName'.
|
||||
# Return Format: { imports = [ ... ]; config = { ... }; options = { ... } }
|
||||
legacyResolveImports =
|
||||
{
|
||||
supportedRoles,
|
||||
serviceConfigs,
|
||||
serviceName,
|
||||
machineName,
|
||||
inventory,
|
||||
directory,
|
||||
getRoleFile,
|
||||
}:
|
||||
let
|
||||
compileServiceModules =
|
||||
serviceName: serviceConfigs:
|
||||
let
|
||||
supportedRoles = clan-core.lib.modules.getRoles inventory.modules serviceName;
|
||||
|
||||
firstRole = import (getRoleFile (builtins.head supportedRoles));
|
||||
|
||||
loadModuleForClassCheck =
|
||||
m:
|
||||
if lib.isFunction m then
|
||||
let
|
||||
args = lib.functionArgs m;
|
||||
in
|
||||
m args
|
||||
else
|
||||
m;
|
||||
|
||||
isClanModule =
|
||||
let
|
||||
module = loadModuleForClassCheck firstRole;
|
||||
in
|
||||
if module ? _class then module._class == "clan" else false;
|
||||
|
||||
getRoleFile = role: builtins.seq role inventory.modules.${serviceName} + "/roles/${role}.nix";
|
||||
|
||||
resolvedRolesPerInstance = lib.mapAttrs (
|
||||
instanceName: instanceConfig:
|
||||
let
|
||||
resolvedRoles = lib.genAttrs 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 supportedRoles) usedRoles;
|
||||
in
|
||||
if unmatchedRoles != [ ] then
|
||||
throw ''
|
||||
Service: '${serviceName}' Instance: '${instanceName}'
|
||||
The following roles do not exist: ${builtins.toJSON unmatchedRoles}
|
||||
Please use one of available roles: ${builtins.toJSON 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 resolvedRolesPerInstance);
|
||||
|
||||
matchedRoles = builtins.attrNames (
|
||||
lib.filterAttrs (_: ms: builtins.elem machineName ms) machinesRoles
|
||||
);
|
||||
in
|
||||
# roleImports = lib.mapAttrsToList (
|
||||
# roleName: _: inventory.modules.${serviceName} + "/roles/${roleName}.nix"
|
||||
# ) (lib.filterAttrs (_: ms: builtins.elem machineName ms) machinesRoles);
|
||||
# CompiledService :: { machineImports :: []; machineRoles :: [ String ] }
|
||||
{
|
||||
inherit
|
||||
machinesRoles
|
||||
matchedRoles
|
||||
resolvedRolesPerInstance
|
||||
firstRole
|
||||
isClanModule
|
||||
supportedRoles
|
||||
;
|
||||
# TODO: Add other attributes
|
||||
machineImports =
|
||||
if isClanModule then
|
||||
throw "Clan modules are not supported yet."
|
||||
else
|
||||
(lib.foldlAttrs (
|
||||
# [ Modules ], String, ServiceConfig
|
||||
# : [ Modules ] -> String -> ServiceConfig -> [ Modules ]
|
||||
acc2: instanceName: serviceConfig:
|
||||
let
|
||||
resolvedRoles = lib.genAttrs supportedRoles (
|
||||
@@ -244,15 +160,103 @@ let
|
||||
else
|
||||
acc2
|
||||
) [ ] (serviceConfigs));
|
||||
in
|
||||
{
|
||||
imports = [ ./interface.nix ];
|
||||
config = {
|
||||
machines = builtins.mapAttrs (
|
||||
machineName: machineConfig: m:
|
||||
let
|
||||
compiledServices = lib.mapAttrs (
|
||||
_: serviceConfigs:
|
||||
(
|
||||
{ config, ... }:
|
||||
let
|
||||
serviceName = config.serviceName;
|
||||
loadModuleForClassCheck =
|
||||
m:
|
||||
if lib.isFunction m then
|
||||
let
|
||||
args = lib.functionArgs m;
|
||||
in
|
||||
m args
|
||||
else
|
||||
m;
|
||||
firstRole = import (getRoleFile (builtins.head config.supportedRoles));
|
||||
getRoleFile = role: builtins.seq role inventory.modules.${serviceName} + "/roles/${role}.nix";
|
||||
|
||||
assertions = lib.mapAttrs' (name: value: {
|
||||
name = "checkservice.${serviceName}.${name}";
|
||||
value = {
|
||||
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 ''
|
||||
Service: '${serviceName}' Instance: '${instanceName}'
|
||||
The following roles do not exist: ${builtins.toJSON unmatchedRoles}
|
||||
Please use one of available roles: ${builtins.toJSON config.supportedRoles}
|
||||
''
|
||||
else
|
||||
resolvedRoles
|
||||
) serviceConfigs;
|
||||
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
|
||||
);
|
||||
inherit resolvedRolesPerInstance;
|
||||
isClanModule =
|
||||
let
|
||||
module = loadModuleForClassCheck firstRole;
|
||||
in
|
||||
if module ? _class then module._class == "clan" else false;
|
||||
|
||||
machinesRoles = builtins.zipAttrsWith (
|
||||
_n: vs:
|
||||
let
|
||||
flat = builtins.foldl' (acc: s: acc ++ s.machines) [ ] vs;
|
||||
in
|
||||
lib.unique flat
|
||||
) (builtins.attrValues resolvedRolesPerInstance);
|
||||
|
||||
# The actual result
|
||||
machineImports =
|
||||
if config.isClanModule then
|
||||
throw "Clan modules are not supported yet."
|
||||
else
|
||||
legacyResolveImports {
|
||||
supportedRoles = config.supportedRoles;
|
||||
inherit
|
||||
serviceConfigs
|
||||
serviceName
|
||||
machineName
|
||||
getRoleFile
|
||||
;
|
||||
};
|
||||
|
||||
# Assertions
|
||||
assertions = {
|
||||
"checkservice.${serviceName}" = {
|
||||
assertion = checkService inventory.modules.${serviceName} serviceName;
|
||||
message = ''
|
||||
Service ${serviceName} cannot be used in inventory. It does not declare the 'inventory' feature.
|
||||
|
||||
|
||||
To allow it add the following to the beginning of the README.md of the module:
|
||||
|
||||
---
|
||||
@@ -265,25 +269,11 @@ let
|
||||
|
||||
'';
|
||||
};
|
||||
}) inventory.services;
|
||||
};
|
||||
}
|
||||
)
|
||||
) (config.inventory.services or { });
|
||||
|
||||
in
|
||||
lib.mapAttrs compileServiceModules inventory.services;
|
||||
in
|
||||
{
|
||||
imports = [ ./interface.nix ];
|
||||
config = {
|
||||
machines = builtins.mapAttrs (
|
||||
machineName: machineConfig:
|
||||
let
|
||||
compiledServices = compileServicesForMachine {
|
||||
inherit
|
||||
machineName
|
||||
inventory
|
||||
directory
|
||||
;
|
||||
};
|
||||
compiledMachine = compileMachine {
|
||||
inherit
|
||||
machineConfig
|
||||
@@ -310,7 +300,8 @@ in
|
||||
++ service.machineImports
|
||||
# Import failed assertions
|
||||
++ failedAssertionsImports
|
||||
) [ ] (builtins.attrValues compiledServices);
|
||||
) [ ] (builtins.attrValues m.config.compiledServices);
|
||||
|
||||
in
|
||||
{
|
||||
inherit machineImports compiledServices compiledMachine;
|
||||
|
||||
@@ -13,19 +13,79 @@ in
|
||||
type = types.raw;
|
||||
};
|
||||
machines = mkOption {
|
||||
type = types.attrsOf (submodule {
|
||||
type = types.attrsOf (
|
||||
submodule (
|
||||
{ name, ... }:
|
||||
let
|
||||
machineName = name;
|
||||
in
|
||||
{
|
||||
options = {
|
||||
compiledMachine = mkOption {
|
||||
type = types.raw;
|
||||
};
|
||||
compiledServices = mkOption {
|
||||
type = types.raw;
|
||||
# type = types.attrsOf;
|
||||
type = types.attrsOf (
|
||||
types.submoduleWith {
|
||||
modules = [
|
||||
(
|
||||
{ name, ... }:
|
||||
let
|
||||
serviceName = name;
|
||||
in
|
||||
{
|
||||
options = {
|
||||
machineName = mkOption {
|
||||
default = machineName;
|
||||
readOnly = true;
|
||||
};
|
||||
serviceName = mkOption {
|
||||
default = serviceName;
|
||||
readOnly = true;
|
||||
};
|
||||
# Outputs
|
||||
machineImports = mkOption {
|
||||
type = types.listOf types.raw;
|
||||
};
|
||||
supportedRoles = mkOption {
|
||||
type = types.listOf types.str;
|
||||
};
|
||||
matchedRoles = mkOption {
|
||||
type = types.listOf types.str;
|
||||
};
|
||||
isClanModule = mkOption {
|
||||
type = types.bool;
|
||||
};
|
||||
machinesRoles = mkOption {
|
||||
type = types.attrsOf (types.listOf types.str);
|
||||
};
|
||||
resolvedRolesPerInstance = mkOption {
|
||||
type = types.attrsOf (
|
||||
types.attrsOf (submodule {
|
||||
options.machines = mkOption {
|
||||
type = types.listOf types.str;
|
||||
};
|
||||
})
|
||||
);
|
||||
};
|
||||
assertions = mkOption {
|
||||
type = types.attrsOf types.raw;
|
||||
};
|
||||
};
|
||||
}
|
||||
)
|
||||
];
|
||||
}
|
||||
);
|
||||
};
|
||||
machineImports = mkOption {
|
||||
type = types.listOf types.raw;
|
||||
};
|
||||
};
|
||||
});
|
||||
}
|
||||
)
|
||||
);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user