158 lines
5.0 KiB
Python
158 lines
5.0 KiB
Python
# purpur_updater
|
|
# 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
|
|
if 'linux' in sys.platform:
|
|
from pwd import getpwnam
|
|
from grp import getgrnam
|
|
|
|
# Messages
|
|
message_connect_success = 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_connect_fail = colored('[ERROR] HTTP connection failed!', 'red')
|
|
message_connect_fail_retry = colored('Retrying connection in 5 seconds...', '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
|
|
while True:
|
|
try:
|
|
test = requests.head("https://api.purpurmc.org")
|
|
if (test.status_code) == 200 or 302:
|
|
print(message_connect_success)
|
|
logging.info("HTTP connection sucessful!")
|
|
break
|
|
except requests.exceptions.RequestException:
|
|
print(message_connect_fail)
|
|
logging.info("[ERROR] HTTP connection failed!")
|
|
print(message_connect_fail_retry)
|
|
logging.info("Retrying connection in 5 seconds...")
|
|
time.sleep(5)
|
|
|
|
# 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) |