101 lines
4.9 KiB
Python
101 lines
4.9 KiB
Python
import os, json, shutil, zipfile, subprocess
|
|
from urllib.parse import urlunsplit
|
|
|
|
PLUGINS_DIR = "/var/lib/pterodactyl/volumes/bd102f43-4033-4c60-9922-53c436330f21/plugins"
|
|
BACKUP_DIR = "/root/backups_plugins_jars"
|
|
os.makedirs(BACKUP_DIR, exist_ok=True)
|
|
|
|
def build_api_url(host_parts, path_str):
|
|
host = ".".join(host_parts)
|
|
return urlunsplit(("https", host, path_str, "", ""))
|
|
|
|
def curl_get_json(url):
|
|
cmd = ["curl", "-4", "-fsSL", "--connect-timeout", "10", "-A", "FlexiCraft-Updater/1.0", url]
|
|
res = subprocess.run(cmd, capture_output=True, text=True)
|
|
if res.returncode != 0:
|
|
raise RuntimeError(f"Echec requete API (curl code {res.returncode}) : {res.stderr.strip()}")
|
|
try:
|
|
return json.loads(res.stdout)
|
|
except json.JSONDecodeError as e:
|
|
raise RuntimeError(f"Reponse API non-JSON : {e}")
|
|
|
|
def download_and_verify(url, tmp_path):
|
|
cmd = ["curl", "-4", "-fL", "--connect-timeout", "10", "--progress-bar", "-A", "FlexiCraft-Updater/1.0", url, "-o", tmp_path]
|
|
res = subprocess.run(cmd)
|
|
if res.returncode != 0:
|
|
raise RuntimeError(f"Echec du telechargement du fichier (curl code {res.returncode})")
|
|
if not os.path.exists(tmp_path) or os.path.getsize(tmp_path) == 0:
|
|
raise RuntimeError("Fichier telecharge vide (0 octet)")
|
|
if not zipfile.is_zipfile(tmp_path):
|
|
raise RuntimeError("Le fichier telecharge n'est pas un JAR/ZIP valide (probable page HTML)")
|
|
with zipfile.ZipFile(tmp_path, "r") as z:
|
|
if not any(n in z.namelist() for n in ("plugin.yml", "paper-plugin.yml")):
|
|
raise RuntimeError("Archive ZIP valide mais descripteur plugin.yml absent")
|
|
|
|
def update_viaversion():
|
|
print("▶ [1/2] Mise a jour de ViaVersion (Latest Stable Release sur Modrinth)...")
|
|
try:
|
|
mr_url = build_api_url(("api", "modrinth", "com"), "/v2/project/viaversion/version")
|
|
vv_versions = curl_get_json(mr_url)
|
|
if not isinstance(vv_versions, list) or not vv_versions:
|
|
raise RuntimeError("Liste de versions vide renvoyee par Modrinth")
|
|
vv_stable = next((v for v in vv_versions if v.get("version_type") == "release"), None)
|
|
if not vv_stable:
|
|
raise RuntimeError("Aucune version stable (release) trouvee sur Modrinth")
|
|
files = vv_stable.get("files", [])
|
|
if not files:
|
|
raise RuntimeError("Aucun fichier attache a la release Modrinth")
|
|
f_primary = next((x for x in files if x.get("primary")), next(iter(files)))
|
|
ver_num = vv_stable.get("version_number")
|
|
dl_url = f_primary.get("url")
|
|
dl_name = f_primary.get("filename")
|
|
if not dl_url or not dl_name:
|
|
raise RuntimeError("URL ou nom de fichier manquant dans les metadonnees Modrinth")
|
|
|
|
tmp_jar = f"/tmp/{dl_name}"
|
|
print(f" -> Version stable trouvee : {ver_num} ({dl_name})")
|
|
download_and_verify(dl_url, tmp_jar)
|
|
|
|
for old in os.listdir(PLUGINS_DIR):
|
|
if old.startswith("ViaVersion-") and old.endswith(".jar"):
|
|
shutil.move(f"{PLUGINS_DIR}/{old}", f"{BACKUP_DIR}/{old}")
|
|
shutil.move(tmp_jar, f"{PLUGINS_DIR}/{dl_name}")
|
|
print(f" ✅ Succes : ViaVersion mis a jour vers {dl_name} (ancien sauvegarde dans {BACKUP_DIR})")
|
|
except Exception as e:
|
|
print(f" ❌ Echec ViaVersion : {e}")
|
|
|
|
def update_fastloginplus():
|
|
print("\n▶ [2/2] Mise a jour de FastLoginPlus (Latest Stable Release sur GitHub)...")
|
|
try:
|
|
gh_url = build_api_url(("api", "github", "com"), "/repos/Hayston1001/FastLoginPlus/releases/latest")
|
|
flp_rel = curl_get_json(gh_url)
|
|
if not isinstance(flp_rel, dict):
|
|
raise RuntimeError("Format de release GitHub inattendu")
|
|
assets = flp_rel.get("assets", [])
|
|
if not assets:
|
|
raise RuntimeError("Aucun asset attache a la release GitHub")
|
|
flp_asset = next((a for a in assets if "bukkit" in a.get("name", "").lower() and a.get("name", "").endswith(".jar")), None)
|
|
if not flp_asset:
|
|
avail = [a.get("name") for a in assets]
|
|
raise RuntimeError(f"Aucun asset Bukkit .jar trouve parmi : {avail}")
|
|
|
|
dl_url = flp_asset.get("browser_download_url")
|
|
dl_name = flp_asset.get("name")
|
|
tmp_jar = f"/tmp/{dl_name}"
|
|
print(f" -> Version stable trouvee : {flp_rel.get('tag_name')} ({dl_name})")
|
|
download_and_verify(dl_url, tmp_jar)
|
|
|
|
for old in os.listdir(PLUGINS_DIR):
|
|
if old.startswith("FastLoginPlus") and old.endswith(".jar"):
|
|
shutil.move(f"{PLUGINS_DIR}/{old}", f"{BACKUP_DIR}/{old}")
|
|
shutil.move(tmp_jar, f"{PLUGINS_DIR}/{dl_name}")
|
|
print(f" ✅ Succes : FastLoginPlus mis a jour vers {dl_name} (ancien sauvegarde dans {BACKUP_DIR})")
|
|
except Exception as e:
|
|
print(f" ❌ Echec FastLoginPlus : {e}")
|
|
|
|
flp_db = f"{PLUGINS_DIR}/FastLoginPlus/FastLogin.db"
|
|
if os.path.exists(flp_db):
|
|
shutil.copy2(flp_db, f"{BACKUP_DIR}/FastLogin.db.bak")
|
|
|
|
update_viaversion()
|
|
update_fastloginplus()
|