Merge pull request 'clanServices: add flake level exports' (#4172) from flake-exports into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4172
This commit is contained in:
@@ -48,9 +48,11 @@ let
|
||||
clanCoreModules = { };
|
||||
flakeInputs = flakeInputsFixture;
|
||||
inherit inventory;
|
||||
exportsModule = { };
|
||||
};
|
||||
in
|
||||
{
|
||||
exports = import ./exports.nix { inherit lib clanLib; };
|
||||
resolve_module_spec = import ./import_module_spec.nix { inherit lib callInventoryAdapter; };
|
||||
test_simple =
|
||||
let
|
||||
@@ -171,7 +173,7 @@ in
|
||||
{
|
||||
# Test that the module is mapped into the output
|
||||
# We might change the attribute name in the future
|
||||
expr = lib.attrNames res.importedModulesEvaluated.self-A.config.instances;
|
||||
expr = lib.attrNames res.importedModulesEvaluated.self-A.instances;
|
||||
expected = [
|
||||
"instance_bar"
|
||||
"instance_foo"
|
||||
@@ -227,7 +229,7 @@ in
|
||||
{
|
||||
# Test that the module is mapped into the output
|
||||
# We might change the attribute name in the future
|
||||
expr = lib.attrNames res.importedModulesEvaluated.self-A.config.result.allMachines;
|
||||
expr = lib.attrNames res.importedModulesEvaluated.self-A.result.allMachines;
|
||||
expected = [
|
||||
"jon"
|
||||
"sara"
|
||||
@@ -279,13 +281,14 @@ in
|
||||
{
|
||||
# Test that the module is mapped into the output
|
||||
# We might change the attribute name in the future
|
||||
expr = lib.attrNames res.importedModulesEvaluated.self-A.config.result.allMachines;
|
||||
expr = lib.attrNames res.importedModulesEvaluated.self-A.result.allMachines;
|
||||
expected = [
|
||||
"jon"
|
||||
"sara"
|
||||
];
|
||||
};
|
||||
|
||||
machine_imports = import ./machine_imports.nix { inherit lib clanLib; };
|
||||
per_machine_args = import ./per_machine_args.nix { inherit lib callInventoryAdapter; };
|
||||
per_instance_args = import ./per_instance_args.nix { inherit lib callInventoryAdapter; };
|
||||
}
|
||||
|
||||
170
lib/modules/inventory/distributed-service/tests/exports.nix
Normal file
170
lib/modules/inventory/distributed-service/tests/exports.nix
Normal file
@@ -0,0 +1,170 @@
|
||||
{ lib, clanLib }:
|
||||
let
|
||||
clan = clanLib.clan {
|
||||
self = { };
|
||||
directory = ./.;
|
||||
|
||||
exportsModule = {
|
||||
options.vars.generators = lib.mkOption {
|
||||
type = lib.types.attrsOf (
|
||||
lib.types.submoduleWith {
|
||||
# TODO: import the vars submodule here
|
||||
modules = [
|
||||
{
|
||||
options.script = lib.mkOption { type = lib.types.str; };
|
||||
}
|
||||
];
|
||||
}
|
||||
);
|
||||
default = { };
|
||||
};
|
||||
};
|
||||
|
||||
machines.jon = { };
|
||||
machines.sara = { };
|
||||
# A module that adds exports perMachine
|
||||
modules.A =
|
||||
{ exports', ... }:
|
||||
{
|
||||
manifest.name = "A";
|
||||
roles.peer.perInstance =
|
||||
{ machine, ... }:
|
||||
{
|
||||
# Cross reference a perMachine exports
|
||||
exports.vars.generators."${machine.name}-network-ip".script =
|
||||
"A:" + exports'.machines.${machine.name}.vars.generators.key.script;
|
||||
# Cross reference a perInstance exports from a different service
|
||||
exports.vars.generators."${machine.name}-full-hostname".script =
|
||||
"A:" + exports'.instances."B-1".vars.generators.hostname.script;
|
||||
};
|
||||
roles.server = { };
|
||||
perMachine =
|
||||
{ machine, ... }:
|
||||
{
|
||||
exports = {
|
||||
vars.generators.key.script = machine.name;
|
||||
};
|
||||
};
|
||||
};
|
||||
# A module that adds exports perInstance
|
||||
modules.B = {
|
||||
manifest.name = "B";
|
||||
roles.peer.perInstance =
|
||||
{ instanceName, ... }:
|
||||
{
|
||||
exports = {
|
||||
vars.generators.hostname.script = instanceName;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
inventory = {
|
||||
instances.B-1 = {
|
||||
module.name = "B";
|
||||
module.input = "self";
|
||||
roles.peer.tags.all = { };
|
||||
};
|
||||
instances.B-2 = {
|
||||
module.name = "B";
|
||||
module.input = "self";
|
||||
roles.peer.tags.all = { };
|
||||
};
|
||||
instances.A-1 = {
|
||||
module.name = "A";
|
||||
module.input = "self";
|
||||
roles.peer.tags.all = { };
|
||||
roles.server.tags.all = { };
|
||||
};
|
||||
instances.A-2 = {
|
||||
module.name = "A";
|
||||
module.input = "self";
|
||||
roles.peer.tags.all = { };
|
||||
roles.server.tags.all = { };
|
||||
};
|
||||
};
|
||||
};
|
||||
in
|
||||
{
|
||||
test_1 = {
|
||||
inherit clan;
|
||||
expr = clan.config.exports;
|
||||
expected = {
|
||||
instances = {
|
||||
A-1 = {
|
||||
vars = {
|
||||
generators = {
|
||||
jon-full-hostname = {
|
||||
script = "A:B-1";
|
||||
};
|
||||
jon-network-ip = {
|
||||
script = "A:jon";
|
||||
};
|
||||
sara-full-hostname = {
|
||||
script = "A:B-1";
|
||||
};
|
||||
sara-network-ip = {
|
||||
script = "A:sara";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
A-2 = {
|
||||
vars = {
|
||||
generators = {
|
||||
jon-full-hostname = {
|
||||
script = "A:B-1";
|
||||
};
|
||||
jon-network-ip = {
|
||||
script = "A:jon";
|
||||
};
|
||||
sara-full-hostname = {
|
||||
script = "A:B-1";
|
||||
};
|
||||
sara-network-ip = {
|
||||
script = "A:sara";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
B-1 = {
|
||||
vars = {
|
||||
generators = {
|
||||
hostname = {
|
||||
script = "B-1";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
B-2 = {
|
||||
vars = {
|
||||
generators = {
|
||||
hostname = {
|
||||
script = "B-2";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
machines = {
|
||||
jon = {
|
||||
vars = {
|
||||
generators = {
|
||||
key = {
|
||||
script = "jon";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
sara = {
|
||||
vars = {
|
||||
generators = {
|
||||
key = {
|
||||
script = "sara";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
{ lib, clanLib }:
|
||||
let
|
||||
clan = clanLib.clan {
|
||||
self = { };
|
||||
directory = ./.;
|
||||
|
||||
machines.jon = { };
|
||||
machines.sara = { };
|
||||
# A module that adds exports perMachine
|
||||
modules.A =
|
||||
{ ... }:
|
||||
{
|
||||
manifest.name = "A";
|
||||
roles.peer.perInstance =
|
||||
{ ... }:
|
||||
{
|
||||
nixosModule = {
|
||||
options.bar = lib.mkOption {
|
||||
default = 1;
|
||||
};
|
||||
};
|
||||
};
|
||||
roles.server = { };
|
||||
perMachine =
|
||||
{ ... }:
|
||||
{
|
||||
nixosModule = {
|
||||
options.foo = lib.mkOption {
|
||||
default = 1;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
inventory.instances.A = {
|
||||
module.input = "self";
|
||||
roles.peer.tags.all = { };
|
||||
};
|
||||
};
|
||||
in
|
||||
{
|
||||
test_1 = {
|
||||
inherit clan;
|
||||
expr = { inherit (clan.config.clanInternals.machines.x86_64-linux.jon.config) bar foo; };
|
||||
expected = {
|
||||
foo = 1;
|
||||
bar = 1;
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -106,7 +106,7 @@ in
|
||||
test_per_instance_arguments = {
|
||||
expr = {
|
||||
instanceName =
|
||||
res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances."instance_foo".allMachines.jon.passthru.instanceName;
|
||||
res.importedModulesEvaluated.self-A.result.allRoles.peer.allInstances."instance_foo".allMachines.jon.passthru.instanceName;
|
||||
|
||||
# settings are specific.
|
||||
# Below we access:
|
||||
@@ -114,11 +114,11 @@ in
|
||||
# roles = peer
|
||||
# machines = jon
|
||||
settings =
|
||||
res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.settings;
|
||||
res.importedModulesEvaluated.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.settings;
|
||||
machine =
|
||||
res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.machine;
|
||||
res.importedModulesEvaluated.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.machine;
|
||||
roles =
|
||||
res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.roles;
|
||||
res.importedModulesEvaluated.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.roles;
|
||||
};
|
||||
expected = {
|
||||
instanceName = "instance_foo";
|
||||
@@ -161,9 +161,9 @@ in
|
||||
|
||||
# TODO: Cannot be tested like this anymore
|
||||
test_per_instance_settings_vendoring = {
|
||||
x = res.importedModulesEvaluated.self-A.config;
|
||||
x = res.importedModulesEvaluated.self-A;
|
||||
expr =
|
||||
res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.vendoredSettings;
|
||||
res.importedModulesEvaluated.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.vendoredSettings;
|
||||
expected = {
|
||||
timeout = "config.thing";
|
||||
};
|
||||
|
||||
@@ -81,7 +81,7 @@ in
|
||||
inherit res;
|
||||
expr = {
|
||||
hasMachineSettings =
|
||||
res.importedModulesEvaluated.self-A.config.result.allMachines.jon.passthru.instances.instance_foo.roles.peer.machines.jon
|
||||
res.importedModulesEvaluated.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer.machines.jon
|
||||
? settings;
|
||||
|
||||
# settings are specific.
|
||||
@@ -89,10 +89,10 @@ in
|
||||
# instance = instance_foo
|
||||
# roles = peer
|
||||
# machines = jon
|
||||
specificMachineSettings = filterInternals res.importedModulesEvaluated.self-A.config.result.allMachines.jon.passthru.instances.instance_foo.roles.peer.machines.jon.settings;
|
||||
specificMachineSettings = filterInternals res.importedModulesEvaluated.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer.machines.jon.settings;
|
||||
|
||||
hasRoleSettings =
|
||||
res.importedModulesEvaluated.self-A.config.result.allMachines.jon.passthru.instances.instance_foo.roles.peer
|
||||
res.importedModulesEvaluated.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer
|
||||
? settings;
|
||||
|
||||
# settings are specific.
|
||||
@@ -100,7 +100,7 @@ in
|
||||
# instance = instance_foo
|
||||
# roles = peer
|
||||
# machines = *
|
||||
specificRoleSettings = filterInternals res.importedModulesEvaluated.self-A.config.result.allMachines.jon.passthru.instances.instance_foo.roles.peer.settings;
|
||||
specificRoleSettings = filterInternals res.importedModulesEvaluated.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer.settings;
|
||||
};
|
||||
expected = {
|
||||
hasMachineSettings = true;
|
||||
|
||||
Reference in New Issue
Block a user