From 30c4077109e80f36cf7bffb9265fa5e91d68c2a4 Mon Sep 17 00:00:00 2001 From: j Date: Tue, 30 Dec 2025 11:25:22 +1300 Subject: [PATCH] feat: Update 14 and remove 1 files --- source/agent-remote/ds_run.sh | 5 +- source/src/commands/backupdata.cpp | 3 +- source/src/commands/destroy.cpp | 4 +- source/src/commands/install.cpp | 7 +- source/src/commands/restoredata.cpp | 5 +- source/src/commands/shared_commands.cpp | 4 +- source/src/commands/ssh.cpp | 2 +- source/src/commands/start.cpp | 2 +- source/src/commands/stop.cpp | 2 +- source/src/commands/uninstall.cpp | 2 +- source/src/servers.cpp | 61 ++---------- source/src/servers.hpp | 10 +- source/src/service_runner.cpp | 117 ------------------------ source/src/utils/directories.cpp | 5 + source/src/utils/directories.hpp | 2 + 15 files changed, 40 insertions(+), 191 deletions(-) delete mode 100644 source/src/service_runner.cpp diff --git a/source/agent-remote/ds_run.sh b/source/agent-remote/ds_run.sh index acf16bf..367c986 100755 --- a/source/agent-remote/ds_run.sh +++ b/source/agent-remote/ds_run.sh @@ -78,7 +78,8 @@ source "${SERVICE_ENV}" set +a # -- Locate and validate command script -- -COMMAND_TO_RUN="${TEMPLATE_PATH}/${DSCOMMAND}" +COMMAND_PATH="${TEMPLATE_PATH}" +COMMAND_TO_RUN="${COMMAND_PATH}/${DSCOMMAND}" if [[ ! -f "${COMMAND_TO_RUN}" ]]; then COMMAND_TO_RUN="${COMMAND_TO_RUN}.sh" fi @@ -86,4 +87,4 @@ fi # -- Execute the command with any remaining arguments -- cd "${TEMPLATE_PATH}" -exec bash "${COMMAND_TO_RUN}" "$@" +exec bash cd "${COMMAND_PATH}" && "${COMMAND_TO_RUN}" "$@" diff --git a/source/src/commands/backupdata.cpp b/source/src/commands/backupdata.cpp index 67fbbf9..b1738d8 100644 --- a/source/src/commands/backupdata.cpp +++ b/source/src/commands/backupdata.cpp @@ -128,7 +128,8 @@ namespace dropshell { // Run backup script shared_commands::cRemoteTempFolder remote_temp_folder(server_env, user); - if (!server_env.run_remote_template_command(service, command, {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}})) + if (!server_env.run_remote_template_command( + service, command, {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}},NULL)) { error << "Backup script failed on remote server: " << remote_backup_file_path << std::endl; return false; diff --git a/source/src/commands/destroy.cpp b/source/src/commands/destroy.cpp index b17ba3f..62c72c7 100644 --- a/source/src/commands/destroy.cpp +++ b/source/src/commands/destroy.cpp @@ -91,14 +91,14 @@ namespace dropshell if (gTemplateManager().template_command_exists(service_info.template_name, "destroy")) { info << "Running destroy script for " << service << " on " << server << std::endl; - if (!server_env.run_remote_template_command(service, "destroy", {}, false, {})) + if (!server_env.run_remote_template_command(service, "destroy", {}, false, {},NULL)) warning << "Failed to run destroy script: " << service << std::endl; } else { info << "No destroy script found for " << service << " on " << server << std::endl; info << "Running uninstall script instead and will clean directories." << std::endl; - if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {})) + if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {},NULL)) warning << "Failed to uninstall service: " << service << std::endl; } } diff --git a/source/src/commands/install.cpp b/source/src/commands/install.cpp index e4aefe6..d5f7ce1 100644 --- a/source/src/commands/install.cpp +++ b/source/src/commands/install.cpp @@ -191,7 +191,7 @@ namespace dropshell } shared_commands::cRemoteTempFolder remote_temp_folder(server_env, user); - if (!server_env.run_remote_template_command(service, "install", {}, false, {{"TEMP_DIR", remote_temp_folder.path()}})) + if (!server_env.run_remote_template_command(service, "install", {}, false, {{"TEMP_DIR", remote_temp_folder.path()}},NULL)) { error << "Failed to run install script for service '" << service << "' on server '" << server << "'" << std::endl; error << "Template: " << service_info.template_name << std::endl; @@ -531,12 +531,11 @@ complete -F _dropshell_completions ds // create server_info.env info << "Creating server_info.env on remote server... " < args, bool silent, - std::map extra_env_vars) const + std::map extra_env_vars, + std::string * output + ) const { std::string user = get_user_for_service(service_name); - auto scommand = construct_standard_template_run_cmd(service_name, command, args, silent); + auto scommand = construct_standard_template_run_cmd(service_name, command, args); if (!scommand.has_value()) return false; @@ -317,28 +319,7 @@ namespace dropshell if (scommand->get_command_to_run().empty()) return false; cMode mode = (command == "ssh") ? (cMode::Interactive) : (silent ? cMode::Silent : cMode::Defaults); - return execute_ssh_command(get_SSH_INFO(user), scommand.value(), mode); - } - - bool ServerConfig::run_remote_template_command_and_capture_output( - const std::string &service_name, - const std::string &command, - std::vector args, - std::string &output, - bool silent, - std::map extra_env_vars) const - { - std::string user = get_user_for_service(service_name); - auto scommand = construct_standard_template_run_cmd(service_name, command, args, false); - if (!scommand.has_value()) - return false; - - // add the extra env vars to the command - for (const auto &[key, value] : extra_env_vars) - scommand->add_env_var(key, value); - - // Use Silent mode when capturing output to avoid printing to console - return execute_ssh_command(get_SSH_INFO(user), scommand.value(), cMode::Silent, &output); + return execute_ssh_command(get_SSH_INFO(user), scommand.value(), mode, output); } std::string ServerConfig::get_variable(const std::string &name) const @@ -351,41 +332,19 @@ namespace dropshell return it->second; } - std::optional ServerConfig::construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector args, const bool silent) const + std::optional ServerConfig::construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector args) const { if (command.empty()) return std::nullopt; std::string user = get_user_for_service(service_name); - std::string remote_service_template_path = remotepath(mServerName, user).service_template(service_name); - std::string script_path = remote_service_template_path + "/" + command + ".sh"; - - ordered_env_vars env_vars; - if (!get_all_service_env_vars(mServerName, service_name, env_vars)) - { - error << "Failed to get all service env vars for " << service_name << std::endl; - return std::nullopt; - } - - set_var(env_vars, "HOST_NAME", get_SSH_HOST()); - std::string argstr = ""; for (const auto &arg : args) - { - argstr += " " + quote(dequote(trim(arg))); - } - - sCommand sc( - remote_service_template_path, - quote(script_path) + argstr + (silent ? " > /dev/null 2>&1" : ""), - env_vars); - - if (sc.empty()) - { - error << "Failed to construct command for " << service_name << " " << command << std::endl; - return std::nullopt; - } + argstr += " " + requote(arg); + + std::string dsrun = remotefile(mServerName, user).ds_run(); + sCommand sc("", dsrun + " " + requote(service_name) + " " + requote(command) + argstr, {}); return sc; } diff --git a/source/src/servers.hpp b/source/src/servers.hpp index 8ea7e4e..b51fb8d 100644 --- a/source/src/servers.hpp +++ b/source/src/servers.hpp @@ -74,13 +74,11 @@ namespace dropshell bool remove_remote_dir(const std::string &dir_path, bool silent, std::string user) 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, - std::vector args, std::string &output, bool silent, std::map extra_env_vars) const; - + std::vector args, bool silent, std::map extra_env_vars, + std::string * output) const; + private: - std::optional construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector args, const bool silent) const; + std::optional construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector args) const; private: std::string mServerName; diff --git a/source/src/service_runner.cpp b/source/src/service_runner.cpp deleted file mode 100644 index 393e433..0000000 --- a/source/src/service_runner.cpp +++ /dev/null @@ -1,117 +0,0 @@ - -// #include -// #include -// #include -// #include -// #include -// #include -// #include -// #include -// #include - -// #include "config.hpp" -// #include "servers.hpp" -// #include "templates.hpp" -// #include "services.hpp" -// #include "utils/directories.hpp" -// #include "utils/utils.hpp" -// #include "command_registry.hpp" -// #include "shared_commands.hpp" - - -// namespace fs = std::filesystem; - -// namespace dropshell { - - -// service_runner::service_runner(const std::string& server_name, const std::string& service_name) : -// mServerEnv(server_name), mServer(server_name), mService(service_name), mValid(false) -// { -// if (server_name.empty() || service_name.empty()) -// return; - -// // Initialize server environment -// if (!mServerEnv.is_valid()) -// return; - -// mServiceInfo = get_service_info(server_name, service_name); -// if (mServiceInfo.service_name.empty()) -// return; - -// mService = mServiceInfo.service_name; - -// mValid = !mServiceInfo.local_template_path.empty(); -// } - - - - -// // ------------------------------------------------------------------------------------------------ -// // Run a command on the service. -// // ------------------------------------------------------------------------------------------------ -// bool service_runner::run_command(const std::string& command, std::vector additional_args, std::map env_vars) { -// if (!mServerEnv.is_valid()) { -// std::cerr << "Error: Server service not initialized" << std::endl; -// return false; -// } -// template_info tinfo = gTemplateManager().get_template_info(mServiceInfo.template_name); -// if (!tinfo.is_set()) { -// std::cerr << "Error: Template '" << mServiceInfo.template_name << "' not found" << std::endl; -// return false; -// } - -// if (!gTemplateManager().template_command_exists(mServiceInfo.template_name, command)) { -// std::cout << "No command script for " << mServiceInfo.template_name << " : " << command << std::endl; -// return true; // nothing to run. -// } - -// // install doesn't require anything on the server yet. -// // if (command == "install") -// // return install_service(mServer, mService, false); - -// std::string script_path = remotepath::service_template(mServer, mService) + "/" + command + ".sh"; - -// // Check if service directory exists -// if (!mServerEnv.check_remote_dir_exists(remotepath::service(mServer, mService))) { -// std::cerr << "Error: Service is not installed: " << mService << std::endl; -// return false; -// } - -// // Check if command script exists -// if (!mServerEnv.check_remote_file_exists(script_path)) { -// std::cerr << "Error: Remote command script not found: " << script_path << std::endl; -// return false; -// } - -// // Check if env file exists -// if (!mServerEnv.check_remote_file_exists(remotefile::service_env(mServer, mService))) { -// std::cerr << "Error: Service config file not found: " << remotefile::service_env(mServer, mService) << std::endl; -// return false; -// } - -// // if (command == "uninstall") -// // return uninstall(); - -// if (command == "ssh") { -// interactive_ssh_service(); -// return true; -// } -// if (command == "restore") { -// if (additional_args.size() < 1) { -// std::cerr << "Error: restore requires a backup file:" << std::endl; -// std::cerr << "dropshell restore " << std::endl; -// return false; -// } -// return restore(additional_args[0], false); -// } -// if (command == "backup") { -// return backup(false); -// } - -// // Run the generic command -// std::vector args; // not passed through yet. -// return mServerEnv.run_remote_template_command(mService, command, args, false, env_vars); -// } - - -// } // namespace dropshell \ No newline at end of file diff --git a/source/src/utils/directories.cpp b/source/src/utils/directories.cpp index c162048..cbb6ec0 100644 --- a/source/src/utils/directories.cpp +++ b/source/src/utils/directories.cpp @@ -175,6 +175,11 @@ namespace dropshell return remotepath(mServer_name, mUser).DROPSHELL_DIR() + "/" + filenames::server_info_env; } + std::string remotefile::ds_run() + { + return remotepath(mServer_name,mUser).agent() + "/" + filenames::ds_run; + } + remotepath::remotepath(const std::string &server_name, const std::string &user) : mServer_name(server_name), mUser(user) {} std::string remotepath::DROPSHELL_DIR() const diff --git a/source/src/utils/directories.hpp b/source/src/utils/directories.hpp index e42fc73..fd42e66 100644 --- a/source/src/utils/directories.hpp +++ b/source/src/utils/directories.hpp @@ -48,6 +48,7 @@ namespace dropshell { static const std::string readme = "README.txt"; static const std::string server_json = "server.json"; static const std::string dropshell_json = "dropshell.json"; + static const std::string ds_run = "ds_run.sh"; } // namespace filenames. namespace localfile { @@ -105,6 +106,7 @@ namespace dropshell { std::string service_env(const std::string &service_name) const; std::string server_json(); std::string server_info_env(); + std::string ds_run(); private: std::string mServer_name; std::string mUser;