uploaded files
This commit is contained in:
parent
902c9b010b
commit
477e2c28b2
Binary file not shown.
|
@ -0,0 +1,152 @@
|
|||
# purpur_updater
|
||||
# Version: 1.0
|
||||
# Author: Willy
|
||||
|
||||
import os
|
||||
import os.path
|
||||
import glob
|
||||
import wget
|
||||
import pycron
|
||||
import requests
|
||||
import subprocess
|
||||
import time
|
||||
import hashlib
|
||||
import sys
|
||||
import yaml
|
||||
import signal
|
||||
import logging
|
||||
|
||||
from art import *
|
||||
from termcolor import colored
|
||||
from glob import glob
|
||||
from pwd import getpwnam
|
||||
from grp import getgrnam
|
||||
|
||||
# Messages
|
||||
message_contect_sucess = colored('HTTP connection sucessful!', 'green')
|
||||
message_finish = colored('Operation complete!', 'green')
|
||||
message_complete = colored('Complete!', 'green')
|
||||
message_standby = colored('On standby...', 'yellow')
|
||||
message_dryrun = colored('[WARN] Running in dryrun mode!', 'yellow')
|
||||
message_invaild_filename = colored('[ERROR] Invaild filename! Must contain .jar! Terminating!', 'red')
|
||||
message_contect_fail = colored('[ERROR] HTTP connection failed!', 'red')
|
||||
message_invaild_version = colored('[Error] Invaild minecraft version! Terminating!', 'red')
|
||||
|
||||
# Load config
|
||||
with open("purpur_updater_config.yml", "r") as b:
|
||||
config = yaml.safe_load(b)
|
||||
|
||||
cron_time = config['cron_time']
|
||||
version = config['version']
|
||||
restart_cmd = config['restart_command']
|
||||
rename = config['filename']
|
||||
verbose = config['verbose']
|
||||
file_permission = config['file_permission']
|
||||
permission = config['permission']
|
||||
file_ownership = config['file_ownership']
|
||||
ownership = config['ownership']
|
||||
ownership_group = config['ownership_group']
|
||||
|
||||
# Logging
|
||||
if (verbose) == True:
|
||||
logging.basicConfig(filename='purpur_updater_log.txt',
|
||||
format='%(asctime)s - %(message)s',
|
||||
level=logging.INFO)
|
||||
|
||||
logging.info("Started!")
|
||||
|
||||
# Check for invaild filename
|
||||
if not rename.endswith(".jar"):
|
||||
print(message_invaild_filename)
|
||||
logging.info("[ERROR] Invaild filename! Must contain .jar! Terminating!")
|
||||
sys.exit()
|
||||
|
||||
# Start
|
||||
Art=text2art('Purpur Updater')
|
||||
print(Art)
|
||||
print(message_standby)
|
||||
|
||||
while True:
|
||||
if pycron.is_now(cron_time): # Date/Time Cron Format
|
||||
|
||||
# HTTP Code
|
||||
try:
|
||||
test = requests.head("https://api.purpurmc.org")
|
||||
if (test.status_code) == 200 or 302:
|
||||
print(message_contect_sucess)
|
||||
logging.info("HTTP connection sucessful!")
|
||||
except requests.ConnectionError:
|
||||
print(message_contect_fail)
|
||||
logging.info("[ERROR] HTTP connection failed!")
|
||||
|
||||
# Download
|
||||
base_url = "https://api.purpurmc.org/v2/purpur/$version/latest/download"
|
||||
download_url = base_url.replace("$version", version)
|
||||
|
||||
http_version = requests.head(download_url)
|
||||
if (http_version.status_code) == 404:
|
||||
print(message_invaild_version)
|
||||
logging.info("[Error] Invaild minecraft version! Terminating!")
|
||||
sys.exit()
|
||||
|
||||
print('Downloading...')
|
||||
filename = wget.download(download_url)
|
||||
print('') # Spacing
|
||||
print(message_complete)
|
||||
logging.info("Download complete!")
|
||||
|
||||
# Rename Jar
|
||||
print("Renaming...")
|
||||
file_exists = os.path.exists(rename)
|
||||
if (file_exists) == True:
|
||||
os.remove(rename)
|
||||
|
||||
path = os.getcwd()
|
||||
f = glob(os.path.join(path,"*.jar"))[0]
|
||||
os.rename(f, os.path.join(path, rename))
|
||||
print(message_complete)
|
||||
logging.info("Renaming compelete!")
|
||||
|
||||
# Change file unix permission/owner
|
||||
if (file_permission) == True:
|
||||
print('Changing permissions...')
|
||||
os.chmod(rename, (permission))
|
||||
print(message_complete)
|
||||
logging.info("Permission change complete!")
|
||||
if (file_ownership) == True:
|
||||
print('Changing ownership...')
|
||||
try:
|
||||
uid = getpwnam(ownership).pw_uid
|
||||
except KeyError:
|
||||
uid = int(ownership)
|
||||
try:
|
||||
gid = getgrnam(ownership_group).gr_gid
|
||||
except KeyError:
|
||||
gid = int(ownership_group)
|
||||
os.chown(rename, uid, gid)
|
||||
print(message_complete)
|
||||
logging.info("Ownership change complete!")
|
||||
|
||||
# Restart Server
|
||||
print('Restarting server...')
|
||||
subprocess.call(restart_cmd, shell=True)
|
||||
print(message_complete)
|
||||
logging.info("Restarting complete!")
|
||||
|
||||
# Print MD5
|
||||
file_exists_md5 = os.path.exists(rename)
|
||||
if (file_exists_md5) == True:
|
||||
with open(rename, "rb") as f:
|
||||
contents = f.read()
|
||||
checksum = hashlib.md5(contents).hexdigest()
|
||||
print("MD5: " + checksum)
|
||||
logging.info("MD5: " + checksum)
|
||||
|
||||
print(message_finish)
|
||||
logging.info("Operation complete!")
|
||||
print(message_standby)
|
||||
logging.info("On standby...")
|
||||
|
||||
time.sleep(30)
|
||||
|
||||
time.sleep(15)
|
|
@ -0,0 +1,38 @@
|
|||
# purpur_updater config
|
||||
# Version: 1.0
|
||||
# Author: Willy
|
||||
|
||||
# How often do you want to run the updater in cron time?
|
||||
# The default is every 1 minute '*/1 * * * *'.
|
||||
cron_time: '*/1 * * * *'
|
||||
|
||||
# Minecraft version of the jar you want to update.
|
||||
# Example: '1.20' '1.20.1'
|
||||
version: '1.20.1'
|
||||
|
||||
# Restart the command or script for your server.
|
||||
# Warning: If your restart command requires elevated privileges, you must run the script as root (sudo/doas python3 purpur_updater.py).
|
||||
# Example: Systemd: 'sudo systemctl restart my-minecraft-server'
|
||||
restart_command: 'systemctl restart my-minecraft-server'
|
||||
|
||||
# Filename that the jar will always be named after.
|
||||
filename: 'purpur.jar'
|
||||
|
||||
# Unix file permissions.
|
||||
# Change the permissions of the downloaded file.
|
||||
# True/False (Leave False if on Windows.)
|
||||
# Example: 777
|
||||
file_permission: False
|
||||
permission: 777
|
||||
|
||||
# Unix file ownership.
|
||||
# Change the ownership of the downloaded file. Must be a vaild user:group.
|
||||
# True/False (Leave False if on Windows.)
|
||||
# Example: user
|
||||
file_ownership: False
|
||||
ownership: 'user'
|
||||
ownership_group: 'user'
|
||||
|
||||
# Enable logging to "purpur_updater_log.txt" in the directory where the script is run.
|
||||
# True/False
|
||||
verbose: False
|
|
@ -0,0 +1,6 @@
|
|||
art==5.7
|
||||
pycron==3.0.0
|
||||
PyYAML==6.0.1
|
||||
Requests==2.31.0
|
||||
termcolor==1.1.0
|
||||
wget==3.2
|
Loading…
Reference in New Issue