Backups working again

This commit is contained in:
Your Name 2025-04-25 20:46:10 +12:00
parent d0b7c27af3
commit 84c252c965
4 changed files with 23 additions and 14 deletions

View File

@ -63,6 +63,14 @@ std::string service_runner::construct_ssh_cmd() const
return construct_ssh_cmd(*m_server_env); 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 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) + "'"; std::string check_dir_cmd = construct_ssh_cmd() + "'test -d " + quote(dir_path) + "'";
@ -176,8 +184,7 @@ bool service_runner::install() {
// Run install script // Run install script
{ {
std::string install_cmd = "'cd " + quote(mRemote_service_template_path) + std::string install_cmd = construct_standard_command_run_cmd("install");
" && /bin/bash install.sh " + quote(mRemote_service_config_path) + "'";
bool ok= execute_ssh_command(install_cmd, "Failed to run install script"); bool ok= execute_ssh_command(install_cmd, "Failed to run install script");
if (!ok) if (!ok)
return false; return false;
@ -204,8 +211,7 @@ bool service_runner::uninstall() {
bool script_exists = check_remote_file_exists(uninstall_script); bool script_exists = check_remote_file_exists(uninstall_script);
if (script_exists) { if (script_exists) {
std::string uninstall_cmd = "'cd " + quote(mRemote_service_template_path) + std::string uninstall_cmd = construct_standard_command_run_cmd("uninstall");
" && /bin/bash "+quote(uninstall_script)+" "+quote(mRemote_service_config_path)+"'";
if (!execute_ssh_command(uninstall_cmd, "Failed to run uninstall script")) { if (!execute_ssh_command(uninstall_cmd, "Failed to run uninstall script")) {
std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl; 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(); return backup();
// Run the generic command // Run the generic command
std::string run_cmd = "'cd " + quote(mRemote_service_template_path) + std::string run_cmd = construct_standard_command_run_cmd(command);
" && /bin/bash "+quote(script_path)+" "+quote(mRemote_service_config_path)+"'";
return execute_ssh_command(run_cmd, "Command returned error code: " + script_path); 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. 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"; std::string script_path = mRemote_service_template_path + "/" + command + ".sh";
if (!template_command_exists(m_service_info.template_name, command)) { if (!template_command_exists(m_service_info.template_name, command)) {
@ -322,8 +327,11 @@ bool service_runner::backup() {
// Run backup script // Run backup script
std::string backup_cmd = "'cd " + quote(mRemote_service_template_path) + 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")) { if (!execute_ssh_command(backup_cmd, "Backup script failed")) {
std::cerr << "Backup script failed: " << backup_cmd << std::endl;
return false; return false;
} }
@ -357,15 +365,13 @@ std::map<std::string, ServiceStatus> 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_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 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); server_env env(server_name);
if (!env.is_valid()) { if (!env.is_valid()) {
std::cerr << "Error: Invalid server environment" << std::endl; std::cerr << "Error: Invalid server environment" << std::endl;
return status; 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) + std::string ssh_cmd = construct_ssh_cmd(env) + "'cd " + quote(remote_service_template_path) +
" && /bin/bash "+quote(script_path)+" "+quote(remote_service_config_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. // 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) if (!ok)
return HealthStatus::UNHEALTHY; return HealthStatus::UNHEALTHY;

View File

@ -96,6 +96,8 @@ class service_runner {
static std::string construct_ssh_cmd(const server_env &env); static std::string construct_ssh_cmd(const server_env &env);
std::string construct_ssh_cmd() const; 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_dir_exists(const std::string& dir_path) const;
bool check_remote_file_exists(const std::string& file_path) const; bool check_remote_file_exists(const std::string& file_path) const;
bool check_remote_items_exist(const std::vector<std::string>& file_paths) const; bool check_remote_items_exist(const std::vector<std::string>& file_paths) const;

View File

@ -40,7 +40,7 @@ load_env() {
env_file="$1/service.env" env_file="$1/service.env"
if [ ! -f "$env_file" ]; then 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 return 1
fi fi

View File

@ -40,7 +40,7 @@ load_env() {
env_file="$1/service.env" env_file="$1/service.env"
if [ ! -f "$env_file" ]; then 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 return 1
fi fi