2016-07-18 22:50:27 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <algorithm>
|
2017-01-10 15:29:06 +00:00
|
|
|
#include <set>
|
2016-07-18 22:50:27 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <tuple>
|
|
|
|
#include <iomanip>
|
2017-01-24 12:57:26 +00:00
|
|
|
#if __APPLE__
|
2017-01-24 11:22:02 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2016-07-18 22:50:27 +00:00
|
|
|
|
2017-05-02 11:17:37 +00:00
|
|
|
#include "machines.hh"
|
2016-07-18 22:50:27 +00:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "pathlocks.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "serialise.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "derivations.hh"
|
2017-10-23 17:06:55 +00:00
|
|
|
#include "local-store.hh"
|
2020-03-30 12:29:29 +00:00
|
|
|
#include "../nix/legacy.hh"
|
2016-07-18 22:50:27 +00:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
using std::cin;
|
|
|
|
|
2017-03-03 15:18:49 +00:00
|
|
|
static void handleAlarm(int sig) {
|
2016-07-18 22:50:27 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 15:35:30 +00:00
|
|
|
std::string escapeUri(std::string uri)
|
|
|
|
{
|
|
|
|
std::replace(uri.begin(), uri.end(), '/', '_');
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2016-07-18 22:50:27 +00:00
|
|
|
static string currentLoad;
|
|
|
|
|
2020-07-30 11:10:49 +00:00
|
|
|
static AutoCloseFD openSlotLock(const Machine & m, uint64_t slot)
|
2017-01-24 14:28:50 +00:00
|
|
|
{
|
2017-05-01 15:35:30 +00:00
|
|
|
return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot), true);
|
2016-07-18 22:50:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 05:03:25 +00:00
|
|
|
static bool allSupportedLocally(const std::set<std::string>& requiredFeatures) {
|
|
|
|
for (auto & feature : requiredFeatures)
|
|
|
|
if (!settings.systemFeatures.get().count(feature)) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-26 09:35:46 +00:00
|
|
|
static int _main(int argc, char * * argv)
|
2016-07-18 22:50:27 +00:00
|
|
|
{
|
2018-10-26 09:35:46 +00:00
|
|
|
{
|
2017-10-24 11:41:52 +00:00
|
|
|
logger = makeJSONLogger(*logger);
|
|
|
|
|
2016-07-18 22:50:27 +00:00
|
|
|
/* Ensure we don't get any SSH passphrase or host key popups. */
|
2017-05-01 15:35:30 +00:00
|
|
|
unsetenv("DISPLAY");
|
|
|
|
unsetenv("SSH_ASKPASS");
|
2016-07-18 22:50:27 +00:00
|
|
|
|
2017-10-23 18:43:04 +00:00
|
|
|
if (argc != 2)
|
2016-07-18 22:50:27 +00:00
|
|
|
throw UsageError("called without required arguments");
|
|
|
|
|
2017-10-23 18:43:04 +00:00
|
|
|
verbosity = (Verbosity) std::stoll(argv[1]);
|
|
|
|
|
|
|
|
FdSource source(STDIN_FILENO);
|
|
|
|
|
|
|
|
/* Read the parent's settings. */
|
|
|
|
while (readInt(source)) {
|
|
|
|
auto name = readString(source);
|
|
|
|
auto value = readString(source);
|
|
|
|
settings.set(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.maxBuildJobs.set("1"); // hack to make tests with local?root= work
|
2016-07-18 22:50:27 +00:00
|
|
|
|
2018-02-08 16:26:18 +00:00
|
|
|
initPlugins();
|
|
|
|
|
2017-10-23 18:43:04 +00:00
|
|
|
auto store = openStore().cast<LocalStore>();
|
2016-07-18 22:50:27 +00:00
|
|
|
|
2017-05-01 13:46:47 +00:00
|
|
|
/* It would be more appropriate to use $XDG_RUNTIME_DIR, since
|
2017-07-30 10:28:50 +00:00
|
|
|
that gets cleared on reboot, but it wouldn't work on macOS. */
|
2017-10-23 17:06:55 +00:00
|
|
|
currentLoad = store->stateDir + "/current-load";
|
2016-07-18 22:50:27 +00:00
|
|
|
|
|
|
|
std::shared_ptr<Store> sshStore;
|
|
|
|
AutoCloseFD bestSlotLock;
|
|
|
|
|
2017-05-02 11:44:10 +00:00
|
|
|
auto machines = getMachines();
|
2017-05-01 12:43:14 +00:00
|
|
|
debug("got %d remote builders", machines.size());
|
|
|
|
|
2017-05-02 10:16:29 +00:00
|
|
|
if (machines.empty()) {
|
|
|
|
std::cerr << "# decline-permanently\n";
|
2018-10-26 09:35:46 +00:00
|
|
|
return 0;
|
2017-05-02 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
std::optional<StorePath> drvPath;
|
2017-05-01 15:35:30 +00:00
|
|
|
string storeUri;
|
2017-10-23 18:43:04 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto s = readString(source);
|
2018-10-26 09:35:46 +00:00
|
|
|
if (s != "try") return 0;
|
|
|
|
} catch (EndOfFile &) { return 0; }
|
2017-10-23 18:43:04 +00:00
|
|
|
|
|
|
|
auto amWilling = readInt(source);
|
|
|
|
auto neededSystem = readString(source);
|
2019-12-05 18:11:09 +00:00
|
|
|
drvPath = store->parseStorePath(readString(source));
|
2017-10-23 18:43:04 +00:00
|
|
|
auto requiredFeatures = readStrings<std::set<std::string>>(source);
|
|
|
|
|
2020-08-05 16:58:00 +00:00
|
|
|
auto canBuildLocally = amWilling
|
2019-03-06 05:03:25 +00:00
|
|
|
&& ( neededSystem == settings.thisSystem
|
|
|
|
|| settings.extraPlatforms.get().count(neededSystem) > 0)
|
|
|
|
&& allSupportedLocally(requiredFeatures);
|
2016-07-18 22:50:27 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2017-03-03 15:18:49 +00:00
|
|
|
Machine * bestMachine = nullptr;
|
2020-07-30 11:10:49 +00:00
|
|
|
uint64_t bestLoad = 0;
|
2016-07-18 22:50:27 +00:00
|
|
|
for (auto & m : machines) {
|
2017-10-23 18:43:04 +00:00
|
|
|
debug("considering building on remote machine '%s'", m.storeUri);
|
2017-05-01 15:35:30 +00:00
|
|
|
|
2016-07-18 22:50:27 +00:00
|
|
|
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;
|
2020-07-30 11:10:49 +00:00
|
|
|
uint64_t load = 0;
|
|
|
|
for (uint64_t slot = 0; slot < m.maxJobs; ++slot) {
|
2017-01-25 11:51:35 +00:00
|
|
|
auto slotLock = openSlotLock(m, slot);
|
2016-07-18 22:50:27 +00:00
|
|
|
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) {
|
2017-03-03 15:18:49 +00:00
|
|
|
if (rightType && !canBuildLocally)
|
|
|
|
std::cerr << "# postpone\n";
|
|
|
|
else
|
2020-08-05 16:58:00 +00:00
|
|
|
{
|
|
|
|
// build the hint template.
|
2020-08-05 17:21:36 +00:00
|
|
|
string hintstring = "derivation: %s\nrequired (system, features): (%s, %s)";
|
2020-08-05 16:58:00 +00:00
|
|
|
hintstring += "\n%s available machines:";
|
|
|
|
hintstring += "\n(systems, maxjobs, supportedFeatures, mandatoryFeatures)";
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < machines.size(); ++i) {
|
|
|
|
hintstring += "\n(%s, %s, %s, %s)";
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the template values.
|
2020-08-05 17:26:06 +00:00
|
|
|
string drvstr;
|
|
|
|
if (drvPath.has_value())
|
|
|
|
drvstr = drvPath->to_string();
|
|
|
|
else
|
|
|
|
drvstr = "<unknown>";
|
|
|
|
|
2020-08-05 16:58:00 +00:00
|
|
|
auto hint = hintformat(hintstring);
|
|
|
|
hint
|
2020-08-05 17:26:06 +00:00
|
|
|
% drvstr
|
2020-08-05 16:58:00 +00:00
|
|
|
% neededSystem
|
|
|
|
% concatStringsSep<StringSet>(", ", requiredFeatures)
|
|
|
|
% machines.size();
|
|
|
|
|
|
|
|
for (auto & m : machines) {
|
|
|
|
hint % concatStringsSep<vector<string>>(", ", m.systemTypes)
|
|
|
|
% m.maxJobs
|
|
|
|
% concatStringsSep<StringSet>(", ", m.supportedFeatures)
|
|
|
|
% concatStringsSep<StringSet>(", ", m.mandatoryFeatures);
|
|
|
|
}
|
|
|
|
|
2020-08-11 16:29:43 +00:00
|
|
|
logErrorInfo(lvlInfo, {
|
2020-08-05 16:58:00 +00:00
|
|
|
.name = "Remote build",
|
|
|
|
.description = "Failed to find a machine for remote build!",
|
|
|
|
.hint = hint
|
|
|
|
});
|
|
|
|
|
2017-03-03 15:18:49 +00:00
|
|
|
std::cerr << "# decline\n";
|
2020-08-05 16:58:00 +00:00
|
|
|
}
|
2016-07-18 22:50:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-24 11:22:02 +00:00
|
|
|
#if __APPLE__
|
|
|
|
futimes(bestSlotLock.get(), NULL);
|
|
|
|
#else
|
2016-07-18 22:50:27 +00:00
|
|
|
futimens(bestSlotLock.get(), NULL);
|
2017-01-24 11:22:02 +00:00
|
|
|
#endif
|
2016-07-18 22:50:27 +00:00
|
|
|
|
|
|
|
lock = -1;
|
|
|
|
|
|
|
|
try {
|
2017-05-01 15:35:30 +00:00
|
|
|
|
2017-10-24 11:41:52 +00:00
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("connecting to '%s'", bestMachine->storeUri));
|
|
|
|
|
2017-10-24 13:16:18 +00:00
|
|
|
Store::Params storeParams;
|
2017-11-25 00:03:23 +00:00
|
|
|
if (hasPrefix(bestMachine->storeUri, "ssh://")) {
|
2019-11-13 16:18:17 +00:00
|
|
|
storeParams["max-connections"] = "1";
|
2017-10-24 13:16:18 +00:00
|
|
|
storeParams["log-fd"] = "4";
|
|
|
|
if (bestMachine->sshKey != "")
|
|
|
|
storeParams["ssh-key"] = bestMachine->sshKey;
|
|
|
|
}
|
2017-05-01 15:35:30 +00:00
|
|
|
|
|
|
|
sshStore = openStore(bestMachine->storeUri, storeParams);
|
2017-05-02 12:18:46 +00:00
|
|
|
sshStore->connect();
|
2017-05-01 15:35:30 +00:00
|
|
|
storeUri = bestMachine->storeUri;
|
|
|
|
|
2016-07-18 22:50:27 +00:00
|
|
|
} catch (std::exception & e) {
|
2018-03-20 14:17:59 +00:00
|
|
|
auto msg = chomp(drainFD(5, false));
|
2020-06-15 12:12:39 +00:00
|
|
|
logError({
|
2020-06-02 14:22:24 +00:00
|
|
|
.name = "Remote build",
|
|
|
|
.hint = hintfmt("cannot build on '%s': %s%s",
|
|
|
|
bestMachine->storeUri, e.what(),
|
|
|
|
(msg.empty() ? "" : ": " + msg))
|
2020-05-03 14:01:25 +00:00
|
|
|
});
|
2016-07-18 22:50:27 +00:00
|
|
|
bestMachine->enabled = false;
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-01 15:35:30 +00:00
|
|
|
|
2016-07-18 22:50:27 +00:00
|
|
|
goto connected;
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 15:18:49 +00:00
|
|
|
|
2016-07-18 22:50:27 +00:00
|
|
|
connected:
|
2018-03-20 14:17:59 +00:00
|
|
|
close(5);
|
|
|
|
|
2017-10-24 12:24:57 +00:00
|
|
|
std::cerr << "# accept\n" << storeUri << "\n";
|
2017-05-01 15:35:30 +00:00
|
|
|
|
2017-10-23 18:43:04 +00:00
|
|
|
auto inputs = readStrings<PathSet>(source);
|
|
|
|
auto outputs = readStrings<PathSet>(source);
|
2017-05-01 15:35:30 +00:00
|
|
|
|
2017-10-24 12:47:23 +00:00
|
|
|
AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + escapeUri(storeUri) + ".upload-lock", true);
|
|
|
|
|
2017-10-24 11:41:52 +00:00
|
|
|
{
|
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("waiting for the upload lock to '%s'", storeUri));
|
2017-05-01 15:35:30 +00:00
|
|
|
|
2017-10-24 11:41:52 +00:00
|
|
|
auto old = signal(SIGALRM, handleAlarm);
|
|
|
|
alarm(15 * 60);
|
|
|
|
if (!lockFile(uploadLock.get(), ltWrite, true))
|
|
|
|
printError("somebody is hogging the upload lock for '%s', continuing...");
|
|
|
|
alarm(0);
|
|
|
|
signal(SIGALRM, old);
|
2017-10-24 12:47:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 21:40:07 +00:00
|
|
|
auto substitute = settings.buildersUseSubstitutes ? Substitute : NoSubstitute;
|
|
|
|
|
2017-10-24 12:47:23 +00:00
|
|
|
{
|
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("copying dependencies to '%s'", storeUri));
|
2019-12-05 18:11:09 +00:00
|
|
|
copyPaths(store, ref<Store>(sshStore), store->parseStorePathSet(inputs), NoRepair, NoCheckSigs, substitute);
|
2017-10-24 11:41:52 +00:00
|
|
|
}
|
2016-07-18 22:50:27 +00:00
|
|
|
|
2017-10-24 12:47:23 +00:00
|
|
|
uploadLock = -1;
|
|
|
|
|
2020-06-12 11:04:52 +00:00
|
|
|
auto drv = store->readDerivation(*drvPath);
|
2019-12-05 18:11:09 +00:00
|
|
|
drv.inputSrcs = store->parseStorePathSet(inputs);
|
2017-05-01 13:00:39 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
auto result = sshStore->buildDerivation(*drvPath, drv);
|
2017-05-08 12:27:12 +00:00
|
|
|
|
|
|
|
if (!result.success())
|
2019-12-05 18:11:09 +00:00
|
|
|
throw Error("build of '%s' on '%s' failed: %s", store->printStorePath(*drvPath), storeUri, result.errorMsg);
|
2016-07-18 22:50:27 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet missing;
|
2017-03-16 12:50:01 +00:00
|
|
|
for (auto & path : outputs)
|
2019-12-05 18:11:09 +00:00
|
|
|
if (!store->isValidPath(store->parseStorePath(path))) missing.insert(store->parseStorePath(path));
|
2017-03-16 12:50:01 +00:00
|
|
|
|
|
|
|
if (!missing.empty()) {
|
2017-10-24 12:47:23 +00:00
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("copying outputs from '%s'", storeUri));
|
2019-12-05 18:11:09 +00:00
|
|
|
for (auto & i : missing)
|
|
|
|
store->locksHeld.insert(store->printStorePath(i)); /* FIXME: ugly */
|
2018-03-13 16:56:51 +00:00
|
|
|
copyPaths(ref<Store>(sshStore), store, missing, NoRepair, NoCheckSigs, NoSubstitute);
|
2016-07-18 22:50:27 +00:00
|
|
|
}
|
2017-03-16 12:50:01 +00:00
|
|
|
|
2018-10-26 09:35:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-07-18 22:50:27 +00:00
|
|
|
}
|
2018-10-26 09:35:46 +00:00
|
|
|
|
|
|
|
static RegisterLegacyCommand s1("build-remote", _main);
|