diff --git a/src/servers.cpp b/src/servers.cpp index 19d0d31..e713c3c 100644 --- a/src/servers.cpp +++ b/src/servers.cpp @@ -128,26 +128,19 @@ void show_server_details(const std::string& server_name) { tableprint tp("Services: " + server_name, false); tp.add_row({"Status", "Service", "Ports"}); - std::vector services = get_server_services_info(server_name); - for (const auto& service : services) { - std::string healthy = "?"; - std::vector ports; - service_runner ss; - if (ss.init(server_name, service.service_name)) - { - healthy = ss.healthmark(); - ports = ss.get_ports(); - } - bool first = true; + + std::map status = service_runner::get_all_services_status(server_name); + + std::set ports_used; + std::string serviceticks = ""; + for (const auto& [service_name, service_status] : status) { + std::string healthy = service_runner::HealthStatus2String(service_status.health); + std::string ports_str = ""; - for (const auto& port : ports) { - if (!first) { - ports_str += ", "; - } - ports_str += std::to_string(port); - first = false; - } - tp.add_row({healthy, service.service_name, ports_str}); + for (const auto& port : service_status.ports) + ports_str += std::to_string(port) + " "; + + tp.add_row({healthy, service_name, ports_str}); } // end of for (const auto& service : services) tp.print(); } // end of list services diff --git a/src/service_runner.cpp b/src/service_runner.cpp index f1cb0a0..eef65dc 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -343,12 +343,12 @@ std::map service_runner::get_all_services_status(std { std::map status; - std::string command = "_allservicesstatus.sh"; + std::string command = "_allservicesstatus"; std::string service_name = "dropshell-agent"; if (!template_command_exists(service_name, command)) { - std::cerr << "Error: " << service_name << " does not contain the _allservicesstatus.sh script" << std::endl; + std::cerr << "Error: " << service_name << " does not contain the " << command << " script" << std::endl; return status; } @@ -358,7 +358,7 @@ std::map service_runner::get_all_services_status(std std::string remote_service_env_file = get_remote_service_env_file(server_name, service_name); - std::string script_path = remote_service_template_path + "/" + command; + std::string script_path = remote_service_template_path + "/" + command + ".sh"; server_env env(server_name); if (!env.is_valid()) { @@ -397,8 +397,10 @@ std::map service_runner::get_all_services_status(std status[service_name].health = HealthStatus::ERROR; } else if (status_type == "PORTS") { // port1,port2,port3 std::vector ports = string2multi(value); - for (const auto& port : ports) - status[service_name].ports.push_back(std::stoi(port)); + for (const auto& port : ports) { + if (port!="unknown") + status[service_name].ports.push_back(str2int(port)); + } } } } @@ -471,69 +473,4 @@ std::string service_runner::healthmark() return HealthStatus2String(status); } -std::vector service_runner::get_ports() -{ - std::vector ports; - if (!m_server_env) { - std::cerr << "Error: Server service not initialized" << std::endl; - return ports; - } - - // Check if ports script exists - std::string command = "ports"; - if (!template_command_exists(m_service_info.template_name, command)) { - return ports; - } - std::string script_path = mRemote_service_template_path + "/" + command + ".sh"; - if (!check_remote_file_exists(script_path)) { - return ports; - } - - // Run the ports script and capture output - std::string run_cmd = "'cd " + mRemote_service_template_path + - " && /bin/bash " + script_path + " " + mRemote_service_config_path + "'"; - - // Create a temporary file to store the output - std::string temp_file = "/tmp/dropshell_ports_" + std::to_string(getpid()); - std::string full_cmd = construct_ssh_cmd() + run_cmd + " > " + temp_file; - - if (!execute_local_command(full_cmd, "Failed to run ports script")) { - return ports; - } - - // Read the output file - std::ifstream in(temp_file); - if (!in.is_open()) { - std::cerr << "Error: Failed to read ports output" << std::endl; - return ports; - } - - // Read the entire file content - std::string content((std::istreambuf_iterator(in)), std::istreambuf_iterator()); - - // Clean up temporary file - std::remove(temp_file.c_str()); - - // Process the content - std::stringstream ss(content); - std::string token; - - // First split by commas - while (std::getline(ss, token, ',')) { - // Then split each comma-separated part by whitespace - std::stringstream token_ss(token); - std::string port_str; - while (token_ss >> port_str) { // This handles all whitespace including newlines - try { - int port = std::stoi(port_str); - ports.push_back(port); - } catch (const std::exception& e) { - std::cerr << "Warning: Invalid port number: " << port_str << std::endl; - } - } - } - - return ports; -} - } // namespace dropshell \ No newline at end of file diff --git a/src/service_runner.hpp b/src/service_runner.hpp index c9945c4..a176182 100644 --- a/src/service_runner.hpp +++ b/src/service_runner.hpp @@ -49,11 +49,6 @@ class service_runner { HealthStatus is_healthy(); - // get the ports of the service - // 1. run _ports.sh on the server - // 2. return the output of the _ports.sh script, handling a port per line or comma separated. - std::vector get_ports(); - std::string healthtick(); std::string healthmark(); diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index c5ae985..b7e9092 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -129,5 +129,14 @@ std::vector string2multi(std::string values) return result; } +int str2int(const std::string &str) +{ + try { + return std::stoi(str); + } catch (const std::exception& e) { + std::cerr << "Error: Invalid integer string: [" << str << "]" << std::endl; + return 0; + } +} } // namespace dropshell \ No newline at end of file diff --git a/src/utils/utils.hpp b/src/utils/utils.hpp index 78719b5..4ffbc13 100644 --- a/src/utils/utils.hpp +++ b/src/utils/utils.hpp @@ -22,5 +22,6 @@ std::string quote(std::string str); std::string multi2string(std::vector values); std::vector string2multi(std::string values); +int str2int(const std::string & str); } // namespace dropshell \ No newline at end of file diff --git a/templates/dropshell-agent/_allservicesstatus.sh b/templates/dropshell-agent/_allservicesstatus.sh index 6e5e51b..020bea9 100644 --- a/templates/dropshell-agent/_allservicesstatus.sh +++ b/templates/dropshell-agent/_allservicesstatus.sh @@ -47,7 +47,7 @@ for SERVICE_NAME in ${SERVICE_NAMES}; do # capture output from the ports script SERVICE_PORTS=$(bash "${PORTS_FILE}" "${SERVICE_PATH}/config" 2>&1) else - SERVICE_PORTS="unknown" + SERVICE_PORTS="" fi # return the health and ports