diff --git a/src/server_env.cpp b/src/server_env.cpp index dacdb6f..6ec8f18 100644 --- a/src/server_env.cpp +++ b/src/server_env.cpp @@ -156,27 +156,29 @@ bool server_env::check_remote_items_exist(const std::vector &file_p bool server_env::execute_ssh_command(const sCommand& command) const { std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd()); - bool okay = execute_local_command(full_cmd); - return okay; + return execute_local_command(full_cmd); } bool server_env::execute_ssh_command_and_capture_output(const sCommand& command, std::string &output) const { std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd()); - bool okay = execute_local_command_and_capture_output(full_cmd, output); - return okay; + 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, bool silent) const { std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args, silent); - bool okay = execute_ssh_command(full_cmd); - return okay; + return execute_ssh_command(full_cmd); +} + +bool server_env::run_remote_template_command_and_capture_output(const std::string &service_name, const std::string &command, std::vector args, std::string &output, bool silent) const +{ + std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args, silent); + return execute_ssh_command_and_capture_output(full_cmd, output); } bool server_env::execute_local_command(const sCommand& command) { - bool okay = (system(command.construct_safecmd().c_str()) == 0); - return okay; + return (system(command.construct_safecmd().c_str()) == 0); } bool server_env::execute_local_command_and_capture_output(const sCommand& command, std::string &output) diff --git a/src/server_env.hpp b/src/server_env.hpp index 5dd2bd4..18177fd 100644 --- a/src/server_env.hpp +++ b/src/server_env.hpp @@ -61,6 +61,7 @@ class server_env { bool check_remote_items_exist(const std::vector& file_paths) const; bool run_remote_template_command(const std::string& service_name, const std::string& command, std::vector args, bool silent=false) const; + bool run_remote_template_command_and_capture_output(const std::string& service_name, const std::string& command, std::vector args, std::string & output, bool silent=false) const; public: bool execute_ssh_command(const sCommand& command) const; diff --git a/src/service_runner.cpp b/src/service_runner.cpp index bd5b101..89f9116 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -18,7 +18,6 @@ namespace fs = std::filesystem; namespace dropshell { - service_runner::service_runner(const std::string& server_name, const std::string& service_name) : m_server_env(server_name), m_server_name(server_name), mValid(false) { @@ -227,10 +226,8 @@ std::map service_runner::get_all_services_status(std return status; } - std::string remote_service_template_path = get_remote_service_template_path(server_name, service_name); - sCommand scommand(remote_service_template_path, "/bin/bash " + quote(remote_service_template_path + "/" + command + ".sh"), {}); std::string output; - if (!env.execute_ssh_command_and_capture_output(scommand, output)) + if (!env.run_remote_template_command_and_capture_output(service_name, command, {}, output)) return status; std::stringstream ss(output); diff --git a/templates/dropshell-agent/_allservicesstatus.sh b/templates/dropshell-agent/_allservicesstatus.sh index 3124523..062bfe8 100644 --- a/templates/dropshell-agent/_allservicesstatus.sh +++ b/templates/dropshell-agent/_allservicesstatus.sh @@ -15,9 +15,32 @@ SCRIPT_DIR="$(dirname "$0")" # // |-- service name # // |-- config <-- this is passed as argument to all scripts # // |-- service.env -# // |-- (user config files) # // |-- template # // |-- (script files) +# // |-- example +# // |-- service.env +# // |-- (other config files for specific server&service) + + +function run_command() { + local service_path=$1 + local command=$2 + + # check if the command is a file + if [ ! -f "${service_path}/template/${command}.sh" ]; then + return; + fi + + # run the command in a subshell to prevent environment changes + ( + source "${service_path}/config/service.env" + # SERVER is correct + export CONFIG_PATH="${service_path}/config" + # update the SERVICE variable + export SERVICE="${SERVICE_NAME}" + bash "${service_path}/template/${command}.sh" 2>&1 + ) +} # Get all services on the server SERVICES_PATH="${SCRIPT_DIR}/../../"