2017-05-02 11:17:37 +00:00
|
|
|
#include "machines.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
#include "globals.hh"
|
2020-08-12 16:32:36 +00:00
|
|
|
#include "store-api.hh"
|
2017-05-02 11:17:37 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
Machine::Machine(decltype(storeUri) storeUri,
|
|
|
|
decltype(systemTypes) systemTypes,
|
|
|
|
decltype(sshKey) sshKey,
|
|
|
|
decltype(maxJobs) maxJobs,
|
|
|
|
decltype(speedFactor) speedFactor,
|
|
|
|
decltype(supportedFeatures) supportedFeatures,
|
2017-05-02 12:36:59 +00:00
|
|
|
decltype(mandatoryFeatures) mandatoryFeatures,
|
|
|
|
decltype(sshPublicHostKey) sshPublicHostKey) :
|
2017-05-02 11:17:37 +00:00
|
|
|
storeUri(
|
2021-06-23 21:07:55 +00:00
|
|
|
// Backwards compatibility: if the URI is schemeless, is not a path,
|
|
|
|
// and is not one of the special store connection words, prepend
|
|
|
|
// ssh://.
|
2017-10-24 13:16:18 +00:00
|
|
|
storeUri.find("://") != std::string::npos
|
2021-06-23 21:07:55 +00:00
|
|
|
|| storeUri.find("/") != std::string::npos
|
|
|
|
|| storeUri == "auto"
|
|
|
|
|| storeUri == "daemon"
|
|
|
|
|| storeUri == "local"
|
|
|
|
|| hasPrefix(storeUri, "auto?")
|
|
|
|
|| hasPrefix(storeUri, "daemon?")
|
|
|
|
|| hasPrefix(storeUri, "local?")
|
|
|
|
|| hasPrefix(storeUri, "?")
|
2017-05-02 11:17:37 +00:00
|
|
|
? storeUri
|
|
|
|
: "ssh://" + storeUri),
|
|
|
|
systemTypes(systemTypes),
|
|
|
|
sshKey(sshKey),
|
|
|
|
maxJobs(maxJobs),
|
2024-03-04 07:40:38 +00:00
|
|
|
speedFactor(speedFactor == 0.0f ? 1.0f : std::move(speedFactor)),
|
2017-05-02 11:17:37 +00:00
|
|
|
supportedFeatures(supportedFeatures),
|
2017-05-02 12:36:59 +00:00
|
|
|
mandatoryFeatures(mandatoryFeatures),
|
|
|
|
sshPublicHostKey(sshPublicHostKey)
|
2024-03-04 07:40:38 +00:00
|
|
|
{
|
|
|
|
if (speedFactor < 0.0)
|
|
|
|
throw UsageError("speed factor must be >= 0");
|
|
|
|
}
|
2017-05-02 11:17:37 +00:00
|
|
|
|
2024-03-04 07:39:24 +00:00
|
|
|
bool Machine::systemSupported(const std::string & system) const
|
|
|
|
{
|
|
|
|
return system == "builtin" || (systemTypes.count(system) > 0);
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
bool Machine::allSupported(const std::set<std::string> & features) const
|
2021-10-29 12:45:13 +00:00
|
|
|
{
|
2017-05-02 11:17:37 +00:00
|
|
|
return std::all_of(features.begin(), features.end(),
|
2022-02-25 15:00:00 +00:00
|
|
|
[&](const std::string & feature) {
|
2017-05-02 11:17:37 +00:00
|
|
|
return supportedFeatures.count(feature) ||
|
|
|
|
mandatoryFeatures.count(feature);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
bool Machine::mandatoryMet(const std::set<std::string> & features) const
|
2021-10-29 12:45:13 +00:00
|
|
|
{
|
2017-05-02 11:17:37 +00:00
|
|
|
return std::all_of(mandatoryFeatures.begin(), mandatoryFeatures.end(),
|
2022-02-25 15:00:00 +00:00
|
|
|
[&](const std::string & feature) {
|
2017-05-02 11:17:37 +00:00
|
|
|
return features.count(feature);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-29 12:45:13 +00:00
|
|
|
ref<Store> Machine::openStore() const
|
|
|
|
{
|
2020-08-12 16:32:36 +00:00
|
|
|
Store::Params storeParams;
|
|
|
|
if (hasPrefix(storeUri, "ssh://")) {
|
|
|
|
storeParams["max-connections"] = "1";
|
|
|
|
storeParams["log-fd"] = "4";
|
2021-02-25 01:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasPrefix(storeUri, "ssh://") || hasPrefix(storeUri, "ssh-ng://")) {
|
2020-08-12 16:32:36 +00:00
|
|
|
if (sshKey != "")
|
|
|
|
storeParams["ssh-key"] = sshKey;
|
2021-02-25 01:52:22 +00:00
|
|
|
if (sshPublicHostKey != "")
|
|
|
|
storeParams["base64-ssh-public-host-key"] = sshPublicHostKey;
|
2020-08-12 16:32:36 +00:00
|
|
|
}
|
2021-02-25 01:52:22 +00:00
|
|
|
|
2020-08-12 16:32:36 +00:00
|
|
|
{
|
|
|
|
auto & fs = storeParams["system-features"];
|
|
|
|
auto append = [&](auto feats) {
|
|
|
|
for (auto & f : feats) {
|
|
|
|
if (fs.size() > 0) fs += ' ';
|
|
|
|
fs += f;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
append(supportedFeatures);
|
|
|
|
append(mandatoryFeatures);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nix::openStore(storeUri, storeParams);
|
|
|
|
}
|
|
|
|
|
2021-10-29 12:45:13 +00:00
|
|
|
static std::vector<std::string> expandBuilderLines(const std::string & builders)
|
|
|
|
{
|
2021-10-12 12:18:44 +00:00
|
|
|
std::vector<std::string> result;
|
2022-02-25 15:00:00 +00:00
|
|
|
for (auto line : tokenizeString<std::vector<std::string>>(builders, "\n;")) {
|
2017-10-24 08:52:34 +00:00
|
|
|
trim(line);
|
2017-05-02 11:17:37 +00:00
|
|
|
line.erase(std::find(line.begin(), line.end(), '#'), line.end());
|
|
|
|
if (line.empty()) continue;
|
2017-10-24 08:52:34 +00:00
|
|
|
|
|
|
|
if (line[0] == '@') {
|
2021-10-12 12:18:44 +00:00
|
|
|
const std::string path = trim(std::string(line, 1));
|
|
|
|
std::string text;
|
2017-10-24 08:52:34 +00:00
|
|
|
try {
|
2021-10-12 12:18:44 +00:00
|
|
|
text = readFile(path);
|
2017-10-24 08:52:34 +00:00
|
|
|
} catch (const SysError & e) {
|
|
|
|
if (e.errNo != ENOENT)
|
|
|
|
throw;
|
2021-10-12 12:18:44 +00:00
|
|
|
debug("cannot find machines file '%s'", path);
|
2017-10-24 08:52:34 +00:00
|
|
|
}
|
2021-10-12 12:18:44 +00:00
|
|
|
|
|
|
|
const auto lines = expandBuilderLines(text);
|
|
|
|
result.insert(end(result), begin(lines), end(lines));
|
2017-10-24 08:52:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:18:44 +00:00
|
|
|
result.emplace_back(line);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-05-02 12:36:59 +00:00
|
|
|
|
2021-10-29 12:45:13 +00:00
|
|
|
static Machine parseBuilderLine(const std::string & line)
|
|
|
|
{
|
2022-02-25 15:00:00 +00:00
|
|
|
const auto tokens = tokenizeString<std::vector<std::string>>(line);
|
2021-10-12 12:18:44 +00:00
|
|
|
|
|
|
|
auto isSet = [&](size_t fieldIndex) {
|
|
|
|
return tokens.size() > fieldIndex && tokens[fieldIndex] != "" && tokens[fieldIndex] != "-";
|
|
|
|
};
|
|
|
|
|
|
|
|
auto parseUnsignedIntField = [&](size_t fieldIndex) {
|
|
|
|
const auto result = string2Int<unsigned int>(tokens[fieldIndex]);
|
|
|
|
if (!result) {
|
|
|
|
throw FormatError("bad machine specification: failed to convert column #%lu in a row: '%s' to 'unsigned int'", fieldIndex, line);
|
|
|
|
}
|
|
|
|
return result.value();
|
|
|
|
};
|
|
|
|
|
2024-03-04 07:40:38 +00:00
|
|
|
auto parseFloatField = [&](size_t fieldIndex) {
|
|
|
|
const auto result = string2Int<float>(tokens[fieldIndex]);
|
|
|
|
if (!result) {
|
|
|
|
throw FormatError("bad machine specification: failed to convert column #%lu in a row: '%s' to 'float'", fieldIndex, line);
|
|
|
|
}
|
|
|
|
return result.value();
|
|
|
|
};
|
|
|
|
|
2021-10-12 12:18:44 +00:00
|
|
|
auto ensureBase64 = [&](size_t fieldIndex) {
|
2021-10-29 12:45:13 +00:00
|
|
|
const auto & str = tokens[fieldIndex];
|
2021-10-12 12:18:44 +00:00
|
|
|
try {
|
|
|
|
base64Decode(str);
|
2021-10-29 12:45:13 +00:00
|
|
|
} catch (const Error & e) {
|
2021-10-12 12:18:44 +00:00
|
|
|
throw FormatError("bad machine specification: a column #%lu in a row: '%s' is not valid base64 string: %s", fieldIndex, line, e.what());
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2021-10-29 12:45:13 +00:00
|
|
|
if (!isSet(0))
|
|
|
|
throw FormatError("bad machine specification: store URL was not found at the first column of a row: '%s'", line);
|
|
|
|
|
|
|
|
return {
|
|
|
|
tokens[0],
|
2024-03-04 07:39:24 +00:00
|
|
|
isSet(1) ? tokenizeString<std::set<std::string>>(tokens[1], ",") : std::set<std::string>{settings.thisSystem},
|
2021-10-29 12:45:13 +00:00
|
|
|
isSet(2) ? tokens[2] : "",
|
|
|
|
isSet(3) ? parseUnsignedIntField(3) : 1U,
|
2024-03-04 07:40:38 +00:00
|
|
|
isSet(4) ? parseFloatField(4) : 1.0f,
|
2022-02-25 15:00:00 +00:00
|
|
|
isSet(5) ? tokenizeString<std::set<std::string>>(tokens[5], ",") : std::set<std::string>{},
|
|
|
|
isSet(6) ? tokenizeString<std::set<std::string>>(tokens[6], ",") : std::set<std::string>{},
|
2021-10-29 12:45:13 +00:00
|
|
|
isSet(7) ? ensureBase64(7) : ""
|
|
|
|
};
|
2021-10-12 12:18:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
static Machines parseBuilderLines(const std::vector<std::string> & builders)
|
|
|
|
{
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines result;
|
|
|
|
std::transform(builders.begin(), builders.end(), std::back_inserter(result), parseBuilderLine);
|
|
|
|
return result;
|
2017-05-02 11:17:37 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 11:44:10 +00:00
|
|
|
Machines getMachines()
|
|
|
|
{
|
2021-10-12 12:18:44 +00:00
|
|
|
const auto builderLines = expandBuilderLines(settings.builders);
|
|
|
|
return parseBuilderLines(builderLines);
|
2017-05-02 11:44:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 11:17:37 +00:00
|
|
|
}
|