Spaces:
Paused
Paused
Update app.py
Browse files1print("Hello World!")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276print("STARTED")# Importsimport gradio as grimport spaces, requests, os, time, threading, subprocess, shutil, tarfile, urllib.request, jsonfrom huggingface_hub import hf_hub_download, upload_file# ========================# PATH SETUP (FIXED)# ========================ROOT_DIR = os.path.dirname(os.path.abspath(__file__))BASE_DIR = os.path.join(ROOT_DIR, "minecraft_server")WORLD_PATH = os.path.join(BASE_DIR, "world")NETHER_PATH = os.path.join(BASE_DIR, "world_nether")END_PATH = os.path.join(BASE_DIR, "world_the_end")print("[DEBUG] ROOT:", ROOT_DIR)print("[DEBUG] BASE:", BASE_DIR)# ========================# ENV VARIABLES# ========================ADMIN_PASSWORD = os.environ.get("ADMIN_PASSWORD", "")HF_TOKEN = os.environ.get("HF_TOKEN", "")PLAYIT_AUTH_KEY = os.environ.get("PLAYIT_AUTH_KEY", "")HF_REPO_ID = "Dalleon/McSaves"WORLD_ID = "world_default"# ========================# GLOBALS# ========================SERVER_PROCESS = NoneTCP_PROCESS = NonePUBLIC_TCP = NoneSERVER_ONLINE = FalseSERVER_OUTPUT_BUFFER = []# ========================# JAVA INSTALL (fallback)# ========================def install_java21_local():jdk_url = "https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz"download_path = os.path.join(BASE_DIR, "jdk21.tar.gz")print("[JAVA] Downloading...")urllib.request.urlretrieve(jdk_url, download_path)extract_dir = os.path.join(BASE_DIR, "jdk21")os.makedirs(extract_dir, exist_ok=True)with tarfile.open(download_path, "r:gz") as tar:tar.extractall(path=extract_dir)for item in os.listdir(extract_dir):if item.startswith("jdk-21"):java_home = os.path.join(extract_dir, item)os.environ["JAVA_HOME"] = java_homeos.environ["PATH"] = os.path.join(java_home, "bin") + ":" + os.environ["PATH"]print("[JAVA] Installed:", java_home)return# ========================# DOWNLOAD PAPER# ========================def download_paper():jar_path = os.path.join(BASE_DIR, "paper.jar")if os.path.exists(jar_path):returnprint("[PAPER] Downloading...")url = "https://api.papermc.io/v2/projects/paper/versions/1.20.6/builds/latest/downloads/paper-1.20.6-latest.jar"urllib.request.urlretrieve(url, jar_path)# ========================# SERVER OUTPUT READER# ========================def read_server_output():global SERVER_PROCESSwhile True:if SERVER_PROCESS and SERVER_PROCESS.stdout:line = SERVER_PROCESS.stdout.readline()if line:print("[MC]", line.strip())SERVER_OUTPUT_BUFFER.append(line)time.sleep(0.1)# ========================# SAVE WORLD# ========================def save_world():global SERVER_PROCESSprint("[SAVE] Saving world...")if SERVER_PROCESS:SERVER_PROCESS.stdin.write("save-off\n")SERVER_PROCESS.stdin.flush()SERVER_PROCESS.stdin.write("save-all\n")SERVER_PROCESS.stdin.flush()time.sleep(10)SERVER_PROCESS.stdin.write("save-on\n")SERVER_PROCESS.stdin.flush()tar_path = os.path.join(BASE_DIR, "world.tar.gz")with tarfile.open(tar_path, "w:gz") as tar:if os.path.exists(WORLD_PATH):tar.add(WORLD_PATH, arcname="world")if os.path.exists(NETHER_PATH):tar.add(NETHER_PATH, arcname="world_nether")if os.path.exists(END_PATH):tar.add(END_PATH, arcname="world_the_end")upload_file(path_or_fileobj=tar_path,path_in_repo="world.tar.gz",repo_id=HF_REPO_ID,token=HF_TOKEN,repo_type="model")print("[SAVE] Done.")# ========================# LOAD WORLD# ========================def load_world():print("[LOAD] Loading world...")try:file_path = hf_hub_download(repo_id=HF_REPO_ID,filename="world.tar.gz",token=HF_TOKEN)except:print("[LOAD] No backup found.")returnif os.path.exists(WORLD_PATH):shutil.rmtree(WORLD_PATH)with tarfile.open(file_path, "r:gz") as tar:tar.extractall(BASE_DIR)print("[LOAD] Done.")# ========================# PLAYIT TUNNEL (FIXED - NON-BLOCKING)# ========================def start_tcp():global PUBLIC_TCP, SERVER_ONLINE, TCP_PROCESSplayit_path = os.path.join(BASE_DIR, "playit")if not os.path.exists(playit_path):print("[PLAYIT] Downloading...")url = "https://github.com/playit-cloud/playit-agent/releases/latest/download/playit-linux-amd64"urllib.request.urlretrieve(url, playit_path)os.chmod(playit_path, 0o755)if not PLAYIT_AUTH_KEY:print("[PLAYIT] Missing auth key")return# ✅ FIX: fully detached process (NO PIPE, NO READLINE, NO FREEZE)TCP_PROCESS = subprocess.Popen([playit_path, "--secret", PLAYIT_AUTH_KEY],cwd=BASE_DIR,stdin=subprocess.DEVNULL,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,start_new_session=True)print("[PLAYIT] Agent started safely in background")# small delay so it can initialize without blocking HFtime.sleep(5)# ❗ IMPORTANT: no blocking log parsing anymorePUBLIC_TCP = "Check Playit dashboard for address"SERVER_ONLINE = Trueprint("[PLAYIT] Ready (no blocking, HF-safe)")# ========================# START SERVER# ========================def start_server():global SERVER_PROCESSload_world()if shutil.which("java") is None:install_java21_local()if shutil.which("java") is None:print("[ERROR] Java not found")returndownload_paper()with open(os.path.join(BASE_DIR, "eula.txt"), "w") as f:f.write("eula=true")SERVER_PROCESS = subprocess.Popen([shutil.which("java"),"-Xms512M","-Xmx1G","-jar","paper.jar","--nogui"],cwd=BASE_DIR,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,text=True)threading.Thread(target=read_server_output, daemon=True).start()time.sleep(5)start_tcp()# ========================# COMMAND EXEC# ========================def execute_command(password, cmd):if password != ADMIN_PASSWORD:return "Wrong password"if not SERVER_PROCESS:return "Server not running"SERVER_PROCESS.stdin.write(cmd + "\n")SERVER_PROCESS.stdin.flush()return "Executed"# ========================# GET ADDRESS# ========================def get_address():if not SERVER_ONLINE:return "Server starting..."return PUBLIC_TCP or "No address yet"# ========================# INIT# ========================threading.Thread(target=start_server, daemon=True).start()# ========================# UI# ========================with gr.Blocks() as app:gr.Markdown("## ⛏️ Minecraft Server")address = gr.Textbox(label="Server Address")btn = gr.Button("Get Address")btn.click(fn=get_address, outputs=address)app.launch()
app.py+276-1