diff --git a/source/agent-remote/datacommands.sh b/source/agent-remote/datacommands.sh index b325a20..c752cbb 100755 --- a/source/agent-remote/datacommands.sh +++ b/source/agent-remote/datacommands.sh @@ -19,8 +19,8 @@ _autocommandrun_volume() { echo "Creating volume ${volume_name}" docker volume create ${volume_name} ;; - nuke) - echo "Nuking volume ${volume_name}" + destroy) + echo "Destroying volume ${volume_name}" docker volume rm ${volume_name} ;; backup) @@ -50,14 +50,14 @@ _autocommandrun_path() { echo "Creating path ${path}" mkdir -p ${path} ;; - nuke) - echo "Nuking path ${path}" + destroy) + echo "Destroying path ${path}" local path_parent=$(dirname ${path}) local path_child=$(basename ${path}) if [ -d "${path_parent}/${path_child}" ]; then - docker run --rm -v ${path_parent}:/volume debian bash -c "rm -rfv /volume/${path_child}" || echo "Failed to nuke path ${path}" + docker run --rm -v ${path_parent}:/volume debian bash -c "rm -rfv /volume/${path_child}" || echo "Failed to destroy path ${path}" else - echo "Path ${path} does not exist - nothing to nuke" + echo "Path ${path} does not exist - nothing to destroy" fi ;; backup) @@ -95,7 +95,7 @@ _autocommandrun_file() { mkdir -p ${filepath_parent} fi ;; - nuke) + destroy) rm -f ${filepath} ;; backup) @@ -181,8 +181,8 @@ datacreate() { } -datanuke() { - _autocommandparse nuke none "$@" +datadestroy() { + _autocommandparse destroy none "$@" } databackup() { diff --git a/source/src/autocomplete.cpp b/source/src/autocomplete.cpp index 592c409..53a08d6 100644 --- a/source/src/autocomplete.cpp +++ b/source/src/autocomplete.cpp @@ -14,9 +14,7 @@ namespace autocomplete { const std::set system_commands_noargs = {"templates","autocomplete_list_servers","autocomplete_list_services","autocomplete_list_commands"}; const std::set system_commands_always_available = {"help","edit"}; const std::set system_commands_require_config = {"server","templates","create-service","create-template","create-server","ssh","list"}; - const std::set system_commands_hidden = {"nuke","_allservicesstatus"}; - - const std::set service_commands_require_config = {"ssh","edit","nuke","_allservicesstatus"}; + const std::set system_commands_hidden = {"_allservicesstatus"}; void merge_commands(std::set &commands, const std::set &new_commands) { diff --git a/source/src/commands/destroy.cpp b/source/src/commands/destroy.cpp index 6d2bc01..eb86b0e 100644 --- a/source/src/commands/destroy.cpp +++ b/source/src/commands/destroy.cpp @@ -13,16 +13,16 @@ namespace dropshell { - int nuke_handler(const CommandContext &ctx); - static std::vector nuke_name_list = {"destroy","nuke"}; + int destroy_handler(const CommandContext &ctx); + static std::vector destroy_name_list = {"destroy"}; // Static registration - struct NukeCommandRegister + struct DestroyCommandRegister { - NukeCommandRegister() + DestroyCommandRegister() { - CommandRegistry::instance().register_command({nuke_name_list, - nuke_handler, + CommandRegistry::instance().register_command({destroy_name_list, + destroy_handler, shared_commands::std_autocomplete, false, // hidden true, // requires_config @@ -33,7 +33,7 @@ namespace dropshell "Destroy a service on a server. Erases everything, both local and remote!", // heredoc R"( - Nuke a service. + Destroy a service. Examples: destroy SERVER SERVICE destroy the given service on the given server. @@ -45,41 +45,46 @@ namespace dropshell Use with caution! )"}); } - } nuke_command_register; + } destroy_command_register; namespace shared_commands { - bool nuke_service(const std::string &server, const std::string &service) + bool destroy_service(const std::string &server, const std::string &service) { ServerConfig server_env(server); - // step 1 - nuke on remote server. + // step 1 - destroy on remote server. if (server_env.is_valid()) { LocalServiceInfo service_info; service_info = get_service_info(server, service); - if (!SIvalid(service_info)) - error << "Invalid service: " << service << std::endl; + bool service_valid = SIvalid(service_info); + if (!service_valid) + warning << "Invalid service: " << service << std::endl; std::string user = server_env.get_user_for_service(service); - if (server_env.check_remote_dir_exists(remotepath(server, user).service(service), user)) + bool remote_dir_exists = server_env.check_remote_dir_exists(remotepath(server, user).service(service), user); + if (remote_dir_exists) { - // run the nuke script on the remote server if it exists. + // run the destroy script on the remote server if it exists. // otherwise just uninstall. - if (gTemplateManager().template_command_exists(service_info.template_name, "nuke")) + if (service_valid) { - info << "Running nuke script for " << service << " on " << server << std::endl; - if (!server_env.run_remote_template_command(service, "nuke", {}, false, {})) - warning << "Failed to run nuke script: " << service << std::endl; - } - else - { - info << "No nuke 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, {})) - warning << "Failed to uninstall service: " << service << std::endl; + 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, {})) + 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, {})) + warning << "Failed to uninstall service: " << service << std::endl; + } } // Remove the service directory from the server, running in a docker container as root. @@ -95,9 +100,9 @@ namespace dropshell warning << "Service not found on remote server: " << remotepath(server, user).service(service) << std::endl; } else - warning << "Can't nuke the remote service as the server is invalid: " << server << std::endl; + warning << "Can't destroy the remote service as the server is invalid: " << server << std::endl; - // step 2 - nuke the local service directory. + // step 2 - destroy the local service directory. std::string local_service_path = localpath::service(server, service); if (local_service_path.empty() || !std::filesystem::exists(local_service_path)) { @@ -110,15 +115,15 @@ namespace dropshell error << "Failed to remove local service directory" << std::endl; } - info << "Nuked service " << service << " on server " << server << std::endl; + info << "Destroyed service " << service << " on server " << server << std::endl; return true; } } // namespace shared_commands - int nuke_handler(const CommandContext &ctx) + int destroy_handler(const CommandContext &ctx) { - ASSERT(ctx.args.size() == 2, "Usage: nuke SERVER SERVICE|all (requires 2 args - you supplied " + std::to_string(ctx.args.size()) + ")"); + ASSERT(ctx.args.size() == 2, "Usage: destroy SERVER SERVICE|all (requires 2 args - you supplied " + std::to_string(ctx.args.size()) + ")"); ASSERT(gConfig().is_config_set(), "No configuration found. Please run 'dropshell config' to set up your configuration."); std::string server = safearg(ctx.args, 0); @@ -141,14 +146,14 @@ namespace dropshell if (entry.is_directory() && entry.path().filename().string().find(".") != 0) { std::string service_name = entry.path().filename().string(); - rval |= (shared_commands::nuke_service(server, service_name) ? 0 : 1); + rval |= (shared_commands::destroy_service(server, service_name) ? 0 : 1); } } return rval; } else { - return (shared_commands::nuke_service(server, service) ? 0 : 1); + return (shared_commands::destroy_service(server, service) ? 0 : 1); } } diff --git a/source/src/commands/help.cpp b/source/src/commands/help.cpp index 15b2681..356fd3e 100644 --- a/source/src/commands/help.cpp +++ b/source/src/commands/help.cpp @@ -141,7 +141,7 @@ int help_handler(const CommandContext& ctx) { info << std::endl; show_command("install"); show_command("uninstall"); - show_command("nuke"); + show_command("destroy"); info << std::endl; show_command("start"); show_command("stop"); diff --git a/source/src/commands/restoredata.cpp b/source/src/commands/restoredata.cpp index 2d1ab85..b0b60f4 100644 --- a/source/src/commands/restoredata.cpp +++ b/source/src/commands/restoredata.cpp @@ -175,9 +175,9 @@ namespace dropshell info << "Backup complete." << std::endl; } - { // nuke the old service - info << "2) Nuking old service..." << std::endl; - if (!shared_commands::nuke_service(server, service)) + { // Destroy the old service + info << "2) Destroying old service..." << std::endl; + if (!shared_commands::destroy_service(server, service)) return 1; } diff --git a/source/src/commands/shared_commands.hpp b/source/src/commands/shared_commands.hpp index b0a1b66..9b176db 100644 --- a/source/src/commands/shared_commands.hpp +++ b/source/src/commands/shared_commands.hpp @@ -90,8 +90,8 @@ namespace dropshell // defined in uninstall.cpp bool uninstall_service(const ServerConfig &server_env, const std::string &service); - // defined in nuke.cpp - bool nuke_service(const std::string &server, const std::string &service); + // defined in destroy.cpp + bool destroy_service(const std::string &server, const std::string &service); // defined in install.cpp bool install_service(const ServerConfig &server_env, const std::string &service); diff --git a/source/src/commands/uninstall.cpp b/source/src/commands/uninstall.cpp index 2f2f2de..493b881 100644 --- a/source/src/commands/uninstall.cpp +++ b/source/src/commands/uninstall.cpp @@ -36,7 +36,7 @@ namespace dropshell uninstall SERVER SERVICE Uninstall the given service on the given server. uninstall SERVER all Uninstall all services on the given server. - Update and reinstall the service with install, or delete all configuration and data with nuke. + Update and reinstall the service with install, or delete all configuration and data with destroy. )"}); } } uninstall_command_register; diff --git a/source/src/main.cpp b/source/src/main.cpp index 1198fe9..af518f2 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -140,115 +140,6 @@ auto command_match = [](const std::string& cmd_list, int argc, char* argv[]) -> } \ } - -// int old_main(int argc, char* argv[]) { -// HAPPYEXIT("hash", hash_demo_raw(safearg(argc,argv,2))) -// HAPPYEXIT("version", printversion()) -// BOOLEXIT("test-template", gTemplateManager().test_template(safearg(argc,argv,2))) -// ASSERT(safearg(argc,argv,1) != "assert", "Hello! Here is an assert."); - -// try { -// // silently attempt to load the config file and templates. -// gConfig().load_config(); -// if (gConfig().is_config_set()) -// gTemplateManager().load_sources(); - -// std::string cmd = argv[1]; - - -// // ------------------------------------------------------------ -// // from here we require the config file to be loaded. -// if (!gConfig().is_config_set()) -// return die("Please run 'dropshell edit' to set up the dropshell configuration."); - - -// const std::vector & server_definition_paths = gConfig().get_local_server_definition_paths(); -// if (server_definition_paths.size()>1) { // only show if there are multiple. -// std::cout << "Server definition paths: "; -// for (auto & dir : server_definition_paths) -// std::cout << "["<< dir << "] "; -// std::cout << std::endl; -// } -// if (gTemplateManager().is_loaded() && gTemplateManager().get_source_count() > 0) -// gTemplateManager().print_sources(); - -// HAPPYEXIT("templates", gTemplateManager().list_templates()); - -// if (cmd == "create-template") { -// if (argc < 3) return die("Error: create-template requires a template name"); -// return (gTemplateManager().create_template(argv[2])) ? 0 : 1; -// } - -// if (cmd == "create-server") { -// if (argc < 3) return die("Error: create-server requires a server name"); -// return (create_server(argv[2])) ? 0 : 1; -// } - -// if (cmd == "create-service") { -// if (argc < 5) return die("Error: not enough arguments.\ndropshell create-service server template service"); -// return (create_service(argv[2], argv[3], argv[4])) ? 0 : 1; -// } - -// if (cmd == "ssh" && argc < 4) { -// if (argc < 3) return die("Error: ssh requires a server name and optionally service name"); -// service_runner::interactive_ssh(argv[2], "bash"); -// return 0; -// } - - -// // handle running a command. -// std::set commands; -// get_all_used_commands(commands); - -// autocomplete::merge_commands(commands, autocomplete::service_commands_require_config); // handled by service_runner, but not in template_shell_commands. - -// if (commands.count(cmd)) { -// std::set safe_commands = {"nuke", "fullnuke"}; -// if (safe_commands.count(cmd) && argc < 4) -// return die("Error: "+cmd+" requires a server name and service name. For safety, can't run on all services."); - -// // get all the services to run the command on. -// ServerAndServices server_and_services; -// if (!getCLIServices(safearg(argc, argv, 2), safearg(argc, argv, 3), server_and_services)) -// return die("Error: "+cmd+" command requires server name and optionally service name"); - -// // run the command on each service. -// for (const auto& service_info : server_and_services.servicelist) { -// if (!SIvalid(service_info)) -// std::cerr<<"Error: Unable to get service information."< additional_args; -// for (int i=4; i additional_args={}, std::map env_vars={}); - -// // check health of service. Silent. -// // 1. run status.sh on the server -// // 2. return the output of the status.sh script - -// //HealthStatus is_healthy(); - -// // std::string healthtick(); -// // std::string healthmark(); - -// public: -// // backup and restore -// bool backup(bool silent=false); -// bool restore(std::string backup_file, bool silent=false); - -// // nuke the service -// bool nuke(bool silent=false); // nukes all data for this service on the remote server -// bool fullnuke(); // nuke all data for this service on the remote server, and then nukes all the local service definitionfiles - -// // launch an interactive ssh session on a server or service -// // replaces the current dropshell process with the ssh process -// bool interactive_ssh_service(); - -// bool scp_file_to_remote(const std::string& local_path, const std::string& remote_path, bool silent=false); -// bool scp_file_from_remote(const std::string& remote_path, const std::string& local_path, bool silent=false); -// public: -// // utility functions -// static std::string get_latest_backup_file(const std::string& server, const std::string& service); -// static bool interactive_ssh(const std::string & server_name, const std::string & command); -// // static std::map get_all_services_status(std::string server_name); - -// private: -// std::string mServer; -// ServerConfig mServerEnv; -// LocalServiceInfo mServiceInfo; -// std::string mService; -// bool mValid; - -// // Helper methods -// public: -// }; - - -// } // namespace dropshell - - - // namespace fs = std::filesystem; // namespace dropshell { @@ -113,47 +45,6 @@ -// bool service_runner::nuke(bool silent) -// { -// maketitle("Nuking " + mService + " (" + mServiceInfo.template_name + ") on " + mServer); - -// if (!mServerEnv.is_valid()) return false; // should never hit this. - -// std::string remote_service_path = remotepath::service(mServer, mService); - -// info << "Service " << mService << " successfully nuked from " << mServer << std::endl; - -// if (!silent) { -// info << "There's nothing left on the remote server." << std::endl; -// info << "You can remove the local files with:" << std::endl; -// info << " rm -rf " << localpath::service(mServer,mService) << std::endl; -// } -// return true; -// } - -// bool service_runner::fullnuke() -// { -// if (!nuke(true)) -// { -// warning << "Nuke script failed, aborting." << std::endl; -// return false; -// } - -// std::string local_service_path = mServiceInfo.local_service_path; -// if (local_service_path.empty() || !fs::exists(local_service_path)) { -// error << "Service directory not found: " << local_service_path << std::endl; -// return false; -// } - -// std::string rm_cmd = "rm -rf " + quote(local_service_path); -// if (!execute_local_command("", rm_cmd, {}, nullptr, cMode::Silent)) { -// error << "Failed to remove service directory" << std::endl; -// return false; -// } - -// return true; -// } - // // ------------------------------------------------------------------------------------------------ // // Run a command on the service. @@ -169,15 +60,6 @@ // return false; // } -// if (command == "fullnuke") -// return fullnuke(); - -// if (command == "nuke") -// { -// std::cout << "Nuking " << mService << " (" << mServiceInfo.template_name << ") on " << mServer << std::endl; -// return nuke(); -// } - // 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. @@ -232,298 +114,4 @@ // } - -// bool service_runner::interactive_ssh(const std::string & server_name, const std::string & command) { -// std::string serverpath = localpath::server(server_name); -// if (serverpath.empty()) { -// std::cerr << "Error: Server not found: " << server_name << std::endl; -// return false; -// } - -// ServerConfig env(server_name); -// if (!env.is_valid()) { -// std::cerr << "Error: Invalid server environment file: " << server_name << std::endl; -// return false; -// } -// sCommand scommand("", "bash",{}); -// return execute_ssh_command(env.get_SSH_INFO(), scommand, cMode::Interactive); -// } - - - -// bool service_runner::interactive_ssh_service() -// { -// std::set used_commands = get_used_commands(mServer, mService); -// if (used_commands.find("ssh") == used_commands.end()) { -// std::cerr << "Error: "<< mService <<" does not support ssh" << std::endl; -// return false; -// } - -// std::vector args; // not passed through yet. -// return mServerEnv.run_remote_template_command(mService, "ssh", args, false, {}); -// } - -// 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_UNPRIVILEGED_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + (silent ? " > /dev/null 2>&1" : ""); -// 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_UNPRIVILEGED_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::Defaults)); -// } - -// bool service_runner::restore(std::string backup_file, bool silent) -// { -// if (backup_file.empty()) { -// std::cerr << "Error: not enough arguments. dropshell restore " << std::endl; -// return false; -// } - -// std::string local_backups_dir = gConfig().get_local_backup_path(); - -// if (backup_file == "latest") { -// // get the latest backup file from the server -// backup_file = get_latest_backup_file(mServer, mService); -// } - -// std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_file).string(); - -// if (! std::filesystem::exists(local_backup_file_path)) { -// std::cerr << "Error: Backup file not found at " << local_backup_file_path << std::endl; -// return false; -// } - -// // split the backup filename into parts based on the magic string -// std::vector parts = dropshell::split(backup_file, "-_-"); -// if (parts.size() != 4) { -// std::cerr << "Error: Backup file format is incompatible, - in one of the names?" << std::endl; -// return false; -// } - -// std::string backup_server_name = parts[0]; -// std::string backup_template_name = parts[1]; -// std::string backup_service_name = parts[2]; -// std::string backup_datetime = parts[3]; - -// if (backup_template_name != mServiceInfo.template_name) { -// std::cerr << "Error: Backup template does not match service template. Can't restore." << std::endl; -// return false; -// } - -// std::string nicedate = std::string(backup_datetime).substr(0, 10); - -// std::cout << "Restoring " << nicedate << " backup of " << backup_template_name << " taken from "< latest_datetime) { -// latest_datetime = datetime; -// latest_file = filename; -// } -// } -// } - -// if (latest_file.empty()) { -// std::cerr << "Error: No backup files found for " << server << ", " << service << std::endl; -// } - -// std::cout << "Latest backup file: " << latest_file << std::endl; -// return latest_file; -// } - // } // namespace dropshell \ No newline at end of file