2016-07-19 00:50:27 +02:00
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <algorithm>
|
2017-01-10 16:29:06 +01:00
|
|
|
|
#include <set>
|
2016-07-19 00:50:27 +02:00
|
|
|
|
#include <memory>
|
|
|
|
|
#include <tuple>
|
|
|
|
|
#include <iomanip>
|
2017-01-24 13:57:26 +01:00
|
|
|
|
#if __APPLE__
|
2017-01-24 12:22:02 +01:00
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#endif
|
2016-07-19 00:50:27 +02:00
|
|
|
|
|
2017-05-02 13:17:37 +02:00
|
|
|
|
#include "machines.hh"
|
2016-07-19 00:50:27 +02:00
|
|
|
|
#include "shared.hh"
|
|
|
|
|
#include "pathlocks.hh"
|
|
|
|
|
#include "globals.hh"
|
|
|
|
|
#include "serialise.hh"
|
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
#include "derivations.hh"
|
|
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
using std::cin;
|
|
|
|
|
|
2017-03-03 16:18:49 +01:00
|
|
|
|
static void handleAlarm(int sig) {
|
2016-07-19 00:50:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-01 17:35:30 +02:00
|
|
|
|
std::string escapeUri(std::string uri)
|
|
|
|
|
{
|
|
|
|
|
std::replace(uri.begin(), uri.end(), '/', '_');
|
|
|
|
|
return uri;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 00:50:27 +02:00
|
|
|
|
static string currentLoad;
|
|
|
|
|
|
2017-03-03 16:18:49 +01:00
|
|
|
|
static AutoCloseFD openSlotLock(const Machine & m, unsigned long long slot)
|
2017-01-24 15:28:50 +01:00
|
|
|
|
{
|
2017-05-01 17:35:30 +02:00
|
|
|
|
return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot), true);
|
2016-07-19 00:50:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main (int argc, char * * argv)
|
|
|
|
|
{
|
|
|
|
|
return handleExceptions(argv[0], [&]() {
|
|
|
|
|
initNix();
|
2017-03-03 16:18:49 +01:00
|
|
|
|
|
2016-07-19 00:50:27 +02:00
|
|
|
|
/* Ensure we don't get any SSH passphrase or host key popups. */
|
2017-05-01 17:35:30 +02:00
|
|
|
|
unsetenv("DISPLAY");
|
|
|
|
|
unsetenv("SSH_ASKPASS");
|
2016-07-19 00:50:27 +02:00
|
|
|
|
|
2017-05-02 13:44:10 +02:00
|
|
|
|
if (argc != 6)
|
2016-07-19 00:50:27 +02:00
|
|
|
|
throw UsageError("called without required arguments");
|
|
|
|
|
|
|
|
|
|
auto store = openStore();
|
|
|
|
|
|
|
|
|
|
auto localSystem = argv[1];
|
2017-05-01 14:43:14 +02:00
|
|
|
|
settings.maxSilentTime = std::stoll(argv[2]);
|
|
|
|
|
settings.buildTimeout = std::stoll(argv[3]);
|
|
|
|
|
verbosity = (Verbosity) std::stoll(argv[4]);
|
2017-05-02 13:44:10 +02:00
|
|
|
|
settings.builders = argv[5];
|
2016-07-19 00:50:27 +02:00
|
|
|
|
|
2017-05-01 15:46:47 +02:00
|
|
|
|
/* It would be more appropriate to use $XDG_RUNTIME_DIR, since
|
|
|
|
|
that gets cleared on reboot, but it wouldn't work on OS X. */
|
|
|
|
|
currentLoad = settings.nixStateDir + "/current-load";
|
2016-07-19 00:50:27 +02:00
|
|
|
|
|
|
|
|
|
std::shared_ptr<Store> sshStore;
|
|
|
|
|
AutoCloseFD bestSlotLock;
|
|
|
|
|
|
2017-05-02 13:44:10 +02:00
|
|
|
|
auto machines = getMachines();
|
2017-05-01 14:43:14 +02:00
|
|
|
|
debug("got %d remote builders", machines.size());
|
|
|
|
|
|
2017-05-02 12:16:29 +02:00
|
|
|
|
if (machines.empty()) {
|
|
|
|
|
std::cerr << "# decline-permanently\n";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 00:50:27 +02:00
|
|
|
|
string drvPath;
|
2017-05-01 17:35:30 +02:00
|
|
|
|
string storeUri;
|
2016-07-19 00:50:27 +02:00
|
|
|
|
for (string line; getline(cin, line);) {
|
|
|
|
|
auto tokens = tokenizeString<std::vector<string>>(line);
|
|
|
|
|
auto sz = tokens.size();
|
2017-03-03 16:18:49 +01:00
|
|
|
|
if (sz != 3 && sz != 4)
|
|
|
|
|
throw Error("invalid build hook line ‘%1%’", line);
|
2016-07-19 00:50:27 +02:00
|
|
|
|
auto amWilling = tokens[0] == "1";
|
|
|
|
|
auto neededSystem = tokens[1];
|
|
|
|
|
drvPath = tokens[2];
|
|
|
|
|
auto requiredFeatures = sz == 3 ?
|
2017-01-10 16:29:06 +01:00
|
|
|
|
std::set<string>{} :
|
|
|
|
|
tokenizeString<std::set<string>>(tokens[3], ",");
|
2016-07-19 00:50:27 +02:00
|
|
|
|
auto canBuildLocally = amWilling && (neededSystem == localSystem);
|
|
|
|
|
|
|
|
|
|
/* 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 16:18:49 +01:00
|
|
|
|
Machine * bestMachine = nullptr;
|
2016-07-19 00:50:27 +02:00
|
|
|
|
unsigned long long bestLoad = 0;
|
|
|
|
|
for (auto & m : machines) {
|
2017-05-01 17:35:30 +02:00
|
|
|
|
debug("considering building on ‘%s’", m.storeUri);
|
|
|
|
|
|
2016-07-19 00:50:27 +02: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 12:51:35 +01:00
|
|
|
|
auto slotLock = openSlotLock(m, slot);
|
2016-07-19 00:50:27 +02: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 16:18:49 +01:00
|
|
|
|
if (rightType && !canBuildLocally)
|
|
|
|
|
std::cerr << "# postpone\n";
|
|
|
|
|
else
|
|
|
|
|
std::cerr << "# decline\n";
|
2016-07-19 00:50:27 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-24 12:22:02 +01:00
|
|
|
|
#if __APPLE__
|
|
|
|
|
futimes(bestSlotLock.get(), NULL);
|
|
|
|
|
#else
|
2016-07-19 00:50:27 +02:00
|
|
|
|
futimens(bestSlotLock.get(), NULL);
|
2017-01-24 12:22:02 +01:00
|
|
|
|
#endif
|
2016-07-19 00:50:27 +02:00
|
|
|
|
|
|
|
|
|
lock = -1;
|
|
|
|
|
|
|
|
|
|
try {
|
2017-05-01 17:35:30 +02:00
|
|
|
|
|
2017-05-02 12:01:46 +02:00
|
|
|
|
Store::Params storeParams{{"max-connections", "1"}, {"log-fd", "4"}};
|
2017-05-01 17:35:30 +02:00
|
|
|
|
if (bestMachine->sshKey != "")
|
|
|
|
|
storeParams["ssh-key"] = bestMachine->sshKey;
|
|
|
|
|
|
|
|
|
|
sshStore = openStore(bestMachine->storeUri, storeParams);
|
|
|
|
|
storeUri = bestMachine->storeUri;
|
|
|
|
|
|
2016-07-19 00:50:27 +02:00
|
|
|
|
} catch (std::exception & e) {
|
2017-03-03 16:18:49 +01:00
|
|
|
|
printError("unable to open SSH connection to ‘%s’: %s; trying other available machines...",
|
2017-05-01 17:35:30 +02:00
|
|
|
|
bestMachine->storeUri, e.what());
|
2016-07-19 00:50:27 +02:00
|
|
|
|
bestMachine->enabled = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-05-01 17:35:30 +02:00
|
|
|
|
|
2016-07-19 00:50:27 +02:00
|
|
|
|
goto connected;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-03 16:18:49 +01:00
|
|
|
|
|
2016-07-19 00:50:27 +02:00
|
|
|
|
connected:
|
2017-03-03 16:18:49 +01:00
|
|
|
|
std::cerr << "# accept\n";
|
2016-07-19 00:50:27 +02:00
|
|
|
|
string line;
|
2017-03-03 16:18:49 +01:00
|
|
|
|
if (!getline(cin, line))
|
2016-07-19 00:50:27 +02:00
|
|
|
|
throw Error("hook caller didn't send inputs");
|
2017-05-01 17:35:30 +02:00
|
|
|
|
|
2017-03-16 13:50:01 +01:00
|
|
|
|
auto inputs = tokenizeString<PathSet>(line);
|
2017-03-03 16:18:49 +01:00
|
|
|
|
if (!getline(cin, line))
|
2016-07-19 00:50:27 +02:00
|
|
|
|
throw Error("hook caller didn't send outputs");
|
2017-05-01 17:35:30 +02:00
|
|
|
|
|
2017-03-16 13:50:01 +01:00
|
|
|
|
auto outputs = tokenizeString<PathSet>(line);
|
2017-05-01 17:35:30 +02:00
|
|
|
|
|
|
|
|
|
AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + escapeUri(storeUri) + ".upload-lock", true);
|
|
|
|
|
|
2017-03-03 16:18:49 +01:00
|
|
|
|
auto old = signal(SIGALRM, handleAlarm);
|
2016-07-19 00:50:27 +02:00
|
|
|
|
alarm(15 * 60);
|
2017-03-03 16:18:49 +01:00
|
|
|
|
if (!lockFile(uploadLock.get(), ltWrite, true))
|
|
|
|
|
printError("somebody is hogging the upload lock for ‘%s’, continuing...");
|
2016-07-19 00:50:27 +02:00
|
|
|
|
alarm(0);
|
|
|
|
|
signal(SIGALRM, old);
|
2017-05-01 20:03:25 +02:00
|
|
|
|
copyPaths(store, ref<Store>(sshStore), inputs, false, true);
|
2016-07-19 00:50:27 +02:00
|
|
|
|
uploadLock = -1;
|
|
|
|
|
|
2017-05-01 15:00:39 +02:00
|
|
|
|
BasicDerivation drv(readDerivation(drvPath));
|
|
|
|
|
drv.inputSrcs = inputs;
|
|
|
|
|
|
|
|
|
|
printError("building ‘%s’ on ‘%s’", drvPath, storeUri);
|
|
|
|
|
sshStore->buildDerivation(drvPath, drv);
|
2016-07-19 00:50:27 +02:00
|
|
|
|
|
2017-03-16 13:50:01 +01: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 */
|
2017-05-01 20:03:25 +02:00
|
|
|
|
copyPaths(ref<Store>(sshStore), store, missing, false, true);
|
2016-07-19 00:50:27 +02:00
|
|
|
|
}
|
2017-03-16 13:50:01 +01:00
|
|
|
|
|
2016-07-19 00:50:27 +02:00
|
|
|
|
return;
|
|
|
|
|
});
|
|
|
|
|
}
|