Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -192,18 +192,17 @@ def check_java():
|
|
192 |
try:
|
193 |
result = subprocess.run([java_path, "-version"], capture_output=True, text=True)
|
194 |
print(f"Found Java: {java_path}")
|
195 |
-
print(result.stderr.split('\n')[0])
|
196 |
return java_path
|
197 |
except:
|
198 |
pass
|
199 |
|
200 |
-
# Check our custom Java installation
|
201 |
custom_java = JAVA_DIR / "bin" / "java"
|
202 |
if custom_java.exists():
|
203 |
try:
|
204 |
result = subprocess.run([str(custom_java), "-version"], capture_output=True, text=True)
|
205 |
print(f"Found custom Java: {custom_java}")
|
206 |
-
print(result.stderr.split('\n')[0])
|
207 |
return str(custom_java)
|
208 |
except:
|
209 |
pass
|
@@ -255,22 +254,18 @@ def install_java():
|
|
255 |
print(f"No Java download available for platform: {platform_key}")
|
256 |
return False
|
257 |
|
258 |
-
# Download Java
|
259 |
java_archive = DATA_DIR / "java.tar.gz"
|
260 |
if not download_file(java_url, java_archive):
|
261 |
return False
|
262 |
|
263 |
-
# Extract Java
|
264 |
print("Extracting Java...")
|
265 |
try:
|
266 |
import tarfile
|
267 |
with tarfile.open(java_archive, 'r:gz') as tar:
|
268 |
-
# Extract to temporary directory first
|
269 |
temp_dir = DATA_DIR / "java_temp"
|
270 |
temp_dir.mkdir(exist_ok=True)
|
271 |
tar.extractall(temp_dir)
|
272 |
|
273 |
-
# Find the extracted JDK directory (usually has a version number)
|
274 |
extracted_dirs = [d for d in temp_dir.iterdir() if d.is_dir() and d.name.startswith('jdk')]
|
275 |
if not extracted_dirs:
|
276 |
print("Could not find extracted JDK directory")
|
@@ -278,18 +273,15 @@ def install_java():
|
|
278 |
|
279 |
jdk_dir = extracted_dirs[0]
|
280 |
|
281 |
-
# Move to final location
|
282 |
if JAVA_DIR.exists():
|
283 |
shutil.rmtree(JAVA_DIR)
|
284 |
shutil.move(str(jdk_dir), str(JAVA_DIR))
|
285 |
|
286 |
-
# Clean up
|
287 |
shutil.rmtree(temp_dir)
|
288 |
java_archive.unlink()
|
289 |
|
290 |
print(f"✓ Java installed to: {JAVA_DIR}")
|
291 |
|
292 |
-
# Make java executable
|
293 |
java_bin = JAVA_DIR / "bin" / "java"
|
294 |
java_bin.chmod(0o755)
|
295 |
|
@@ -300,12 +292,8 @@ def install_java():
|
|
300 |
return False
|
301 |
|
302 |
def setup_minecraft_server():
|
303 |
-
"""Set up the Minecraft server in persistent storage"""
|
304 |
-
|
305 |
-
# Ensure the data directory exists
|
306 |
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
307 |
|
308 |
-
# Check and install Java if needed
|
309 |
java_path = check_java()
|
310 |
if not java_path:
|
311 |
print("Java not found. Installing Java...")
|
@@ -317,7 +305,6 @@ def setup_minecraft_server():
|
|
317 |
print("Java installation failed or not working.")
|
318 |
return False
|
319 |
|
320 |
-
# Download the server JAR if it doesn't exist
|
321 |
if not JAR_PATH.exists():
|
322 |
print(f"Downloading Minecraft server to {JAR_PATH}")
|
323 |
if not download_file(PAPER_JAR_URL, JAR_PATH):
|
@@ -325,7 +312,6 @@ def setup_minecraft_server():
|
|
325 |
else:
|
326 |
print(f"Server JAR already exists at {JAR_PATH}")
|
327 |
|
328 |
-
# Create server.properties file if it doesn't exist
|
329 |
server_properties_path = DATA_DIR / "server.properties"
|
330 |
if not server_properties_path.exists():
|
331 |
print("Creating server.properties...")
|
@@ -333,11 +319,13 @@ def setup_minecraft_server():
|
|
333 |
server-port=25565
|
334 |
gamemode=survival
|
335 |
difficulty=easy
|
336 |
-
spawn-protection=
|
337 |
-
max-players=
|
338 |
-
online-mode=
|
339 |
white-list=false
|
340 |
-
motd=Hugging Face Minecraft Server
|
|
|
|
|
341 |
"""
|
342 |
with open(server_properties_path, 'w') as f:
|
343 |
f.write(server_properties)
|
@@ -390,7 +378,6 @@ def start_server():
|
|
390 |
bufsize=1
|
391 |
)
|
392 |
|
393 |
-
# Print server output in real-time
|
394 |
for line in process.stdout:
|
395 |
print(line.strip())
|
396 |
|
@@ -411,24 +398,20 @@ if __name__ == "__main__":
|
|
411 |
print("=== Minecraft Server Setup ===")
|
412 |
|
413 |
try:
|
414 |
-
# Step 1: Setup Minecraft server
|
415 |
if not setup_minecraft_server():
|
416 |
print("Setup failed. Cannot start server.")
|
417 |
sys.exit(1)
|
418 |
|
419 |
-
# Step 2: Setup ngrok
|
420 |
print("\n=== Setting up ngrok ===")
|
421 |
if not setup_ngrok():
|
422 |
print("ngrok setup failed. Server will start but won't be accessible externally.")
|
423 |
tunnel = None
|
424 |
else:
|
425 |
-
# Step 3: Start ngrok tunnel
|
426 |
print("\n=== Starting ngrok tunnel ===")
|
427 |
tunnel, tunnel_url = start_ngrok_tunnel()
|
428 |
if not tunnel:
|
429 |
print("Failed to start ngrok tunnel. Server will start but won't be accessible externally.")
|
430 |
|
431 |
-
# Step 4: Start the server
|
432 |
print("\n=== Starting Minecraft Server ===")
|
433 |
start_server()
|
434 |
|
|
|
192 |
try:
|
193 |
result = subprocess.run([java_path, "-version"], capture_output=True, text=True)
|
194 |
print(f"Found Java: {java_path}")
|
195 |
+
print(result.stderr.split('\n')[0])
|
196 |
return java_path
|
197 |
except:
|
198 |
pass
|
199 |
|
|
|
200 |
custom_java = JAVA_DIR / "bin" / "java"
|
201 |
if custom_java.exists():
|
202 |
try:
|
203 |
result = subprocess.run([str(custom_java), "-version"], capture_output=True, text=True)
|
204 |
print(f"Found custom Java: {custom_java}")
|
205 |
+
print(result.stderr.split('\n')[0])
|
206 |
return str(custom_java)
|
207 |
except:
|
208 |
pass
|
|
|
254 |
print(f"No Java download available for platform: {platform_key}")
|
255 |
return False
|
256 |
|
|
|
257 |
java_archive = DATA_DIR / "java.tar.gz"
|
258 |
if not download_file(java_url, java_archive):
|
259 |
return False
|
260 |
|
|
|
261 |
print("Extracting Java...")
|
262 |
try:
|
263 |
import tarfile
|
264 |
with tarfile.open(java_archive, 'r:gz') as tar:
|
|
|
265 |
temp_dir = DATA_DIR / "java_temp"
|
266 |
temp_dir.mkdir(exist_ok=True)
|
267 |
tar.extractall(temp_dir)
|
268 |
|
|
|
269 |
extracted_dirs = [d for d in temp_dir.iterdir() if d.is_dir() and d.name.startswith('jdk')]
|
270 |
if not extracted_dirs:
|
271 |
print("Could not find extracted JDK directory")
|
|
|
273 |
|
274 |
jdk_dir = extracted_dirs[0]
|
275 |
|
|
|
276 |
if JAVA_DIR.exists():
|
277 |
shutil.rmtree(JAVA_DIR)
|
278 |
shutil.move(str(jdk_dir), str(JAVA_DIR))
|
279 |
|
|
|
280 |
shutil.rmtree(temp_dir)
|
281 |
java_archive.unlink()
|
282 |
|
283 |
print(f"✓ Java installed to: {JAVA_DIR}")
|
284 |
|
|
|
285 |
java_bin = JAVA_DIR / "bin" / "java"
|
286 |
java_bin.chmod(0o755)
|
287 |
|
|
|
292 |
return False
|
293 |
|
294 |
def setup_minecraft_server():
|
|
|
|
|
|
|
295 |
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
296 |
|
|
|
297 |
java_path = check_java()
|
298 |
if not java_path:
|
299 |
print("Java not found. Installing Java...")
|
|
|
305 |
print("Java installation failed or not working.")
|
306 |
return False
|
307 |
|
|
|
308 |
if not JAR_PATH.exists():
|
309 |
print(f"Downloading Minecraft server to {JAR_PATH}")
|
310 |
if not download_file(PAPER_JAR_URL, JAR_PATH):
|
|
|
312 |
else:
|
313 |
print(f"Server JAR already exists at {JAR_PATH}")
|
314 |
|
|
|
315 |
server_properties_path = DATA_DIR / "server.properties"
|
316 |
if not server_properties_path.exists():
|
317 |
print("Creating server.properties...")
|
|
|
319 |
server-port=25565
|
320 |
gamemode=survival
|
321 |
difficulty=easy
|
322 |
+
spawn-protection=0
|
323 |
+
max-players=64
|
324 |
+
online-mode=true
|
325 |
white-list=false
|
326 |
+
motd=Hugging Face Spaces Minecraft Server
|
327 |
+
enable-query=true
|
328 |
+
query.port=25565
|
329 |
"""
|
330 |
with open(server_properties_path, 'w') as f:
|
331 |
f.write(server_properties)
|
|
|
378 |
bufsize=1
|
379 |
)
|
380 |
|
|
|
381 |
for line in process.stdout:
|
382 |
print(line.strip())
|
383 |
|
|
|
398 |
print("=== Minecraft Server Setup ===")
|
399 |
|
400 |
try:
|
|
|
401 |
if not setup_minecraft_server():
|
402 |
print("Setup failed. Cannot start server.")
|
403 |
sys.exit(1)
|
404 |
|
|
|
405 |
print("\n=== Setting up ngrok ===")
|
406 |
if not setup_ngrok():
|
407 |
print("ngrok setup failed. Server will start but won't be accessible externally.")
|
408 |
tunnel = None
|
409 |
else:
|
|
|
410 |
print("\n=== Starting ngrok tunnel ===")
|
411 |
tunnel, tunnel_url = start_ngrok_tunnel()
|
412 |
if not tunnel:
|
413 |
print("Failed to start ngrok tunnel. Server will start but won't be accessible externally.")
|
414 |
|
|
|
415 |
print("\n=== Starting Minecraft Server ===")
|
416 |
start_server()
|
417 |
|