From 59561824c76752956be4e0cf469a280f3d5a0cb1 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 27 Apr 2025 19:56:44 +1200 Subject: [PATCH] Restore command --- src/main_commands.cpp | 31 ++++++++++++---------- src/service_runner.cpp | 3 +-- src/services.hpp | 2 -- src/status.cpp | 58 ------------------------------------------ src/status.hpp | 7 ----- 5 files changed, 19 insertions(+), 82 deletions(-) delete mode 100644 src/status.cpp delete mode 100644 src/status.hpp diff --git a/src/main_commands.cpp b/src/main_commands.cpp index cf3b642..05064f6 100644 --- a/src/main_commands.cpp +++ b/src/main_commands.cpp @@ -71,10 +71,15 @@ int restore(const std::vector &args) return 1; } + server_env env(server_name); + if (!env.is_valid()) { + std::cerr << "Error: Invalid server environment" << std::endl; + return 1; + } + std::string local_backups_dir = get_local_backup_path(); std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_file).string(); - if (! std::filesystem::exists(local_backup_file_path)) { std::cerr << "Error: Backup file not found at " << local_backup_file_path << std::endl; return 1; @@ -127,25 +132,25 @@ int restore(const std::vector &args) } std::cout << "Backup complete." << std::endl; } - { // uninstalling the current service + { // silently uninstalling the current service std::cout << "2) Uninstalling current service..." << std::endl; - if (!uninstall(uninstall_args,true)) // silent=true - { - std::cerr << std::endl; - std::cerr << "Error: Uninstall failed, restore aborted." << std::endl; - return 1; - } + env.run_remote_template_command(service_name, "uninstall", {}, true); } { // restore service from backup - std::cout << "2) Restoring service from backup..." << std::endl; + std::cout << "3) Restoring service from backup..." << std::endl; + env.run_remote_template_command(service_name, "restore", {local_backup_file_path}, true); } - { // initialise service - std::cout << "3) Initialising service..." << std::endl; - } + // healthcheck the service + std::cout << "4) Healthchecking service..." << std::endl; + std::string green_tick = "\033[32m✓\033[0m"; + std::string red_cross = "\033[31m✗\033[0m"; + if (!env.run_remote_template_command(service_name, "status", {}, true)) + std::cout << green_tick << " Service is healthy." << std::endl; + else + std::cout << red_cross << " Service is NOT healthy." << std::endl; - // run the restore script return 0; } diff --git a/src/service_runner.cpp b/src/service_runner.cpp index 8e235ea..4b0ddcb 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -281,8 +281,7 @@ HealthStatus service_runner::is_healthy() } // Run status script, does not display output. - std::vector args; // not passed through yet. - if (!m_server_env.run_remote_template_command(m_service_info.service_name, "status", args)) + if (!m_server_env.run_remote_template_command(m_service_info.service_name, "status", {}, true)) return HealthStatus::UNHEALTHY; return HealthStatus::HEALTHY; } diff --git a/src/services.hpp b/src/services.hpp index a29d259..86751ca 100644 --- a/src/services.hpp +++ b/src/services.hpp @@ -25,6 +25,4 @@ namespace dropshell { bool create_service(const std::string& server_name, const std::string& template_name, const std::string& service_name, bool silent=false); } // namespace dropshell - - #endif diff --git a/src/status.cpp b/src/status.cpp deleted file mode 100644 index 024f50b..0000000 --- a/src/status.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "status.hpp" -#include -#include -#include -#include -#include -#include - -namespace dropshell { - -void check_status() { - // Get current time - auto now = std::chrono::system_clock::now(); - auto time = std::chrono::system_clock::to_time_t(now); - std::cout << "System Status:" << std::endl; - std::cout << "Date: " << std::ctime(&time); - - // Get uptime - struct sysinfo si; - if (sysinfo(&si) == 0) { - int days = si.uptime / 86400; - int hours = (si.uptime % 86400) / 3600; - int minutes = (si.uptime % 3600) / 60; - std::cout << "Uptime: " << days << " days, " << hours << " hours, " << minutes << " minutes" << std::endl; - } - - // Get memory usage - std::ifstream meminfo("/proc/meminfo"); - if (meminfo.is_open()) { - std::string line; - long total_mem = 0, free_mem = 0; - while (std::getline(meminfo, line)) { - if (line.find("MemTotal:") == 0) { - total_mem = std::stol(line.substr(9)); - } else if (line.find("MemAvailable:") == 0) { - free_mem = std::stol(line.substr(13)); - } - } - std::cout << "Memory Usage:" << std::endl; - std::cout << "Total: " << total_mem / 1024 << " MB" << std::endl; - std::cout << "Available: " << free_mem / 1024 << " MB" << std::endl; - } - - // Get disk usage - struct statvfs vfs; - if (statvfs("/", &vfs) == 0) { - uint64_t total = vfs.f_blocks * vfs.f_frsize; - uint64_t free = vfs.f_bfree * vfs.f_frsize; - uint64_t used = total - free; - - std::cout << "Disk Usage:" << std::endl; - std::cout << "Total: " << total / (1024*1024*1024) << " GB" << std::endl; - std::cout << "Used: " << used / (1024*1024*1024) << " GB" << std::endl; - std::cout << "Free: " << free / (1024*1024*1024) << " GB" << std::endl; - } -} - -} // namespace dropshell \ No newline at end of file diff --git a/src/status.hpp b/src/status.hpp deleted file mode 100644 index c52b28e..0000000 --- a/src/status.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef STATUS_HPP -#define STATUS_HPP - -namespace dropshell { - void check_status(); -} -#endif \ No newline at end of file