2017-02-07 20:28:40 +02:00
|
|
|
#include "archive.hh"
|
|
|
|
#include "pool.hh"
|
|
|
|
#include "remote-store.hh"
|
|
|
|
#include "serve-protocol.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "worker-protocol.hh"
|
2017-03-03 20:05:50 +02:00
|
|
|
#include "ssh.hh"
|
2017-05-01 17:08:13 +03:00
|
|
|
#include "derivations.hh"
|
2017-02-07 20:28:40 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2017-03-16 15:19:32 +02:00
|
|
|
static std::string uriScheme = "ssh://";
|
2017-02-07 20:28:40 +02:00
|
|
|
|
|
|
|
struct LegacySSHStore : public Store
|
|
|
|
{
|
2017-04-13 16:55:38 +03:00
|
|
|
const Setting<int> maxConnections{this, 1, "max-connections", "maximum number of concurrent SSH connections"};
|
|
|
|
const Setting<Path> sshKey{this, "", "ssh-key", "path to an SSH private key"};
|
|
|
|
const Setting<bool> compress{this, false, "compress", "whether to compress the connection"};
|
2018-03-21 16:10:46 +02:00
|
|
|
const Setting<Path> remoteProgram{this, "nix-store", "remote-program", "path to the nix-store executable on the remote system"};
|
2017-04-13 16:55:38 +03:00
|
|
|
|
2017-05-02 13:01:46 +03:00
|
|
|
// Hack for getting remote build log output.
|
|
|
|
const Setting<int> logFD{this, -1, "log-fd", "file descriptor to which SSH's stderr is connected"};
|
|
|
|
|
2017-02-07 20:28:40 +02:00
|
|
|
struct Connection
|
|
|
|
{
|
2017-03-03 20:05:50 +02:00
|
|
|
std::unique_ptr<SSHMaster::Connection> sshConn;
|
2017-02-07 20:28:40 +02:00
|
|
|
FdSink to;
|
|
|
|
FdSource from;
|
2017-05-01 17:08:13 +03:00
|
|
|
int remoteVersion;
|
2017-02-07 20:28:40 +02:00
|
|
|
};
|
|
|
|
|
2017-03-03 20:05:50 +02:00
|
|
|
std::string host;
|
2017-02-07 20:28:40 +02:00
|
|
|
|
|
|
|
ref<Pool<Connection>> connections;
|
|
|
|
|
2017-03-03 20:05:50 +02:00
|
|
|
SSHMaster master;
|
2017-02-07 20:28:40 +02:00
|
|
|
|
2017-03-03 20:05:50 +02:00
|
|
|
LegacySSHStore(const string & host, const Params & params)
|
2017-02-07 20:28:40 +02:00
|
|
|
: Store(params)
|
|
|
|
, host(host)
|
|
|
|
, connections(make_ref<Pool<Connection>>(
|
2017-04-13 16:55:38 +03:00
|
|
|
std::max(1, (int) maxConnections),
|
2017-02-07 20:28:40 +02:00
|
|
|
[this]() { return openConnection(); },
|
|
|
|
[](const ref<Connection> & r) { return true; }
|
|
|
|
))
|
2017-03-03 20:05:50 +02:00
|
|
|
, master(
|
|
|
|
host,
|
2017-04-13 16:55:38 +03:00
|
|
|
sshKey,
|
2017-03-03 20:05:50 +02:00
|
|
|
// Use SSH master only if using more than 1 connection.
|
|
|
|
connections->capacity() > 1,
|
2017-05-02 13:01:46 +03:00
|
|
|
compress,
|
|
|
|
logFD)
|
2017-02-07 20:28:40 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ref<Connection> openConnection()
|
|
|
|
{
|
|
|
|
auto conn = make_ref<Connection>();
|
2018-03-21 16:10:46 +02:00
|
|
|
conn->sshConn = master.startCommand(fmt("%s --serve --write", remoteProgram));
|
2017-03-03 20:05:50 +02:00
|
|
|
conn->to = FdSink(conn->sshConn->in.get());
|
|
|
|
conn->from = FdSource(conn->sshConn->out.get());
|
2017-02-07 20:28:40 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
conn->to << SERVE_MAGIC_1 << SERVE_PROTOCOL_VERSION;
|
|
|
|
conn->to.flush();
|
|
|
|
|
|
|
|
unsigned int magic = readInt(conn->from);
|
|
|
|
if (magic != SERVE_MAGIC_2)
|
2017-07-30 14:27:57 +03:00
|
|
|
throw Error("protocol mismatch with 'nix-store --serve' on '%s'", host);
|
2017-05-01 17:08:13 +03:00
|
|
|
conn->remoteVersion = readInt(conn->from);
|
|
|
|
if (GET_PROTOCOL_MAJOR(conn->remoteVersion) != 0x200)
|
2017-07-30 14:27:57 +03:00
|
|
|
throw Error("unsupported 'nix-store --serve' protocol version on '%s'", host);
|
2017-02-07 20:28:40 +02:00
|
|
|
|
|
|
|
} catch (EndOfFile & e) {
|
2017-07-30 14:27:57 +03:00
|
|
|
throw Error("cannot connect to '%1%'", host);
|
2017-02-07 20:28:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
};
|
|
|
|
|
|
|
|
string getUri() override
|
|
|
|
{
|
|
|
|
return uriScheme + host;
|
|
|
|
}
|
|
|
|
|
|
|
|
void queryPathInfoUncached(const Path & path,
|
2018-03-27 23:16:01 +03:00
|
|
|
Callback<std::shared_ptr<ValidPathInfo>> callback) override
|
2017-02-07 20:28:40 +02:00
|
|
|
{
|
2018-03-27 23:16:01 +03:00
|
|
|
try {
|
2017-02-07 20:28:40 +02:00
|
|
|
auto conn(connections->get());
|
|
|
|
|
2017-07-30 14:27:57 +03:00
|
|
|
debug("querying remote host '%s' for info on '%s'", host, path);
|
2017-02-07 20:28:40 +02:00
|
|
|
|
|
|
|
conn->to << cmdQueryPathInfos << PathSet{path};
|
|
|
|
conn->to.flush();
|
|
|
|
|
|
|
|
auto info = std::make_shared<ValidPathInfo>();
|
|
|
|
conn->from >> info->path;
|
2018-03-27 23:16:01 +03:00
|
|
|
if (info->path.empty()) return callback(nullptr);
|
2017-02-07 20:28:40 +02:00
|
|
|
assert(path == info->path);
|
|
|
|
|
|
|
|
PathSet references;
|
|
|
|
conn->from >> info->deriver;
|
|
|
|
info->references = readStorePaths<PathSet>(*this, conn->from);
|
|
|
|
readLongLong(conn->from); // download size
|
|
|
|
info->narSize = readLongLong(conn->from);
|
|
|
|
|
2017-09-08 17:55:27 +03:00
|
|
|
if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 4) {
|
|
|
|
auto s = readString(conn->from);
|
|
|
|
info->narHash = s.empty() ? Hash() : Hash(s);
|
|
|
|
conn->from >> info->ca;
|
|
|
|
info->sigs = readStrings<StringSet>(conn->from);
|
|
|
|
}
|
|
|
|
|
2017-02-07 20:28:40 +02:00
|
|
|
auto s = readString(conn->from);
|
|
|
|
assert(s == "");
|
|
|
|
|
2018-03-27 23:16:01 +03:00
|
|
|
callback(std::move(info));
|
|
|
|
} catch (...) { callback.rethrow(); }
|
2017-02-07 20:28:40 +02:00
|
|
|
}
|
|
|
|
|
2018-03-22 00:12:22 +02:00
|
|
|
void addToStore(const ValidPathInfo & info, Source & source,
|
2017-06-28 19:11:01 +03:00
|
|
|
RepairFlag repair, CheckSigsFlag checkSigs,
|
2017-02-07 20:28:40 +02:00
|
|
|
std::shared_ptr<FSAccessor> accessor) override
|
|
|
|
{
|
2017-07-30 14:27:57 +03:00
|
|
|
debug("adding path '%s' to remote host '%s'", info.path, host);
|
2017-02-07 20:28:40 +02:00
|
|
|
|
|
|
|
auto conn(connections->get());
|
|
|
|
|
|
|
|
conn->to
|
|
|
|
<< cmdImportPaths
|
|
|
|
<< 1;
|
2018-03-22 00:12:22 +02:00
|
|
|
copyNAR(source, conn->to);
|
2017-02-07 20:28:40 +02:00
|
|
|
conn->to
|
|
|
|
<< exportMagic
|
|
|
|
<< info.path
|
|
|
|
<< info.references
|
|
|
|
<< info.deriver
|
|
|
|
<< 0
|
|
|
|
<< 0;
|
|
|
|
conn->to.flush();
|
|
|
|
|
|
|
|
if (readInt(conn->from) != 1)
|
2017-07-30 14:27:57 +03:00
|
|
|
throw Error("failed to add path '%s' to remote host '%s', info.path, host");
|
2017-02-07 20:28:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void narFromPath(const Path & path, Sink & sink) override
|
|
|
|
{
|
|
|
|
auto conn(connections->get());
|
|
|
|
|
|
|
|
conn->to << cmdDumpStorePath << path;
|
|
|
|
conn->to.flush();
|
2018-03-21 23:56:02 +02:00
|
|
|
copyNAR(conn->from, sink);
|
2017-02-07 20:28:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PathSet queryAllValidPaths() override { unsupported(); }
|
|
|
|
|
|
|
|
void queryReferrers(const Path & path, PathSet & referrers) override
|
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
PathSet queryDerivationOutputs(const Path & path) override
|
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
StringSet queryDerivationOutputNames(const Path & path) override
|
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
Path queryPathFromHashPart(const string & hashPart) override
|
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
Path addToStore(const string & name, const Path & srcPath,
|
|
|
|
bool recursive, HashType hashAlgo,
|
2017-06-28 19:11:01 +03:00
|
|
|
PathFilter & filter, RepairFlag repair) override
|
2017-02-07 20:28:40 +02:00
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
Path addTextToStore(const string & name, const string & s,
|
2017-06-28 19:11:01 +03:00
|
|
|
const PathSet & references, RepairFlag repair) override
|
2017-02-07 20:28:40 +02:00
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
BuildResult buildDerivation(const Path & drvPath, const BasicDerivation & drv,
|
|
|
|
BuildMode buildMode) override
|
2017-05-01 17:08:13 +03:00
|
|
|
{
|
|
|
|
auto conn(connections->get());
|
|
|
|
|
|
|
|
conn->to
|
|
|
|
<< cmdBuildDerivation
|
|
|
|
<< drvPath
|
|
|
|
<< drv
|
|
|
|
<< settings.maxSilentTime
|
|
|
|
<< settings.buildTimeout;
|
|
|
|
if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 2)
|
|
|
|
conn->to
|
|
|
|
<< settings.maxLogSize;
|
|
|
|
if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 3)
|
|
|
|
conn->to
|
|
|
|
<< settings.buildRepeat
|
|
|
|
<< settings.enforceDeterminism;
|
|
|
|
|
|
|
|
conn->to.flush();
|
|
|
|
|
|
|
|
BuildResult status;
|
|
|
|
status.status = (BuildResult::Status) readInt(conn->from);
|
|
|
|
conn->from >> status.errorMsg;
|
|
|
|
|
|
|
|
if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 3)
|
|
|
|
conn->from >> status.timesBuilt >> status.isNonDeterministic >> status.startTime >> status.stopTime;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2017-02-07 20:28:40 +02:00
|
|
|
|
|
|
|
void ensurePath(const Path & path) override
|
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
void addTempRoot(const Path & path) override
|
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
void addIndirectRoot(const Path & path) override
|
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
Roots findRoots() override
|
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
void collectGarbage(const GCOptions & options, GCResults & results) override
|
|
|
|
{ unsupported(); }
|
|
|
|
|
2017-02-21 14:15:07 +02:00
|
|
|
ref<FSAccessor> getFSAccessor() override
|
2017-02-07 20:28:40 +02:00
|
|
|
{ unsupported(); }
|
|
|
|
|
|
|
|
void addSignatures(const Path & storePath, const StringSet & sigs) override
|
|
|
|
{ unsupported(); }
|
|
|
|
|
2017-03-16 12:44:01 +02:00
|
|
|
void computeFSClosure(const PathSet & paths,
|
|
|
|
PathSet & out, bool flipDirection = false,
|
|
|
|
bool includeOutputs = false, bool includeDerivers = false) override
|
|
|
|
{
|
|
|
|
if (flipDirection || includeDerivers) {
|
|
|
|
Store::computeFSClosure(paths, out, flipDirection, includeOutputs, includeDerivers);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto conn(connections->get());
|
|
|
|
|
|
|
|
conn->to
|
|
|
|
<< cmdQueryClosure
|
|
|
|
<< includeOutputs
|
|
|
|
<< paths;
|
|
|
|
conn->to.flush();
|
|
|
|
|
|
|
|
auto res = readStorePaths<PathSet>(*this, conn->from);
|
|
|
|
|
|
|
|
out.insert(res.begin(), res.end());
|
|
|
|
}
|
|
|
|
|
2017-06-28 19:11:01 +03:00
|
|
|
PathSet queryValidPaths(const PathSet & paths,
|
|
|
|
SubstituteFlag maybeSubstitute = NoSubstitute) override
|
2017-03-16 14:50:01 +02:00
|
|
|
{
|
|
|
|
auto conn(connections->get());
|
|
|
|
|
|
|
|
conn->to
|
|
|
|
<< cmdQueryValidPaths
|
|
|
|
<< false // lock
|
|
|
|
<< maybeSubstitute
|
|
|
|
<< paths;
|
|
|
|
conn->to.flush();
|
|
|
|
|
|
|
|
return readStorePaths<PathSet>(*this, conn->from);
|
|
|
|
}
|
2017-05-02 15:18:46 +03:00
|
|
|
|
|
|
|
void connect() override
|
|
|
|
{
|
|
|
|
auto conn(connections->get());
|
|
|
|
}
|
2017-02-07 20:28:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static RegisterStoreImplementation regStore([](
|
|
|
|
const std::string & uri, const Store::Params & params)
|
|
|
|
-> std::shared_ptr<Store>
|
|
|
|
{
|
|
|
|
if (std::string(uri, 0, uriScheme.size()) != uriScheme) return 0;
|
|
|
|
return std::make_shared<LegacySSHStore>(std::string(uri, uriScheme.size()), params);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|