From 84c252c965f529d0d9e2e9b6903b06fd9bde1701 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 25 Apr 2025 20:46:10 +1200 Subject: [PATCH] Backups working again --- src/service_runner.cpp | 31 +++++++++++++++++++------------ src/service_runner.hpp | 2 ++ templates/example/_common.sh | 2 +- templates/watchtower/_common.sh | 2 +- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/service_runner.cpp b/src/service_runner.cpp index eef65dc..901dfa2 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -63,6 +63,14 @@ std::string service_runner::construct_ssh_cmd() const return construct_ssh_cmd(*m_server_env); } +std::string service_runner::construct_standard_command_run_cmd(const std::string &command) const +{ + std::string script_path = mRemote_service_template_path + "/" + command + ".sh"; + std::string run_cmd = "'cd " + quote(mRemote_service_template_path) + + " && /bin/bash "+quote(script_path)+" "+quote(mRemote_service_config_path)+"'"; + return run_cmd; +} + bool service_runner::check_remote_dir_exists(const std::string &dir_path) const { std::string check_dir_cmd = construct_ssh_cmd() + "'test -d " + quote(dir_path) + "'"; @@ -176,8 +184,7 @@ bool service_runner::install() { // Run install script { - std::string install_cmd = "'cd " + quote(mRemote_service_template_path) + - " && /bin/bash install.sh " + quote(mRemote_service_config_path) + "'"; + std::string install_cmd = construct_standard_command_run_cmd("install"); bool ok= execute_ssh_command(install_cmd, "Failed to run install script"); if (!ok) return false; @@ -204,8 +211,7 @@ bool service_runner::uninstall() { bool script_exists = check_remote_file_exists(uninstall_script); if (script_exists) { - std::string uninstall_cmd = "'cd " + quote(mRemote_service_template_path) + - " && /bin/bash "+quote(uninstall_script)+" "+quote(mRemote_service_config_path)+"'"; + std::string uninstall_cmd = construct_standard_command_run_cmd("uninstall"); if (!execute_ssh_command(uninstall_cmd, "Failed to run uninstall script")) { std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl; } @@ -266,8 +272,7 @@ bool service_runner::run_command(const std::string& command) { return backup(); // Run the generic command - std::string run_cmd = "'cd " + quote(mRemote_service_template_path) + - " && /bin/bash "+quote(script_path)+" "+quote(mRemote_service_config_path)+"'"; + std::string run_cmd = construct_standard_command_run_cmd(command); return execute_ssh_command(run_cmd, "Command returned error code: " + script_path); } @@ -276,7 +281,7 @@ bool service_runner::backup() { if (!m_server_env) return false; // should never hit this. - std::string command = "_backup"; + std::string command = "backup"; std::string script_path = mRemote_service_template_path + "/" + command + ".sh"; if (!template_command_exists(m_service_info.template_name, command)) { @@ -322,8 +327,11 @@ bool service_runner::backup() { // Run backup script std::string backup_cmd = "'cd " + quote(mRemote_service_template_path) + - " && /bin/bash "+quote(script_path)+" "+quote(mRemote_service_env_file)+" "+quote(server_backup_path)+"'"; + " && /bin/bash "+quote(script_path)+" "+quote(mRemote_service_config_path)+" "+ + quote(server_backup_path)+"'"; + if (!execute_ssh_command(backup_cmd, "Backup script failed")) { + std::cerr << "Backup script failed: " << backup_cmd << std::endl; return false; } @@ -357,15 +365,13 @@ std::map service_runner::get_all_services_status(std std::string remote_service_template_path = get_remote_service_template_path(server_name, service_name); std::string remote_service_env_file = get_remote_service_env_file(server_name, service_name); - - std::string script_path = remote_service_template_path + "/" + command + ".sh"; - server_env env(server_name); if (!env.is_valid()) { std::cerr << "Error: Invalid server environment" << std::endl; return status; } + std::string script_path = remote_service_template_path + "/" + command + ".sh"; std::string ssh_cmd = construct_ssh_cmd(env) + "'cd " + quote(remote_service_template_path) + " && /bin/bash "+quote(script_path)+" "+quote(remote_service_config_path)+"'"; @@ -428,7 +434,8 @@ HealthStatus service_runner::is_healthy() } // Run status script, does not display output. - bool ok = execute_ssh_command("'cd " + mRemote_service_template_path + " && /bin/bash " + script_path + " " + mRemote_service_config_path + " > /dev/null 2>&1'", ""); + std::string run_cmd = construct_standard_command_run_cmd("status"); + bool ok = execute_ssh_command(run_cmd, ""); if (!ok) return HealthStatus::UNHEALTHY; diff --git a/src/service_runner.hpp b/src/service_runner.hpp index a176182..7c0b9d9 100644 --- a/src/service_runner.hpp +++ b/src/service_runner.hpp @@ -96,6 +96,8 @@ class service_runner { static std::string construct_ssh_cmd(const server_env &env); std::string construct_ssh_cmd() const; + std::string construct_standard_command_run_cmd(const std::string& command) const; + bool check_remote_dir_exists(const std::string& dir_path) const; bool check_remote_file_exists(const std::string& file_path) const; bool check_remote_items_exist(const std::vector& file_paths) const; diff --git a/templates/example/_common.sh b/templates/example/_common.sh index b1e7e6e..e4e99d5 100755 --- a/templates/example/_common.sh +++ b/templates/example/_common.sh @@ -40,7 +40,7 @@ load_env() { env_file="$1/service.env" if [ ! -f "$env_file" ]; then - echo "Warning: service.env file not found in $1" + echo "Warning: service.env file not found at $1/service.env" return 1 fi diff --git a/templates/watchtower/_common.sh b/templates/watchtower/_common.sh index b1e7e6e..e4e99d5 100755 --- a/templates/watchtower/_common.sh +++ b/templates/watchtower/_common.sh @@ -40,7 +40,7 @@ load_env() { env_file="$1/service.env" if [ ! -f "$env_file" ]; then - echo "Warning: service.env file not found in $1" + echo "Warning: service.env file not found at $1/service.env" return 1 fi