Wrench
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled

This commit is contained in:
Your Name
2025-05-17 18:17:25 +12:00
parent 583bb18676
commit 5bf93dc954
13 changed files with 353 additions and 343 deletions

View File

@ -22,7 +22,7 @@ namespace dropshell
{
CommandRegistry::instance().register_command({health_name_list,
health_handler,
std_autocomplete_allowstar,
std_autocomplete_allowall,
false, // hidden
true, // requires_config
true, // requires_install

View File

@ -27,7 +27,7 @@ namespace dropshell
{
CommandRegistry::instance().register_command({install_name_list,
install_handler,
std_autocomplete_allowstar,
std_autocomplete_allowall,
false, // hidden
false, // requires_config
false, // requires_install
@ -91,6 +91,12 @@ namespace dropshell
if (!tinfo.is_set())
return false;
if (!tinfo.template_valid())
{
std::cerr << "Template is not valid: " << service_info.template_name << std::endl;
return false;
}
// Create service directory
std::string remote_service_path = remotepath::service(server, service);
std::string mkdir_cmd = "mkdir -p " + quote(remote_service_path);

View File

@ -11,22 +11,6 @@
#pragma message ("TODO: Fix issues with Nuke below.")
/*
j@twelve:~/code/dropshell$ ds nuke localhost test-squashkiwi
---------------------------------------
| Nuking test-squashkiwi on localhost |
---------------------------------------
---------------------------------------------
| Uninstalling test-squashkiwi on localhost |
---------------------------------------------
Service is not installed: test-squashkiwi
bash: line 1: cd: /home/dropshell/dropshell_deploy/services/test-squashkiwi/template: Permission denied
bash: line 1: /home/dropshell/dropshell_deploy/services/test-squashkiwi/template/nuke.sh: Permission denied
Warning: Failed to run nuke script: test-squashkiwi
*/
namespace dropshell {
int nuke_handler(const CommandContext& ctx);
@ -44,52 +28,58 @@ struct NukeCommandRegister {
true, // requires_install
2, // min_args (after command)
2, // max_args (after command)
"nuke SERVER [SERVICE|*] ",
"nuke SERVER SERVICE|all",
"Nuke a service on a server. Destroys everything, both local and remote!",
// heredoc
R"(
Nuke a service on a server. Destroys everything, both local and remote!
nuke SERVER SERVICE nuke the given service on the given server.
nuke SERVER * nuke all services on the given server.
nuke SERVER all nuke all services on the given server.
)"
});
}
} nuke_command_register;
int nuke_handler(const CommandContext &ctx)
int nuke_one(std::string server, std::string service)
{
ASSERT(ctx.args.size() > 1, "Usage: nuke <server> <service>");
ASSERT(gConfig().is_config_set(), "No configuration found. Please run 'dropshell config' to set up your configuration.");
std::string server = safearg(ctx.args, 0);
std::string service = safearg(ctx.args, 1);
maketitle("Nuking " + service + " on " + server);
server_env_manager server_env(server);
LocalServiceInfo service_info;
// step 1 - nuke on remote server.
if (server_env.is_valid())
{
LocalServiceInfo service_info;
service_info = get_service_info(server, service);
if (!SIvalid(service_info))
std::cerr << "Warning: Invalid service: " << service << std::endl;
if (!uninstall_service(server, service, false))
std::cerr << "Warning: Failed to uninstall service: " << service << std::endl;
// run the nuke script on the remote server if it exists.
// otherwise just uninstall.
if (gTemplateManager().template_command_exists(service_info.template_name, "nuke"))
{
if (!server_env.run_remote_template_command(service, "nuke", {}, false, {}))
std::cerr << "Warning: Failed to run nuke script: " << service << std::endl;
}
else
{
if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {}))
std::cerr << "Warning: Failed to uninstall service: " << service << std::endl;
}
// Remove the service directory from the server, running in a docker container as root.
if (server_env.remove_remote_dir(remotepath::service(server, service), true))
{
ASSERT(!server_env.check_remote_dir_exists(remotepath::service(server, service)), "Service directory still found on server after uninstall");
std::cout << "Removed remote service directory " << remotepath::service(server, service) << std::endl;
}
else
std::cerr << "Warning: Failed to remove remote service directory" << std::endl;
}
else
std::cerr << "Warning: Invalid server: " << server << std::endl;
std::cerr << "Warning: Can't nuke the remote service as the server is invalid: " << server << std::endl;
// remove the local service directory
std::string local_service_path = service_info.local_service_path;
// step 2 - nuke the local service directory.
std::string local_service_path = localpath::service(server, service);
if (local_service_path.empty() || !std::filesystem::exists(local_service_path))
{
std::cerr << "Warning: Local service directory not found: " << local_service_path << std::endl;
@ -103,4 +93,40 @@ int nuke_handler(const CommandContext &ctx)
return ret == 0 ? 0 : 1;
}
int nuke_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(gConfig().is_config_set(), "No configuration found. Please run 'dropshell config' to set up your configuration.");
std::string server = safearg(ctx.args, 0);
std::string service = safearg(ctx.args, 1);
if (service == "all")
{
int rval = 0;
// iterate through all service folders in the server directory.
std::string server_path = localpath::server(server);
if (server_path.empty())
{
std::cerr << "Error: Server not found: " << server << std::endl;
return 1;
}
for (const auto& entry : std::filesystem::directory_iterator(server_path))
{
if (entry.is_directory() && entry.path().filename().string().find(".") != 0)
{
std::string service_name = entry.path().filename().string();
rval |= nuke_one(server, service_name);
}
}
return rval;
}
else
{
return nuke_one(server, service);
}
}
} // namespace dropshell

