Your Name 60907e5e02
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 11s
List tidy
2025-05-24 17:00:43 +12:00

92 lines
3.4 KiB
C++

#include "command_registry.hpp"
#include "config.hpp"
#include "utils/utils.hpp"
#include "utils/directories.hpp"
#include "shared_commands.hpp"
#include "servers.hpp"
#include "services.hpp"
#include "servers.hpp"
#include "utils/output.hpp"
namespace dropshell
{
int stop_handler(const CommandContext &ctx);
static std::vector<std::string> stop_name_list = {"stop", "stop-service"};
// Static registration
struct StopCommandRegister
{
StopCommandRegister()
{
CommandRegistry::instance().register_command({stop_name_list,
stop_handler,
shared_commands::std_autocomplete_allowall,
false, // hidden
true, // requires_config
true, // requires_install
1, // min_args (after command)
2, // max_args (after command)
"stop SERVER SERVICE|all",
"Stop a service or all services on a server.",
R"(
stop SERVER SERVICE Stops the given service on the given server.
stop SERVER all Stops all services on the given server.
Note: This command will not destroy any data or configuration.
It will simply stop the service on the remote server.
Restart the service with start, or update and start it with install.
)"});
}
} stop_command_register;
bool stop_service(const std::string &server, const std::string &service)
{
ServerConfig server_env(server);
if (!server_env.is_valid())
{
error << "Server " << server << " is not valid" << std::endl;
return false;
}
// run the stop script.
bool stopped = server_env.run_remote_template_command(service, "stop", {}, false, {});
if (stopped)
{
info << "Service " << service << " on server " << server << " stopped." << std::endl;
return true;
}
error << "Failed to stop service " << service << " on server " << server << std::endl;
return false;
}
int stop_handler(const CommandContext &ctx)
{
if (ctx.args.size() < 2)
{
error << "Server name and service name are both required" << std::endl;
return 1;
}
std::string server = safearg(ctx.args, 0);
std::string service = safearg(ctx.args, 1);
if (service == "all")
{
// install all services on the server
maketitle("Stopping all services on " + server);
bool okay = true;
std::vector<LocalServiceInfo> services = get_server_services_info(server);
for (const auto &service : services)
okay &= stop_service(server, service.service_name);
return okay ? 0 : 1;
}
// stop the specific service.
return stop_service(server, service) ? 0 : 1;
}
} // namespace dropshell