diff --git a/source/agent-local/agent-install.sh b/source/agent-local/agent-install.sh index 8f8faad..20eb834 100755 --- a/source/agent-local/agent-install.sh +++ b/source/agent-local/agent-install.sh @@ -50,7 +50,7 @@ function install_bb64() { _die "Curl is not installed. Curl is required for agent installation." fi - curl -fsSL "https://gitea.jde.nz/public/bb64/releases/download/latest/install.sh" | bash -s -- "$AGENT_PATH" "$(id -u $USER):$(id -g $USER)" + curl -fsSL "https://gitea.jde.nz/public/bb64/releases/download/latest/install.sh" | bash -s -- "$AGENT_LOCAL_PATH" "$(id -u $USER):$(id -g $USER)" # test result code from curl if [ $? -ne 0 ]; then @@ -58,7 +58,7 @@ function install_bb64() { fi # test if bb64 is installed - "$AGENT_PATH/bb64" -v + "$AGENT_LOCAL_PATH/bb64" -v if [ $? -ne 0 ]; then _die "bb64 did not install correctly." fi @@ -71,11 +71,11 @@ function install_bb64() { #------------------------------------------------------------------------- set -a -AGENT_PATH="$SCRIPT_DIR" +AGENT_LOCAL_PATH="$SCRIPT_DIR" set +a -_check_required_env_vars "AGENT_PATH" -echo "Installing host agent into $AGENT_PATH" +_check_required_env_vars "AGENT_LOCAL_PATH" +echo "Installing host agent into $AGENT_LOCAL_PATH" _check_docker_installed || _die "Docker is required." diff --git a/source/src/commands/create-server.cpp b/source/src/commands/create-server.cpp index 0bfce2f..ef9a585 100644 --- a/source/src/commands/create-server.cpp +++ b/source/src/commands/create-server.cpp @@ -27,9 +27,9 @@ struct CreateServerCommandRegister { create_server_handler, create_server_autocomplete, false, // hidden - false, // requires_config - false, // requires_install - 0, // min_args (after command) + true, // requires_config + true, // requires_install + 1, // min_args (after command) 1, // max_args (after command) "create-server [SERVER]", "Create a new server entry on this host.", diff --git a/source/src/commands/create-template.cpp b/source/src/commands/create-template.cpp new file mode 100644 index 0000000..2c182b4 --- /dev/null +++ b/source/src/commands/create-template.cpp @@ -0,0 +1,63 @@ +#include "command_registry.hpp" +#include "config.hpp" +#include "utils/utils.hpp" +#include "utils/directories.hpp" +#include "shared_commands.hpp" +#include "version.hpp" +#include "utils/assert.hpp" +#include "templates.hpp" + +#include +#include +#include +#include +#include + +namespace dropshell { + +void create_template_autocomplete(const CommandContext& ctx); +int create_template_handler(const CommandContext& ctx); + +static std::vector create_template_name_list={"create-template"}; + +// Static registration +struct CreateTemplateCommandRegister { + CreateTemplateCommandRegister() { + CommandRegistry::instance().register_command({ + create_template_name_list, + create_template_handler, + create_template_autocomplete, + false, // hidden + true, // requires_config + true, // requires_install + 1, // min_args (after command) + 1, // max_args (after command) + "create-template TEMPLATE", + "Create a new template.", + // heredoc + R"( + Create a new template. + + create-template TEMPLATE + )" + }); + } +} create_template_command_register; + + +void create_template_autocomplete(const CommandContext& ctx) { + return; // can't autocomplete as it's a new server! +} + + +int create_template_handler(const CommandContext& ctx) { + // create a new server entry on this host + if (ctx.args.size() == 0) { + error << "No template name provided" << std::endl; + return 1; + } + bool ok = gTemplateManager().create_template(ctx.args[0]); + return ok ? 0 : 1; +} + +} // namespace dropshell \ No newline at end of file diff --git a/source/src/commands/help.cpp b/source/src/commands/help.cpp index 0de6f9a..15b2681 100644 --- a/source/src/commands/help.cpp +++ b/source/src/commands/help.cpp @@ -45,7 +45,7 @@ struct HelpCommandRegister { void help_autocomplete(const CommandContext& ctx) { - if (ctx.args.size() == 1) { + if (ctx.args.size() == 0) { // list all commands for (const auto& cmd : CommandRegistry::instance().list_primary_commands(false)) { rawout << cmd << std::endl; diff --git a/source/src/commands/install.cpp b/source/src/commands/install.cpp index 334b1cb..480d9fa 100644 --- a/source/src/commands/install.cpp +++ b/source/src/commands/install.cpp @@ -238,17 +238,22 @@ namespace dropshell { maketitle("Installing dropshell agent on this computer..."); + // clear out old cruft. + std::filesystem::remove_all(localpath::agent_local()); + std::filesystem::remove_all(localpath::agent_remote()); + + // recreate the directories. localpath::create_directories(); - // create the agent-local directory. - recreate_agent_local::recreate_tree(localpath::agent()); + // populate the agent-local directory. + recreate_agent_local::recreate_tree(localpath::agent_local()); // run the local agent installer. - execute_local_command(localpath::agent(), "agent-install.sh",{}, nullptr, cMode::Defaults | cMode::NoBB64); + execute_local_command(localpath::agent_local(), "agent-install.sh",{}, nullptr, cMode::Defaults | cMode::NoBB64); - // create the agent-remote directory. + // populate the agent-remote directory. info << "Creating local files to copy to remote agents..." << std::endl; - recreate_agent_remote::recreate_tree(localpath::files_for_remote_agent()); + recreate_agent_remote::recreate_tree(localpath::agent_remote()); return 0; } @@ -275,7 +280,7 @@ namespace dropshell // now create the agent. // copy across from the local agent files. info << "Copying local agent files to remote server... " << std::flush; - shared_commands::rsync_tree_to_remote(localpath::files_for_remote_agent(), agent_path, server_env, false); + shared_commands::rsync_tree_to_remote(localpath::agent_remote(), agent_path, server_env, false); info << "done." << std::endl; // run the agent installer. Can't use BB64 yet, as we're installing it on the remote server. diff --git a/source/src/config.cpp b/source/src/config.cpp index aaf8bc5..73975ef 100644 --- a/source/src/config.cpp +++ b/source/src/config.cpp @@ -114,7 +114,7 @@ bool config::is_config_set() const bool config::is_agent_installed() { - return std::filesystem::exists(localpath::agent() + "/bb64"); + return std::filesystem::exists(localfile::bb64()); } std::vector config::get_template_registry_urls() { diff --git a/source/src/utils/directories.cpp b/source/src/utils/directories.cpp index 593641c..c8eb446 100644 --- a/source/src/utils/directories.cpp +++ b/source/src/utils/directories.cpp @@ -41,6 +41,16 @@ namespace localfile { return (servicepath.empty() ? "" : (fs::path(servicepath) / ".template_info.env").string()); } + std::string template_example() + { + return localpath::agent_local() + "/template_example"; + } + + std::string bb64() + { + return localpath::agent_local() + "/bb64"; + } + } // namespace localfile @@ -66,12 +76,13 @@ namespace localpath { return ((template_cache_path.empty() || service_name.empty()) ? "" : (template_cache_path+"/remote_versions/"+service_name+".json")); } - std::string agent(){ - return current_user_home() + "/.local/dropshell_agent"; - } - std::string files_for_remote_agent() + std::string agent_local() { - return agent() + "/files_for_remote_agent"; + return current_user_home()+"/.local/dropshell_agent/agent-local"; + } + std::string agent_remote() + { + return current_user_home() + "/.local/dropshell_agent/agent-remote"; } std::string current_user_home() { @@ -110,15 +121,17 @@ namespace localpath { { std::vector paths = { dropshell_files(), + agent_local(), + agent_remote(), template_cache(), backups(), - temp_files(), - agent()}; + temp_files() + }; for (auto &p : gConfig().get_local_server_definition_paths()) paths.push_back(p); for (auto &p : paths) - if (!std::filesystem::exists(p)) + if (!p.empty() && !std::filesystem::exists(p)) { info << "Creating directory: " << p << std::endl; std::filesystem::create_directories(p); diff --git a/source/src/utils/directories.hpp b/source/src/utils/directories.hpp index 3bf4bbe..35c9d2f 100644 --- a/source/src/utils/directories.hpp +++ b/source/src/utils/directories.hpp @@ -14,9 +14,12 @@ namespace dropshell { // ~/.config/dropshell/dropshell.json // ~/.local/dropshell_agent - // |-- bb64 (only used locally, as it's for the local machine's architecture!) - // |-- files_for_remote_agent - // |-- (other agent files, including _allservicesstatus.sh) + // |-- agent-local + // |-- agent-install.sh + // |-- bb64 (only used locally, as it's for the local machine's architecture!) + // |-- template_example + // |-- agent-remote + // |-- (remote agent files, including _allservicesstatus.sh) // ~/.local/dropshell_files // |-- backups @@ -52,6 +55,8 @@ namespace dropshell { std::string server_json(const std::string &server_name); std::string service_env(const std::string &server_name, const std::string &service_name); std::string template_info_env(const std::string &server_name, const std::string &service_name); + std::string template_example(); + std::string bb64(); } // namespace localfile namespace localpath { @@ -60,8 +65,8 @@ namespace dropshell { std::string remote_versions(const std::string &server_name, const std::string &service_name); - std::string agent(); - std::string files_for_remote_agent(); + std::string agent_local(); + std::string agent_remote(); std::string current_user_home(); std::string dropshell_files(); @@ -69,7 +74,6 @@ namespace dropshell { std::string temp_files(); std::string template_cache(); - bool create_directories(); } // namespace local diff --git a/source/src/utils/execute.cpp b/source/src/utils/execute.cpp index 998ad1a..4731771 100644 --- a/source/src/utils/execute.cpp +++ b/source/src/utils/execute.cpp @@ -29,7 +29,7 @@ namespace dropshell { if (command.get_command_to_run().empty()) return false; - std::string full_command = command.construct_cmd(localpath::agent()+"/bb64"); // Get the command string + std::string full_command = command.construct_cmd(localfile::bb64()); // Get the command string pid_t pid = fork(); @@ -130,7 +130,7 @@ namespace dropshell std::string full_cmd; if (!hasFlag(mode, cMode::NoBB64)) - full_cmd = command.construct_cmd(localpath::agent()+"/bb64"); + full_cmd = command.construct_cmd(localfile::bb64()); else full_cmd = command.construct_cmd("");