View File

@ -14,8 +14,6 @@ namespace dropshell {
bool silent);
// defined in install.cpp
bool install_service(const std::string& server, const std::string& service, bool silent);
bool uninstall_service(const std::string& server, const std::string& service, bool silent);
std::string get_arch();
// defined in health.cpp
@ -24,7 +22,7 @@ namespace dropshell {
// defined in standard_autocomplete.cpp
void std_autocomplete(const CommandContext& ctx);
void std_autocomplete_allowstar(const CommandContext& ctx);
void std_autocomplete_allowall(const CommandContext& ctx);
} // namespace dropshell

View File

@ -26,7 +26,7 @@ void std_autocomplete(const CommandContext &ctx)
}
}
void std_autocomplete_allowstar(const CommandContext &ctx)
void std_autocomplete_allowall(const CommandContext &ctx)
{
std_autocomplete(ctx);
if (ctx.args.size() == 1)

View File

@ -19,49 +19,25 @@ namespace dropshell
{
CommandRegistry::instance().register_command({uninstall_name_list,
uninstall_handler,
std_autocomplete_allowstar,
std_autocomplete_allowall,
false, // hidden
true, // requires_config
true, // requires_install
1, // min_args (after command)
2, // min_args (after command)
2, // max_args (after command)
"uninstall SERVER [SERVICE|*]",
"uninstall SERVER SERVICE|all",
"Uninstall a service on a server. Does not remove configuration or user data.",
// heredoc
R"(
Uninstall a service on a server. Does not remove configuration or user data.
uninstall SERVER SERVICE uninstall the given service on the given server.
uninstall SERVER * uninstall all services on the given server.
uninstall SERVER all uninstall all services on the given server.
)"});
}
} uninstall_command_register;
int uninstall_handler(const CommandContext &ctx)
{
if (ctx.args.size() < 1)
{
std::cerr << "Error: uninstall requires a server and (optionally) a service" << std::endl;
return 1;
}
std::string server = safearg(ctx.args, 0);
if (ctx.args.size() == 1)
{
// uninstall all services on the server
bool okay = true;
std::vector<LocalServiceInfo> services = get_server_services_info(server);
for (const auto &service : services)
{
if (!uninstall_service(server, service.service_name, false))
okay = false;
}
return okay ? 0 : 1;
}
std::string service = safearg(ctx.args, 1);
return uninstall_service(server, service, false) ? 0 : 1;
}
bool uninstall_service(const std::string &server, const std::string &service, bool silent)
{
@ -84,11 +60,9 @@ namespace dropshell
// 3. Run uninstall script if it exists
std::string uninstall_script = remotepath::service_template(server, service) + "/uninstall.sh";
if (gTemplateManager().template_command_exists(service, "uninstall"))
if (server_env.check_remote_file_exists(uninstall_script))
if (!server_env.run_remote_template_command(service, "uninstall", {}, silent, {}))
if (!silent)
std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl;
if (!server_env.run_remote_template_command(service, "uninstall", {}, silent, {}))
if (!silent)
std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl;
// 4. Remove the service directory from the server, running in a docker container as root.
if (server_env.remove_remote_dir(remotepath::service(server, service), silent))
@ -105,4 +79,33 @@ namespace dropshell
return true;
}
int uninstall_handler(const CommandContext &ctx)
{
if (ctx.args.size() < 1)
{
std::cerr << "Error: uninstall requires a server and a service (or all)" << std::endl;
return 1;
}
std::string server = safearg(ctx.args, 0);
if (safearg(ctx.args, 1) == "all")
{
// uninstall all services on the server
bool okay = true;
std::vector<LocalServiceInfo> services = get_server_services_info(server);
for (const auto &service : services)
{
if (!uninstall_service(server, service.service_name, false))
okay = false;
}
return okay ? 0 : 1;
}
std::string service = safearg(ctx.args, 1);
return uninstall_service(server, service, false) ? 0 : 1;
}
} // namespace dropshell