Bug fixing.

This commit is contained in:
Your Name 2025-04-25 20:31:25 +12:00
parent f73e6f1ac5
commit d0b7c27af3
6 changed files with 30 additions and 95 deletions

View File

@ -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<ServiceInfo> services = get_server_services_info(server_name);
for (const auto& service : services) {
std::string healthy = "?";
std::vector<int> ports;
service_runner ss;
if (ss.init(server_name, service.service_name))
{
healthy = ss.healthmark();
ports = ss.get_ports();
}
bool first = true;
std::map<std::string, ServiceStatus> status = service_runner::get_all_services_status(server_name);
std::set<int> 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

View File

@ -343,12 +343,12 @@ std::map<std::string, ServiceStatus> service_runner::get_all_services_status(std
{
std::map<std::string, ServiceStatus> 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<std::string, ServiceStatus> 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<std::string, ServiceStatus> service_runner::get_all_services_status(std
status[service_name].health = HealthStatus::ERROR;
} else if (status_type == "PORTS") { // port1,port2,port3
std::vector<std::string> 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<int> service_runner::get_ports()
{
std::vector<int> 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<char>(in)), std::istreambuf_iterator<char>());
// 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

View File

@ -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<int> get_ports();
std::string healthtick();
std::string healthmark();

View File

@ -129,5 +129,14 @@ std::vector<std::string> 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

View File

@ -22,5 +22,6 @@ std::string quote(std::string str);
std::string multi2string(std::vector<std::string> values);
std::vector<std::string> string2multi(std::string values);
int str2int(const std::string & str);
} // namespace dropshell

View File

@ -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