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),
|
|
|
|
speedFactor(std::max(1U, speedFactor)),
|
|
|
|
supportedFeatures(supportedFeatures),
|
2017-05-02 12:36:59 +00:00
|
|
|
mandatoryFeatures(mandatoryFeatures),
|
|
|
|
sshPublicHostKey(sshPublicHostKey)
|
2017-05-02 11:17:37 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
bool Machine::allSupported(const std::set<string> & features) const {
|
|
|
|
return std::all_of(features.begin(), features.end(),
|
|
|
|
[&](const string & feature) {
|
|
|
|
return supportedFeatures.count(feature) ||
|
|
|
|
mandatoryFeatures.count(feature);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Machine::mandatoryMet(const std::set<string> & features) const {
|
|
|
|
return std::all_of(mandatoryFeatures.begin(), mandatoryFeatures.end(),
|
|
|
|
[&](const string & feature) {
|
|
|
|
return features.count(feature);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:32:36 +00:00
|
|
|
ref<Store> Machine::openStore() const {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:17:37 +00:00
|
|
|
void parseMachines(const std::string & s, Machines & machines)
|
|
|
|
{
|
2017-05-02 11:44:10 +00:00
|
|
|
for (auto line : tokenizeString<std::vector<string>>(s, "\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] == '@') {
|
|
|
|
auto file = trim(std::string(line, 1));
|
|
|
|
try {
|
|
|
|
parseMachines(readFile(file), machines);
|
|
|
|
} catch (const SysError & e) {
|
|
|
|
if (e.errNo != ENOENT)
|
|
|
|
throw;
|
|
|
|
debug("cannot find machines file '%s'", file);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:17:37 +00:00
|
|
|
auto tokens = tokenizeString<std::vector<string>>(line);
|
|
|
|
auto sz = tokens.size();
|
|
|
|
if (sz < 1)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw FormatError("bad machine specification '%s'", line);
|
2017-05-02 12:36:59 +00:00
|
|
|
|
2017-05-05 15:45:22 +00:00
|
|
|
auto isSet = [&](size_t n) {
|
2017-05-02 12:36:59 +00:00
|
|
|
return tokens.size() > n && tokens[n] != "" && tokens[n] != "-";
|
|
|
|
};
|
|
|
|
|
2017-05-02 11:17:37 +00:00
|
|
|
machines.emplace_back(tokens[0],
|
2017-05-02 12:36:59 +00:00
|
|
|
isSet(1) ? tokenizeString<std::vector<string>>(tokens[1], ",") : std::vector<string>{settings.thisSystem},
|
|
|
|
isSet(2) ? tokens[2] : "",
|
|
|
|
isSet(3) ? std::stoull(tokens[3]) : 1LL,
|
|
|
|
isSet(4) ? std::stoull(tokens[4]) : 1LL,
|
|
|
|
isSet(5) ? tokenizeString<std::set<string>>(tokens[5], ",") : std::set<string>{},
|
|
|
|
isSet(6) ? tokenizeString<std::set<string>>(tokens[6], ",") : std::set<string>{},
|
|
|
|
isSet(7) ? tokens[7] : "");
|
2017-05-02 11:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:44:10 +00:00
|
|
|
Machines getMachines()
|
|
|
|
{
|
2019-04-01 19:09:35 +00:00
|
|
|
static auto machines = [&]() {
|
|
|
|
Machines machines;
|
|
|
|
parseMachines(settings.builders, machines);
|
|
|
|
return machines;
|
|
|
|
}();
|
2017-05-02 11:44:10 +00:00
|
|
|
return machines;
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:17:37 +00:00
|
|
|
}
|