update inventory implementation
This commit is contained in:
committed by
hsjobeki
parent
b4a8a3d047
commit
1b226c9e09
@@ -7,22 +7,25 @@ let
|
||||
|
||||
machines = machinesFromInventory syncthing_inventory;
|
||||
|
||||
resolveGroups =
|
||||
inventory: members:
|
||||
lib.unique (
|
||||
builtins.foldl' (
|
||||
acc: currMember:
|
||||
let
|
||||
groupName = builtins.substring 6 (builtins.stringLength currMember - 6) currMember;
|
||||
groupMembers =
|
||||
if inventory.groups.machines ? ${groupName} then
|
||||
inventory.groups.machines.${groupName}
|
||||
else
|
||||
throw "Machine group ${currMember} not found. Key: groups.machines.${groupName} not in inventory.";
|
||||
in
|
||||
if lib.hasPrefix "group:" currMember then (acc ++ groupMembers) else acc ++ [ currMember ]
|
||||
) [ ] members
|
||||
);
|
||||
resolveTags =
|
||||
# Inventory, { machines :: [string], tags :: [string] }
|
||||
inventory: members: {
|
||||
machines =
|
||||
members.machines or [ ]
|
||||
++ (builtins.foldl' (
|
||||
acc: tag:
|
||||
let
|
||||
tagMembers = builtins.attrNames (
|
||||
lib.filterAttrs (_n: v: builtins.elem tag v.tags or [ ]) inventory.machines
|
||||
);
|
||||
in
|
||||
# throw "Machine tag ${tag} not found. Not machine with: tag ${tagName} not in inventory.";
|
||||
if tagMembers == [ ] then
|
||||
throw "Machine tag ${tag} not found. Not machine with: tag ${tag} not in inventory."
|
||||
else
|
||||
acc ++ tagMembers
|
||||
) [ ] members.tags or [ ]);
|
||||
};
|
||||
|
||||
/*
|
||||
Returns a NixOS configuration for every machine in the inventory.
|
||||
@@ -45,29 +48,53 @@ let
|
||||
acc2: instanceName: serviceConfig:
|
||||
let
|
||||
resolvedRoles = builtins.mapAttrs (
|
||||
_roleName: members: resolveGroups inventory members
|
||||
_roleName: members: resolveTags inventory members
|
||||
) serviceConfig.roles;
|
||||
|
||||
isInService = builtins.any (members: builtins.elem machineName members) (
|
||||
isInService = builtins.any (members: builtins.elem machineName members.machines) (
|
||||
builtins.attrValues resolvedRoles
|
||||
);
|
||||
|
||||
# Inverse map of roles. Allows for easy lookup of roles for a given machine.
|
||||
# { ${machine_name} :: [roles]
|
||||
inverseRoles = lib.foldlAttrs (
|
||||
acc: roleName:
|
||||
{ machines }:
|
||||
acc
|
||||
// builtins.foldl' (
|
||||
acc2: machineName: acc2 // { ${machineName} = (acc.${machineName} or [ ]) ++ [ roleName ]; }
|
||||
) { } machines
|
||||
) { } resolvedRoles;
|
||||
|
||||
machineServiceConfig = (serviceConfig.machines.${machineName} or { }).config or { };
|
||||
globalConfig = serviceConfig.config;
|
||||
|
||||
# TODO: maybe optimize this dont lookup the role in inverse roles. Imports are not lazy
|
||||
roleModules = builtins.map (
|
||||
role:
|
||||
let
|
||||
path = "${clan-core.clanModules.${moduleName}}/roles/${role}.nix";
|
||||
in
|
||||
if builtins.pathExists path then
|
||||
path
|
||||
else
|
||||
throw "Role doesnt have a module: ${role}. Path: ${path} not found."
|
||||
) inverseRoles.${machineName} or [ ];
|
||||
in
|
||||
if isInService then
|
||||
acc2
|
||||
++ [
|
||||
{
|
||||
imports = [ clan-core.clanModules.${moduleName} ];
|
||||
imports = [ clan-core.clanModules.${moduleName} ] ++ roleModules;
|
||||
config.clan.${moduleName} = lib.mkMerge [
|
||||
globalConfig
|
||||
machineServiceConfig
|
||||
];
|
||||
}
|
||||
{
|
||||
config.clan.inventory.${instanceName} = {
|
||||
config.clan.inventory.${moduleName}.${instanceName} = {
|
||||
roles = resolvedRoles;
|
||||
# inherit inverseRoles;
|
||||
};
|
||||
}
|
||||
]
|
||||
@@ -78,8 +105,64 @@ let
|
||||
) inventory.machines;
|
||||
in
|
||||
{
|
||||
inherit clan-core;
|
||||
|
||||
new_clan = clan-core.lib.buildInventory {
|
||||
# High level services.
|
||||
# If you need multiple instances of a service configure them via:
|
||||
# inventory.services.[serviceName].[instanceName] = ...
|
||||
services = {
|
||||
borbackup = {
|
||||
roles.server.machines = [ "vyr" ];
|
||||
roles.client.tags = [ "laptop" ];
|
||||
machines.vyr = {
|
||||
config = {
|
||||
|
||||
};
|
||||
};
|
||||
config = {
|
||||
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
# Low level inventory i.e. if you need multiple instances of a service
|
||||
# Or if you want to manipulate the created inventory directly.
|
||||
inventory.services.borbackup.default = { };
|
||||
|
||||
# Machines. each machine can be referenced by its attribute name under services.
|
||||
machines = {
|
||||
camina = {
|
||||
# This is added to machine tags
|
||||
clan.tags = [ "laptop" ];
|
||||
# These are the inventory machine fields
|
||||
clan.meta.description = "";
|
||||
clan.meta.name = "";
|
||||
clan.meta.icon = "";
|
||||
# Config ...
|
||||
};
|
||||
vyr = {
|
||||
# Config ...
|
||||
};
|
||||
vi = {
|
||||
clan.networking.targetHost = "root@78.47.164.46";
|
||||
# Config ...
|
||||
};
|
||||
aya = {
|
||||
clan.networking.targetHost = "root@78.47.164.46";
|
||||
# Config ...
|
||||
};
|
||||
ezra = {
|
||||
# Config ...
|
||||
};
|
||||
rianon = {
|
||||
# Config ...
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
clan = clan-core.lib.buildClan {
|
||||
meta.name = "vis clans";
|
||||
meta.name = "vi's clans";
|
||||
# Should usually point to the directory of flake.nix
|
||||
directory = self;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user