From df03f5ef9126c76361d0af054f2079d9e34835ae Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 11 May 2025 21:33:49 +1200 Subject: [PATCH] execute_ssh_command is not returning the correct return code. --- src/commands/nuke.cpp | 16 +++------------- src/commands/uninstall.cpp | 32 +++++++++++++++----------------- src/server_env_manager.cpp | 27 +++++++++++++++++++++++++++ src/server_env_manager.hpp | 2 ++ 4 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/commands/nuke.cpp b/src/commands/nuke.cpp index 1426e4f..23350c8 100644 --- a/src/commands/nuke.cpp +++ b/src/commands/nuke.cpp @@ -54,9 +54,9 @@ int nuke_handler(const CommandContext &ctx) service_info = get_service_info(server, service); if (!SIvalid(service_info)) std::cerr << "Warning: Invalid service: " << service << std::endl; - else - if (!uninstall_service(server, service, false)) - std::cerr << "Warning: Failed to uninstall service: " << service << std::endl; + + if (!uninstall_service(server, service, false)) + std::cerr << "Warning: Failed to uninstall service: " << service << std::endl; // run the nuke script on the remote server if it exists. if (gTemplateManager().template_command_exists(service_info.template_name, "nuke")) @@ -64,16 +64,6 @@ int nuke_handler(const CommandContext &ctx) if (!server_env.run_remote_template_command(service, "nuke", {}, false, {})) std::cerr << "Warning: Failed to run nuke script: " << service << std::endl; } - - // remove the remote service directory, running in a docker container as root. - std::string rm_cmd = "docker run --rm -v " + quote(remotepath::service(server, service)) + ":/service alpine rm -rf /service"; - if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand(rm_cmd), cMode::Silent)) - { - std::cerr << "Warning: Failed to remove remote service directory" << std::endl; - std::cerr << "You may need to log in and delete it manually." << std::endl; - std::cerr << " ssh " << server << std::endl; - std::cerr << " rm -rf " << remotepath::service(server, service) << std::endl; - } } else std::cerr << "Warning: Invalid server: " << server << std::endl; diff --git a/src/commands/uninstall.cpp b/src/commands/uninstall.cpp index 7f09731..a1f57f3 100644 --- a/src/commands/uninstall.cpp +++ b/src/commands/uninstall.cpp @@ -1,6 +1,9 @@ #include "command_registry.hpp" #include "directories.hpp" #include "shared_commands.hpp" +#include "templates.hpp" + +#include namespace dropshell { @@ -80,29 +83,24 @@ namespace dropshell // 3. Run uninstall script if it exists std::string uninstall_script = remotepath::service_template(server, service) + "/uninstall.sh"; - bool script_exists = server_env.check_remote_file_exists(uninstall_script); + if (gTemplateManager().template_command_exists(service, "uninstall")) + if (server_env.check_remote_file_exists(uninstall_script)) + if (!server_env.run_remote_template_command(service, "uninstall", {}, silent, {})) + if (!silent) + std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl; - if (script_exists) + // 4. Remove the service directory from the server, running in a docker container as root. + if (server_env.remove_remote_dir(remotepath::service(server, service), silent)) { - if (!server_env.run_remote_template_command(service, "uninstall", {}, silent, {})) - if (!silent) - std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl; - } - else + ASSERT(!server_env.check_remote_dir_exists(remotepath::service(server, service)), "Service directory still found on server after uninstall"); if (!silent) - std::cerr << "Warning: No uninstall script found, continuing with direcotry removal." << std::endl; - - // 4. Remove the service directory from the server - std::string rm_cmd = "rm -rf " + quote(remotepath::service(server, service)); - if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand(rm_cmd), cMode::Silent)) - { - if (!silent) - std::cerr << "Failed to remove service directory" << std::endl; - return false; + std::cout << "Removed remote service directory " << remotepath::service(server, service) << std::endl; } + else if (!silent) + std::cerr << "Warning: Failed to remove remote service directory" << std::endl; if (!silent) - std::cout << "Service " << service << " successfully uninstalled from " << server << std::endl; + std::cout << "Completed service " << service << " uninstall on " << server << std::endl; return true; } diff --git a/src/server_env_manager.cpp b/src/server_env_manager.cpp index 85a7a51..3c0ccca 100644 --- a/src/server_env_manager.cpp +++ b/src/server_env_manager.cpp @@ -161,6 +161,33 @@ bool server_env_manager::check_remote_items_exist(const std::vector return true; } +bool server_env_manager::remove_remote_dir(const std::string &dir_path, bool silent) const +{ + std::filesystem::path path(dir_path); + std::filesystem::path parent_path = path.parent_path(); + std::string target_dir = path.filename().string(); + + if (parent_path.empty()) + parent_path="/"; + + if (target_dir.empty()) + return false; + + if (!silent) + std::cout << "Removing remote directory " << target_dir << " in " << parent_path << " on " << mServerName << std::endl; + std::string remote_cmd = + "docker run --rm -v " + quote(parent_path.string()) + ":/parent " + + " alpine rm -rf \"/parent/" + target_dir + "\""; + + if (!silent) + std::cout << "Running command: " << remote_cmd << std::endl; + + sCommand scommand(remote_cmd); + cMode mode = (silent ? cMode::Silent : cMode::None); + + return execute_ssh_command(get_SSH_INFO(), scommand, mode); +} + bool server_env_manager::run_remote_template_command(const std::string &service_name, const std::string &command, std::vector args, bool silent, std::map extra_env_vars) const { sCommand scommand = construct_standard_template_run_cmd(service_name, command, args, silent); diff --git a/src/server_env_manager.hpp b/src/server_env_manager.hpp index 8964bbc..63f888a 100644 --- a/src/server_env_manager.hpp +++ b/src/server_env_manager.hpp @@ -53,6 +53,8 @@ class server_env_manager { bool check_remote_file_exists(const std::string& file_path) const; bool check_remote_items_exist(const std::vector& file_paths) const; + bool remove_remote_dir(const std::string &dir_path, bool silent) const; + bool run_remote_template_command(const std::string& service_name, const std::string& command, std::vector args, bool silent, std::map extra_env_vars) const; bool run_remote_template_command_and_capture_output(const std::string& service_name, const std::string& command,