1
0
forked from clan/clan-core

lib: add fact loaders to clan lib

This commit is contained in:
a-kenji 2024-07-02 11:26:50 +02:00 committed by kenji
parent f228239834
commit 1a969d884e
2 changed files with 27 additions and 18 deletions

View File

@ -6,8 +6,9 @@
}: }:
{ {
evalClanModules = import ./eval-clan-modules { inherit clan-core nixpkgs lib; }; evalClanModules = import ./eval-clan-modules { inherit clan-core nixpkgs lib; };
buildClan = import ./build-clan { inherit clan-core lib nixpkgs; };
facts = import ./facts.nix { inherit lib; };
inventory = import ./inventory { inherit lib clan-core; }; inventory = import ./inventory { inherit lib clan-core; };
jsonschema = import ./jsonschema { inherit lib; }; jsonschema = import ./jsonschema { inherit lib; };
modules = import ./description.nix { inherit clan-core lib; }; modules = import ./description.nix { inherit clan-core lib; };
buildClan = import ./build-clan { inherit clan-core lib nixpkgs; };
} }

View File

@ -1,19 +1,17 @@
{ lib, machineDir, ... }: { lib, ... }:
machineDir:
let let
allMachineNames = lib.mapAttrsToList (name: _: name) (builtins.readDir machineDir); allMachineNames = lib.mapAttrsToList (name: _: name) (builtins.readDir machineDir);
getFactPath = fact: machine: getFactPath = fact: machine: "${machineDir}/${machine}/facts/${fact}";
"${machineDir}/${machine}/facts/${fact}";
readFact = fact: machine: readFact =
fact: machine:
let let
path = getFactPath fact machine; path = getFactPath fact machine;
in in
if builtins.pathExists path then if builtins.pathExists path then builtins.readFile path else null;
builtins.readFile path
else
null;
# Example: # Example:
# #
@ -22,7 +20,8 @@ let
# machineA = "1.2.3.4"; # machineA = "1.2.3.4";
# machineB = "5.6.7.8"; # machineB = "5.6.7.8";
# }; # };
readFactFromAllMachines = fact: readFactFromAllMachines =
fact:
let let
machines = allMachineNames; machines = allMachineNames;
facts = lib.genAttrs machines (readFact fact); facts = lib.genAttrs machines (readFact fact);
@ -47,18 +46,27 @@ let
# "synching.pub" = "23456719"; # "synching.pub" = "23456719";
# }; # };
# }; # };
readFactsFromAllMachines = facts: readFactsFromAllMachines =
facts:
let let
# machine -> fact -> factvalue # machine -> fact -> factvalue
machinesFactsAttrs = lib.genAttrs allMachineNames (machine: lib.genAttrs facts (fact: readFact fact machine)); machinesFactsAttrs = lib.genAttrs allMachineNames (
machine: lib.genAttrs facts (fact: readFact fact machine)
);
# remove all machines which don't have all facts set # remove all machines which don't have all facts set
filteredMachineFactAttrs = filteredMachineFactAttrs = lib.filterAttrs (
lib.filterAttrs (_machine: values: builtins.all (fact: values.${fact} != null) facts) _machine: values: builtins.all (fact: values.${fact} != null) facts
machinesFactsAttrs; ) machinesFactsAttrs;
in in
filteredMachineFactAttrs; filteredMachineFactAttrs;
in in
{ inherit allMachineNames getFactPath readFact readFactFromAllMachines readFactsFromAllMachines; } {
inherit
allMachineNames
getFactPath
readFact
readFactFromAllMachines
readFactsFromAllMachines
;
}