Skip to content

Commit

Permalink
Merge pull request NixOS#9833 from obsidiansystems/ssh-arg-split
Browse files Browse the repository at this point in the history
Make `SSHMaster::startCommand` work on an args list
  • Loading branch information
edolstra authored Jan 23, 2024
2 parents d210252 + b716731 commit b6aee9a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
11 changes: 8 additions & 3 deletions src/libstore/legacy-ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ LegacySSHStore::LegacySSHStore(const std::string & scheme, const std::string & h
ref<LegacySSHStore::Connection> LegacySSHStore::openConnection()
{
auto conn = make_ref<Connection>();
conn->sshConn = master.startCommand(
fmt("%s --serve --write", remoteProgram)
+ (remoteStore.get() == "" ? "" : " --store " + shellEscape(remoteStore.get())));
Strings command = remoteProgram.get();
command.push_back("--serve");
command.push_back("--write");
if (remoteStore.get() != "") {
command.push_back("--store");
command.push_back(remoteStore.get());
}
conn->sshConn = master.startCommand(std::move(command));
conn->to = FdSink(conn->sshConn->in.get());
conn->from = FdSource(conn->sshConn->out.get());

Expand Down
2 changes: 1 addition & 1 deletion src/libstore/legacy-ssh-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct LegacySSHStoreConfig : virtual CommonSSHStoreConfig
{
using CommonSSHStoreConfig::CommonSSHStoreConfig;

const Setting<Path> remoteProgram{this, "nix-store", "remote-program",
const Setting<Strings> remoteProgram{this, {"nix-store"}, "remote-program",
"Path to the `nix-store` executable on the remote machine."};

const Setting<int> maxConnections{this, 1, "max-connections",
Expand Down
19 changes: 10 additions & 9 deletions src/libstore/ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct SSHStoreConfig : virtual RemoteStoreConfig, virtual CommonSSHStoreConfig
using RemoteStoreConfig::RemoteStoreConfig;
using CommonSSHStoreConfig::CommonSSHStoreConfig;

const Setting<Path> remoteProgram{this, "nix-daemon", "remote-program",
const Setting<Strings> remoteProgram{this, {"nix-daemon"}, "remote-program",
"Path to the `nix-daemon` executable on the remote machine."};

const std::string name() override { return "Experimental SSH Store"; }
Expand Down Expand Up @@ -212,14 +212,15 @@ class MountedSSHStore : public virtual MountedSSHStoreConfig, public virtual SSH
ref<RemoteStore::Connection> SSHStore::openConnection()
{
auto conn = make_ref<Connection>();

std::string command = remoteProgram + " --stdio";
if (remoteStore.get() != "")
command += " --store " + shellEscape(remoteStore.get());
for (auto & arg : extraRemoteProgramArgs)
command += " " + shellEscape(arg);

conn->sshConn = master.startCommand(command);
Strings command = remoteProgram.get();
command.push_back("--stdio");
if (remoteStore.get() != "") {
command.push_back("--store");
command.push_back(remoteStore.get());
}
command.insert(command.end(),
extraRemoteProgramArgs.begin(), extraRemoteProgramArgs.end());
conn->sshConn = master.startCommand(std::move(command));
conn->to = FdSink(conn->sshConn->in.get());
conn->from = FdSource(conn->sshConn->out.get());
return conn;
Expand Down
12 changes: 7 additions & 5 deletions src/libstore/ssh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ bool SSHMaster::isMasterRunning() {
return res.first == 0;
}

std::unique_ptr<SSHMaster::Connection> SSHMaster::startCommand(const std::string & command)
std::unique_ptr<SSHMaster::Connection> SSHMaster::startCommand(
Strings && command, Strings && extraSshArgs)
{
Path socketPath = startMaster();

Expand Down Expand Up @@ -84,18 +85,19 @@ std::unique_ptr<SSHMaster::Connection> SSHMaster::startCommand(const std::string

Strings args;

if (fakeSSH) {
args = { "bash", "-c" };
} else {
if (!fakeSSH) {
args = { "ssh", host.c_str(), "-x" };
addCommonSSHOpts(args);
if (socketPath != "")
args.insert(args.end(), {"-S", socketPath});
if (verbosity >= lvlChatty)
args.push_back("-v");
args.splice(args.end(), std::move(extraSshArgs));
args.push_back("--");
}

args.push_back(command);
args.splice(args.end(), std::move(command));

execvp(args.begin()->c_str(), stringsToCharPtrs(args).data());

// could not exec ssh/bash
Expand Down
11 changes: 10 additions & 1 deletion src/libstore/ssh.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ public:
AutoCloseFD out, in;
};

std::unique_ptr<Connection> startCommand(const std::string & command);
/**
* @param command The command (arg vector) to execute.
*
* @param extraSShArgs Extra args to pass to SSH (not the command to
* execute). Will not be used when "fake SSHing" to the local
* machine.
*/
std::unique_ptr<Connection> startCommand(
Strings && command,
Strings && extraSshArgs = {});

Path startMaster();
};
Expand Down

0 comments on commit b6aee9a

Please sign in to comment.