From 64639adcf0c44b292a4b9e6a10908f20320ddbd5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 11 May 2025 13:47:51 +1200 Subject: [PATCH] ... --- src/{ => commands}/command_registry.cpp | 0 src/{ => commands}/command_registry.hpp | 0 src/commands/install.cpp | 55 +++++++++++++++++++------ src/commands/shared_commands.hpp | 19 +++++++++ 4 files changed, 62 insertions(+), 12 deletions(-) rename src/{ => commands}/command_registry.cpp (100%) rename src/{ => commands}/command_registry.hpp (100%) create mode 100644 src/commands/shared_commands.hpp diff --git a/src/command_registry.cpp b/src/commands/command_registry.cpp similarity index 100% rename from src/command_registry.cpp rename to src/commands/command_registry.cpp diff --git a/src/command_registry.hpp b/src/commands/command_registry.hpp similarity index 100% rename from src/command_registry.hpp rename to src/commands/command_registry.hpp diff --git a/src/commands/install.cpp b/src/commands/install.cpp index 0fb1684..b318db0 100644 --- a/src/commands/install.cpp +++ b/src/commands/install.cpp @@ -5,6 +5,8 @@ #include "utils/directories.hpp" #include "standard_autocomplete.hpp" #include "templates.hpp" +#include "shared_commands.hpp" + #include #include #include @@ -46,22 +48,49 @@ struct InstallCommandRegister { // ------------------------------------------------------------------------------------------------ -// install service over ssh +// rsync_tree_to_remote : SHARED COMMAND +// ------------------------------------------------------------------------------------------------ +bool rsync_tree_to_remote( + const std::string &local_path, + const std::string &remote_path, + server_env_manager &server_env, + bool silent) +{ + ASSERT(!local_path.empty() && !remote_path.empty()); + + std::string rsync_cmd = "rsync --delete --mkpath -zrpc -e 'ssh -p " + server_env.get_SSH_PORT() + "' " + + quote(local_path + "/") + " "+ + quote(server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" + + remote_path + "/"); + return execute_local_command(rsync_cmd, nullptr, (silent ? cMode::Silent : cMode::None) + cMode::RawCommand); +} + +// ------------------------------------------------------------------------------------------------ +// install service over ssh : SHARED COMMAND // ------------------------------------------------------------------------------------------------ bool install_service(const std::string& server, const std::string& service, bool silent) { - maketitle("Installing " + mService + " (" + mServiceInfo.template_name + ") on " + mServer); + LocalServiceInfo service_info = get_service_info(server, service); + if (!SIvalid(service_info)) + return false; - if (!mServerEnv.is_valid()) return false; // should never hit this. + server_env_manager server_env(server); + if (!server_env.is_valid()) + return false; + + + maketitle("Installing " + service + " (" + service_info.template_name + ") on " + server); + + if (!server_env.is_valid()) return false; // should never hit this. // Check if template exists - template_info tinfo = gTemplateManager().get_template_info(mServiceInfo.template_name); + template_info tinfo = gTemplateManager().get_template_info(service_info.template_name); if (!tinfo.is_set()) return false; // Create service directory - std::string remote_service_path = remotepath::service(mServer, mService); + std::string remote_service_path = remotepath::service(server, service); std::string mkdir_cmd = "mkdir -p " + quote(remote_service_path); - if (!execute_ssh_command(mServerEnv.get_SSH_INFO(), sCommand(mkdir_cmd), cMode::Silent)) + if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand(mkdir_cmd), cMode::Silent)) { std::cerr << "Failed to create service directory " << remote_service_path << std::endl; return false; @@ -69,23 +98,25 @@ bool install_service(const std::string& server, const std::string& service, bool // Check if rsync is installed on remote host std::string check_rsync_cmd = "which rsync"; - if (!execute_ssh_command(mServerEnv.get_SSH_INFO(), sCommand(check_rsync_cmd), cMode::Silent)) + if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand(check_rsync_cmd), cMode::Silent)) { std::cerr << "rsync is not installed on the remote host" << std::endl; return false; } // Copy template files - std::cout << "Copying: [LOCAL] " << tinfo.local_template_path() << std::endl << std::string(8,' ')<<"[REMOTE] " << remotepath::service_template(mServer, mService) << "/" << std::endl; - if (!rsync_tree_to_remote(tinfo.local_template_path().string(), remotepath::service_template(mServer, mService), silent)) + std::cout << "Copying: [LOCAL] " << tinfo.local_template_path() << std::endl << std::string(8,' ')<<"[REMOTE] " << remotepath::service_template(server, service) << "/" << std::endl; + if (!rsync_tree_to_remote(tinfo.local_template_path().string(), remotepath::service_template(server, service), + server_env, silent)) { std::cerr << "Failed to copy template files using rsync" << std::endl; return false; } // Copy service files - std::cout << "Copying: [LOCAL] " << localpath::service(mServer,mService) << std::endl << std::string(8,' ')<<"[REMOTE] " << remotepath::service_config(mServer,mService) << std::endl; - if (!rsync_tree_to_remote(localpath::service(mServer,mService), remotepath::service_config(mServer,mService), silent)) + std::cout << "Copying: [LOCAL] " << localpath::service(server,service) << std::endl << std::string(8,' ')<<"[REMOTE] " << remotepath::service_config(server,service) << std::endl; + if (!rsync_tree_to_remote(localpath::service(server,service), remotepath::service_config(server,service), + server_env, silent)) { std::cerr << "Failed to copy service files using rsync" << std::endl; return false; @@ -93,7 +124,7 @@ bool install_service(const std::string& server, const std::string& service, bool // Run install script { - mServerEnv.run_remote_template_command(mService, "install", {}, silent, {}); + server_env.run_remote_template_command(service, "install", {}, silent, {}); } // print health tick diff --git a/src/commands/shared_commands.hpp b/src/commands/shared_commands.hpp new file mode 100644 index 0000000..e5fee20 --- /dev/null +++ b/src/commands/shared_commands.hpp @@ -0,0 +1,19 @@ +#ifndef SHARED_COMMANDS_HPP +#define SHARED_COMMANDS_HPP + +#include "servers.hpp" + +namespace dropshell { + + // defined in install.cpp + bool rsync_tree_to_remote( + const std::string &local_path, + const std::string &remote_path, + server_env_manager &server_env, + bool silent); + + // defined in install.cpp + bool install_service(const std::string& server, const std::string& service, bool silent); + +} // namespace dropshell +#endif