This commit is contained in:
Your Name 2025-04-21 15:34:18 +12:00
parent 005fbbecbe
commit 3fa5d5076a
7 changed files with 66 additions and 32 deletions

View File

@ -67,9 +67,6 @@ dropshell help
# Show version
dropshell version
# Check system status
dropshell status
# List configured servers
dropshell servers
```

View File

@ -20,7 +20,6 @@ void print_help() {
std::cout << " version Show version information" << std::endl;
std::cout << " init DIR Initialize the user directory for server configurations" << std::endl;
std::cout << std::endl;
std::cout << " status Check system status" << std::endl;
std::cout << " servers List configured servers" << std::endl;
std::cout << " servers NAME Show details for specific server" << std::endl;
std::cout << " templates List available templates" << std::endl;
@ -36,6 +35,7 @@ void print_help() {
std::cout << " dropshell templates" << std::endl;
std::cout << " dropshell install myserver myservice" << std::endl;
std::cout << " dropshell run myserver myservice status" << std::endl;
std::cout << " dropshell backup myserver myservice" << std::endl;
}
} // namespace dropshell
@ -67,11 +67,6 @@ int main(int argc, char* argv[]) {
return 0;
}
if (cmd == "status") {
dropshell::check_status();
return 0;
}
if (cmd == "servers") {
if (argc > 2) {
// Show details for specific server

View File

@ -118,6 +118,11 @@ std::string server_env::get_variable(const std::string& name) {
return value;
}
const std::map<std::string, std::string> &server_env::get_variables() const
{
return variables;
}
std::string server_env::get_SSH_HOST() {
return get_variable("SSH_HOST");
}

View File

@ -22,6 +22,7 @@ class server_env {
public:
server_env(const std::string& path);
std::string get_variable(const std::string& name);
const std::map<std::string, std::string>& get_variables() const;
std::string get_SSH_HOST();
std::string get_SSH_USER();

View File

@ -289,4 +289,28 @@ bool server_service::backup() {
return true;
}
bool server_service::is_healthy()
{
if (!m_server_env) {
std::cerr << "Error: Server service not initialized" << std::endl;
return false;
}
std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
std::string script_dir = service_dir + "/template";
std::string env_path = service_dir + "/" + m_service_name + ".env";
std::stringstream ssh_cmd;
ssh_cmd << "ssh -p " << m_server_env->get_SSH_PORT() << " "
<< m_server_env->get_SSH_USER() << "@" << m_server_env->get_SSH_HOST() << " ";
// Run status script, does not display output.
std::string run_cmd = ssh_cmd.str() + "'cd " + script_dir +
" && /bin/bash status.sh " + env_path + " > /dev/null 2>&1'";
if (system(run_cmd.c_str()) != 0) {
return false;
}
return true;
}
} // namespace dropshell

View File

@ -44,6 +44,12 @@ class server_service {
// 4. copy it to the local user_dir/backups folder
bool backup();
// check health of service. Silent.
// 1. run status.sh on the server
// 2. return the output of the status.sh script
bool is_healthy();
private:
std::string m_server_name;
std::string m_service_name;

View File

@ -1,6 +1,7 @@
#include "init_user_directory.hpp"
#include "dropshell.hpp"
#include "server_env.hpp"
#include "server_service.hpp"
#include <iostream>
#include <fstream>
#include <iomanip>
@ -94,34 +95,19 @@ void show_server_details(const std::string& server_name) {
return;
}
fs::path env_file = server_dir / "_server.env";
if (!fs::exists(env_file)) {
std::cerr << "Error: Server configuration file not found" << std::endl;
return;
}
std::cout << "Server Details: " << server_name << std::endl;
//---------------------
std::cout << "Server Configuration: " << server_name << std::endl;
std::cout << std::string(40, '-') << std::endl;
std::ifstream file(env_file.string());
std::string line;
while (std::getline(file, line)) {
if (!line.empty() && line[0] != '#') {
std::cout << line << std::endl;
}
server_env env(server_dir.string());
for (const auto& [key, value] : env.get_variables()) {
std::cout << key << ": " << value << std::endl;
}
//---------------------
// Check if server is reachable via SSH
std::string ssh_address;
file.clear();
file.seekg(0);
while (std::getline(file, line)) {
if (boost::starts_with(line, "SSH_ADDRESS=")) {
ssh_address = line.substr(12);
break;
}
}
std::string ssh_address = env.get_SSH_HOST();
if (!ssh_address.empty()) {
std::cout << std::endl << "Server Status:" << std::endl;
std::cout << std::string(40, '-') << std::endl;
@ -142,6 +128,26 @@ void show_server_details(const std::string& server_name) {
std::cout << "Status: Offline" << std::endl;
}
}
//---------------------
// list services, and run healthcheck on each
std::cout << "Services: " << server_name << std::endl;
std::cout << std::string(40, '-') << std::endl;
std::string green_tick = "\033[32m✓\033[0m";
std::string red_cross = "\033[31m✗\033[0m";
std::vector<std::string> services = get_server_services(server_name);
for (const auto& service : services) {
bool healthy = false;
server_service ss;
if (ss.init(server_name, service))
if (ss.is_healthy())
healthy=true;
std::cout << service << ": " << (healthy ? green_tick : red_cross) << std::endl;
}
}
} // namespace dropshell