This commit is contained in:
John 2025-04-27 09:34:04 +12:00
parent 3c50260ed3
commit c6665f3044
11 changed files with 75 additions and 37 deletions

View File

@ -28,7 +28,6 @@ configure_file(
) )
# Find required packages # Find required packages
find_package(Boost REQUIRED COMPONENTS program_options filesystem system)
find_package(Curses REQUIRED) find_package(Curses REQUIRED)
find_package(TBB REQUIRED) find_package(TBB REQUIRED)

View File

@ -41,7 +41,7 @@ print_status "Detected OS: $OS $VER"
case $OS in case $OS in
"Ubuntu"|"Debian GNU/Linux") "Ubuntu"|"Debian GNU/Linux")
# Common packages for both Ubuntu and Debian # 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" print_error "Unsupported distribution: $OS"
@ -82,11 +82,17 @@ for tool in cmake make g++; do
fi fi
done done
# Check Boost installation # Check TBB installation
if [ ! -d "/usr/include/boost" ]; then if [ ! -d "/usr/include/tbb" ]; then
print_error "Boost headers not found" print_error "TBB headers not found"
exit 1 exit 1
fi 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 "All dependencies installed successfully!"
print_status "You can now run ./build.sh to build the project" print_status "You can now run ./build.sh to build the project"

View File

@ -22,7 +22,7 @@ config::~config() {
} }
bool config::load_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)) if (config_path.empty() || !std::filesystem::exists(config_path))
return false; return false;
@ -56,7 +56,10 @@ void config::save_config()
return; 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); envmanager config_env(config_path);
config_env.set_variable("local.config.directories", multi2string(mLocalConfigPaths)); config_env.set_variable("local.config.directories", multi2string(mLocalConfigPaths));

View File

