diff --git a/src/server_env.cpp b/src/server_env.cpp index 76f3e19..1859009 100644 --- a/src/server_env.cpp +++ b/src/server_env.cpp @@ -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); } +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 args, const std::string &error_msg) const { 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; } -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"; FILE *pipe = popen(full_cmd.c_str(), "r"); 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; } char buffer[128]; diff --git a/src/server_env.hpp b/src/server_env.hpp index 8e23b1e..589d2a2 100644 --- a/src/server_env.hpp +++ b/src/server_env.hpp @@ -35,18 +35,19 @@ class server_env { // helper functions public: bool check_remote_items_exist(const std::vector& 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 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_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_file_exists(const std::string& file_path) const; - std::string construct_ssh_cmd() const; - 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 args) const; diff --git a/src/service_runner.cpp b/src/service_runner.cpp index de9ee21..6a032a4 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -217,11 +217,9 @@ std::map service_runner::get_all_services_status(std } 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; - 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; std::stringstream ss(output);