109 lines
3.6 KiB
C++
109 lines
3.6 KiB
C++
#include "command_registry.hpp"
|
|
#include "config.hpp"
|
|
#include "utils/utils.hpp"
|
|
#include "utils/directories.hpp"
|
|
#include "shared_commands.hpp"
|
|
#include "server_env_manager.hpp"
|
|
#include "services.hpp"
|
|
#include "servers.hpp"
|
|
#include "templates.hpp"
|
|
|
|
namespace dropshell
|
|
{
|
|
|
|
int ssh_handler(const CommandContext &ctx);
|
|
|
|
static std::vector<std::string> ssh_name_list = {"ssh"};
|
|
|
|
// Static registration
|
|
struct SSHCommandRegister
|
|
{
|
|
SSHCommandRegister()
|
|
{
|
|
CommandRegistry::instance().register_command({ssh_name_list,
|
|
ssh_handler,
|
|
shared_commands::std_autocomplete,
|
|
false, // hidden
|
|
true, // requires_config
|
|
true, // requires_install
|
|
1, // min_args (after command)
|
|
2, // max_args (after command)
|
|
"ssh SERVER",
|
|
"SSH into a server, or into a docker container for a service.",
|
|
R"(
|
|
|
|
ssh SERVER SERVICE SSH into a docker container for a service.
|
|
ssh SERVER SSH into a server.
|
|
)"});
|
|
}
|
|
} ssh_command_register;
|
|
|
|
|
|
|
|
bool ssh_into_server(const std::string &server)
|
|
{
|
|
server_env_manager server_env(server);
|
|
if (!server_env.is_valid())
|
|
{
|
|
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);
|
|
return true;
|
|
}
|
|
|
|
bool ssh_into_service(const std::string &server, const std::string &service)
|
|
{
|
|
server_env_manager server_env(server);
|
|
if (!server_env.is_valid())
|
|
{
|
|
error << "Server " << server << " is not valid" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
LocalServiceInfo sinfo = get_service_info(server, service);
|
|
if (!SIvalid(sinfo))
|
|
{
|
|
error << "Service " << service << " is not valid" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
if (!gTemplateManager().has_template(sinfo.template_name))
|
|
{
|
|
error << "Template " << sinfo.template_name << " is not valid" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
if (!gTemplateManager().template_command_exists(sinfo.template_name, "ssh"))
|
|
{
|
|
error << "Template " << sinfo.template_name << " does not have an ssh command" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
server_env.run_remote_template_command(service,"ssh",{},false,{}); // explicitly supports interactive ssh!
|
|
return true;
|
|
}
|
|
|
|
int ssh_handler(const CommandContext &ctx)
|
|
{
|
|
if (ctx.args.size() < 1)
|
|
{
|
|
error << "Server name is required" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
std::string server = safearg(ctx.args, 0);
|
|
|
|
if (ctx.args.size() < 2)
|
|
{
|
|
// ssh into the server
|
|
return ssh_into_server(server) ? 0 : 1;
|
|
}
|
|
|
|
std::string service = safearg(ctx.args, 1);
|
|
|
|
// ssh into the specific service.
|
|
return ssh_into_service(server, service) ? 0 : 1;
|
|
}
|
|
|
|
} // namespace dropshell
|