From 22e37b212a4a02be2be24f0ad3547df036b0ff17 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 14 May 2025 23:12:18 +1200 Subject: [PATCH] Installs bb64 correctly. --- src/commands/install.cpp | 55 +++++++++++++++++++++----------------- src/server_env_manager.cpp | 2 +- src/service_runner.cpp | 4 +-- src/utils/execute.hpp | 8 +++--- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/commands/install.cpp b/src/commands/install.cpp index 2b3cfcf..d042f4f 100644 --- a/src/commands/install.cpp +++ b/src/commands/install.cpp @@ -18,7 +18,7 @@ namespace dropshell int install_handler(const CommandContext &ctx); - static std::vector install_name_list = {"install","reinstall","update"}; + static std::vector install_name_list = {"install", "reinstall", "update"}; // Static registration struct InstallCommandRegister @@ -65,7 +65,7 @@ namespace dropshell 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)); + return execute_local_command(rsync_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults)); } // ------------------------------------------------------------------------------------------------ @@ -145,27 +145,29 @@ namespace dropshell { // determine the architecture of the system std::string arch; - #ifdef __aarch64__ +#ifdef __aarch64__ arch = "arm64"; - #elif __x86_64__ +#elif __x86_64__ arch = "amd64"; - #endif +#endif return arch; } // ------------------------------------------------------------------------------------------------ - // update_dropshell + // update_dropshell // ------------------------------------------------------------------------------------------------ - - std::string _exec(const char* cmd) { + std::string _exec(const char *cmd) + { char buffer[128]; std::string result = ""; - FILE* pipe = popen(cmd, "r"); - if (!pipe) { + FILE *pipe = popen(cmd, "r"); + if (!pipe) + { throw std::runtime_error("popen() failed!"); } - while (!feof(pipe)) { + while (!feof(pipe)) + { if (fgets(buffer, 128, pipe) != nullptr) result += buffer; } @@ -225,9 +227,8 @@ namespace dropshell return 0; - std::string bash_script_2 = "docker run --rm -v " + parent_path.string() + ":/target gitea.jde.nz/public/debian-curl:latest " + - "sh -c \"mv /target/dropshell_temp /target/dropshell\""; + "sh -c \"mv /target/dropshell_temp /target/dropshell\""; rval = system(bash_script_2.c_str()); if (rval != 0) { @@ -249,8 +250,7 @@ namespace dropshell gConfig().get_local_template_cache_path(), gConfig().get_local_backup_path(), gConfig().get_local_tempfiles_path(), - localpath::agent() - }; + localpath::agent()}; for (auto &p : gConfig().get_local_server_definition_paths()) paths.push_back(p); @@ -262,7 +262,7 @@ namespace dropshell } // download bb64. - if (!std::filesystem::exists(localpath::agent()+"bb64")) + if (!std::filesystem::exists(localpath::agent() + "bb64")) { std::string cmd = "cd " + localpath::agent() + " && curl -fsSL -o bb64 https://gitea.jde.nz/public/bb64/releases/download/latest/bb64.amd64 && chmod a+x bb64"; int ret = system(cmd.c_str()); @@ -270,9 +270,11 @@ namespace dropshell std::cout << "Downloaded bb64 to " << localpath::agent() << std::endl; else std::cerr << "Failed to download bb64 to " << localpath::agent() << std::endl; - } else { + } + else + { std::cout << "Updating bb64..." << std::endl; - system((localpath::agent()+"bb64 -u").c_str()); // update. + system((localpath::agent() + "bb64 -u").c_str()); // update. } return 0; } @@ -294,7 +296,6 @@ namespace dropshell return 0; } - int install_server(const std::string &server) { // install the dropshell agent on the given server. @@ -308,23 +309,27 @@ namespace dropshell } server_env_manager server_env(server); - if (!server_env.is_valid()) { + if (!server_env.is_valid()) + { std::cerr << "Invalid server environment for " << server << std::endl; return 1; } // first install bb64. + std::cout << "Installing bb64 on " << server << std::endl + << std::flush; std::string remote_cmd = "ssh -p " + server_env.get_SSH_INFO().port + " " + server_env.get_SSH_INFO().user + "@" + server_env.get_SSH_INFO().host + - " 'mkdir -p " + quote(agent_path) + " && curl -fsSL \"https://gitea.jde.nz/public/bb64/releases/download/latest/install.sh\" | bash -s -- " + - quote(agent_path) + " " + quote("$(id -u "+server_env.get_SSH_USER()+"):$(id -g "+server_env.get_SSH_USER()+")")+"'"; + " 'mkdir -p " + quote(agent_path) + " && curl -fsSL \"https://gitea.jde.nz/public/bb64/releases/download/latest/install.sh\" | bash -s -- " + + quote(agent_path) + " " + quote("$(id -u " + server_env.get_SSH_USER() + "):$(id -g " + server_env.get_SSH_USER() + ")") + "'"; std::cout << "Executing: " << remote_cmd << std::endl; - if (!execute_local_command(remote_cmd, nullptr, cMode::Silent)) { + if (!execute_local_command(remote_cmd, nullptr, cMode::Silent)) std::cerr << "Failed to download bb64 to " << agent_path << " on remote server." << std::endl; - } else { + else std::cout << "Downloaded bb64 to " << agent_path << " on remote server." << std::endl; - } + + return 0; // NOTIMPL } diff --git a/src/server_env_manager.cpp b/src/server_env_manager.cpp index bf066ed..7cbd650 100644 --- a/src/server_env_manager.cpp +++ b/src/server_env_manager.cpp @@ -187,7 +187,7 @@ bool server_env_manager::remove_remote_dir(const std::string &dir_path, bool sil std::cout << "Running command: " << remote_cmd << std::endl; sCommand scommand("", remote_cmd,{}); - cMode mode = (silent ? cMode::Silent : cMode::None); + cMode mode = (silent ? cMode::Silent : cMode::Defaults); return execute_ssh_command(get_SSH_INFO(), scommand, mode); } diff --git a/src/service_runner.cpp b/src/service_runner.cpp index 973b183..dfe1cc2 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -262,13 +262,13 @@ bool service_runner::interactive_ssh_service() bool service_runner::scp_file_to_remote(const std::string &local_path, const std::string &remote_path, bool silent) { std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + quote(local_path) + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + (silent ? " > /dev/null 2>&1" : ""); - return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::None)); + return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults)); } bool service_runner::scp_file_from_remote(const std::string &remote_path, const std::string &local_path, bool silent) { std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + " " + quote(local_path) + (silent ? " > /dev/null 2>&1" : ""); - return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::None)); + return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults)); } bool service_runner::restore(std::string backup_file, bool silent) diff --git a/src/utils/execute.hpp b/src/utils/execute.hpp index 59fc934..9785054 100644 --- a/src/utils/execute.hpp +++ b/src/utils/execute.hpp @@ -10,7 +10,7 @@ class sCommand; // mode bitset enum class cMode { - None = 0, + Defaults = 0, Interactive = 1, Silent = 2, CaptureOutput = 4 @@ -31,9 +31,9 @@ typedef struct sSSHInfo { std::string server_ID; // dropshell name for server. } sSSHInfo; -bool execute_local_command(std::string command, std::string * output = nullptr, cMode mode = cMode::None); -bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map & env_vars, std::string * output = nullptr, cMode mode = cMode::None); -bool execute_ssh_command(const sSSHInfo & ssh_info, const sCommand & remote_command, cMode mode = cMode::None, std::string * output = nullptr); +bool execute_local_command(std::string command, std::string * output = nullptr, cMode mode = cMode::Defaults); +bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map & env_vars, std::string * output = nullptr, cMode mode = cMode::Defaults); +bool execute_ssh_command(const sSSHInfo & ssh_info, const sCommand & remote_command, cMode mode = cMode::Defaults, std::string * output = nullptr); // ------------------------------------------------------------------------------------------------