Inventory: refactor build-inventory in more independent parts
This commit is contained in:
@@ -76,7 +76,7 @@ let
|
||||
(machines.${name} or { })
|
||||
# Inherit the inventory assertions ?
|
||||
# { inherit (mergedInventory) assertions; }
|
||||
{ imports = serviceConfigs.${name} or [ ]; }
|
||||
{ imports = serviceConfigs.machines.${name}.machineImports or []; }
|
||||
(
|
||||
{
|
||||
# Settings
|
||||
|
||||
@@ -42,55 +42,30 @@ let
|
||||
builtins.elem "inventory"
|
||||
(clan-core.lib.modules.getFrontmatter modulepath serviceName).features or [ ];
|
||||
|
||||
extendMachine =
|
||||
{ machineConfig, inventory }:
|
||||
[
|
||||
compileMachine =
|
||||
{ machineConfig }:
|
||||
{
|
||||
machineImports = [
|
||||
(lib.optionalAttrs (machineConfig.deploy.targetHost or null != null) {
|
||||
config.clan.core.networking.targetHost = machineConfig.deploy.targetHost;
|
||||
})
|
||||
{
|
||||
assertions = lib.foldlAttrs (
|
||||
acc: serviceName: _serviceConfigs:
|
||||
acc
|
||||
++ [
|
||||
{
|
||||
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;
|
||||
}
|
||||
];
|
||||
assertions = { };
|
||||
};
|
||||
|
||||
mapMachineConfigToNixOSConfig =
|
||||
compileServicesForMachine =
|
||||
# Returns a NixOS configuration for the machine 'machineName'.
|
||||
# Return Format: { imports = [ ... ]; config = { ... }; options = { ... } }
|
||||
{
|
||||
machineName,
|
||||
machineConfig,
|
||||
inventory,
|
||||
directory,
|
||||
}:
|
||||
let
|
||||
compileServiceModules = serviceName: serviceConfigs: {
|
||||
# TODO: Add other attributes
|
||||
machineImports = (
|
||||
lib.foldlAttrs (
|
||||
# [ Modules ], String, { ${instance_name} :: ServiceConfig }
|
||||
initialServiceModules: serviceName: serviceConfigs:
|
||||
initialServiceModules
|
||||
# Collect service config
|
||||
++ (lib.foldlAttrs (
|
||||
# [ Modules ], String, ServiceConfig
|
||||
acc2: instanceName: serviceConfig:
|
||||
|
||||
@@ -187,30 +162,69 @@ let
|
||||
]
|
||||
else
|
||||
acc2
|
||||
) [ ] (serviceConfigs))
|
||||
) [ ] inventory.services
|
||||
# Global extension for each machine
|
||||
++ (extendMachine { inherit machineConfig inventory; });
|
||||
) [ ] (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
|
||||
lib.mapAttrs compileServiceModules inventory.services;
|
||||
|
||||
/*
|
||||
Returns a NixOS configuration for every machine in the inventory.
|
||||
Returns a set with NixOS configuration for every machine in the inventory.
|
||||
|
||||
machinesFromInventory :: Inventory -> { ${machine_name} :: NixOSConfiguration }
|
||||
*/
|
||||
buildInventory =
|
||||
{ inventory, directory }:
|
||||
# For every machine in the inventory, build a NixOS configuration
|
||||
# For each machine generate config, forEach service, if the machine is used.
|
||||
builtins.mapAttrs (
|
||||
{
|
||||
machines = builtins.mapAttrs (
|
||||
machineName: machineConfig:
|
||||
mapMachineConfigToNixOSConfig {
|
||||
let
|
||||
compiledServices = compileServicesForMachine {
|
||||
inherit
|
||||
machineName
|
||||
machineConfig
|
||||
inventory
|
||||
directory
|
||||
;
|
||||
};
|
||||
compiledMachine = compileMachine {
|
||||
inherit
|
||||
machineConfig
|
||||
;
|
||||
};
|
||||
machineImports =
|
||||
compiledMachine.machineImports
|
||||
++ builtins.foldl' (acc: service: acc ++ service.machineImports) [ ] (
|
||||
builtins.attrValues compiledServices
|
||||
);
|
||||
in
|
||||
{
|
||||
inherit machineImports compiledServices compiledMachine;
|
||||
}
|
||||
) (inventory.machines or { });
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit buildInventory;
|
||||
|
||||
Reference in New Issue
Block a user