lix/src/build-remote/build-remote.cc

240 lines
8 KiB
C++
Raw Normal View History

2016-07-18 22:50:27 +00:00
#include <cstdlib>
#include <cstring>
#include <algorithm>
#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__
#include <sys/time.h>
#endif
2016-07-18 22:50:27 +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"
#include "local-store.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
}
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;
2017-03-03 15:18:49 +00:00
static AutoCloseFD openSlotLock(const Machine & m, unsigned long long slot)
{
return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot), true);
2016-07-18 22:50:27 +00:00
}
int main (int argc, char * * argv)
{
return handleExceptions(argv[0], [&]() {
initNix();
2017-03-03 15:18:49 +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. */
unsetenv("DISPLAY");
unsetenv("SSH_ASKPASS");
2016-07-18 22:50:27 +00:00
if (argc != 2)
2016-07-18 22:50:27 +00:00
throw UsageError("called without required arguments");
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
auto store = openStore().cast<LocalStore>();
2016-07-18 22:50:27 +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. */
currentLoad = store->stateDir + "/current-load";
2016-07-18 22:50:27 +00:00
std::shared_ptr<Store> sshStore;
AutoCloseFD bestSlotLock;
auto machines = getMachines();
2017-05-01 12:43:14 +00:00
debug("got %d remote builders", machines.size());
if (machines.empty()) {
std::cerr << "# decline-permanently\n";
return;
}
2016-07-18 22:50:27 +00:00
string drvPath;
string storeUri;
while (true) {
try {
auto s = readString(source);
if (s != "try") return;
} catch (EndOfFile &) { return; }
auto amWilling = readInt(source);
auto neededSystem = readString(source);
source >> drvPath;
auto requiredFeatures = readStrings<std::set<std::string>>(source);
auto canBuildLocally = amWilling && (neededSystem == settings.thisSystem);
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;
2016-07-18 22:50:27 +00:00
unsigned long long bestLoad = 0;
for (auto & m : machines) {
debug("considering building on remote machine '%s'", m.storeUri);
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;
unsigned long long load = 0;
for (unsigned long long 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
std::cerr << "# decline\n";
2016-07-18 22:50:27 +00:00
break;
}
#if __APPLE__
futimes(bestSlotLock.get(), NULL);
#else
2016-07-18 22:50:27 +00:00
futimens(bestSlotLock.get(), NULL);
#endif
2016-07-18 22:50:27 +00:00
lock = -1;
try {
Activity act(*logger, lvlTalkative, actUnknown, fmt("connecting to '%s'", bestMachine->storeUri));
Store::Params storeParams{{"max-connections", "1"}, {"log-fd", "4"}};
if (bestMachine->sshKey != "")
storeParams["ssh-key"] = bestMachine->sshKey;
sshStore = openStore(bestMachine->storeUri, storeParams);
sshStore->connect();
storeUri = bestMachine->storeUri;
2016-07-18 22:50:27 +00:00
} catch (std::exception & e) {
printError("unable to open SSH connection to '%s': %s; trying other available machines...",
bestMachine->storeUri, e.what());
2016-07-18 22:50:27 +00:00
bestMachine->enabled = false;
continue;
}
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:
std::cerr << "# accept\n" << storeUri << "\n";
auto inputs = readStrings<PathSet>(source);
auto outputs = readStrings<PathSet>(source);
{
Activity act(*logger, lvlTalkative, actUnknown, fmt("waiting for the upload lock to '%s'", storeUri));
AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + escapeUri(storeUri) + ".upload-lock", true);
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);
copyPaths(store, ref<Store>(sshStore), inputs, NoRepair, NoCheckSigs);
uploadLock = -1;
}
2016-07-18 22:50:27 +00:00
BasicDerivation drv(readDerivation(store->realStoreDir + "/" + baseNameOf(drvPath)));
drv.inputSrcs = inputs;
printInfo("building '%s' on '%s'", drvPath, storeUri);
auto result = sshStore->buildDerivation(drvPath, drv);
if (!result.success())
throw Error("build of '%s' on '%s' failed: %s", drvPath, storeUri, result.errorMsg);
2016-07-18 22:50:27 +00:00
PathSet missing;
for (auto & path : outputs)
if (!store->isValidPath(path)) missing.insert(path);
if (!missing.empty()) {
setenv("NIX_HELD_LOCKS", concatStringsSep(" ", missing).c_str(), 1); /* FIXME: ugly */
copyPaths(ref<Store>(sshStore), store, missing, NoRepair, NoCheckSigs);
2016-07-18 22:50:27 +00:00
}
2016-07-18 22:50:27 +00:00
return;
});
}