@ -10,6 +10,7 @@
#include "autocomplete.hpp" #include "autocomplete.hpp"
#include "main_commands.hpp" #include "main_commands.hpp"
#include <filesystem>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -25,7 +26,9 @@ void print_help() {
std::cout << std::endl; std::cout << std::endl;
std::cout << "dropshell ..." << std::endl; std::cout << "dropshell ..." << std::endl;
std::cout << " help Show this help message" << 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 << " server NAME Show details for specific server" << std::endl;
std::cout << " templates List all available templates" << std::endl; std::cout << " templates List all available templates" << std::endl;
std::cout << std::endl; std::cout << std::endl;
@ -44,6 +47,12 @@ void print_help() {
std::cout << " create-template TEMPLATE" << std::endl; std::cout << " create-template TEMPLATE" << std::endl;
std::cout << " create-server SERVER" << std::endl; std::cout << " create-server SERVER" << std::endl;
std::cout << " create-service SERVER TEMPLATE SERVICE" << 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 } // namespace dropshell
@ -99,6 +108,12 @@ int main(int argc, char* argv[]) {
return 0; 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. // from here we require the config file to be loaded.
if (!cfg->is_config_set()) { if (!cfg->is_config_set()) {
@ -173,10 +188,8 @@ int main(int argc, char* argv[]) {
} }
if (cmd == "edit" && argc < 4) { if (cmd == "edit" && argc < 4) {
if (argc < 3) ASSERT_ALWAYS(argc>=3);
dropshell::edit_file(dropshell::get_local_dropshell_config_path(), "Please ensure any directories you have introduced in the config file exist."); dropshell::edit_server(argv[2]);
else
dropshell::edit_server(argv[2]);
return 0; return 0;
} }

View File

@ -31,7 +31,7 @@ int init(const std::vector<std::string> &args)
} }
try { try {
if (!cfg->add_local_config_directory(args[2])) if (!cfg->add_local_config_directory(args[2]))
return 1; // error already reported return 1; // error already reported
cfg->save_config(); cfg->save_config();
std::cout << "Config directory added: " << cfg->get_local_config_directories().back() << std::endl; std::cout << "Config directory added: " << cfg->get_local_config_directories().back() << std::endl;

View File

@ -91,7 +91,7 @@ std::string server_env::construct_ssh_cmd() const {
return ssh_cmd.str(); 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<std::string> args) const
{ {
std::string remote_service_template_path = get_remote_service_template_path(mServer_name,service_name); 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); 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); 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<std::string> 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 server_env::execute_local_command(const std::string& command, const std::string& error_msg) {
bool okay = (system(command.c_str()) == 0); bool okay = (system(command.c_str()) == 0);

View File

@ -34,17 +34,22 @@ class server_env {
// helper functions // helper functions
public: 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<std::string>& file_paths) const; bool check_remote_items_exist(const std::vector<std::string>& file_paths) const;
bool execute_ssh_command(const std::string& command, const std::string& error_msg) const; 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<std::string> 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(const std::string& command, const std::string& error_msg="");
static bool execute_local_command_and_capture_output(const std::string& command, std::string & output); 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_dir_exists(const std::string &dir_path) const;
bool check_remote_file_exists(const std::string& file_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<std::string> args) const;
private: private:
std::string mServer_name; std::string mServer_name;
std::map<std::string, std::string> variables; std::map<std::string, std::string> variables;

View File

@ -87,10 +87,8 @@ bool service_runner::install() {
// Run install script // Run install script
{ {
std::string install_cmd = m_server_env.construct_standard_command_run_cmd(m_service_info.service_name, "install"); std::vector<std::string> args; // not passed through yet.
bool ok= m_server_env.execute_ssh_command(install_cmd, "Failed to run install script"); m_server_env.run_remote_template_command(m_service_info.service_name, "install", args);
if (!ok)
return false;
} }
// print health tick // print health tick
@ -114,10 +112,11 @@ bool service_runner::uninstall() {
bool script_exists = m_server_env.check_remote_file_exists(uninstall_script); bool script_exists = m_server_env.check_remote_file_exists(uninstall_script);
if (script_exists) { if (script_exists) {
std::string uninstall_cmd = m_server_env.construct_standard_command_run_cmd(m_service_info.service_name, "uninstall"); std::vector<std::string> args; // not passed through yet.
if (!m_server_env.execute_ssh_command(uninstall_cmd, "Failed to run uninstall script")) { 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; std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl;
} }
} else { } else {
std::cerr << "Warning: No uninstall script found. Unable to uninstall service." << std::endl; std::cerr << "Warning: No uninstall script found. Unable to uninstall service." << std::endl;
return false; return false;
@ -188,8 +187,8 @@ bool service_runner::run_command(const std::string& command) {
} }
// Run the generic command // Run the generic command
std::string run_cmd = m_server_env.construct_standard_command_run_cmd(m_service_info.service_name, command); std::vector<std::string> args; // not passed through yet.
return m_server_env.execute_ssh_command(run_cmd, "Command returned error code: " + script_path); 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. // 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"); std::vector<std::string> args; // not passed through yet.
bool ok = m_server_env.execute_ssh_command(run_cmd, ""); if (!m_server_env.run_remote_template_command(m_service_info.service_name, "status", args))
if (!ok)
return HealthStatus::UNHEALTHY; return HealthStatus::UNHEALTHY;
return HealthStatus::HEALTHY; return HealthStatus::HEALTHY;
} }
@ -408,8 +405,8 @@ void service_runner::interactive_ssh_service()
return; return;
} }
std::string command = m_server_env.construct_standard_command_run_cmd(m_service_info.service_name, "ssh"); std::vector<std::string> args; // not passed through yet.
interactive_ssh(m_server_name, "/bin/bash -c '"+command+"'"); m_server_env.run_remote_template_command(m_service_info.service_name, "ssh", args);
} }
void service_runner::edit_service_config() void service_runner::edit_service_config()

View File

@ -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 // Try ~/.config/dropshell/dropshell.conf
const char* home = std::getenv("HOME"); const char* home = std::getenv("HOME");
if (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(); return user_path.string();
} }
std::cerr << "Warning: Couldn't determine user directory" << std::endl; std::cerr << "Warning: Couldn't determine user directory" << std::endl;
return std::string(); 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() std::string get_local_system_templates_path()
{ {
return "/opt/dropshell/templates"; return "/opt/dropshell/templates";

View File

@ -6,7 +6,8 @@
namespace dropshell { namespace dropshell {
// local paths - return empty string on failure // 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_system_templates_path();
std::string get_local_backup_path(); std::string get_local_backup_path();

View File

@ -37,7 +37,7 @@ load_env() {
return 1 return 1
fi fi
env_file="$1" env_file="$1/service.env"
if [ ! -f "$env_file" ]; then if [ ! -f "$env_file" ]; then
echo "Warning: service.env file not found at $1" echo "Warning: service.env file not found at $1"