Inventory: docs improvements

This commit is contained in:
Johannes Kirschbauer
2024-09-16 13:19:50 +02:00
parent ff9adee375
commit 56b76f9827
29 changed files with 237 additions and 195 deletions

View File

@@ -68,7 +68,7 @@ let
hwConfig = "${directory}/machines/${name}/hardware-configuration.nix";
facterModules = lib.optionals (builtins.pathExists facterJson) [
"${clan-core.inputs.nixos-facter-modules}/modules/nixos/facter.nix"
clan-core.inputs.nixos-facter-modules.nixosModules.facter
{ config.facter.reportPath = facterJson; }
];
in
@@ -231,6 +231,9 @@ in
)
);
}
{
inventory.machines = lib.mapAttrs (_n: _: { }) config.machines;
}
# Merge the meta attributes from the buildClan function
{ inventory.meta = if config.meta != null then config.meta else { }; }
];

View File

@@ -12,7 +12,7 @@ rec {
in
readmeContents;
getShortDescription =
getFrontmatter =
modulename:
let
content = getReadme modulename;
@@ -21,14 +21,21 @@ rec {
parsed = builtins.partition ({ index, ... }: if index >= 2 then false else true) (
lib.filter ({ index, ... }: index != 0) (lib.imap0 (index: part: { inherit index part; }) parts)
);
# Use this if the content is needed
# readmeContent = lib.concatMapStrings (v: "---" + v.part) parsed.wrong;
meta = builtins.fromTOML (builtins.head parsed.right).part;
in
if (builtins.length parts >= 3) then
meta.description
meta
else
throw "Short description delimiter `---` not found in README.md for module ${modulename}";
throw ''
TOML Frontmatter not found in README.md for module ${modulename}
Please add the following to the top of your README.md:
---
description = "Your description here"
categories = [ "Your categories here" ]
features = [ "inventory" ]
---
...rest of your README.md...
'';
}

View File

@@ -74,4 +74,5 @@
) [ ] config.services;
}
];
}

View File

@@ -37,44 +37,18 @@ let
) [ ] members.tags or [ ]);
};
checkService =
serviceName:
let
frontmatter = clan-core.lib.modules.getFrontmatter serviceName;
in
if builtins.elem "inventory" frontmatter.features or [ ] then true else false;
/*
Returns a NixOS configuration for every machine in the inventory.
machinesFromInventory :: Inventory -> { ${machine_name} :: NixOSConfiguration }
*/
# { client_1_machine = { tags = [ "backup" ]; }; client_2_machine = { tags = [ "backup" ]; }; not_used_machine = { }; }
getAllMachines =
inventory:
lib.foldlAttrs (
res: serviceName: serviceConfigs:
(lib.foldlAttrs (
res: instanceName: serviceConfig:
lib.foldlAttrs (
res: roleName: members:
let
resolved = resolveTags {
inherit
serviceName
instanceName
roleName
inventory
members
;
};
in
res
// builtins.listToAttrs (
builtins.map (m: {
name = m;
value = { };
}) resolved.machines
)
) res serviceConfig.roles
) res serviceConfigs)
) { } (inventory.services or { })
// inventory.machines or { };
buildInventory =
{ inventory, directory }:
# For every machine in the inventory, build a NixOS configuration
@@ -178,7 +152,7 @@ let
]
else
acc2
) [ ] serviceConfigs)
) [ ] (serviceConfigs))
) [ ] inventory.services
# Append each machine config
++ [
@@ -188,9 +162,35 @@ let
(lib.optionalAttrs (machineConfig.deploy.targetHost or null != null) {
config.clan.core.networking.targetHost = machineConfig.deploy.targetHost;
})
{
assertions = lib.foldlAttrs (
acc: serviceName: _:
acc
++ [
{
assertion = checkService 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;
}
]
) (getAllMachines inventory);
) inventory.machines;
in
{
inherit buildInventory getAllMachines;
inherit buildInventory;
}

View File

@@ -1,37 +1,8 @@
{ inventory, clan-core, ... }:
let
inherit (inventory) buildInventory getAllMachines;
inherit (inventory) buildInventory;
in
{
test_get_all_used_machines = {
# Test that all machines are returned
expr = getAllMachines {
machines = {
machine_3 = {
tags = [ "tag_3" ];
};
};
services = {
borgbackup.instance_1 = {
roles.server.machines = [ "backup_server" ];
roles.client.machines = [
"client_1_machine"
"client_2_machine"
];
roles.client.tags = [ "tag_3" ];
};
};
};
expected = {
backup_server = { };
client_1_machine = { };
client_2_machine = { };
machine_3 = {
tags = [ "tag_3" ];
};
};
};
test_inventory_empty = {
# Empty inventory should return an empty module
expr = buildInventory {