This commit is contained in:
Your Name
2025-05-23 21:41:33 +12:00
parent 94f77994f0
commit 048345c636
21 changed files with 377 additions and 203 deletions

View File

@@ -7,6 +7,7 @@
#include "services.hpp"
#include "servers.hpp"
#include "templates.hpp"
#include "assert.hpp"
namespace dropshell
{
@@ -24,8 +25,8 @@ namespace dropshell
ssh_handler,
shared_commands::std_autocomplete,
false, // hidden
true, // requires_config
true, // requires_install
true, // requires_config
true, // requires_install
1, // min_args (after command)
2, // max_args (after command)
"ssh SERVER",
@@ -34,13 +35,12 @@ namespace dropshell
ssh SERVER SERVICE SSH into a docker container for a service.
ssh SERVER SSH into a server.
ssh USER@SERVER SSH into a server as a specific user.
)"});
}
} ssh_command_register;
bool ssh_into_server(const std::string &server)
bool ssh_into_server(const std::string &server, std::string user)
{
server_env_manager server_env(server);
if (!server_env.is_valid())
@@ -48,7 +48,7 @@ namespace dropshell
error << "Server " << server << " is not valid" << std::endl;
return false;
}
execute_ssh_command(server_env.get_SSH_INFO(), sCommand(remotepath::DROPSHELL_DIR(server), "ls --color && bash", {}), cMode::Interactive);
execute_ssh_command(server_env.get_SSH_INFO(user), sCommand(remotepath(server, user).DROPSHELL_DIR(), "ls --color && bash", {}), cMode::Interactive);
return true;
}
@@ -80,7 +80,7 @@ namespace dropshell
return false;
}
server_env.run_remote_template_command(service,"ssh",{},false,{}); // explicitly supports interactive ssh!
server_env.run_remote_template_command(service, "ssh", {}, false, {}); // explicitly supports interactive ssh!
return true;
}
@@ -92,18 +92,41 @@ namespace dropshell
return 1;
}
std::string server = safearg(ctx.args, 0);
// ssh into the server
if (ctx.args.size() < 2)
{
// ssh into the server
return ssh_into_server(server) ? 0 : 1;
std::string arg1 = safearg(ctx.args, 0);
std::string server, user;
// parse either user@server or server
if (arg1.find("@") != std::string::npos)
{
user = arg1.substr(0, arg1.find("@"));
server = arg1.substr(arg1.find("@") + 1);
}
else
{
server = arg1;
// get the first user from the server.env file, and ssh in as that user.
server_env_manager server_env(server);
if (!server_env.is_valid())
{
error << "Server " << server << " is not valid" << std::endl;
return 1;
}
ASSERT(server_env.get_users().size() > 0, "Server " + server + " has no users");
user = server_env.get_users()[0].user;
}
return ssh_into_server(server, user) ? 0 : 1;
}
else
{ // ssh into a service on the server.
std::string server = safearg(ctx.args, 0);
std::string service = safearg(ctx.args, 1);
return ssh_into_service(server, service) ? 0 : 1;
}
std::string service = safearg(ctx.args, 1);
// ssh into the specific service.
return ssh_into_service(server, service) ? 0 : 1;
}
} // namespace dropshell