Inventory: migrate import and config resolution into a module
This commit is contained in:
@@ -57,233 +57,223 @@ let
|
|||||||
assertions = { };
|
assertions = { };
|
||||||
};
|
};
|
||||||
|
|
||||||
compileServicesForMachine =
|
legacyResolveImports =
|
||||||
# Returns a NixOS configuration for the machine 'machineName'.
|
|
||||||
# Return Format: { imports = [ ... ]; config = { ... }; options = { ... } }
|
|
||||||
{
|
{
|
||||||
|
supportedRoles,
|
||||||
|
serviceConfigs,
|
||||||
|
serviceName,
|
||||||
machineName,
|
machineName,
|
||||||
inventory,
|
getRoleFile,
|
||||||
directory,
|
|
||||||
}:
|
}:
|
||||||
let
|
(lib.foldlAttrs (
|
||||||
compileServiceModules =
|
# : [ Modules ] -> String -> ServiceConfig -> [ Modules ]
|
||||||
serviceName: serviceConfigs:
|
acc2: instanceName: serviceConfig:
|
||||||
let
|
let
|
||||||
supportedRoles = clan-core.lib.modules.getRoles inventory.modules serviceName;
|
resolvedRoles = lib.genAttrs supportedRoles (
|
||||||
|
roleName:
|
||||||
|
resolveTags {
|
||||||
|
members = serviceConfig.roles.${roleName} or { };
|
||||||
|
inherit
|
||||||
|
serviceName
|
||||||
|
instanceName
|
||||||
|
roleName
|
||||||
|
inventory
|
||||||
|
;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
firstRole = import (getRoleFile (builtins.head supportedRoles));
|
isInService = builtins.any (members: builtins.elem machineName members.machines) (
|
||||||
|
builtins.attrValues resolvedRoles
|
||||||
|
);
|
||||||
|
|
||||||
loadModuleForClassCheck =
|
# all roles where the machine is present
|
||||||
m:
|
machineRoles = builtins.attrNames (
|
||||||
if lib.isFunction m then
|
lib.filterAttrs (_role: roleConfig: builtins.elem machineName roleConfig.machines) resolvedRoles
|
||||||
let
|
);
|
||||||
args = lib.functionArgs m;
|
machineServiceConfig = (serviceConfig.machines.${machineName} or { }).config or { };
|
||||||
in
|
globalConfig = serviceConfig.config or { };
|
||||||
m args
|
|
||||||
else
|
|
||||||
m;
|
|
||||||
|
|
||||||
isClanModule =
|
globalExtraModules = serviceConfig.extraModules or [ ];
|
||||||
let
|
machineExtraModules = serviceConfig.machines.${machineName}.extraModules or [ ];
|
||||||
module = loadModuleForClassCheck firstRole;
|
roleServiceExtraModules = builtins.foldl' (
|
||||||
in
|
acc: role: acc ++ serviceConfig.roles.${role}.extraModules or [ ]
|
||||||
if module ? _class then module._class == "clan" else false;
|
) [ ] machineRoles;
|
||||||
|
|
||||||
getRoleFile = role: builtins.seq role inventory.modules.${serviceName} + "/roles/${role}.nix";
|
# TODO: maybe optimize this dont lookup the role in inverse roles. Imports are not lazy
|
||||||
|
roleModules = builtins.map (
|
||||||
|
role:
|
||||||
|
if builtins.elem role supportedRoles && inventory.modules ? ${serviceName} then
|
||||||
|
getRoleFile role
|
||||||
|
else
|
||||||
|
throw "Module ${serviceName} doesn't have role: '${role}'. Role: ${
|
||||||
|
inventory.modules.${serviceName}
|
||||||
|
}/roles/${role}.nix not found."
|
||||||
|
) machineRoles;
|
||||||
|
|
||||||
resolvedRolesPerInstance = lib.mapAttrs (
|
roleServiceConfigs = builtins.filter (m: m != { }) (
|
||||||
instanceName: instanceConfig:
|
builtins.map (role: serviceConfig.roles.${role}.config or { }) machineRoles
|
||||||
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 (
|
extraModules = map (s: if builtins.typeOf s == "string" then "${directory}/${s}" else s) (
|
||||||
_n: vs:
|
globalExtraModules ++ machineExtraModules ++ roleServiceExtraModules
|
||||||
let
|
);
|
||||||
flat = builtins.foldl' (acc: s: acc ++ s.machines) [ ] vs;
|
|
||||||
in
|
|
||||||
lib.unique flat
|
|
||||||
) (builtins.attrValues resolvedRolesPerInstance);
|
|
||||||
|
|
||||||
matchedRoles = builtins.attrNames (
|
nonExistingRoles = builtins.filter (role: !(builtins.elem role supportedRoles)) (
|
||||||
lib.filterAttrs (_: ms: builtins.elem machineName ms) machinesRoles
|
builtins.attrNames (serviceConfig.roles or { })
|
||||||
);
|
);
|
||||||
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
|
|
||||||
acc2: instanceName: serviceConfig:
|
|
||||||
let
|
|
||||||
resolvedRoles = lib.genAttrs supportedRoles (
|
|
||||||
roleName:
|
|
||||||
resolveTags {
|
|
||||||
members = serviceConfig.roles.${roleName} or { };
|
|
||||||
inherit
|
|
||||||
serviceName
|
|
||||||
instanceName
|
|
||||||
roleName
|
|
||||||
inventory
|
|
||||||
;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
isInService = builtins.any (members: builtins.elem machineName members.machines) (
|
constraintAssertions = clan-core.lib.modules.checkConstraints {
|
||||||
builtins.attrValues resolvedRoles
|
moduleName = serviceName;
|
||||||
);
|
allModules = inventory.modules;
|
||||||
|
inherit resolvedRoles instanceName;
|
||||||
# all roles where the machine is present
|
|
||||||
machineRoles = builtins.attrNames (
|
|
||||||
lib.filterAttrs (_role: roleConfig: builtins.elem machineName roleConfig.machines) resolvedRoles
|
|
||||||
);
|
|
||||||
machineServiceConfig = (serviceConfig.machines.${machineName} or { }).config or { };
|
|
||||||
globalConfig = serviceConfig.config or { };
|
|
||||||
|
|
||||||
globalExtraModules = serviceConfig.extraModules or [ ];
|
|
||||||
machineExtraModules = serviceConfig.machines.${machineName}.extraModules or [ ];
|
|
||||||
roleServiceExtraModules = builtins.foldl' (
|
|
||||||
acc: role: acc ++ serviceConfig.roles.${role}.extraModules or [ ]
|
|
||||||
) [ ] machineRoles;
|
|
||||||
|
|
||||||
# TODO: maybe optimize this dont lookup the role in inverse roles. Imports are not lazy
|
|
||||||
roleModules = builtins.map (
|
|
||||||
role:
|
|
||||||
if builtins.elem role supportedRoles && inventory.modules ? ${serviceName} then
|
|
||||||
getRoleFile role
|
|
||||||
else
|
|
||||||
throw "Module ${serviceName} doesn't have role: '${role}'. Role: ${
|
|
||||||
inventory.modules.${serviceName}
|
|
||||||
}/roles/${role}.nix not found."
|
|
||||||
) machineRoles;
|
|
||||||
|
|
||||||
roleServiceConfigs = builtins.filter (m: m != { }) (
|
|
||||||
builtins.map (role: serviceConfig.roles.${role}.config or { }) machineRoles
|
|
||||||
);
|
|
||||||
|
|
||||||
extraModules = map (s: if builtins.typeOf s == "string" then "${directory}/${s}" else s) (
|
|
||||||
globalExtraModules ++ machineExtraModules ++ roleServiceExtraModules
|
|
||||||
);
|
|
||||||
|
|
||||||
nonExistingRoles = builtins.filter (role: !(builtins.elem role supportedRoles)) (
|
|
||||||
builtins.attrNames (serviceConfig.roles or { })
|
|
||||||
);
|
|
||||||
|
|
||||||
constraintAssertions = clan-core.lib.modules.checkConstraints {
|
|
||||||
moduleName = serviceName;
|
|
||||||
allModules = inventory.modules;
|
|
||||||
inherit resolvedRoles instanceName;
|
|
||||||
};
|
|
||||||
in
|
|
||||||
if (nonExistingRoles != [ ]) then
|
|
||||||
throw "Roles ${builtins.toString nonExistingRoles} are not defined in the service ${serviceName}."
|
|
||||||
else if !(serviceConfig.enabled or true) then
|
|
||||||
acc2
|
|
||||||
else if isInService then
|
|
||||||
acc2
|
|
||||||
++ [
|
|
||||||
{
|
|
||||||
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 != [ ])
|
|
||||||
{
|
|
||||||
clan.${serviceName} = lib.mkMerge (
|
|
||||||
[
|
|
||||||
globalConfig
|
|
||||||
machineServiceConfig
|
|
||||||
]
|
|
||||||
++ roleServiceConfigs
|
|
||||||
);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
]
|
|
||||||
else
|
|
||||||
acc2
|
|
||||||
) [ ] (serviceConfigs));
|
|
||||||
|
|
||||||
assertions = lib.mapAttrs' (name: value: {
|
|
||||||
name = "checkservice.${serviceName}.${name}";
|
|
||||||
value = {
|
|
||||||
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:
|
|
||||||
|
|
||||||
---
|
|
||||||
...
|
|
||||||
|
|
||||||
features = [ "inventory" ]
|
|
||||||
---
|
|
||||||
|
|
||||||
Also make sure to test the module with the 'inventory' feature enabled.
|
|
||||||
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
}) inventory.services;
|
|
||||||
};
|
};
|
||||||
|
in
|
||||||
|
if (nonExistingRoles != [ ]) then
|
||||||
|
throw "Roles ${builtins.toString nonExistingRoles} are not defined in the service ${serviceName}."
|
||||||
|
else if !(serviceConfig.enabled or true) then
|
||||||
|
acc2
|
||||||
|
else if isInService then
|
||||||
|
acc2
|
||||||
|
++ [
|
||||||
|
{
|
||||||
|
imports = roleModules ++ extraModules;
|
||||||
|
|
||||||
in
|
clan.inventory.assertions = constraintAssertions;
|
||||||
lib.mapAttrs compileServiceModules inventory.services;
|
clan.inventory.services.${serviceName}.${instanceName} = {
|
||||||
|
roles = resolvedRoles;
|
||||||
|
# TODO: Add inverseRoles to the service config if needed
|
||||||
|
# inherit inverseRoles;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
(lib.optionalAttrs (globalConfig != { } || machineServiceConfig != { } || roleServiceConfigs != [ ])
|
||||||
|
{
|
||||||
|
clan.${serviceName} = lib.mkMerge (
|
||||||
|
[
|
||||||
|
globalConfig
|
||||||
|
machineServiceConfig
|
||||||
|
]
|
||||||
|
++ roleServiceConfigs
|
||||||
|
);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
]
|
||||||
|
else
|
||||||
|
acc2
|
||||||
|
) [ ] (serviceConfigs));
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
imports = [ ./interface.nix ];
|
imports = [ ./interface.nix ];
|
||||||
config = {
|
config = {
|
||||||
machines = builtins.mapAttrs (
|
machines = builtins.mapAttrs (
|
||||||
machineName: machineConfig:
|
machineName: machineConfig: m:
|
||||||
let
|
let
|
||||||
compiledServices = compileServicesForMachine {
|
compiledServices = lib.mapAttrs (
|
||||||
inherit
|
_: serviceConfigs:
|
||||||
machineName
|
(
|
||||||
inventory
|
{ config, ... }:
|
||||||
directory
|
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";
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
---
|
||||||
|
...
|
||||||
|
|
||||||
|
features = [ "inventory" ]
|
||||||
|
---
|
||||||
|
|
||||||
|
Also make sure to test the module with the 'inventory' feature enabled.
|
||||||
|
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
)
|
||||||
|
) (config.inventory.services or { });
|
||||||
|
|
||||||
compiledMachine = compileMachine {
|
compiledMachine = compileMachine {
|
||||||
inherit
|
inherit
|
||||||
machineConfig
|
machineConfig
|
||||||
@@ -310,7 +300,8 @@ in
|
|||||||
++ service.machineImports
|
++ service.machineImports
|
||||||
# Import failed assertions
|
# Import failed assertions
|
||||||
++ failedAssertionsImports
|
++ failedAssertionsImports
|
||||||
) [ ] (builtins.attrValues compiledServices);
|
) [ ] (builtins.attrValues m.config.compiledServices);
|
||||||
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
inherit machineImports compiledServices compiledMachine;
|
inherit machineImports compiledServices compiledMachine;
|
||||||
|
|||||||
@@ -13,19 +13,79 @@ in
|
|||||||
type = types.raw;
|
type = types.raw;
|
||||||
};
|
};
|
||||||
machines = mkOption {
|
machines = mkOption {
|
||||||
type = types.attrsOf (submodule {
|
type = types.attrsOf (
|
||||||
options = {
|
submodule (
|
||||||
compiledMachine = mkOption {
|
{ name, ... }:
|
||||||
type = types.raw;
|
let
|
||||||
};
|
machineName = name;
|
||||||
compiledServices = mkOption {
|
in
|
||||||
type = types.raw;
|
{
|
||||||
};
|
options = {
|
||||||
machineImports = mkOption {
|
compiledMachine = mkOption {
|
||||||
type = types.listOf types.raw;
|
type = types.raw;
|
||||||
};
|
};
|
||||||
};
|
compiledServices = mkOption {
|
||||||
});
|
# 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