From 64e23d0a38f316a07cef0960d0ed74a450214283 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sat, 8 Feb 2014 00:05:46 -0500 Subject: Add download-via-ssh substituter This substituter connects to a remote host, runs nix-store --serve there, and then forwards substituter commands on to the remote host and sends their results to the calling program. The ssh-substituter-hosts option can be specified as a list of hosts to try. This is an initial implementation and, while it works, it has some limitations: * Only the first host is used * There is no caching of query results (all queries are sent to the remote machine) * There is no informative output (such as progress bars) * Some failure modes may cause unhelpful error messages * There is no concept of trusted-ssh-substituter-hosts Signed-off-by: Shea Levy --- src/download-via-ssh/download-via-ssh.cc | 129 +++++++++++++++++++++++++++++++ src/download-via-ssh/local.mk | 9 +++ 2 files changed, 138 insertions(+) create mode 100644 src/download-via-ssh/download-via-ssh.cc create mode 100644 src/download-via-ssh/local.mk (limited to 'src/download-via-ssh') diff --git a/src/download-via-ssh/download-via-ssh.cc b/src/download-via-ssh/download-via-ssh.cc new file mode 100644 index 000000000000..7adb93e10dfe --- /dev/null +++ b/src/download-via-ssh/download-via-ssh.cc @@ -0,0 +1,129 @@ +#include "shared.hh" +#include "util.hh" +#include "serialise.hh" +#include "archive.hh" +#include "affinity.hh" +#include "globals.hh" + +#include +#include + +using namespace nix; +using std::pair; +using std::cout; +using std::endl; + +// !!! TODO: +// * Respect more than the first host +// * use a database +// * show progress + +static pair connect(string conn) { + Pipe to, from; + to.create(); + from.create(); + pid_t child = fork(); + switch (child) { + case -1: + throw SysError("unable to fork"); + case 0: + try { + restoreAffinity(); + if (dup2(to.readSide, STDIN_FILENO) == -1) + throw SysError("dupping stdin"); + if (dup2(from.writeSide, STDOUT_FILENO) == -1) + throw SysError("dupping stdout"); + execlp("ssh" + , "ssh" + , "-x" + , "-T" + , conn.c_str() + , "nix-store --serve" + , NULL); + throw SysError("executing ssh"); + } catch (std::exception & e) { + std::cerr << "error: " << e.what() << std::endl; + } + _exit(1); + } + // If child exits unexpectedly, we'll EPIPE. If we exit unexpectedly, child will + // So no need to keep track of it. + + return pair(to.writeSide.borrow(), from.readSide.borrow()); +} + +static void substitute(pair & pipes, Path storePath, Path destPath) { + writeString("substitute", pipes.first); + writeString(storePath, pipes.first); + pipes.first.flush(); + restorePath(destPath, pipes.second); + cout << endl; +} + +static void query(pair & pipes) { + using std::cin; + writeString("query", pipes.first); + for (string line; getline(cin, line);) { + Strings tokenized = tokenizeString(line); + string cmd = tokenized.front(); + writeString(cmd, pipes.first); + tokenized.pop_front(); + foreach (Strings::iterator, i, tokenized) + writeStrings(tokenized, pipes.first); + pipes.first.flush(); + if (cmd == "have") { + PathSet paths = readStrings(pipes.second); + foreach (PathSet::iterator, i, paths) + cout << *i << endl; + } else if (cmd == "info") { + for (Path path = readString(pipes.second); !path.empty(); path = readString(pipes.second)) { + cout << path << endl; + cout << readString(pipes.second) << endl; + PathSet references = readStrings(pipes.second); + cout << references.size() << endl; + foreach (PathSet::iterator, i, references) + cout << *i << endl; + cout << readLongLong(pipes.second) << endl; + cout << readLongLong(pipes.second) << endl; + } + } else + throw Error(format("Unknown substituter query `%1%'") % cmd); + cout << endl; + } + writeString("", pipes.first); +} + +void run(Strings args) +{ + if (args.empty()) + throw UsageError("download-via-ssh requires an argument"); + + if (settings.sshSubstituterHosts.empty()) + return; + + cout << endl; + + pair pipes = connect(settings.sshSubstituterHosts.front()); + + Strings::iterator i = args.begin(); + if (*i == "--query") + query(pipes); + else if (*i == "--substitute") + if (args.size() != 3) + throw UsageError("download-via-ssh: --substitute takes exactly two arguments"); + else { + Path storePath = *++i; + Path destPath = *++i; + substitute(pipes, storePath, destPath); + } + else + throw UsageError(format("download-via-ssh: unknown command `%1%'") % *i); +} + +void printHelp() +{ + std::cerr << "Usage: download-via-ssh --query|--substitute store-path dest-path" << std::endl; +} + + +string programId = "download-via-ssh"; diff --git a/src/download-via-ssh/local.mk b/src/download-via-ssh/local.mk new file mode 100644 index 000000000000..92bf1159469d --- /dev/null +++ b/src/download-via-ssh/local.mk @@ -0,0 +1,9 @@ +programs += download-via-ssh + +download-via-ssh_DIR := $(d) + +download-via-ssh_SOURCES := $(d)/download-via-ssh.cc + +download-via-ssh_INSTALL_DIR := $(libexecdir)/nix/substituters + +download-via-ssh_LIBS = libmain libstore libutil libformat -- cgit 1.4.1 From 78d979567fa304fa4445fe7403932d9d97183ebd Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Mon, 10 Feb 2014 06:43:29 -0500 Subject: Clarify comment Signed-off-by: Shea Levy --- src/download-via-ssh/download-via-ssh.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/download-via-ssh') diff --git a/src/download-via-ssh/download-via-ssh.cc b/src/download-via-ssh/download-via-ssh.cc index 7adb93e10dfe..003d7de2bf86 100644 --- a/src/download-via-ssh/download-via-ssh.cc +++ b/src/download-via-ssh/download-via-ssh.cc @@ -46,7 +46,8 @@ static pair connect(string conn) { } _exit(1); } - // If child exits unexpectedly, we'll EPIPE. If we exit unexpectedly, child will + // If child exits unexpectedly, we'll EPIPE or EOF early. + // If we exit unexpectedly, child will EPIPE or EOF early. // So no need to keep track of it. return pair(to.writeSide.borrow(), from.readSide.borrow()); -- cgit 1.4.1 From c89d6b9b63b629ff936a56855be5689523910c58 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Mon, 10 Feb 2014 07:43:13 -0500 Subject: nix-store --serve: Use a versioned protocol Signed-off-by: Shea Levy --- src/download-via-ssh/download-via-ssh.cc | 28 ++++++++--- src/download-via-ssh/local.mk | 2 + src/nix-store/nix-store.cc | 82 +++++++++++++++++++++----------- src/nix-store/serve-protocol.hh | 24 ++++++++++ 4 files changed, 102 insertions(+), 34 deletions(-) create mode 100644 src/nix-store/serve-protocol.hh (limited to 'src/download-via-ssh') diff --git a/src/download-via-ssh/download-via-ssh.cc b/src/download-via-ssh/download-via-ssh.cc index 003d7de2bf86..be70a374fe77 100644 --- a/src/download-via-ssh/download-via-ssh.cc +++ b/src/download-via-ssh/download-via-ssh.cc @@ -4,6 +4,7 @@ #include "archive.hh" #include "affinity.hh" #include "globals.hh" +#include "serve-protocol.hh" #include #include @@ -54,7 +55,7 @@ static pair connect(string conn) { } static void substitute(pair & pipes, Path storePath, Path destPath) { - writeString("substitute", pipes.first); + writeInt(cmdSubstitute, pipes.first); writeString(storePath, pipes.first); pipes.first.flush(); restorePath(destPath, pipes.second); @@ -63,20 +64,24 @@ static void substitute(pair & pipes, Path storePath, Path dest static void query(pair & pipes) { using std::cin; - writeString("query", pipes.first); + writeInt(cmdQuery, pipes.first); for (string line; getline(cin, line);) { Strings tokenized = tokenizeString(line); string cmd = tokenized.front(); - writeString(cmd, pipes.first); tokenized.pop_front(); - foreach (Strings::iterator, i, tokenized) - writeStrings(tokenized, pipes.first); - pipes.first.flush(); if (cmd == "have") { + writeInt(qCmdHave, pipes.first); + foreach (Strings::iterator, i, tokenized) + writeStrings(tokenized, pipes.first); + pipes.first.flush(); PathSet paths = readStrings(pipes.second); foreach (PathSet::iterator, i, paths) cout << *i << endl; } else if (cmd == "info") { + writeInt(qCmdInfo, pipes.first); + foreach (Strings::iterator, i, tokenized) + writeStrings(tokenized, pipes.first); + pipes.first.flush(); for (Path path = readString(pipes.second); !path.empty(); path = readString(pipes.second)) { cout << path << endl; cout << readString(pipes.second) << endl; @@ -91,7 +96,6 @@ static void query(pair & pipes) { throw Error(format("Unknown substituter query `%1%'") % cmd); cout << endl; } - writeString("", pipes.first); } void run(Strings args) @@ -106,6 +110,16 @@ void run(Strings args) pair pipes = connect(settings.sshSubstituterHosts.front()); + /* Exchange the greeting */ + writeInt(SERVE_MAGIC_1, pipes.first); + pipes.first.flush(); + unsigned int magic = readInt(pipes.second); + if (magic != SERVE_MAGIC_2) + throw Error("protocol mismatch"); + readInt(pipes.second); // Server version, unused for now + writeInt(SERVE_PROTOCOL_VERSION, pipes.first); + pipes.first.flush(); + Strings::iterator i = args.begin(); if (*i == "--query") query(pipes); diff --git a/src/download-via-ssh/local.mk b/src/download-via-ssh/local.mk index 92bf1159469d..80f4c385acb3 100644 --- a/src/download-via-ssh/local.mk +++ b/src/download-via-ssh/local.mk @@ -6,4 +6,6 @@ download-via-ssh_SOURCES := $(d)/download-via-ssh.cc download-via-ssh_INSTALL_DIR := $(libexecdir)/nix/substituters +download-via-ssh_CXXFLAGS = -Isrc/nix-store + download-via-ssh_LIBS = libmain libstore libutil libformat diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 68ad9026747f..638d24498821 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -6,6 +6,7 @@ #include "xmlgraph.hh" #include "local-store.hh" #include "util.hh" +#include "serve-protocol.hh" #include #include @@ -843,34 +844,61 @@ static void opServe(Strings opFlags, Strings opArgs) FdSource in(STDIN_FILENO); FdSink out(STDOUT_FILENO); - string cmd = readString(in); - if (cmd == "query") { - for (cmd = readString(in); !cmd.empty(); cmd = readString(in)) { - PathSet paths = readStrings(in); - if (cmd == "have") { - writeStrings(store->queryValidPaths(paths), out); - } else if (cmd == "info") { - // !!! Maybe we want a queryPathInfos? - foreach (PathSet::iterator, i, paths) { - if (!store->isValidPath(*i)) - continue; - ValidPathInfo info = store->queryPathInfo(*i); - writeString(info.path, out); - writeString(info.deriver, out); - writeStrings(info.references, out); - // !!! Maybe we want compression? - writeLongLong(info.narSize, out); // downloadSize - writeLongLong(info.narSize, out); + /* Exchange the greeting. */ + unsigned int magic = readInt(in); + if (magic != SERVE_MAGIC_1) throw Error("protocol mismatch"); + writeInt(SERVE_MAGIC_2, out); + writeInt(SERVE_PROTOCOL_VERSION, out); + out.flush(); + readInt(in); // Client version, unused for now + + ServeCommand cmd = (ServeCommand) readInt(in); + switch (cmd) { + case cmdQuery: + while (true) { + QueryCommand qCmd; + try { + qCmd = (QueryCommand) readInt(in); + } catch (EndOfFile & e) { + break; } - writeString("", out); - } else - throw Error(format("Unknown serve query `%1%'") % cmd); - out.flush(); - } - } else if (cmd == "substitute") - dumpPath(readString(in), out); - else - throw Error(format("Unknown serve command `%1%'") % cmd); + switch (qCmd) { + case qCmdHave: + { + PathSet paths = readStrings(in); + writeStrings(store->queryValidPaths(paths), out); + } + break; + case qCmdInfo: + { + PathSet paths = readStrings(in); + // !!! Maybe we want a queryPathInfos? + foreach (PathSet::iterator, i, paths) { + if (!store->isValidPath(*i)) + continue; + ValidPathInfo info = store->queryPathInfo(*i); + writeString(info.path, out); + writeString(info.deriver, out); + writeStrings(info.references, out); + // !!! Maybe we want compression? + writeLongLong(info.narSize, out); // downloadSize + writeLongLong(info.narSize, out); + } + writeString("", out); + } + break; + default: + throw Error(format("Unknown serve query `%1%'") % cmd); + } + out.flush(); + } + break; + case cmdSubstitute: + dumpPath(readString(in), out); + break; + default: + throw Error(format("Unknown serve command `%1%'") % cmd); + } } diff --git a/src/nix-store/serve-protocol.hh b/src/nix-store/serve-protocol.hh new file mode 100644 index 000000000000..69277bc1b99b --- /dev/null +++ b/src/nix-store/serve-protocol.hh @@ -0,0 +1,24 @@ +#pragma once + +namespace nix { + + +#define SERVE_MAGIC_1 0x390c9deb +#define SERVE_MAGIC_2 0x5452eecb + +#define SERVE_PROTOCOL_VERSION 0x101 +#define GET_PROTOCOL_MAJOR(x) ((x) & 0xff00) +#define GET_PROTOCOL_MINOR(x) ((x) & 0x00ff) + + +typedef enum { + cmdQuery = 0, + cmdSubstitute = 1, +} ServeCommand; + +typedef enum { + qCmdHave = 0, + qCmdInfo = 1, +} QueryCommand; + +} -- cgit 1.4.1 From 2246aa77d291e07141f6a508e46730e2c28e1d84 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Wed, 12 Feb 2014 07:22:36 -0500 Subject: Remove using declarations from download-via-ssh Signed-off-by: Shea Levy --- src/download-via-ssh/download-via-ssh.cc | 36 ++++++++++++++------------------ 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'src/download-via-ssh') diff --git a/src/download-via-ssh/download-via-ssh.cc b/src/download-via-ssh/download-via-ssh.cc index be70a374fe77..5aa1e00ee38a 100644 --- a/src/download-via-ssh/download-via-ssh.cc +++ b/src/download-via-ssh/download-via-ssh.cc @@ -10,16 +10,13 @@ #include using namespace nix; -using std::pair; -using std::cout; -using std::endl; // !!! TODO: // * Respect more than the first host // * use a database // * show progress -static pair connect(string conn) { +static std::pair connect(string conn) { Pipe to, from; to.create(); from.create(); @@ -51,21 +48,20 @@ static pair connect(string conn) { // If we exit unexpectedly, child will EPIPE or EOF early. // So no need to keep track of it. - return pair(to.writeSide.borrow(), from.readSide.borrow()); + return std::pair(to.writeSide.borrow(), from.readSide.borrow()); } -static void substitute(pair & pipes, Path storePath, Path destPath) { +static void substitute(std::pair & pipes, Path storePath, Path destPath) { writeInt(cmdSubstitute, pipes.first); writeString(storePath, pipes.first); pipes.first.flush(); restorePath(destPath, pipes.second); - cout << endl; + std::cout << std::endl; } -static void query(pair & pipes) { - using std::cin; +static void query(std::pair & pipes) { writeInt(cmdQuery, pipes.first); - for (string line; getline(cin, line);) { + for (string line; getline(std::cin, line);) { Strings tokenized = tokenizeString(line); string cmd = tokenized.front(); tokenized.pop_front(); @@ -76,25 +72,25 @@ static void query(pair & pipes) { pipes.first.flush(); PathSet paths = readStrings(pipes.second); foreach (PathSet::iterator, i, paths) - cout << *i << endl; + std::cout << *i << std::endl; } else if (cmd == "info") { writeInt(qCmdInfo, pipes.first); foreach (Strings::iterator, i, tokenized) writeStrings(tokenized, pipes.first); pipes.first.flush(); for (Path path = readString(pipes.second); !path.empty(); path = readString(pipes.second)) { - cout << path << endl; - cout << readString(pipes.second) << endl; + std::cout << path << std::endl; + std::cout << readString(pipes.second) << std::endl; PathSet references = readStrings(pipes.second); - cout << references.size() << endl; + std::cout << references.size() << std::endl; foreach (PathSet::iterator, i, references) - cout << *i << endl; - cout << readLongLong(pipes.second) << endl; - cout << readLongLong(pipes.second) << endl; + std::cout << *i << std::endl; + std::cout << readLongLong(pipes.second) << std::endl; + std::cout << readLongLong(pipes.second) << std::endl; } } else throw Error(format("Unknown substituter query `%1%'") % cmd); - cout << endl; + std::cout << std::endl; } } @@ -106,9 +102,9 @@ void run(Strings args) if (settings.sshSubstituterHosts.empty()) return; - cout << endl; + std::cout << std::endl; - pair pipes = connect(settings.sshSubstituterHosts.front()); + std::pair pipes = connect(settings.sshSubstituterHosts.front()); /* Exchange the greeting */ writeInt(SERVE_MAGIC_1, pipes.first); -- cgit 1.4.1 From 7438f0bc2bc4b92bddf7159744ab2923e34b7457 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Wed, 12 Feb 2014 07:26:35 -0500 Subject: error messages start in lowercase Signed-off-by: Shea Levy --- src/download-via-ssh/download-via-ssh.cc | 2 +- src/nix-store/nix-store.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/download-via-ssh') diff --git a/src/download-via-ssh/download-via-ssh.cc b/src/download-via-ssh/download-via-ssh.cc index 5aa1e00ee38a..dee4fd78c9d0 100644 --- a/src/download-via-ssh/download-via-ssh.cc +++ b/src/download-via-ssh/download-via-ssh.cc @@ -89,7 +89,7 @@ static void query(std::pair & pipes) { std::cout << readLongLong(pipes.second) << std::endl; } } else - throw Error(format("Unknown substituter query `%1%'") % cmd); + throw Error(format("unknown substituter query `%1%'") % cmd); std::cout << std::endl; } } diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 638d24498821..038f099649b0 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -888,7 +888,7 @@ static void opServe(Strings opFlags, Strings opArgs) } break; default: - throw Error(format("Unknown serve query `%1%'") % cmd); + throw Error(format("unknown serve query `%1%'") % cmd); } out.flush(); } @@ -897,7 +897,7 @@ static void opServe(Strings opFlags, Strings opArgs) dumpPath(readString(in), out); break; default: - throw Error(format("Unknown serve command `%1%'") % cmd); + throw Error(format("unknown serve command `%1%'") % cmd); } } -- cgit 1.4.1 From 62eb9eb76ddacc1aa97400bef9f25b6ca4c50c8c Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Wed, 12 Feb 2014 07:27:45 -0500 Subject: Remove relic of old code Signed-off-by: Shea Levy --- src/download-via-ssh/download-via-ssh.cc | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/download-via-ssh') diff --git a/src/download-via-ssh/download-via-ssh.cc b/src/download-via-ssh/download-via-ssh.cc index dee4fd78c9d0..d85f1572f33e 100644 --- a/src/download-via-ssh/download-via-ssh.cc +++ b/src/download-via-ssh/download-via-ssh.cc @@ -67,7 +67,6 @@ static void query(std::pair & pipes) { tokenized.pop_front(); if (cmd == "have") { writeInt(qCmdHave, pipes.first); - foreach (Strings::iterator, i, tokenized) writeStrings(tokenized, pipes.first); pipes.first.flush(); PathSet paths = readStrings(pipes.second); @@ -75,7 +74,6 @@ static void query(std::pair & pipes) { std::cout << *i << std::endl; } else if (cmd == "info") { writeInt(qCmdInfo, pipes.first); - foreach (Strings::iterator, i, tokenized) writeStrings(tokenized, pipes.first); pipes.first.flush(); for (Path path = readString(pipes.second); !path.empty(); path = readString(pipes.second)) { -- cgit 1.4.1