2016-07-19 01:50:27 +03:00
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <algorithm>
|
2017-01-10 17:29:06 +02:00
|
|
|
|
#include <set>
|
2016-07-19 01:50:27 +03:00
|
|
|
|
#include <memory>
|
|
|
|
|
#include <tuple>
|
|
|
|
|
#include <iomanip>
|
2017-01-24 14:57:26 +02:00
|
|
|
|
#if __APPLE__
|
2017-01-24 13:22:02 +02:00
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#endif
|
2016-07-19 01:50:27 +03: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 17:18:49 +02:00
|
|
|
|
static void handleAlarm(int sig) {
|
2016-07-19 01:50:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 17:18:49 +02:00
|
|
|
|
class Machine {
|
2017-01-10 17:29:06 +02:00
|
|
|
|
const std::set<string> supportedFeatures;
|
|
|
|
|
const std::set<string> mandatoryFeatures;
|
2016-07-19 01:50:27 +03:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
const string hostName;
|
|
|
|
|
const std::vector<string> systemTypes;
|
|
|
|
|
const string sshKey;
|
2017-03-03 17:18:49 +02:00
|
|
|
|
const unsigned int maxJobs;
|
|
|
|
|
const unsigned int speedFactor;
|
2016-07-19 01:50:27 +03:00
|
|
|
|
bool enabled;
|
|
|
|
|
|
2017-01-10 17:29:06 +02:00
|
|
|
|
bool allSupported(const std::set<string> & features) const {
|
2016-07-19 01:50:27 +03:00
|
|
|
|
return std::all_of(features.begin(), features.end(),
|
|
|
|
|
[&](const string & feature) {
|
2017-01-10 17:29:06 +02:00
|
|
|
|
return supportedFeatures.count(feature) ||
|
|
|
|
|
mandatoryFeatures.count(feature);
|
2016-07-19 01:50:27 +03:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 17:29:06 +02:00
|
|
|
|
bool mandatoryMet(const std::set<string> & features) const {
|
2016-07-19 01:50:27 +03:00
|
|
|
|
return std::all_of(mandatoryFeatures.begin(), mandatoryFeatures.end(),
|
|
|
|
|
[&](const string & feature) {
|
2017-01-10 17:29:06 +02:00
|
|
|
|
return features.count(feature);
|
2016-07-19 01:50:27 +03:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 17:18:49 +02:00
|
|
|
|
Machine(decltype(hostName) hostName,
|
2016-07-19 01:50:27 +03:00
|
|
|
|
decltype(systemTypes) systemTypes,
|
|
|
|
|
decltype(sshKey) sshKey,
|
|
|
|
|
decltype(maxJobs) maxJobs,
|
|
|
|
|
decltype(speedFactor) speedFactor,
|
|
|
|
|
decltype(supportedFeatures) supportedFeatures,
|
|
|
|
|
decltype(mandatoryFeatures) mandatoryFeatures) :
|
2017-03-03 17:18:49 +02:00
|
|
|
|
supportedFeatures(supportedFeatures),
|
|
|
|
|
mandatoryFeatures(mandatoryFeatures),
|
|
|
|
|
hostName(hostName),
|
|
|
|
|
systemTypes(systemTypes),
|
|
|
|
|
sshKey(sshKey),
|
|
|
|
|
maxJobs(maxJobs),
|
|
|
|
|
speedFactor(std::max(1U, speedFactor)),
|
|
|
|
|
enabled(true)
|
|
|
|
|
{};
|
2016-07-19 01:50:27 +03:00
|
|
|
|
};;
|
|
|
|
|
|
2017-03-03 17:18:49 +02:00
|
|
|
|
static std::vector<Machine> readConf()
|
2017-01-24 16:28:50 +02:00
|
|
|
|
{
|
2016-07-19 01:50:27 +03:00
|
|
|
|
auto conf = getEnv("NIX_REMOTE_SYSTEMS", SYSCONFDIR "/nix/machines");
|
|
|
|
|
|
2017-03-03 17:18:49 +02:00
|
|
|
|
auto machines = std::vector<Machine>{};
|
2017-01-10 17:36:26 +02:00
|
|
|
|
auto lines = std::vector<string>{};
|
|
|
|
|
try {
|
|
|
|
|
lines = tokenizeString<std::vector<string>>(readFile(conf), "\n");
|
|
|
|
|
} catch (const SysError & e) {
|
|
|
|
|
if (e.errNo != ENOENT)
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
for (auto line : lines) {
|
|
|
|
|
chomp(line);
|
|
|
|
|
line.erase(std::find(line.begin(), line.end(), '#'), line.end());
|
|
|
|
|
if (line.empty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
auto tokens = tokenizeString<std::vector<string>>(line);
|
|
|
|
|
auto sz = tokens.size();
|
2017-03-03 17:18:49 +02:00
|
|
|
|
if (sz < 4)
|
|
|
|
|
throw FormatError("bad machines.conf file ‘%1%’", conf);
|
2017-01-10 17:36:26 +02:00
|
|
|
|
machines.emplace_back(tokens[0],
|
|
|
|
|
tokenizeString<std::vector<string>>(tokens[1], ","),
|
|
|
|
|
tokens[2],
|
|
|
|
|
stoull(tokens[3]),
|
|
|
|
|
sz >= 5 ? stoull(tokens[4]) : 1LL,
|
|
|
|
|
sz >= 6 ?
|
|
|
|
|
tokenizeString<std::set<string>>(tokens[5], ",") :
|
|
|
|
|
std::set<string>{},
|
|
|
|
|
sz >= 7 ?
|
|
|
|
|
tokenizeString<std::set<string>>(tokens[6], ",") :
|
|
|
|
|
std::set<string>{});
|
2016-07-19 01:50:27 +03:00
|
|
|
|
}
|
|
|
|
|
return machines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string currentLoad;
|
|
|
|
|
|
2017-03-03 17:18:49 +02:00
|
|
|
|
static AutoCloseFD openSlotLock(const Machine & m, unsigned long long slot)
|
2017-01-24 16:28:50 +02:00
|
|
|
|
{
|
|
|
|
|
std::ostringstream fn_stream(currentLoad, std::ios_base::ate | std::ios_base::out);
|
2016-07-19 01:50:27 +03:00
|
|
|
|
fn_stream << "/";
|
|
|
|
|
for (auto t : m.systemTypes) {
|
|
|
|
|
fn_stream << t << "-";
|
|
|
|
|
}
|
|
|
|
|
fn_stream << m.hostName << "-" << slot;
|
|
|
|
|
return openLockFile(fn_stream.str(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char display_env[] = "DISPLAY=";
|
|
|
|
|
static char ssh_env[] = "SSH_ASKPASS=";
|
|
|
|
|
|
|
|
|
|
int main (int argc, char * * argv)
|
|
|
|
|
{
|
|
|
|
|
return handleExceptions(argv[0], [&]() {
|
|
|
|
|
initNix();
|
2017-03-03 17:18:49 +02:00
|
|
|
|
|
2016-07-19 01:50:27 +03:00
|
|
|
|
/* Ensure we don't get any SSH passphrase or host key popups. */
|
|
|
|
|
if (putenv(display_env) == -1 ||
|
2017-03-03 17:18:49 +02:00
|
|
|
|
putenv(ssh_env) == -1)
|
|
|
|
|
throw SysError("setting SSH env vars");
|
2016-07-19 01:50:27 +03:00
|
|
|
|
|
2017-05-01 15:43:14 +03:00
|
|
|
|
if (argc != 5)
|
2016-07-19 01:50:27 +03:00
|
|
|
|
throw UsageError("called without required arguments");
|
|
|
|
|
|
|
|
|
|
auto store = openStore();
|
|
|
|
|
|
|
|
|
|
auto localSystem = argv[1];
|
2017-05-01 15:43:14 +03:00
|
|
|
|
settings.maxSilentTime = std::stoll(argv[2]);
|
|
|
|
|
settings.buildTimeout = std::stoll(argv[3]);
|
|
|
|
|
verbosity = (Verbosity) std::stoll(argv[4]);
|
2016-07-19 01:50:27 +03:00
|
|
|
|
|
|
|
|
|
currentLoad = getEnv("NIX_CURRENT_LOAD", "/run/nix/current-load");
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Store> sshStore;
|
|
|
|
|
AutoCloseFD bestSlotLock;
|
|
|
|
|
|
2017-03-03 17:18:49 +02:00
|
|
|
|
auto machines = readConf();
|
2017-05-01 15:43:14 +03:00
|
|
|
|
debug("got %d remote builders", machines.size());
|
|
|
|
|
|
2016-07-19 01:50:27 +03:00
|
|
|
|
string drvPath;
|
|
|
|
|
string hostName;
|
|
|
|
|
for (string line; getline(cin, line);) {
|
|
|
|
|
auto tokens = tokenizeString<std::vector<string>>(line);
|
|
|
|
|
auto sz = tokens.size();
|
2017-03-03 17:18:49 +02:00
|
|
|
|
if (sz != 3 && sz != 4)
|
|
|
|
|
throw Error("invalid build hook line ‘%1%’", line);
|
2016-07-19 01:50:27 +03:00
|
|
|
|
auto amWilling = tokens[0] == "1";
|
|
|
|
|
auto neededSystem = tokens[1];
|
|
|
|
|
drvPath = tokens[2];
|
|
|
|
|
auto requiredFeatures = sz == 3 ?
|
2017-01-10 17:29:06 +02:00
|
|
|
|
std::set<string>{} :
|
|
|
|
|
tokenizeString<std::set<string>>(tokens[3], ",");
|
2016-07-19 01:50:27 +03: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 17:18:49 +02:00
|
|
|
|
Machine * bestMachine = nullptr;
|
2016-07-19 01:50:27 +03:00
|
|
|
|
unsigned long long bestLoad = 0;
|
|
|
|
|
for (auto & m : machines) {
|
|
|
|
|
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 13:51:35 +02:00
|
|
|
|
auto slotLock = openSlotLock(m, slot);
|
2016-07-19 01:50:27 +03: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 17:18:49 +02:00
|
|
|
|
if (rightType && !canBuildLocally)
|
|
|
|
|
std::cerr << "# postpone\n";
|
|
|
|
|
else
|
|
|
|
|
std::cerr << "# decline\n";
|
2016-07-19 01:50:27 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-24 13:22:02 +02:00
|
|
|
|
#if __APPLE__
|
|
|
|
|
futimes(bestSlotLock.get(), NULL);
|
|
|
|
|
#else
|
2016-07-19 01:50:27 +03:00
|
|
|
|
futimens(bestSlotLock.get(), NULL);
|
2017-01-24 13:22:02 +02:00
|
|
|
|
#endif
|
2016-07-19 01:50:27 +03:00
|
|
|
|
|
|
|
|
|
lock = -1;
|
|
|
|
|
|
|
|
|
|
try {
|
2017-03-16 15:19:32 +02:00
|
|
|
|
sshStore = openStore("ssh-ng://" + bestMachine->hostName,
|
2017-03-03 20:23:20 +02:00
|
|
|
|
{ {"ssh-key", bestMachine->sshKey },
|
|
|
|
|
{"max-connections", "1" } });
|
2016-07-19 01:50:27 +03:00
|
|
|
|
hostName = bestMachine->hostName;
|
|
|
|
|
} catch (std::exception & e) {
|
2017-03-03 17:18:49 +02:00
|
|
|
|
printError("unable to open SSH connection to ‘%s’: %s; trying other available machines...",
|
|
|
|
|
bestMachine->hostName, e.what());
|
2016-07-19 01:50:27 +03:00
|
|
|
|
bestMachine->enabled = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
goto connected;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-03 17:18:49 +02:00
|
|
|
|
|
2016-07-19 01:50:27 +03:00
|
|
|
|
connected:
|
2017-03-03 17:18:49 +02:00
|
|
|
|
std::cerr << "# accept\n";
|
2016-07-19 01:50:27 +03:00
|
|
|
|
string line;
|
2017-03-03 17:18:49 +02:00
|
|
|
|
if (!getline(cin, line))
|
2016-07-19 01:50:27 +03:00
|
|
|
|
throw Error("hook caller didn't send inputs");
|
2017-03-16 14:50:01 +02:00
|
|
|
|
auto inputs = tokenizeString<PathSet>(line);
|
2017-03-03 17:18:49 +02:00
|
|
|
|
if (!getline(cin, line))
|
2016-07-19 01:50:27 +03:00
|
|
|
|
throw Error("hook caller didn't send outputs");
|
2017-03-16 14:50:01 +02:00
|
|
|
|
auto outputs = tokenizeString<PathSet>(line);
|
2016-07-19 01:50:27 +03:00
|
|
|
|
AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + hostName + ".upload-lock", true);
|
2017-03-03 17:18:49 +02:00
|
|
|
|
auto old = signal(SIGALRM, handleAlarm);
|
2016-07-19 01:50:27 +03:00
|
|
|
|
alarm(15 * 60);
|
2017-03-03 17:18:49 +02:00
|
|
|
|
if (!lockFile(uploadLock.get(), ltWrite, true))
|
|
|
|
|
printError("somebody is hogging the upload lock for ‘%s’, continuing...");
|
2016-07-19 01:50:27 +03:00
|
|
|
|
alarm(0);
|
|
|
|
|
signal(SIGALRM, old);
|
|
|
|
|
copyPaths(store, ref<Store>(sshStore), inputs);
|
|
|
|
|
uploadLock = -1;
|
|
|
|
|
|
2017-03-03 17:18:49 +02:00
|
|
|
|
printError("building ‘%s’ on ‘%s’", drvPath, hostName);
|
2016-07-19 01:50:27 +03:00
|
|
|
|
sshStore->buildDerivation(drvPath, readDerivation(drvPath));
|
|
|
|
|
|
2017-03-16 14:50:01 +02: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);
|
2016-07-19 01:50:27 +03:00
|
|
|
|
}
|
2017-03-16 14:50:01 +02:00
|
|
|
|
|
2016-07-19 01:50:27 +03:00
|
|
|
|
return;
|
|
|
|
|
});
|
|
|
|
|
}
|