From ebc9f36a8111ddecc8e265e8a6a70048218f244d Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 2 May 2017 13:17:37 +0200 Subject: Factor out machines.conf parsing This allows hydra-queue-runner to use it. --- src/libstore/machines.cc | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ src/libstore/machines.hh | 35 ++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/libstore/machines.cc create mode 100644 src/libstore/machines.hh (limited to 'src/libstore') diff --git a/src/libstore/machines.cc b/src/libstore/machines.cc new file mode 100644 index 000000000000..471ce8efb9a2 --- /dev/null +++ b/src/libstore/machines.cc @@ -0,0 +1,65 @@ +#include "machines.hh" +#include "util.hh" +#include "globals.hh" + +#include + +namespace nix { + +Machine::Machine(decltype(storeUri) storeUri, + decltype(systemTypes) systemTypes, + decltype(sshKey) sshKey, + decltype(maxJobs) maxJobs, + decltype(speedFactor) speedFactor, + decltype(supportedFeatures) supportedFeatures, + decltype(mandatoryFeatures) mandatoryFeatures) : + storeUri( + // Backwards compatibility: if the URI is a hostname, + // prepend ssh://. + storeUri.find("://") != std::string::npos || hasPrefix(storeUri, "local") || hasPrefix(storeUri, "remote") || hasPrefix(storeUri, "auto") + ? storeUri + : "ssh://" + storeUri), + systemTypes(systemTypes), + sshKey(sshKey), + maxJobs(maxJobs), + speedFactor(std::max(1U, speedFactor)), + supportedFeatures(supportedFeatures), + mandatoryFeatures(mandatoryFeatures) +{} + +bool Machine::allSupported(const std::set & 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 & features) const { + return std::all_of(mandatoryFeatures.begin(), mandatoryFeatures.end(), + [&](const string & feature) { + return features.count(feature); + }); +} + +void parseMachines(const std::string & s, Machines & machines) +{ + for (auto line : tokenizeString>(s, "\n")) { + chomp(line); + line.erase(std::find(line.begin(), line.end(), '#'), line.end()); + if (line.empty()) continue; + auto tokens = tokenizeString>(line); + auto sz = tokens.size(); + if (sz < 1) + throw FormatError("bad machine specification ā€˜%sā€™", line); + machines.emplace_back(tokens[0], + sz >= 2 ? tokenizeString>(tokens[1], ",") : std::vector{settings.thisSystem}, + sz >= 3 ? tokens[2] : "", + sz >= 4 ? std::stoull(tokens[3]) : 1LL, + sz >= 5 ? std::stoull(tokens[4]) : 1LL, + sz >= 6 ? tokenizeString>(tokens[5], ",") : std::set{}, + sz >= 7 ? tokenizeString>(tokens[6], ",") : std::set{}); + } +} + +} diff --git a/src/libstore/machines.hh b/src/libstore/machines.hh new file mode 100644 index 000000000000..96c4bd81a462 --- /dev/null +++ b/src/libstore/machines.hh @@ -0,0 +1,35 @@ +#pragma once + +#include "types.hh" + +namespace nix { + +struct Machine { + + const string storeUri; + const std::vector systemTypes; + const string sshKey; + const unsigned int maxJobs; + const unsigned int speedFactor; + const std::set supportedFeatures; + const std::set mandatoryFeatures; + bool enabled = true; + + bool allSupported(const std::set & features) const; + + bool mandatoryMet(const std::set & features) const; + + Machine(decltype(storeUri) storeUri, + decltype(systemTypes) systemTypes, + decltype(sshKey) sshKey, + decltype(maxJobs) maxJobs, + decltype(speedFactor) speedFactor, + decltype(supportedFeatures) supportedFeatures, + decltype(mandatoryFeatures) mandatoryFeatures); +}; + +typedef std::vector Machines; + +void parseMachines(const std::string & s, Machines & machines); + +} -- cgit 1.4.1