Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import threading
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import time
|
| 6 |
+
import shutil
|
| 7 |
+
|
| 8 |
+
# ========================
|
| 9 |
+
# CONFIG
|
| 10 |
+
# ========================
|
| 11 |
+
MINECRAFT_VERSION = "1.21.1"
|
| 12 |
+
PAPER_JAR_URL = "https://api.papermc.io/v2/projects/paper/versions/1.21.1/builds/70/downloads/paper-1.21.1-70.jar"
|
| 13 |
+
SERVER_DIR = "mcserver"
|
| 14 |
+
JAR_FILE = f"{SERVER_DIR}/server.jar"
|
| 15 |
+
PLAYIT_URL = "https://github.com/playit-cloud/playit-agent/releases/latest/download/playit-linux-amd64"
|
| 16 |
+
PLAYIT_BIN = f"{SERVER_DIR}/playit"
|
| 17 |
+
MCMYADMIN_URL = "https://mcmya.s3.amazonaws.com/McMyAdmin2-Linux.tar.gz" # public link
|
| 18 |
+
MCMYADMIN_DIR = "McMyAdmin"
|
| 19 |
+
GOOGLE_DRIVE_MOUNT = "/content/drive"
|
| 20 |
+
|
| 21 |
+
# ========================
|
| 22 |
+
# INSTALL DEPENDENCIES
|
| 23 |
+
# ========================
|
| 24 |
+
def setup_environment():
|
| 25 |
+
os.makedirs(SERVER_DIR, exist_ok=True)
|
| 26 |
+
|
| 27 |
+
# Install Java if missing
|
| 28 |
+
subprocess.run("apt-get update && apt-get install -y openjdk-21-jre-headless", shell=True)
|
| 29 |
+
|
| 30 |
+
# Download PaperMC
|
| 31 |
+
if not os.path.exists(JAR_FILE):
|
| 32 |
+
subprocess.run(f"wget -O {JAR_FILE} {PAPER_JAR_URL}", shell=True)
|
| 33 |
+
|
| 34 |
+
# Accept EULA
|
| 35 |
+
with open(f"{SERVER_DIR}/eula.txt", "w") as f:
|
| 36 |
+
f.write("eula=true\n")
|
| 37 |
+
|
| 38 |
+
# Download Playit
|
| 39 |
+
if not os.path.exists(PLAYIT_BIN):
|
| 40 |
+
subprocess.run(f"wget -O {PLAYIT_BIN} {PLAYIT_URL} && chmod +x {PLAYIT_BIN}", shell=True)
|
| 41 |
+
|
| 42 |
+
# Install McMyAdmin2
|
| 43 |
+
if not os.path.exists(MCMYADMIN_DIR):
|
| 44 |
+
subprocess.run(f"wget -O McMyAdmin2.tar.gz {MCMYADMIN_URL}", shell=True)
|
| 45 |
+
subprocess.run("mkdir -p McMyAdmin && tar -xzf McMyAdmin2.tar.gz -C McMyAdmin", shell=True)
|
| 46 |
+
subprocess.run("chmod +x McMyAdmin/McMyAdmin", shell=True)
|
| 47 |
+
|
| 48 |
+
setup_environment()
|
| 49 |
+
|
| 50 |
+
# ========================
|
| 51 |
+
# SERVER CONTROL
|
| 52 |
+
# ========================
|
| 53 |
+
server_process = None
|
| 54 |
+
playit_process = None
|
| 55 |
+
mcmya_process = None
|
| 56 |
+
|
| 57 |
+
def start_server():
|
| 58 |
+
global server_process, playit_process, mcmya_process
|
| 59 |
+
if server_process is None:
|
| 60 |
+
server_process = subprocess.Popen(
|
| 61 |
+
["java", "-Xms1G", "-Xmx2G", "-jar", JAR_FILE, "--nogui"],
|
| 62 |
+
cwd=SERVER_DIR,
|
| 63 |
+
stdout=subprocess.PIPE,
|
| 64 |
+
stderr=subprocess.STDOUT
|
| 65 |
+
)
|
| 66 |
+
# Start Playit tunnel
|
| 67 |
+
playit_process = subprocess.Popen(
|
| 68 |
+
[PLAYIT_BIN],
|
| 69 |
+
cwd=SERVER_DIR,
|
| 70 |
+
stdout=subprocess.PIPE,
|
| 71 |
+
stderr=subprocess.STDOUT
|
| 72 |
+
)
|
| 73 |
+
# Start McMyAdmin Panel
|
| 74 |
+
mcmya_process = subprocess.Popen(
|
| 75 |
+
["./McMyAdmin"],
|
| 76 |
+
cwd=MCMYADMIN_DIR,
|
| 77 |
+
stdout=subprocess.PIPE,
|
| 78 |
+
stderr=subprocess.STDOUT
|
| 79 |
+
)
|
| 80 |
+
return "โ
Minecraft server + Playit + McMyAdmin started!"
|
| 81 |
+
return "โ ๏ธ Server already running."
|
| 82 |
+
|
| 83 |
+
def stop_server():
|
| 84 |
+
global server_process, playit_process, mcmya_process
|
| 85 |
+
if server_process:
|
| 86 |
+
server_process.terminate()
|
| 87 |
+
playit_process.terminate()
|
| 88 |
+
mcmya_process.terminate()
|
| 89 |
+
server_process, playit_process, mcmya_process = None, None, None
|
| 90 |
+
return "๐ Server stopped."
|
| 91 |
+
return "โ ๏ธ Server not running."
|
| 92 |
+
|
| 93 |
+
def server_logs():
|
| 94 |
+
if server_process:
|
| 95 |
+
try:
|
| 96 |
+
return server_process.stdout.read().decode("utf-8", errors="ignore")
|
| 97 |
+
except:
|
| 98 |
+
return "โ ๏ธ No logs available yet."
|
| 99 |
+
return "โ ๏ธ Server not running."
|
| 100 |
+
|
| 101 |
+
# ========================
|
| 102 |
+
# GRADIO UI
|
| 103 |
+
# ========================
|
| 104 |
+
with gr.Blocks() as demo:
|
| 105 |
+
gr.Markdown("# ๐ Minecraft Server on Hugging Face (with McMyAdmin + Playit)")
|
| 106 |
+
|
| 107 |
+
start_btn = gr.Button("๐ Start Server")
|
| 108 |
+
stop_btn = gr.Button("๐ Stop Server")
|
| 109 |
+
log_box = gr.Textbox(label="Server Logs", lines=20)
|
| 110 |
+
|
| 111 |
+
start_btn.click(start_server, outputs=log_box)
|
| 112 |
+
stop_btn.click(stop_server, outputs=log_box)
|
| 113 |
+
|
| 114 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|