From c6665f304488bc519990cc9c3d4ced70744e77fe Mon Sep 17 00:00:00 2001 From: John Date: Sun, 27 Apr 2025 09:34:04 +1200 Subject: [PATCH] tidying --- CMakeLists.txt | 1 - install_prerequisites.sh | 14 ++++++++++---- src/config.cpp | 7 +++++-- src/main.cpp | 23 ++++++++++++++++++----- src/main_commands.cpp | 2 +- src/server_env.cpp | 8 +++++++- src/server_env.hpp | 15 ++++++++++----- src/service_runner.cpp | 25 +++++++++++-------------- src/utils/directories.cpp | 12 ++++++++++-- src/utils/directories.hpp | 3 ++- templates/watchtower/_common.sh | 2 +- 11 files changed, 75 insertions(+), 37 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index babffea..40bd14f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,6 @@ configure_file( ) # Find required packages -find_package(Boost REQUIRED COMPONENTS program_options filesystem system) find_package(Curses REQUIRED) find_package(TBB REQUIRED) diff --git a/install_prerequisites.sh b/install_prerequisites.sh index 85f7097..9ca91c5 100755 --- a/install_prerequisites.sh +++ b/install_prerequisites.sh @@ -41,7 +41,7 @@ print_status "Detected OS: $OS $VER" case $OS in "Ubuntu"|"Debian GNU/Linux") # Common packages for both Ubuntu and Debian - PACKAGES="cmake make g++ libboost-all-dev devscripts debhelper" + PACKAGES="cmake make g++ devscripts debhelper libtbb-dev" ;; *) print_error "Unsupported distribution: $OS" @@ -82,11 +82,17 @@ for tool in cmake make g++; do fi done -# Check Boost installation -if [ ! -d "/usr/include/boost" ]; then - print_error "Boost headers not found" +# Check TBB installation +if [ ! -d "/usr/include/tbb" ]; then + print_error "TBB headers not found" exit 1 fi +# # Check Boost installation +# if [ ! -d "/usr/include/boost" ]; then +# print_error "Boost headers not found" +# exit 1 +# fi + print_status "All dependencies installed successfully!" print_status "You can now run ./build.sh to build the project" \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp index 396e6b1..81a113f 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -22,7 +22,7 @@ config::~config() { } bool config::load_config() { - std::string config_path = get_local_dropshell_config_path(); + std::string config_path = get_local_dropshell_config_file(); if (config_path.empty() || !std::filesystem::exists(config_path)) return false; @@ -56,7 +56,10 @@ void config::save_config() return; } - std::string config_path = get_local_dropshell_config_path(); + std::string parent_path = get_local_dropshell_config_parent_path(); + std::filesystem::create_directories(parent_path); + + std::string config_path = get_local_dropshell_config_file(); envmanager config_env(config_path); config_env.set_variable("local.config.directories", multi2string(mLocalConfigPaths)); diff --git a/src/main.cpp b/src/main.cpp index 3f31beb..f1c2935 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "autocomplete.hpp" #include "main_commands.hpp" +#include #include #include #include @@ -25,7 +26,9 @@ void print_help() { std::cout << std::endl; std::cout << "dropshell ..." << std::endl; std::cout << " help Show this help message" << std::endl; - std::cout << " init DIR Add DIR as a local configuration directory for dropshell (can add several)" << std::endl; + std::cout << " init DIR Add DIR as a local server config directory (can add several)" << std::endl; + + if (get_global_config()->is_config_set()) { std::cout << " server NAME Show details for specific server" << std::endl; std::cout << " templates List all available templates" << std::endl; std::cout << std::endl; @@ -44,6 +47,12 @@ void print_help() { std::cout << " create-template TEMPLATE" << std::endl; std::cout << " create-server SERVER" << std::endl; std::cout << " create-service SERVER TEMPLATE SERVICE" << std::endl; + } + else { + std::cout << " edit Edit the configuration of dropshell" << std::endl; + std::cout << std::endl; + std::cout << "Other commands available once initialised." << std::endl; + } } } // namespace dropshell @@ -99,6 +108,12 @@ int main(int argc, char* argv[]) { return 0; } + if (cmd == "edit" && argc < 3) { + std::filesystem::create_directories(dropshell::get_local_dropshell_config_parent_path()); + dropshell::edit_file(dropshell::get_local_dropshell_config_file(), "Please ensure any directories you have introduced in the config file exist."); + return 0; + } + // ------------------------------------------------------------ // from here we require the config file to be loaded. if (!cfg->is_config_set()) { @@ -173,10 +188,8 @@ int main(int argc, char* argv[]) { } if (cmd == "edit" && argc < 4) { - if (argc < 3) - dropshell::edit_file(dropshell::get_local_dropshell_config_path(), "Please ensure any directories you have introduced in the config file exist."); - else - dropshell::edit_server(argv[2]); + ASSERT_ALWAYS(argc>=3); + dropshell::edit_server(argv[2]); return 0; } diff --git a/src/main_commands.cpp b/src/main_commands.cpp index db651fc..a3afe8b 100644 --- a/src/main_commands.cpp +++ b/src/main_commands.cpp @@ -31,7 +31,7 @@ int init(const std::vector &args) } try { if (!cfg->add_local_config_directory(args[2])) - return 1; // error already reported + return 1; // error already reported cfg->save_config(); std::cout << "Config directory added: " << cfg->get_local_config_directories().back() << std::endl; diff --git a/src/server_env.cpp b/src/server_env.cpp index af59c6e..76f3e19 100644 --- a/src/server_env.cpp +++ b/src/server_env.cpp @@ -91,7 +91,7 @@ std::string server_env::construct_ssh_cmd() const { return ssh_cmd.str(); } -std::string server_env::construct_standard_command_run_cmd(const std::string &service_name, const std::string &command) const +std::string server_env::construct_standard_command_run_cmd(const std::string &service_name, const std::string &command, std::vector args) const { std::string remote_service_template_path = get_remote_service_template_path(mServer_name,service_name); std::string remote_service_config_path = get_remote_service_config_path(mServer_name,service_name); @@ -144,6 +144,12 @@ bool server_env::execute_ssh_command(const std::string& command, const std::stri return execute_local_command(full_cmd, error_msg); } +bool server_env::run_remote_template_command(const std::string &service_name, const std::string &command, std::vector args, const std::string &error_msg) const +{ + std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args); + return execute_ssh_command(full_cmd, error_msg); +} + bool server_env::execute_local_command(const std::string& command, const std::string& error_msg) { bool okay = (system(command.c_str()) == 0); diff --git a/src/server_env.hpp b/src/server_env.hpp index 93ba3ff..8e23b1e 100644 --- a/src/server_env.hpp +++ b/src/server_env.hpp @@ -34,17 +34,22 @@ class server_env { // helper functions public: - std::string construct_ssh_cmd() const; - std::string construct_standard_command_run_cmd(const std::string& service_name, const std::string& command) const; bool check_remote_items_exist(const std::vector& file_paths) const; - bool execute_ssh_command(const std::string& command, const std::string& error_msg) const; - - static bool execute_local_command(const std::string& command, const std::string& error_msg); + bool execute_ssh_command(const std::string& command, const std::string& error_msg="") const; + bool run_remote_template_command(const std::string& service_name, const std::string& command, std::vector args, const std::string& error_msg="") const; + + static bool execute_local_command(const std::string& command, const std::string& error_msg=""); static bool execute_local_command_and_capture_output(const std::string& command, std::string & output); bool check_remote_dir_exists(const std::string &dir_path) const; bool check_remote_file_exists(const std::string& file_path) const; + std::string construct_ssh_cmd() const; + + private: + std::string construct_standard_command_run_cmd(const std::string& service_name, const std::string& command, std::vector args) const; + + private: std::string mServer_name; std::map variables; diff --git a/src/service_runner.cpp b/src/service_runner.cpp index 71071c3..de9ee21 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -87,10 +87,8 @@ bool service_runner::install() { // Run install script { - std::string install_cmd = m_server_env.construct_standard_command_run_cmd(m_service_info.service_name, "install"); - bool ok= m_server_env.execute_ssh_command(install_cmd, "Failed to run install script"); - if (!ok) - return false; + std::vector args; // not passed through yet. + m_server_env.run_remote_template_command(m_service_info.service_name, "install", args); } // print health tick @@ -114,10 +112,11 @@ bool service_runner::uninstall() { bool script_exists = m_server_env.check_remote_file_exists(uninstall_script); if (script_exists) { - std::string uninstall_cmd = m_server_env.construct_standard_command_run_cmd(m_service_info.service_name, "uninstall"); - if (!m_server_env.execute_ssh_command(uninstall_cmd, "Failed to run uninstall script")) { + std::vector args; // not passed through yet. + if (!m_server_env.run_remote_template_command(m_service_info.service_name, "uninstall", args)) { std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl; } + } else { std::cerr << "Warning: No uninstall script found. Unable to uninstall service." << std::endl; return false; @@ -188,8 +187,8 @@ bool service_runner::run_command(const std::string& command) { } // Run the generic command - std::string run_cmd = m_server_env.construct_standard_command_run_cmd(m_service_info.service_name, command); - return m_server_env.execute_ssh_command(run_cmd, "Command returned error code: " + script_path); + std::vector args; // not passed through yet. + return m_server_env.run_remote_template_command(m_service_info.service_name, command, args); } @@ -280,11 +279,9 @@ HealthStatus service_runner::is_healthy() } // Run status script, does not display output. - std::string run_cmd = m_server_env.construct_standard_command_run_cmd(m_service_info.service_name, "status"); - bool ok = m_server_env.execute_ssh_command(run_cmd, ""); - if (!ok) + std::vector args; // not passed through yet. + if (!m_server_env.run_remote_template_command(m_service_info.service_name, "status", args)) return HealthStatus::UNHEALTHY; - return HealthStatus::HEALTHY; } @@ -408,8 +405,8 @@ void service_runner::interactive_ssh_service() return; } - std::string command = m_server_env.construct_standard_command_run_cmd(m_service_info.service_name, "ssh"); - interactive_ssh(m_server_name, "/bin/bash -c '"+command+"'"); + std::vector args; // not passed through yet. + m_server_env.run_remote_template_command(m_service_info.service_name, "ssh", args); } void service_runner::edit_service_config() diff --git a/src/utils/directories.cpp b/src/utils/directories.cpp index 6639d8d..05ed728 100644 --- a/src/utils/directories.cpp +++ b/src/utils/directories.cpp @@ -11,18 +11,26 @@ namespace dropshell { -std::string get_local_dropshell_config_path() +std::string get_local_dropshell_config_parent_path() { // Try ~/.config/dropshell/dropshell.conf const char* home = std::getenv("HOME"); if (home) { - fs::path user_path = fs::path(home) / ".config" / "dropshell" / "dropshell.env"; + fs::path user_path = fs::path(home) / ".config" / "dropshell"; return user_path.string(); } std::cerr << "Warning: Couldn't determine user directory" << std::endl; return std::string(); } +std::string get_local_dropshell_config_file() +{ + std::string parent_path = get_local_dropshell_config_parent_path(); + if (parent_path.empty()) + return std::string(); + return parent_path + "/dropshell.conf"; +} + std::string get_local_system_templates_path() { return "/opt/dropshell/templates"; diff --git a/src/utils/directories.hpp b/src/utils/directories.hpp index 230931b..f571792 100644 --- a/src/utils/directories.hpp +++ b/src/utils/directories.hpp @@ -6,7 +6,8 @@ namespace dropshell { // local paths - return empty string on failure - std::string get_local_dropshell_config_path(); + std::string get_local_dropshell_config_parent_path(); + std::string get_local_dropshell_config_file(); std::string get_local_system_templates_path(); std::string get_local_backup_path(); diff --git a/templates/watchtower/_common.sh b/templates/watchtower/_common.sh index 7961272..ffc5c07 100755 --- a/templates/watchtower/_common.sh +++ b/templates/watchtower/_common.sh @@ -37,7 +37,7 @@ load_env() { return 1 fi - env_file="$1" + env_file="$1/service.env" if [ ! -f "$env_file" ]; then echo "Warning: service.env file not found at $1"