Skip to content

Commit ebc9f36

Browse files
committed
Factor out machines.conf parsing
This allows hydra-queue-runner to use it.
1 parent 174b68a commit ebc9f36

3 files changed

Lines changed: 108 additions & 87 deletions

File tree

src/build-remote/build-remote.cc

Lines changed: 8 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <sys/time.h>
1010
#endif
1111

12+
#include "machines.hh"
1213
#include "shared.hh"
1314
#include "pathlocks.hh"
1415
#include "globals.hh"
@@ -22,92 +23,6 @@ using std::cin;
2223
static void handleAlarm(int sig) {
2324
}
2425

25-
class Machine {
26-
const std::set<string> supportedFeatures;
27-
const std::set<string> mandatoryFeatures;
28-
29-
public:
30-
const string storeUri;
31-
const std::vector<string> systemTypes;
32-
const string sshKey;
33-
const unsigned int maxJobs;
34-
const unsigned int speedFactor;
35-
bool enabled = true;
36-
37-
bool allSupported(const std::set<string> & features) const {
38-
return std::all_of(features.begin(), features.end(),
39-
[&](const string & feature) {
40-
return supportedFeatures.count(feature) ||
41-
mandatoryFeatures.count(feature);
42-
});
43-
}
44-
45-
bool mandatoryMet(const std::set<string> & features) const {
46-
return std::all_of(mandatoryFeatures.begin(), mandatoryFeatures.end(),
47-
[&](const string & feature) {
48-
return features.count(feature);
49-
});
50-
}
51-
52-
Machine(decltype(storeUri) storeUri,
53-
decltype(systemTypes) systemTypes,
54-
decltype(sshKey) sshKey,
55-
decltype(maxJobs) maxJobs,
56-
decltype(speedFactor) speedFactor,
57-
decltype(supportedFeatures) supportedFeatures,
58-
decltype(mandatoryFeatures) mandatoryFeatures) :
59-
supportedFeatures(supportedFeatures),
60-
mandatoryFeatures(mandatoryFeatures),
61-
storeUri(
62-
// Backwards compatibility: if the URI is a hostname,
63-
// prepend ssh://.
64-
storeUri.find("://") != std::string::npos || hasPrefix(storeUri, "local") || hasPrefix(storeUri, "remote") || hasPrefix(storeUri, "auto")
65-
? storeUri
66-
: "ssh://" + storeUri),
67-
systemTypes(systemTypes),
68-
sshKey(sshKey),
69-
maxJobs(maxJobs),
70-
speedFactor(std::max(1U, speedFactor))
71-
{}
72-
};
73-
74-
static std::vector<Machine> readConf()
75-
{
76-
auto conf = getEnv("NIX_REMOTE_SYSTEMS", SYSCONFDIR "/nix/machines");
77-
78-
auto machines = std::vector<Machine>{};
79-
auto lines = std::vector<string>{};
80-
try {
81-
lines = tokenizeString<std::vector<string>>(readFile(conf), "\n");
82-
} catch (const SysError & e) {
83-
if (e.errNo != ENOENT)
84-
throw;
85-
}
86-
for (auto line : lines) {
87-
chomp(line);
88-
line.erase(std::find(line.begin(), line.end(), '#'), line.end());
89-
if (line.empty()) {
90-
continue;
91-
}
92-
auto tokens = tokenizeString<std::vector<string>>(line);
93-
auto sz = tokens.size();
94-
if (sz < 1)
95-
throw FormatError("bad machines.conf file ‘%1%’", conf);
96-
machines.emplace_back(tokens[0],
97-
sz >= 2 ? tokenizeString<std::vector<string>>(tokens[1], ",") : std::vector<string>{settings.thisSystem},
98-
sz >= 3 ? tokens[2] : "",
99-
sz >= 4 ? std::stoull(tokens[3]) : 1LL,
100-
sz >= 5 ? std::stoull(tokens[4]) : 1LL,
101-
sz >= 6 ?
102-
tokenizeString<std::set<string>>(tokens[5], ",") :
103-
std::set<string>{},
104-
sz >= 7 ?
105-
tokenizeString<std::set<string>>(tokens[6], ",") :
106-
std::set<string>{});
107-
}
108-
return machines;
109-
}
110-
11126
std::string escapeUri(std::string uri)
11227
{
11328
std::replace(uri.begin(), uri.end(), '/', '_');
@@ -147,7 +62,13 @@ int main (int argc, char * * argv)
14762
std::shared_ptr<Store> sshStore;
14863
AutoCloseFD bestSlotLock;
14964

150-
auto machines = readConf();
65+
Machines machines;
66+
try {
67+
parseMachines(readFile(getEnv("NIX_REMOTE_SYSTEMS", SYSCONFDIR "/nix/machines")), machines);
68+
} catch (const SysError & e) {
69+
if (e.errNo != ENOENT)
70+
throw;
71+
}
15172
debug("got %d remote builders", machines.size());
15273

15374
if (machines.empty()) {

src/libstore/machines.cc

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "machines.hh"
2+
#include "util.hh"
3+
#include "globals.hh"
4+
5+
#include <algorithm>
6+
7+
namespace nix {
8+
9+
Machine::Machine(decltype(storeUri) storeUri,
10+
decltype(systemTypes) systemTypes,
11+
decltype(sshKey) sshKey,
12+
decltype(maxJobs) maxJobs,
13+
decltype(speedFactor) speedFactor,
14+
decltype(supportedFeatures) supportedFeatures,
15+
decltype(mandatoryFeatures) mandatoryFeatures) :
16+
storeUri(
17+
// Backwards compatibility: if the URI is a hostname,
18+
// prepend ssh://.
19+
storeUri.find("://") != std::string::npos || hasPrefix(storeUri, "local") || hasPrefix(storeUri, "remote") || hasPrefix(storeUri, "auto")
20+
? storeUri
21+
: "ssh://" + storeUri),
22+
systemTypes(systemTypes),
23+
sshKey(sshKey),
24+
maxJobs(maxJobs),
25+
speedFactor(std::max(1U, speedFactor)),
26+
supportedFeatures(supportedFeatures),
27+
mandatoryFeatures(mandatoryFeatures)
28+
{}
29+
30+
bool Machine::allSupported(const std::set<string> & features) const {
31+
return std::all_of(features.begin(), features.end(),
32+
[&](const string & feature) {
33+
return supportedFeatures.count(feature) ||
34+
mandatoryFeatures.count(feature);
35+
});
36+
}
37+
38+
bool Machine::mandatoryMet(const std::set<string> & features) const {
39+
return std::all_of(mandatoryFeatures.begin(), mandatoryFeatures.end(),
40+
[&](const string & feature) {
41+
return features.count(feature);
42+
});
43+
}
44+
45+
void parseMachines(const std::string & s, Machines & machines)
46+
{
47+
for (auto line : tokenizeString<std::vector<string>>(s, "\n")) {
48+
chomp(line);
49+
line.erase(std::find(line.begin(), line.end(), '#'), line.end());
50+
if (line.empty()) continue;
51+
auto tokens = tokenizeString<std::vector<string>>(line);
52+
auto sz = tokens.size();
53+
if (sz < 1)
54+
throw FormatError("bad machine specification ‘%s’", line);
55+
machines.emplace_back(tokens[0],
56+
sz >= 2 ? tokenizeString<std::vector<string>>(tokens[1], ",") : std::vector<string>{settings.thisSystem},
57+
sz >= 3 ? tokens[2] : "",
58+
sz >= 4 ? std::stoull(tokens[3]) : 1LL,
59+
sz >= 5 ? std::stoull(tokens[4]) : 1LL,
60+
sz >= 6 ? tokenizeString<std::set<string>>(tokens[5], ",") : std::set<string>{},
61+
sz >= 7 ? tokenizeString<std::set<string>>(tokens[6], ",") : std::set<string>{});
62+
}
63+
}
64+
65+
}

src/libstore/machines.hh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include "types.hh"
4+
5+
namespace nix {
6+
7+
struct Machine {
8+
9+
const string storeUri;
10+
const std::vector<string> systemTypes;
11+
const string sshKey;
12+
const unsigned int maxJobs;
13+
const unsigned int speedFactor;
14+
const std::set<string> supportedFeatures;
15+
const std::set<string> mandatoryFeatures;
16+
bool enabled = true;
17+
18+
bool allSupported(const std::set<string> & features) const;
19+
20+
bool mandatoryMet(const std::set<string> & features) const;
21+
22+
Machine(decltype(storeUri) storeUri,
23+
decltype(systemTypes) systemTypes,
24+
decltype(sshKey) sshKey,
25+
decltype(maxJobs) maxJobs,
26+
decltype(speedFactor) speedFactor,
27+
decltype(supportedFeatures) supportedFeatures,
28+
decltype(mandatoryFeatures) mandatoryFeatures);
29+
};
30+
31+
typedef std::vector<Machine> Machines;
32+
33+
void parseMachines(const std::string & s, Machines & machines);
34+
35+
}

0 commit comments

Comments
 (0)