Merge pull request 'Some nits' (#1) from Etzelia/restart-script:stuff into master

Reviewed-on: https://git.canopymc.net/ZeroHD/restart-script/pulls/1
master
Joey Hines 2021-07-26 23:23:56 +00:00
commit 29087013b5
3 changed files with 18 additions and 10 deletions

5
.gitignore vendored 100644
View File

@ -0,0 +1,5 @@
# PyCharm
.idea/
# virtualenv
venv/

View File

@ -12,12 +12,12 @@ 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
if ! screen -list | grep -q $SCREEN_NAME; then
screen -dmS $SCREEN_NAME
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"
screen -S $SCREEN_NAME -p 0 -X stuff "^Mcd $SERVER_DIRECTORY^M"
screen -S $SCREEN_NAME -p 0 -X stuff "./$STARTUP_SCRIPT^M"
```

View File

@ -9,24 +9,25 @@ 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):
def send_discord_webhook(url: str, msg: str) -> bool:
request = {"content": msg}
r = requests.post(url, json=request)
return r.ok
def send_request(url, token, endpoint, request):
def send_request(url: str, token: str, endpoint: str, request: dict) -> bool:
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):
def send_command(url: str, token: str, command: str, *args) -> bool:
request = {"command": command, "args": list(args)}
return send_request(url, token, "custom", request)
def send_broadcast(url, token, sender, msg):
def send_broadcast(url: str, token: str, sender: str, msg: str) -> bool:
request = {"from": sender, "message": msg}
return send_request(url, token, "broadcast", request)
@ -57,7 +58,7 @@ def main():
print("Sending final warnings")
for i in range(0, 5):
r = send_command(url, token, "broadcast", "The server is restarting get safe!")
r = send_command(url, token, "broadcast", "The server is restarting, get safe!")
if not r:
print("Failed to send final restart broadcast")
quit(1)
@ -65,7 +66,9 @@ def main():
time.sleep(2)
if webhook:
send_discord_webhook(webhook, "Server is going offline for an automated restarted")
r = send_discord_webhook(webhook, "Server is going offline for an automated restart.")
if not r:
print("Failed to send webhook")
print("Restarting")
r = send_command(url, token, "restart")