1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <memory>
#include <set>
#include <tuple>
#include <absl/strings/ascii.h>
#include <absl/strings/match.h>
#include <absl/strings/str_cat.h>
#include <glog/logging.h>
#include "derivations.hh"
#include "globals.hh"
#include "legacy.hh"
#include "local-store.hh"
#include "machines.hh"
#include "pathlocks.hh"
#include "serialise.hh"
#include "shared.hh"
#include "store-api.hh"
using namespace nix;
using std::cin;
static void handleAlarm(int sig) {}
std::string escapeUri(std::string uri) {
std::replace(uri.begin(), uri.end(), '/', '_');
return uri;
}
static std::string currentLoad;
static AutoCloseFD openSlotLock(const Machine& m, unsigned long long slot) {
return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot),
true);
}
static bool allSupportedLocally(const std::set<std::string>& requiredFeatures) {
for (auto& feature : requiredFeatures) {
if (settings.systemFeatures.get().count(feature) == 0u) {
return false;
}
}
return true;
}
static int _main(int argc, char* argv[]) {
{
/* Ensure we don't get any SSH passphrase or host key popups. */
unsetenv("DISPLAY");
unsetenv("SSH_ASKPASS");
if (argc != 2) {
throw UsageError("called without required arguments");
}
FdSource source(STDIN_FILENO);
/* Read the parent's settings. */
while (readInt(source) != 0u) {
auto name = readString(source);
auto value = readString(source);
settings.set(name, value);
}
settings.maxBuildJobs.set("1"); // hack to make tests with local?root= work
initPlugins();
auto store = openStore().cast<LocalStore>();
/* It would be more appropriate to use $XDG_RUNTIME_DIR, since
that gets cleared on reboot, but it wouldn't work on macOS. */
currentLoad = store->stateDir + "/current-load";
std::shared_ptr<Store> sshStore;
AutoCloseFD bestSlotLock;
auto machines = getMachines();
DLOG(INFO) << "got " << machines.size() << " remote builders";
if (machines.empty()) {
std::cerr << "# decline-permanently\n";
return 0;
}
std::string drvPath;
std::string storeUri;
while (true) {
try {
auto s = readString(source);
if (s != "try") {
return 0;
}
} catch (EndOfFile&) {
return 0;
}
auto amWilling = readInt(source);
auto neededSystem = readString(source);
source >> drvPath;
auto requiredFeatures = readStrings<std::set<std::string>>(source);
auto canBuildLocally =
(amWilling != 0u) &&
(neededSystem == settings.thisSystem ||
settings.extraPlatforms.get().count(neededSystem) > 0) &&
allSupportedLocally(requiredFeatures);
/* Error ignored here, will be caught later */
mkdir(currentLoad.c_str(), 0777);
while (true) {
bestSlotLock = -1;
AutoCloseFD lock = openLockFile(currentLoad + "/main-lock", true);
lockFile(lock.get(), ltWrite, true);
bool rightType = false;
Machine* bestMachine = nullptr;
unsigned long long bestLoad = 0;
for (auto& m : machines) {
DLOG(INFO) << "considering building on remote machine '" << m.storeUri
<< "'";
if (m.enabled &&
std::find(m.systemTypes.begin(), m.systemTypes.end(),
neededSystem) != m.systemTypes.end() &&
m.allSupported(requiredFeatures) &&
m.mandatoryMet(requiredFeatures)) {
rightType = true;
AutoCloseFD free;
unsigned long long load = 0;
for (unsigned long long slot = 0; slot < m.maxJobs; ++slot) {
auto slotLock = openSlotLock(m, slot);
if (lockFile(slotLock.get(), ltWrite, false)) {
if (!free) {
free = std::move(slotLock);
}
} else {
++load;
}
}
if (!free) {
continue;
}
bool best = false;
if (!bestSlotLock) {
best = true;
} else if (load / m.speedFactor <
bestLoad / bestMachine->speedFactor) {
best = true;
} else if (load / m.speedFactor ==
bestLoad / bestMachine->speedFactor) {
if (m.speedFactor > bestMachine->speedFactor) {
best = true;
} else if (m.speedFactor == bestMachine->speedFactor) {
if (load < bestLoad) {
best = true;
}
}
}
if (best) {
bestLoad = load;
bestSlotLock = std::move(free);
bestMachine = &m;
}
}
}
if (!bestSlotLock) {
if (rightType && !canBuildLocally) {
std::cerr << "# postpone\n";
} else {
std::cerr << "# decline\n";
}
break;
}
futimens(bestSlotLock.get(), nullptr);
lock = -1;
try {
DLOG(INFO) << "connecting to '" << bestMachine->storeUri << "'";
Store::Params storeParams;
if (absl::StartsWith(bestMachine->storeUri, "ssh://")) {
storeParams["max-connections"] = "1";
storeParams["log-fd"] = "4";
if (!bestMachine->sshKey.empty()) {
storeParams["ssh-key"] = bestMachine->sshKey;
}
}
sshStore = openStore(bestMachine->storeUri, storeParams);
sshStore->connect();
storeUri = bestMachine->storeUri;
} catch (std::exception& e) {
auto msg = absl::StripTrailingAsciiWhitespace(drainFD(5, false));
LOG(ERROR) << "cannot build on '" << bestMachine->storeUri
<< "': " << e.what()
<< (msg.empty() ? "" : absl::StrCat(": ", msg));
bestMachine->enabled = false;
continue;
}
goto connected;
}
}
connected:
close(5);
std::cerr << "# accept\n" << storeUri << "\n";
auto inputs = readStrings<PathSet>(source);
auto outputs = readStrings<PathSet>(source);
AutoCloseFD uploadLock = openLockFile(
currentLoad + "/" + escapeUri(storeUri) + ".upload-lock", true);
{
DLOG(INFO) << "waiting for the upload lock to '" << storeUri << "'";
auto old = signal(SIGALRM, handleAlarm);
alarm(15 * 60);
if (!lockFile(uploadLock.get(), ltWrite, true)) {
LOG(ERROR) << "somebody is hogging the upload lock, continuing...";
}
alarm(0);
signal(SIGALRM, old);
}
auto substitute =
settings.buildersUseSubstitutes ? Substitute : NoSubstitute;
{
DLOG(INFO) << "copying dependencies to '" << storeUri << "'";
copyPaths(store, ref<Store>(sshStore), inputs, NoRepair, NoCheckSigs,
substitute);
}
uploadLock = -1;
BasicDerivation drv(
readDerivation(store->realStoreDir + "/" + baseNameOf(drvPath)));
drv.inputSrcs = inputs;
auto result = sshStore->buildDerivation(drvPath, drv);
if (!result.success()) {
throw Error("build of '%s' on '%s' failed: %s", drvPath, storeUri,
result.errorMsg);
}
PathSet missing;
for (auto& path : outputs) {
if (!store->isValidPath(path)) {
missing.insert(path);
}
}
if (!missing.empty()) {
DLOG(INFO) << "copying outputs from '" << storeUri << "'";
store->locksHeld.insert(missing.begin(), missing.end()); /* FIXME: ugly */
copyPaths(ref<Store>(sshStore), store, missing, NoRepair, NoCheckSigs,
NoSubstitute);
}
return 0;
}
}
static RegisterLegacyCommand s1("build-remote", _main);
|