Tidy ssh commands

This commit is contained in:
John 2025-04-27 09:43:02 +12:00
parent c6665f3044
commit e3e8e5f7d3
3 changed files with 16 additions and 10 deletions

View File

@ -144,6 +144,12 @@ bool server_env::execute_ssh_command(const std::string& command, const std::stri
return execute_local_command(full_cmd, error_msg); return execute_local_command(full_cmd, error_msg);
} }
bool server_env::execute_ssh_command_and_capture_output(const std::string &command, std::string &output, const std::string &error_msg) const
{
std::string full_cmd = construct_ssh_cmd() + command;
return execute_local_command_and_capture_output(full_cmd, output);
}
bool server_env::run_remote_template_command(const std::string &service_name, const std::string &command, std::vector<std::string> args, const std::string &error_msg) const bool server_env::run_remote_template_command(const std::string &service_name, const std::string &command, std::vector<std::string> args, const std::string &error_msg) const
{ {
std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args); std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args);
@ -158,12 +164,13 @@ bool server_env::execute_local_command(const std::string& command, const std::st
return okay; return okay;
} }
bool server_env::execute_local_command_and_capture_output(const std::string &command, std::string &output) bool server_env::execute_local_command_and_capture_output(const std::string &command, std::string &output, const std::string& error_msg)
{ {
std::string full_cmd = command + " 2>&1"; std::string full_cmd = command + " 2>&1";
FILE *pipe = popen(full_cmd.c_str(), "r"); FILE *pipe = popen(full_cmd.c_str(), "r");
if (!pipe) { if (!pipe) {
std::cerr << "Error: Failed to execute command: " << command << std::endl; if (!error_msg.empty())
std::cerr << "Error: " << error_msg << std::endl;
return false; return false;
} }
char buffer[128]; char buffer[128];

View File

@ -35,18 +35,19 @@ class server_env {
// helper functions // helper functions
public: public:
bool check_remote_items_exist(const std::vector<std::string>& file_paths) const; bool check_remote_items_exist(const std::vector<std::string>& file_paths) const;
bool execute_ssh_command(const std::string& command, const std::string& error_msg="") const;
bool run_remote_template_command(const std::string& service_name, const std::string& command, std::vector<std::string> args, const std::string& error_msg="") const; bool run_remote_template_command(const std::string& service_name, const std::string& command, std::vector<std::string> args, const std::string& error_msg="") const;
bool execute_ssh_command(const std::string& command, const std::string& error_msg="") const;
bool execute_ssh_command_and_capture_output(const std::string& command, std::string & output, const std::string& error_msg="") const;
static bool execute_local_command(const std::string& command, const std::string& error_msg=""); static bool execute_local_command(const std::string& command, const std::string& error_msg="");
static bool execute_local_command_and_capture_output(const std::string& command, std::string & output); static bool execute_local_command_and_capture_output(const std::string& command, std::string & output, const std::string& error_msg="");
bool check_remote_dir_exists(const std::string &dir_path) const; bool check_remote_dir_exists(const std::string &dir_path) const;
bool check_remote_file_exists(const std::string& file_path) const; bool check_remote_file_exists(const std::string& file_path) const;
std::string construct_ssh_cmd() const;
private: private:
std::string construct_ssh_cmd() const;
std::string construct_standard_command_run_cmd(const std::string& service_name, const std::string& command, std::vector<std::string> args) const; std::string construct_standard_command_run_cmd(const std::string& service_name, const std::string& command, std::vector<std::string> args) const;

View File

@ -217,11 +217,9 @@ std::map<std::string, ServiceStatus> service_runner::get_all_services_status(std
} }
std::string script_path = remote_service_template_path + "/" + command + ".sh"; std::string script_path = remote_service_template_path + "/" + command + ".sh";
std::string ssh_cmd = env.construct_ssh_cmd() + "'cd " + quote(remote_service_template_path) +
" && /bin/bash "+quote(script_path)+" "+quote(remote_service_config_path)+"'";
std::string output; std::string output;
if (!env.execute_local_command_and_capture_output(ssh_cmd, output)) if (!env.execute_ssh_command_and_capture_output("'cd " + quote(remote_service_template_path) +
" && /bin/bash "+quote(script_path)+"'", output))
return status; return status;
std::stringstream ss(output); std::stringstream ss(output);