libstore/machines: add enable flag for toml machines

Provide a way to statically disable a machine.
This is fully static and not dynamic.

Change-Id: I394433ad533bca5dbf6a2f7fea22b8aa0f5aad3f
This commit is contained in:
Commentator2.0
2025-12-04 12:11:23 +01:00
parent 3fcfedc216
commit 8f5bf1e905
3 changed files with 53 additions and 1 deletions
@@ -172,6 +172,10 @@ speed-factor = 2.0
supported-features = ["kvm", "benchmark"]
ssh-key = "/home/deepslate/.ssh/id_ed25519"
[machines.legacy]
uri = "ssh://nix@nix-15-11.nixos.org"
enable = false
```
> **Note**
@@ -183,6 +187,13 @@ For testing purposes, one can also define a builder ad hoc on the CLI as follows
`--builders 'machines.andesite = {uri = "ssh://lix@andesite.lix.systems", jobs = 8}'`
#### Special handling of fields
- `enable` (**optional**)
If set to false, the declared machine will not be loaded.
This allows one to statically disable machines.
Defaults to true
### Using the legacy format
> **Warning**
>
+11 -1
View File
@@ -242,6 +242,7 @@ static const std::set<std::string> EXPECTED_KEYS = {
"supported-features",
"mandatory-features",
"ssh-public-host-key",
"enable",
};
static toml::result<float, std::string> getSpeedFactor(const toml::value & data)
@@ -408,8 +409,17 @@ static toml::result<Machines, std::vector<std::string>> parseToml(const toml::va
auto err = res.as_err();
parserErrors.push_back(fmt("for machine %s:", name));
parserErrors.insert(parserErrors.end(), err.begin(), err.end());
continue;
}
auto enable = parse<bool>(machine, "enable", true);
if (enable.is_ok()) {
if (enable.unwrap()) {
// Check if it hasn't been statically disabled
// But still throw parsing errors if it was
machines.push_back(res.unwrap());
}
} else {
machines.push_back(res.unwrap());
parserErrors.push_back(enable.as_err());
}
}
+31
View File
@@ -233,6 +233,21 @@ TEST(machines, getMachinesTOMLWithIncorrectTyping)
settings.builders.override("machines.a = \"lix@andesite.lix.sytems\"\n");
EXPECT_MESSAGE_THROW(getMachines(), UsageError, "Each machine must be a table");
settings.builders.override(
"version = \"1\"\n"
"[machines.scratchy]\n"
"uri = \"nix@scratchy.labs.cs.uu.nl\"\n"
);
EXPECT_MESSAGE_THROW(getMachines(), UsageError, "bad_cast to integer");
settings.builders.override(
"version = 1\n"
"[machines.legacy]\n"
"uri = \"ssh://nix@nix-15-11.nixos.org\"\n"
"enable = 0\n"
);
EXPECT_MESSAGE_THROW(getMachines(), UsageError, "bad_cast to boolean");
}
TEST(machines, getMachinesTOMLBadVersion)
@@ -282,4 +297,20 @@ TEST(machines, getMachinesTOMLInvalidSyntaxButClearlyTOML)
EXPECT_MESSAGE_THROW(getMachines(), UsageError, "invalid Machines TOML syntax:");
}
TEST(machines, getMachinesTOMLOneDisabled)
{
settings.builders.override(
"version = 1\n"
"[machines.a]\n"
"uri = \"ssh://test\"\n"
"enable = false\n"
"\n"
"[machines.b]\n"
"uri = \"ssh://test2\"\n"
);
auto actual = getMachines();
ASSERT_THAT(actual, SizeIs(1));
EXPECT_THAT(actual[0], Field(&Machine::storeUri, EndsWith("test2")));
}
#undef EXPECT_MESSAGE_THROW