This commit is contained in:
Your Name
2025-04-25 10:21:41 +12:00
parent 40b992efeb
commit 5dd4a9dce6
13 changed files with 72 additions and 303 deletions

View File

@ -65,10 +65,21 @@ void list_servers() {
service_runner ss;
if (ss.init(server.name, service.service_name))
{
if (ss.is_healthy())
serviceticks += ":tick: ";
else
serviceticks += ":cross: ";
switch (ss.is_healthy())
{
case service_runner::HealthStatus::HEALTHY:
serviceticks += ":tick: ";
break;
case service_runner::HealthStatus::UNHEALTHY:
serviceticks += ":cross: ";
break;
case service_runner::HealthStatus::NOTINSTALLED:
serviceticks += ":warning: ";
break;
case service_runner::HealthStatus::ERROR:
serviceticks += ":error: ";
break;
}
}
else std::cout<<"Error: Failed to initialise service runner for server: ["<<server.name<<"] and service: ["<<service.service_name<<"]"<<std::endl;
std::vector<int> ports = ss.get_ports();
@ -140,13 +151,26 @@ void show_server_details(const std::string& server_name) {
std::vector<ServiceInfo> services = get_server_services_info(server_name);
for (const auto& service : services) {
bool healthy = false;
std::string healthy = "?";
std::vector<int> ports;
service_runner ss;
if (ss.init(server_name, service.service_name))
{
if (ss.is_healthy())
healthy=true;
switch (ss.is_healthy())
{
case service_runner::HealthStatus::HEALTHY:
healthy = ":check:";
break;
case service_runner::HealthStatus::UNHEALTHY:
healthy = ":cross:";
break;
case service_runner::HealthStatus::NOTINSTALLED:
healthy = ":warning:";
break;
default:
healthy = ":error:";
break;
}
ports = ss.get_ports();
}
bool first = true;
@ -158,7 +182,7 @@ void show_server_details(const std::string& server_name) {
ports_str += std::to_string(port);
first = false;
}
tp.add_row({healthy ? ":tick:" : ":cross:", service.service_name, ports_str});
tp.add_row({healthy, service.service_name, ports_str});
} // end of for (const auto& service : services)
tp.print();
} // end of list services

View File

@ -56,8 +56,8 @@ std::string service_runner::construct_ssh_cmd() const {
return ssh_cmd.str();
}
bool service_runner::check_remote_dir_exists(const std::string& ssh_cmd, const std::string& dir_path) const {
std::string check_dir_cmd = ssh_cmd + "'test -d " + dir_path + "'";
bool service_runner::check_remote_dir_exists(const std::string& dir_path) const {
std::string check_dir_cmd = construct_ssh_cmd() + "'test -d " + dir_path + "'";
if (system(check_dir_cmd.c_str()) != 0) {
std::cerr << "Error: Directory not found on remote server:" << dir_path << std::endl;
return false;
@ -65,8 +65,8 @@ bool service_runner::check_remote_dir_exists(const std::string& ssh_cmd, const s
return true;
}
bool service_runner::check_remote_file_exists(const std::string& ssh_cmd, const std::string& file_path) const {
std::string check_cmd = ssh_cmd + "'test -f " + file_path + "'";
bool service_runner::check_remote_file_exists(const std::string& file_path) const {
std::string check_cmd = construct_ssh_cmd() + "'test -f " + file_path + "'";
if (system(check_cmd.c_str()) != 0) {
std::cerr << "Error: File not found on remote server: " << file_path << std::endl;
return false;
@ -164,21 +164,20 @@ bool service_runner::run_command(const std::string& command) {
return false;
}
std::string ssh_cmd = construct_ssh_cmd();
std::string script_path = mRemote_service_template_path + "/" + command + ".sh";
// Check if service directory exists
if (!check_remote_dir_exists(ssh_cmd, mRemote_service_path)) {
if (!check_remote_dir_exists(mRemote_service_path)) {
return false;
}
// Check if command script exists
if (!check_remote_file_exists(ssh_cmd, script_path)) {
if (!check_remote_file_exists(script_path)) {
return false;
}
// Check if env file exists
if (!check_remote_file_exists(ssh_cmd, mRemote_service_env_file)) {
if (!check_remote_file_exists(mRemote_service_env_file)) {
return false;
}
@ -196,11 +195,10 @@ bool service_runner::backup() {
return false;
}
std::string ssh_cmd = construct_ssh_cmd();
std::string script_path = mRemote_service_template_path + "/_backup.sh";
// Check if basic installed stuff is in place.
if (!check_remote_dir_exists(ssh_cmd, mRemote_service_path) || !check_remote_file_exists(ssh_cmd, script_path) || !check_remote_file_exists(ssh_cmd, mRemote_service_env_file))
if (!check_remote_dir_exists(mRemote_service_path) || !check_remote_file_exists(script_path) || !check_remote_file_exists(mRemote_service_env_file))
return false;
// Create backups directory on server if it doesn't exist
@ -246,26 +244,40 @@ bool service_runner::backup() {
return true;
}
bool service_runner::is_healthy()
service_runner::HealthStatus service_runner::is_healthy()
{
if (!m_server_env) {
std::cerr << "Error: Server service not initialized" << std::endl;
return false;
return HealthStatus::ERROR;
}
// Check if status script exists
std::string script_path = mRemote_service_template_path + "/_status.sh";
if (!check_remote_file_exists(script_path)) {
return HealthStatus::NOTINSTALLED;
}
// Run status script, does not display output.
return execute_ssh_command("'cd " + mRemote_service_template_path + " && /bin/bash _status.sh " + mRemote_service_env_file + " > /dev/null 2>&1'","");
bool ok = execute_ssh_command("'cd " + mRemote_service_template_path + " && /bin/bash _status.sh " + mRemote_service_env_file + " > /dev/null 2>&1'", "");
if (!ok)
return HealthStatus::UNHEALTHY;
return HealthStatus::HEALTHY;
}
std::string service_runner::healthtick()
{
std::string green_tick = "\033[32m✓\033[0m";
std::string red_cross = "\033[31m✗\033[0m";
std::string yellow_exclamation = "\033[33m!\033[0m";
if (is_healthy())
HealthStatus status = is_healthy();
if (status == HealthStatus::HEALTHY)
return green_tick;
else
else if (status == HealthStatus::UNHEALTHY)
return red_cross;
else
return yellow_exclamation;
}
std::vector<int> service_runner::get_ports()
@ -276,11 +288,10 @@ std::vector<int> service_runner::get_ports()
return ports;
}
std::string ssh_cmd = construct_ssh_cmd();
std::string script_path = mRemote_service_template_path + "/_ports.sh";
// Check if ports script exists
if (!check_remote_file_exists(ssh_cmd, script_path)) {
if (!check_remote_file_exists(script_path)) {
return ports;
}
@ -290,7 +301,7 @@ std::vector<int> service_runner::get_ports()
// Create a temporary file to store the output
std::string temp_file = "/tmp/dropshell_ports_" + std::to_string(getpid());
std::string full_cmd = ssh_cmd + run_cmd + " > " + temp_file;
std::string full_cmd = construct_ssh_cmd() + run_cmd + " > " + temp_file;
if (!execute_local_command(full_cmd, "Failed to run ports script")) {
return ports;

View File

@ -47,7 +47,13 @@ class service_runner {
// check health of service. Silent.
// 1. run status.sh on the server
// 2. return the output of the status.sh script
bool is_healthy();
enum class HealthStatus {
HEALTHY,
UNHEALTHY,
NOTINSTALLED,
ERROR
};
HealthStatus is_healthy();
// get the ports of the service
// 1. run _ports.sh on the server
@ -68,8 +74,8 @@ class service_runner {
// Helper methods
std::string construct_ssh_cmd() const;
bool check_remote_dir_exists(const std::string& ssh_cmd, const std::string& dir_path) const;
bool check_remote_file_exists(const std::string& ssh_cmd, const std::string& file_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 execute_ssh_command(const std::string& command, const std::string& error_msg) const;
bool execute_local_command(const std::string& command, const std::string& error_msg) const;
void maketitle(const std::string& title) const;

View File

@ -34,7 +34,8 @@ const std::map<std::string, coloredText> kReplacements = {
{":warning:", {"!", kTextColor_Yellow}},
{":info:", {"i", kTextColor_Blue}},
{":check:", {"+", kTextColor_Green}},
{":x:", {"x", kTextColor_Red}}
{":x:", {"x", kTextColor_Red}},
{":error:", {"!", kTextColor_Red}}
};
// Helper function to get ANSI color code