about summary refs log tree commit diff
path: root/src/build-remote/build-remote.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2017-05-01T15·35+0200
committerEelco Dolstra <edolstra@gmail.com>2017-05-01T15·35+0200
commit031d70e5009fcce58afabc9113d5a5de4a16b19a (patch)
tree1b65c2d98cc364f82ea9a42eff129128c2606339 /src/build-remote/build-remote.cc
parent3e4bdfedee747868a32b8a9c7a89a6f860415be8 (diff)
Support arbitrary store URIs in nix.machines
For backwards compatibility, if the URI is just a hostname, ssh://
(i.e. LegacySSHStore) is prepended automatically.

Also, all fields except the URI are now optional. For example, this is
a valid nix.machines file:

  local?root=/tmp/nix

This is useful for testing the remote build machinery since you don't
have to mess around with ssh.
Diffstat (limited to 'src/build-remote/build-remote.cc')
-rw-r--r--src/build-remote/build-remote.cc77
1 files changed, 44 insertions, 33 deletions
diff --git a/src/build-remote/build-remote.cc b/src/build-remote/build-remote.cc
index c41383bcf2..a19dac241d 100644
--- a/src/build-remote/build-remote.cc
+++ b/src/build-remote/build-remote.cc
@@ -27,12 +27,12 @@ class Machine {
     const std::set<string> mandatoryFeatures;
 
 public:
-    const string hostName;
+    const string storeUri;
     const std::vector<string> systemTypes;
     const string sshKey;
     const unsigned int maxJobs;
     const unsigned int speedFactor;
-    bool enabled;
+    bool enabled = true;
 
     bool allSupported(const std::set<string> & features) const {
         return std::all_of(features.begin(), features.end(),
@@ -49,7 +49,7 @@ public:
             });
     }
 
-    Machine(decltype(hostName) hostName,
+    Machine(decltype(storeUri) storeUri,
         decltype(systemTypes) systemTypes,
         decltype(sshKey) sshKey,
         decltype(maxJobs) maxJobs,
@@ -58,14 +58,18 @@ public:
         decltype(mandatoryFeatures) mandatoryFeatures) :
         supportedFeatures(supportedFeatures),
         mandatoryFeatures(mandatoryFeatures),
-        hostName(hostName),
+        storeUri(
+            // Backwards compatibility: if the URI is a hostname,
+            // prepend ssh://.
+            storeUri.find("://") != std::string::npos || hasPrefix(storeUri, "local") || hasPrefix(storeUri, "remote") || hasPrefix(storeUri, "auto")
+            ? storeUri
+            : "ssh://" + storeUri),
         systemTypes(systemTypes),
         sshKey(sshKey),
         maxJobs(maxJobs),
-        speedFactor(std::max(1U, speedFactor)),
-        enabled(true)
-    {};
-};;
+        speedFactor(std::max(1U, speedFactor))
+    {}
+};
 
 static std::vector<Machine> readConf()
 {
@@ -87,13 +91,13 @@ static std::vector<Machine> readConf()
         }
         auto tokens = tokenizeString<std::vector<string>>(line);
         auto sz = tokens.size();
-        if (sz < 4)
+        if (sz < 1)
             throw FormatError("bad machines.conf file ‘%1%’", conf);
         machines.emplace_back(tokens[0],
-            tokenizeString<std::vector<string>>(tokens[1], ","),
-            tokens[2],
-            stoull(tokens[3]),
-            sz >= 5 ? stoull(tokens[4]) : 1LL,
+            sz >= 2 ? tokenizeString<std::vector<string>>(tokens[1], ",") : std::vector<string>{settings.thisSystem},
+            sz >= 3 ? tokens[2] : "",
+            sz >= 4 ? std::stoull(tokens[3]) : 1LL,
+            sz >= 5 ? std::stoull(tokens[4]) : 1LL,
             sz >= 6 ?
             tokenizeString<std::set<string>>(tokens[5], ",") :
             std::set<string>{},
@@ -104,31 +108,27 @@ static std::vector<Machine> readConf()
     return machines;
 }
 
+std::string escapeUri(std::string uri)
+{
+    std::replace(uri.begin(), uri.end(), '/', '_');
+    return uri;
+}
+
 static string currentLoad;
 
 static AutoCloseFD openSlotLock(const Machine & m, unsigned long long slot)
 {
-    std::ostringstream fn_stream(currentLoad, std::ios_base::ate | std::ios_base::out);
-    fn_stream << "/";
-    for (auto t : m.systemTypes) {
-        fn_stream << t << "-";
-    }
-    fn_stream << m.hostName << "-" << slot;
-    return openLockFile(fn_stream.str(), true);
+    return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot), true);
 }
 
-static char display_env[] = "DISPLAY=";
-static char ssh_env[] = "SSH_ASKPASS=";
-
 int main (int argc, char * * argv)
 {
     return handleExceptions(argv[0], [&]() {
         initNix();
 
         /* Ensure we don't get any SSH passphrase or host key popups. */
-        if (putenv(display_env) == -1 ||
-            putenv(ssh_env) == -1)
-            throw SysError("setting SSH env vars");
+        unsetenv("DISPLAY");
+        unsetenv("SSH_ASKPASS");
 
         if (argc != 5)
             throw UsageError("called without required arguments");
@@ -151,7 +151,7 @@ int main (int argc, char * * argv)
         debug("got %d remote builders", machines.size());
 
         string drvPath;
-        string hostName;
+        string storeUri;
         for (string line; getline(cin, line);) {
             auto tokens = tokenizeString<std::vector<string>>(line);
             auto sz = tokens.size();
@@ -178,6 +178,8 @@ int main (int argc, char * * argv)
                 Machine * bestMachine = nullptr;
                 unsigned long long bestLoad = 0;
                 for (auto & m : machines) {
+                    debug("considering building on ‘%s’", m.storeUri);
+
                     if (m.enabled && std::find(m.systemTypes.begin(),
                             m.systemTypes.end(),
                             neededSystem) != m.systemTypes.end() &&
@@ -238,16 +240,21 @@ int main (int argc, char * * argv)
                 lock = -1;
 
                 try {
-                    sshStore = openStore("ssh-ng://" + bestMachine->hostName,
-                        { {"ssh-key", bestMachine->sshKey },
-                          {"max-connections", "1" } });
-                    hostName = bestMachine->hostName;
+
+                    Store::Params storeParams{{"max-connections", "1"}};
+                    if (bestMachine->sshKey != "")
+                        storeParams["ssh-key"] = bestMachine->sshKey;
+
+                    sshStore = openStore(bestMachine->storeUri, storeParams);
+                    storeUri = bestMachine->storeUri;
+
                 } catch (std::exception & e) {
                     printError("unable to open SSH connection to ‘%s’: %s; trying other available machines...",
-                        bestMachine->hostName, e.what());
+                        bestMachine->storeUri, e.what());
                     bestMachine->enabled = false;
                     continue;
                 }
+
                 goto connected;
             }
         }
@@ -257,11 +264,15 @@ connected:
         string line;
         if (!getline(cin, line))
             throw Error("hook caller didn't send inputs");
+
         auto inputs = tokenizeString<PathSet>(line);
         if (!getline(cin, line))
             throw Error("hook caller didn't send outputs");
+
         auto outputs = tokenizeString<PathSet>(line);
-        AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + hostName + ".upload-lock", true);
+
+        AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + escapeUri(storeUri) + ".upload-lock", true);
+
         auto old = signal(SIGALRM, handleAlarm);
         alarm(15 * 60);
         if (!lockFile(uploadLock.get(), ltWrite, true))