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.
This commit is contained in:
		
							parent
							
								
									3e4bdfedee
								
							
						
					
					
						commit
						031d70e500
					
				
					 4 changed files with 66 additions and 46 deletions
				
			
		|  | @ -27,12 +27,12 @@ class Machine { | ||||||
|     const std::set<string> mandatoryFeatures; |     const std::set<string> mandatoryFeatures; | ||||||
| 
 | 
 | ||||||
| public: | public: | ||||||
|     const string hostName; |     const string storeUri; | ||||||
|     const std::vector<string> systemTypes; |     const std::vector<string> systemTypes; | ||||||
|     const string sshKey; |     const string sshKey; | ||||||
|     const unsigned int maxJobs; |     const unsigned int maxJobs; | ||||||
|     const unsigned int speedFactor; |     const unsigned int speedFactor; | ||||||
|     bool enabled; |     bool enabled = true; | ||||||
| 
 | 
 | ||||||
|     bool allSupported(const std::set<string> & features) const { |     bool allSupported(const std::set<string> & features) const { | ||||||
|         return std::all_of(features.begin(), features.end(), |         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(systemTypes) systemTypes, | ||||||
|         decltype(sshKey) sshKey, |         decltype(sshKey) sshKey, | ||||||
|         decltype(maxJobs) maxJobs, |         decltype(maxJobs) maxJobs, | ||||||
|  | @ -58,14 +58,18 @@ public: | ||||||
|         decltype(mandatoryFeatures) mandatoryFeatures) : |         decltype(mandatoryFeatures) mandatoryFeatures) : | ||||||
|         supportedFeatures(supportedFeatures), |         supportedFeatures(supportedFeatures), | ||||||
|         mandatoryFeatures(mandatoryFeatures), |         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), |         systemTypes(systemTypes), | ||||||
|         sshKey(sshKey), |         sshKey(sshKey), | ||||||
|         maxJobs(maxJobs), |         maxJobs(maxJobs), | ||||||
|         speedFactor(std::max(1U, speedFactor)), |         speedFactor(std::max(1U, speedFactor)) | ||||||
|         enabled(true) |     {} | ||||||
|     {}; | }; | ||||||
| };; |  | ||||||
| 
 | 
 | ||||||
| static std::vector<Machine> readConf() | static std::vector<Machine> readConf() | ||||||
| { | { | ||||||
|  | @ -87,13 +91,13 @@ static std::vector<Machine> readConf() | ||||||
|         } |         } | ||||||
|         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 < 1) | ||||||
|             throw FormatError("bad machines.conf file ‘%1%’", conf); |             throw FormatError("bad machines.conf file ‘%1%’", conf); | ||||||
|         machines.emplace_back(tokens[0], |         machines.emplace_back(tokens[0], | ||||||
|             tokenizeString<std::vector<string>>(tokens[1], ","), |             sz >= 2 ? tokenizeString<std::vector<string>>(tokens[1], ",") : std::vector<string>{settings.thisSystem}, | ||||||
|             tokens[2], |             sz >= 3 ? tokens[2] : "", | ||||||
|             stoull(tokens[3]), |             sz >= 4 ? std::stoull(tokens[3]) : 1LL, | ||||||
|             sz >= 5 ? stoull(tokens[4]) : 1LL, |             sz >= 5 ? std::stoull(tokens[4]) : 1LL, | ||||||
|             sz >= 6 ? |             sz >= 6 ? | ||||||
|             tokenizeString<std::set<string>>(tokens[5], ",") : |             tokenizeString<std::set<string>>(tokens[5], ",") : | ||||||
|             std::set<string>{}, |             std::set<string>{}, | ||||||
|  | @ -104,31 +108,27 @@ static std::vector<Machine> readConf() | ||||||
|     return machines; |     return machines; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | std::string escapeUri(std::string uri) | ||||||
|  | { | ||||||
|  |     std::replace(uri.begin(), uri.end(), '/', '_'); | ||||||
|  |     return uri; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| 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); |     return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot), true); | ||||||
|     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) | 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 || |         unsetenv("DISPLAY"); | ||||||
|             putenv(ssh_env) == -1) |         unsetenv("SSH_ASKPASS"); | ||||||
|             throw SysError("setting SSH env vars"); |  | ||||||
| 
 | 
 | ||||||
