Merge pull request 'clanInternals.machines: expose information as json' (#358) from Mic92-main into main

This commit is contained in:
clan-bot
2023-09-27 16:45:18 +00:00
4 changed files with 22 additions and 13 deletions

View File

@@ -39,23 +39,29 @@ let
nixosConfigurations = lib.mapAttrs (name: _: nixosConfiguration { inherit name; }) allMachines; nixosConfigurations = lib.mapAttrs (name: _: nixosConfiguration { inherit name; }) allMachines;
# This instantiates nixos for each system that we support: # This instantiates nixos for each system that we support:
# clanInternals.machinesForAllSystems.<system>.<machine> # configPerSystem = <system>.<machine>.nixosConfiguration
# We need this to build nixos secret generators for each system # We need this to build nixos secret generators for each system
machinesForAllSystems = builtins.listToAttrs configPerSystem = builtins.listToAttrs
(builtins.map (builtins.map
(system: lib.nameValuePair system (system: lib.nameValuePair system
(lib.mapAttrs (name: _: nixosConfiguration { inherit name system; }) allMachines)) (lib.mapAttrs (name: _: nixosConfiguration { inherit name system; }) allMachines))
supportedSystems); supportedSystems);
machinesPerSystem = lib.mapAttrs (_: machine:
let
config = {
inherit (machine.config.system.clan) uploadSecrets generateSecrets;
inherit (machine.config.clan.networking) deploymentAddress;
};
in
config // {
json = machine.pkgs.writeText "config.json" (builtins.toJSON config);
});
in in
{ {
inherit nixosConfigurations; inherit nixosConfigurations;
clanInternals = { clanInternals = {
machines = lib.mapAttrs machines = lib.mapAttrs (_: machinesPerSystem) configPerSystem;
(_: lib.mapAttrs (_: machine: {
inherit (machine.config.system.clan) uploadSecrets generateSecrets;
inherit (machine.config.clan.networking) deploymentAddress;
}))
machinesForAllSystems;
}; };
} }

View File

@@ -22,12 +22,14 @@ def create_flake(
# in the flake.nix file replace the string __CLAN_URL__ with the the clan flake # in the flake.nix file replace the string __CLAN_URL__ with the the clan flake
# provided by get_test_flake_toplevel # provided by get_test_flake_toplevel
flake_nix = flake / "flake.nix" flake_nix = flake / "flake.nix"
# this is where we would install the sops key to, when updating
sops_key = str(flake.joinpath("sops.key"))
for line in fileinput.input(flake_nix, inplace=True): for line in fileinput.input(flake_nix, inplace=True):
line = line.replace("__NIXPKGS__", str(nixpkgs_source())) line = line.replace("__NIXPKGS__", str(nixpkgs_source()))
if clan_core_flake: if clan_core_flake:
line = line.replace("__CLAN_CORE__", str(clan_core_flake)) line = line.replace("__CLAN_CORE__", str(clan_core_flake))
print(line) line = line.replace("__CLAN_SOPS_KEY_PATH__", sops_key)
# check that an empty config is returned if no json file exists print(line, end="")
monkeypatch.chdir(flake) monkeypatch.chdir(flake)
monkeypatch.setenv("HOME", str(home)) monkeypatch.setenv("HOME", str(home))
yield flake yield flake

View File

@@ -12,8 +12,8 @@
machines = { machines = {
vm1 = { lib, ... }: { vm1 = { lib, ... }: {
clan.networking.deploymentAddress = "__CLAN_DEPLOYMENT_ADDRESS__"; clan.networking.deploymentAddress = "__CLAN_DEPLOYMENT_ADDRESS__";
sops.age.keyFile = "__CLAN_SOPS_KEY_PATH__";
system.stateVersion = lib.version; system.stateVersion = lib.version;
sops.age.keyFile = "__CLAN_SOPS_KEY_PATH__";
clan.networking.zerotier.controller.enable = true; clan.networking.zerotier.controller.enable = true;

View File

@@ -31,10 +31,11 @@ def test_secrets_upload(
host = host_group.hosts[0] host = host_group.hosts[0]
addr = f"{host.user}@{host.host}:{host.port}?StrictHostKeyChecking=no&UserKnownHostsFile=/dev/null&IdentityFile={host.key}" addr = f"{host.user}@{host.host}:{host.port}?StrictHostKeyChecking=no&UserKnownHostsFile=/dev/null&IdentityFile={host.key}"
new_text = flake.read_text().replace("__CLAN_DEPLOYMENT_ADDRESS__", addr) new_text = flake.read_text().replace("__CLAN_DEPLOYMENT_ADDRESS__", addr)
sops_key = test_flake_with_core.joinpath("sops.key")
new_text = new_text.replace("__CLAN_SOPS_KEY_PATH__", str(sops_key))
flake.write_text(new_text) flake.write_text(new_text)
cli.run(["secrets", "upload", "vm1"]) cli.run(["secrets", "upload", "vm1"])
# the flake defines this path as the location where the sops key should be installed
sops_key = test_flake_with_core.joinpath("sops.key")
assert sops_key.exists() assert sops_key.exists()
assert sops_key.read_text() == age_keys[0].privkey assert sops_key.read_text() == age_keys[0].privkey