This commit is contained in:
@ -13,16 +13,16 @@
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
int nuke_handler(const CommandContext &ctx);
|
||||
static std::vector<std::string> nuke_name_list = {"destroy","nuke"};
|
||||
int destroy_handler(const CommandContext &ctx);
|
||||
static std::vector<std::string> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user