build-remote: Misc cleanup

This commit is contained in:
Eelco Dolstra 2017-03-03 16:18:49 +01:00
parent 2f992692e2
commit 5a1fb03b8f
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -17,13 +17,12 @@
#include "derivations.hh" #include "derivations.hh"
using namespace nix; using namespace nix;
using std::cerr;
using std::cin; using std::cin;
static void handle_alarm(int sig) { static void handleAlarm(int sig) {
} }
class machine { class Machine {
const std::set<string> supportedFeatures; const std::set<string> supportedFeatures;
const std::set<string> mandatoryFeatures; const std::set<string> mandatoryFeatures;
@ -31,8 +30,8 @@ public:
const string hostName; const string hostName;
const std::vector<string> systemTypes; const std::vector<string> systemTypes;
const string sshKey; const string sshKey;
const unsigned long long maxJobs; const unsigned int maxJobs;
const unsigned long long speedFactor; const unsigned int speedFactor;
bool enabled; bool enabled;
bool allSupported(const std::set<string> & features) const { bool allSupported(const std::set<string> & features) const {
@ -50,28 +49,29 @@ public:
}); });
} }
machine(decltype(hostName) hostName, Machine(decltype(hostName) hostName,
decltype(systemTypes) systemTypes, decltype(systemTypes) systemTypes,
decltype(sshKey) sshKey, decltype(sshKey) sshKey,
decltype(maxJobs) maxJobs, decltype(maxJobs) maxJobs,
decltype(speedFactor) speedFactor, decltype(speedFactor) speedFactor,
decltype(supportedFeatures) supportedFeatures, decltype(supportedFeatures) supportedFeatures,
decltype(mandatoryFeatures) mandatoryFeatures) : decltype(mandatoryFeatures) mandatoryFeatures) :
supportedFeatures{std::move(supportedFeatures)}, supportedFeatures(supportedFeatures),
mandatoryFeatures{std::move(mandatoryFeatures)}, mandatoryFeatures(mandatoryFeatures),
hostName{std::move(hostName)}, hostName(hostName),
systemTypes{std::move(systemTypes)}, systemTypes(systemTypes),
sshKey{std::move(sshKey)}, sshKey(sshKey),
maxJobs{std::move(maxJobs)}, maxJobs(maxJobs),
speedFactor{speedFactor == 0 ? 1 : std::move(speedFactor)}, speedFactor(std::max(1U, speedFactor)),
enabled{true} {}; enabled(true)
{};
};; };;
static std::vector<machine> read_conf() static std::vector<Machine> readConf()
{ {
auto conf = getEnv("NIX_REMOTE_SYSTEMS", SYSCONFDIR "/nix/machines"); auto conf = getEnv("NIX_REMOTE_SYSTEMS", SYSCONFDIR "/nix/machines");
auto machines = std::vector<machine>{}; auto machines = std::vector<Machine>{};
auto lines = std::vector<string>{}; auto lines = std::vector<string>{};
try { try {
lines = tokenizeString<std::vector<string>>(readFile(conf), "\n"); lines = tokenizeString<std::vector<string>>(readFile(conf), "\n");
@ -87,10 +87,8 @@ static std::vector<machine> read_conf()
} }
auto tokens = tokenizeString<std::vector<string>>(line); auto tokens = tokenizeString<std::vector<string>>(line);
auto sz = tokens.size(); auto sz = tokens.size();
if (sz < 4) { if (sz < 4)
throw new FormatError(format("Bad machines.conf file %1%") throw FormatError("bad machines.conf file %1%", conf);
% conf);
}
machines.emplace_back(tokens[0], machines.emplace_back(tokens[0],
tokenizeString<std::vector<string>>(tokens[1], ","), tokenizeString<std::vector<string>>(tokens[1], ","),
tokens[2], tokens[2],
@ -108,7 +106,7 @@ static std::vector<machine> read_conf()
static string currentLoad; static string currentLoad;
static AutoCloseFD openSlotLock(const machine & m, unsigned long long slot) static AutoCloseFD openSlotLock(const Machine & m, unsigned long long slot)
{ {
std::ostringstream fn_stream(currentLoad, std::ios_base::ate | std::ios_base::out); std::ostringstream fn_stream(currentLoad, std::ios_base::ate | std::ios_base::out);
fn_stream << "/"; fn_stream << "/";
@ -126,15 +124,14 @@ int main (int argc, char * * argv)
{ {
return handleExceptions(argv[0], [&]() { return handleExceptions(argv[0], [&]() {
initNix(); initNix();
/* Ensure we don't get any SSH passphrase or host key popups. */ /* Ensure we don't get any SSH passphrase or host key popups. */
if (putenv(display_env) == -1 || if (putenv(display_env) == -1 ||
putenv(ssh_env) == -1) { putenv(ssh_env) == -1)
throw SysError("Setting SSH env vars"); throw SysError("setting SSH env vars");
}
if (argc != 4) { if (argc != 4)
throw UsageError("called without required arguments"); throw UsageError("called without required arguments");
}
auto store = openStore(); auto store = openStore();
@ -147,15 +144,14 @@ int main (int argc, char * * argv)
std::shared_ptr<Store> sshStore; std::shared_ptr<Store> sshStore;
AutoCloseFD bestSlotLock; AutoCloseFD bestSlotLock;
auto machines = read_conf(); auto machines = readConf();
string drvPath; string drvPath;
string hostName; string hostName;
for (string line; getline(cin, line);) { for (string line; getline(cin, line);) {
auto tokens = tokenizeString<std::vector<string>>(line); auto tokens = tokenizeString<std::vector<string>>(line);
auto sz = tokens.size(); auto sz = tokens.size();
if (sz != 3 && sz != 4) { if (sz != 3 && sz != 4)
throw Error(format("invalid build hook line %1%") % line); throw Error("invalid build hook line %1%", line);
}
auto amWilling = tokens[0] == "1"; auto amWilling = tokens[0] == "1";
auto neededSystem = tokens[1]; auto neededSystem = tokens[1];
drvPath = tokens[2]; drvPath = tokens[2];
@ -174,7 +170,7 @@ int main (int argc, char * * argv)
bool rightType = false; bool rightType = false;
machine * bestMachine = nullptr; Machine * bestMachine = nullptr;
unsigned long long bestLoad = 0; unsigned long long bestLoad = 0;
for (auto & m : machines) { for (auto & m : machines) {
if (m.enabled && std::find(m.systemTypes.begin(), if (m.enabled && std::find(m.systemTypes.begin(),
@ -221,11 +217,10 @@ int main (int argc, char * * argv)
} }
if (!bestSlotLock) { if (!bestSlotLock) {
if (rightType && !canBuildLocally) { if (rightType && !canBuildLocally)
cerr << "# postpone\n"; std::cerr << "# postpone\n";
} else { else
cerr << "# decline\n"; std::cerr << "# decline\n";
}
break; break;
} }
@ -241,37 +236,35 @@ int main (int argc, char * * argv)
sshStore = openStore("ssh://" + bestMachine->hostName + "?key=" + bestMachine->sshKey); sshStore = openStore("ssh://" + bestMachine->hostName + "?key=" + bestMachine->sshKey);
hostName = bestMachine->hostName; hostName = bestMachine->hostName;
} catch (std::exception & e) { } catch (std::exception & e) {
cerr << e.what() << '\n'; printError("unable to open SSH connection to %s: %s; trying other available machines...",
cerr << "unable to open SSH connection to " << bestMachine->hostName << ", trying other available machines...\n"; bestMachine->hostName, e.what());
bestMachine->enabled = false; bestMachine->enabled = false;
continue; continue;
} }
goto connected; goto connected;
} }
} }
connected: connected:
cerr << "# accept\n"; std::cerr << "# accept\n";
string line; string line;
if (!getline(cin, line)) { if (!getline(cin, line))
throw Error("hook caller didn't send inputs"); throw Error("hook caller didn't send inputs");
}
auto inputs = tokenizeString<std::list<string>>(line); auto inputs = tokenizeString<std::list<string>>(line);
if (!getline(cin, line)) { if (!getline(cin, line))
throw Error("hook caller didn't send outputs"); throw Error("hook caller didn't send outputs");
}
auto outputs = tokenizeString<Strings>(line); auto outputs = tokenizeString<Strings>(line);
AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + hostName + ".upload-lock", true); AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + hostName + ".upload-lock", true);
auto old = signal(SIGALRM, handle_alarm); auto old = signal(SIGALRM, handleAlarm);
alarm(15 * 60); alarm(15 * 60);
if (!lockFile(uploadLock.get(), ltWrite, true)) { if (!lockFile(uploadLock.get(), ltWrite, true))
cerr << "somebody is hogging the upload lock for " << hostName << ", continuing...\n"; printError("somebody is hogging the upload lock for %s, continuing...");
}
alarm(0); alarm(0);
signal(SIGALRM, old); signal(SIGALRM, old);
copyPaths(store, ref<Store>(sshStore), inputs); copyPaths(store, ref<Store>(sshStore), inputs);
uploadLock = -1; uploadLock = -1;
cerr << "building " << drvPath << " on " << hostName << "\n"; printError("building %s on %s", drvPath, hostName);
sshStore->buildDerivation(drvPath, readDerivation(drvPath)); sshStore->buildDerivation(drvPath, readDerivation(drvPath));
std::remove_if(outputs.begin(), outputs.end(), [=](const Path & path) { return store->isValidPath(path); }); std::remove_if(outputs.begin(), outputs.end(), [=](const Path & path) { return store->isValidPath(path); });