From a8e12cc8bdffdfd67d5d25725e0879544ba0631a Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 25 Jul 2021 16:05:56 -0600 Subject: [PATCH] Initial commit --- LICENSE | 7 +++++ README.md | 23 ++++++++++++++ requirements.txt | 1 + server-restart.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 requirements.txt create mode 100644 server-restart.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c0db645 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2021 ZeroHD + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f15553f --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Server Restart Script + +Hooks into ServerAPI to run the restart command on the server. A restart +script must be specified in `spigot.yml` for this work. + +## Example Restart Script +```bash +#!/bin/bash + +SCREEN_NAME="server" +SERVER_DIRECTORY="/home/minecraft/server" +STARTUP_SCRIPT="startup.sh" + +# Start the screen if it is not already running +if ! screen -list | grep -q $SCREE_NAME; then + screen -dmS s1_canopy +fi + +# Run the startup script in a screen +screen -S s1_canopy -p 0 -X stuff "^Mcd $SCREEN_DIRECTORY^M" +screen -S s1_canopy -p 0 -X stuff "./$STARTUP_SCRIPT^M" + +``` \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b0548a0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests~=2.26.0 \ No newline at end of file diff --git a/server-restart.py b/server-restart.py new file mode 100644 index 0000000..041de44 --- /dev/null +++ b/server-restart.py @@ -0,0 +1,79 @@ +import time +import requests +import argparse + +parser = argparse.ArgumentParser(description='Automated Server Restart Script') + +parser.add_argument('api_url', metavar='server_api_url', type=str, help='ServerAPI Base URL') +parser.add_argument('token', metavar='token', type=str, help='ServerAPI Token') +parser.add_argument('--webhook', metavar='discord_webhook', type=str, help='Discord Webhook URL') + + +def send_discord_webhook(url, msg): + request = {"content": msg} + r = requests.post(url, json=request) + + +def send_request(url, token, endpoint, request): + headers = {"X-ServerAPI-Token": token} + + r = requests.post(f"{url}{endpoint}/", headers=headers, json=request) + return r.ok + + +def send_command(url, token, command, *args): + request = {"command": command, "args": list(args)} + return send_request(url, token, "custom", request) + + +def send_broadcast(url, token, sender, msg): + request = {"from": sender, "message": msg} + return send_request(url, token, "broadcast", request) + + +def main(): + args = parser.parse_args() + url = args.api_url + token = args.token + webhook = args.webhook + + print("Sending 5 minute warning") + r = send_command(url, token, "broadcast", "There will be an automatic server restart in 5 minutes.") + + if not r: + print("Failed to send 5 minute warning") + quit(1) + + time.sleep(60*4) + + print("Sending 1 minute warning") + r = send_command(url, token, "broadcast", "There will be an automatic server restart in 1 minute.") + + if not r: + print("Failed to send 1 minute broadcast") + quit(1) + + time.sleep(60) + + print("Sending final warnings") + for i in range(0, 5): + r = send_command(url, token, "broadcast", "The server is restarting get safe!") + if not r: + print("Failed to send final restart broadcast") + quit(1) + + time.sleep(2) + + if webhook: + send_discord_webhook(webhook, "Server is going offline for an automated restarted") + + print("Restarting") + r = send_command(url, token, "restart") + + if not r: + print("Failed to restart server") + quit(1) + + +if __name__ == "__main__": + main()