|         if (argc != 5) |         if (argc != 5) | ||||||
|             throw UsageError("called without required arguments"); |             throw UsageError("called without required arguments"); | ||||||
|  | @ -151,7 +151,7 @@ int main (int argc, char * * argv) | ||||||
|         debug("got %d remote builders", machines.size()); |         debug("got %d remote builders", machines.size()); | ||||||
| 
 | 
 | ||||||
|         string drvPath; |         string drvPath; | ||||||
|         string hostName; |         string storeUri; | ||||||
|         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(); | ||||||
|  | @ -178,6 +178,8 @@ int main (int argc, char * * argv) | ||||||
|                 Machine * bestMachine = nullptr; |                 Machine * bestMachine = nullptr; | ||||||
|                 unsigned long long bestLoad = 0; |                 unsigned long long bestLoad = 0; | ||||||
|                 for (auto & m : machines) { |                 for (auto & m : machines) { | ||||||
|  |                     debug("considering building on ‘%s’", m.storeUri); | ||||||
|  | 
 | ||||||
|                     if (m.enabled && std::find(m.systemTypes.begin(), |                     if (m.enabled && std::find(m.systemTypes.begin(), | ||||||
|                             m.systemTypes.end(), |                             m.systemTypes.end(), | ||||||
|                             neededSystem) != m.systemTypes.end() && |                             neededSystem) != m.systemTypes.end() && | ||||||
|  | @ -238,16 +240,21 @@ int main (int argc, char * * argv) | ||||||
|                 lock = -1; |                 lock = -1; | ||||||
| 
 | 
 | ||||||
|                 try { |                 try { | ||||||
|                     sshStore = openStore("ssh-ng://" + bestMachine->hostName, | 
 | ||||||
|                         { {"ssh-key", bestMachine->sshKey }, |                     Store::Params storeParams{{"max-connections", "1"}}; | ||||||
|                           {"max-connections", "1" } }); |                     if (bestMachine->sshKey != "") | ||||||
|                     hostName = bestMachine->hostName; |                         storeParams["ssh-key"] = bestMachine->sshKey; | ||||||
|  | 
 | ||||||
|  |                     sshStore = openStore(bestMachine->storeUri, storeParams); | ||||||
|  |                     storeUri = bestMachine->storeUri; | ||||||
|  | 
 | ||||||
|                 } catch (std::exception & e) { |                 } catch (std::exception & e) { | ||||||
|                     printError("unable to open SSH connection to ‘%s’: %s; trying other available machines...", |                     printError("unable to open SSH connection to ‘%s’: %s; trying other available machines...", | ||||||
|                         bestMachine->hostName, e.what()); |                         bestMachine->storeUri, e.what()); | ||||||
|                     bestMachine->enabled = false; |                     bestMachine->enabled = false; | ||||||
|                     continue; |                     continue; | ||||||
|                 } |                 } | ||||||
|  | 
 | ||||||
|                 goto connected; |                 goto connected; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | @ -257,11 +264,15 @@ connected: | ||||||
|         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<PathSet>(line); |         auto inputs = tokenizeString<PathSet>(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<PathSet>(line); |         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); |         auto old = signal(SIGALRM, handleAlarm); | ||||||
|         alarm(15 * 60); |         alarm(15 * 60); | ||||||
|         if (!lockFile(uploadLock.get(), ltWrite, true)) |         if (!lockFile(uploadLock.get(), ltWrite, true)) | ||||||
|  |  | ||||||
|  | @ -1862,6 +1862,7 @@ void DerivationGoal::startBuilder() | ||||||
|                 dirsInChroot[i] = r; |                 dirsInChroot[i] = r; | ||||||
|             else { |             else { | ||||||
|                 Path p = chrootRootDir + i; |                 Path p = chrootRootDir + i; | ||||||
|  |                 debug("linking ‘%1%’ to ‘%2%’", p, r); | ||||||
|                 if (link(r.c_str(), p.c_str()) == -1) { |                 if (link(r.c_str(), p.c_str()) == -1) { | ||||||
|                     /* Hard-linking fails if we exceed the maximum
 |                     /* Hard-linking fails if we exceed the maximum
 | ||||||
|                        link count on a file (e.g. 32000 of ext3), |                        link count on a file (e.g. 32000 of ext3), | ||||||
|  |  | ||||||
|  | @ -709,10 +709,11 @@ namespace nix { | ||||||
| RegisterStoreImplementation::Implementations * RegisterStoreImplementation::implementations = 0; | RegisterStoreImplementation::Implementations * RegisterStoreImplementation::implementations = 0; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| ref<Store> openStore(const std::string & uri_) | ref<Store> openStore(const std::string & uri_, | ||||||
|  |     const Store::Params & extraParams) | ||||||
| { | { | ||||||
|     auto uri(uri_); |     auto uri(uri_); | ||||||
|     Store::Params params; |     Store::Params params(extraParams); | ||||||
|     auto q = uri.find('?'); |     auto q = uri.find('?'); | ||||||
|     if (q != std::string::npos) { |     if (q != std::string::npos) { | ||||||
|         for (auto s : tokenizeString<Strings>(uri.substr(q + 1), "&")) { |         for (auto s : tokenizeString<Strings>(uri.substr(q + 1), "&")) { | ||||||
|  | @ -722,11 +723,7 @@ ref<Store> openStore(const std::string & uri_) | ||||||
|         } |         } | ||||||
|         uri = uri_.substr(0, q); |         uri = uri_.substr(0, q); | ||||||
|     } |     } | ||||||
|     return openStore(uri, params); |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| ref<Store> openStore(const std::string & uri, const Store::Params & params) |  | ||||||
| { |  | ||||||
|     for (auto fun : *RegisterStoreImplementation::implementations) { |     for (auto fun : *RegisterStoreImplementation::implementations) { | ||||||
|         auto store = fun(uri, params); |         auto store = fun(uri, params); | ||||||
|         if (store) { |         if (store) { | ||||||
|  | @ -735,7 +732,7 @@ ref<Store> openStore(const std::string & uri, const Store::Params & params) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     throw Error(format("don't know how to open Nix store ‘%s’") % uri); |     throw Error("don't know how to open Nix store ‘%s’", uri); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -668,20 +668,31 @@ void removeTempRoots(); | ||||||
| /* Return a Store object to access the Nix store denoted by
 | /* Return a Store object to access the Nix store denoted by
 | ||||||
|    ‘uri’ (slight misnomer...). Supported values are: |    ‘uri’ (slight misnomer...). Supported values are: | ||||||
| 
 | 
 | ||||||
|    * ‘direct’: The Nix store in /nix/store and database in |    * ‘local’: The Nix store in /nix/store and database in | ||||||
|      /nix/var/nix/db, accessed directly. |      /nix/var/nix/db, accessed directly. | ||||||
| 
 | 
 | ||||||
|    * ‘daemon’: The Nix store accessed via a Unix domain socket |    * ‘daemon’: The Nix store accessed via a Unix domain socket | ||||||
|      connection to nix-daemon. |      connection to nix-daemon. | ||||||
| 
 | 
 | ||||||
|  |    * ‘auto’ or ‘’: Equivalent to ‘local’ or ‘daemon’ depending on | ||||||
|  |      whether the user has write access to the local Nix | ||||||
|  |      store/database. | ||||||
|  | 
 | ||||||
|    * ‘file://<path>’: A binary cache stored in <path>.
 |    * ‘file://<path>’: A binary cache stored in <path>.
 | ||||||
| 
 | 
 | ||||||
|    If ‘uri’ is empty, it defaults to ‘direct’ or ‘daemon’ depending on |    * ‘https://<path>’: A binary cache accessed via HTTP.
 | ||||||
|    whether the user has write access to the local Nix store/database. |  | ||||||
|    set to true *unless* you're going to collect garbage. */ |  | ||||||
| ref<Store> openStore(const std::string & uri = getEnv("NIX_REMOTE")); |  | ||||||
| 
 | 
 | ||||||
| ref<Store> openStore(const std::string & uri, const Store::Params & params); |    * ‘s3://<path>’: A writable binary cache stored on Amazon's Simple
 | ||||||
|  |      Storage Service. | ||||||
|  | 
 | ||||||
|  |    * ‘ssh://[user@]<host>’: A remote Nix store accessed by running
 | ||||||
|  |      ‘nix-store --serve’ via SSH. | ||||||
|  | 
 | ||||||
|  |    You can pass parameters to the store implementation by appending | ||||||
|  |    ‘?key=value&key=value&...’ to the URI. | ||||||
|  | */ | ||||||
|  | ref<Store> openStore(const std::string & uri = getEnv("NIX_REMOTE"), | ||||||
|  |     const Store::Params & extraParams = Store::Params()); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| void copyPaths(ref<Store> from, ref<Store> to, const PathSet & storePaths, bool substitute = false); | void copyPaths(ref<Store> from, ref<Store> to, const PathSet & storePaths, bool substitute = false); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue