diff --git a/TEMPLATES.md b/TEMPLATES.md index 3869d64..991b347 100644 --- a/TEMPLATES.md +++ b/TEMPLATES.md @@ -296,7 +296,7 @@ esac ``` ### logs.sh -Shows container logs: +Shows container logs. **OPTIONAL** - if provided, `dropshell log SERVER SERVICE` (or `dropshell logs`) will execute this script and display the output. ```bash #!/bin/bash @@ -306,6 +306,12 @@ _check_required_env_vars "CONTAINER_NAME" docker logs "$CONTAINER_NAME" "$@" ``` +**Usage:** +```bash +dropshell log myserver myservice +dropshell logs myserver myservice # Alias +``` + ### check-config.sh Validates the service configuration without restarting or modifying the service. **OPTIONAL** - if not provided, `dropshell check-config` reports that the template doesn't support this command. diff --git a/source/src/commands/log.cpp b/source/src/commands/log.cpp new file mode 100644 index 0000000..cd40252 --- /dev/null +++ b/source/src/commands/log.cpp @@ -0,0 +1,79 @@ +#include "command_registry.hpp" +#include "config.hpp" +#include "utils/utils.hpp" +#include "shared_commands.hpp" +#include "servers.hpp" +#include "services.hpp" +#include "utils/output.hpp" + +namespace dropshell +{ + + int log_handler(const CommandContext &ctx); + + static std::vector log_name_list = {"log", "logs"}; + + // Static registration + struct LogCommandRegister + { + LogCommandRegister() + { + CommandRegistry::instance().register_command({log_name_list, + log_handler, + shared_commands::std_autocomplete, + false, // hidden + true, // requires_config + true, // requires_install + 2, // min_args (after command) + 2, // max_args (after command) + "log SERVER SERVICE", + "View logs for a service on a server.", + R"( + + log SERVER SERVICE View logs for the given service on the server. + logs SERVER SERVICE Alias for log. + + Runs the logs.sh script on the remote server/service if it exists + and displays the output. + )"}); + } + } log_command_register; + + int log_handler(const CommandContext &ctx) + { + std::string server = safearg(ctx.args, 0); + std::string service = safearg(ctx.args, 1); + + // Check if server is disabled + if (gConfig().is_server_disabled(server)) + { + warning << "Server '" << server << "' is disabled." << std::endl; + info << "Use 'dropshell enable " << server << "' to re-enable this server." << std::endl; + return 1; + } + + ServerConfig server_env(server); + if (!server_env.is_valid()) + { + error << "Server " << server << " is not valid" << std::endl; + return 1; + } + + if (!legal_service_name(service)) + { + error << "Service name contains illegal characters: " << service << std::endl; + return 1; + } + + // run the logs script + bool okay = server_env.run_remote_template_command(service, "logs", {}, false, {}, NULL); + + if (!okay) + { + error << "Failed to retrieve logs for service " << service << " on server " << server << std::endl; + return 1; + } + return 0; + } + +} // namespace dropshell