Initial commit

pull/1/head
Joey Hines 2021-07-25 16:05:56 -06:00
commit a8e12cc8bd
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
4 changed files with 110 additions and 0 deletions

7
LICENSE 100644
View File

@ -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.

23
README.md 100644
View File

@ -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"
```

1
requirements.txt 100644
View File

@ -0,0 +1 @@
requests~=2.26.0

79
server-restart.py 100644
View File

@ -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()