Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import time
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def run_bg(command):
|
| 7 |
+
# Run command in background and ignore output to keep logs clean
|
| 8 |
+
return subprocess.Popen(
|
| 9 |
+
command,
|
| 10 |
+
shell=True,
|
| 11 |
+
env={**os.environ, "DISPLAY": ":1"}
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def main():
|
| 15 |
+
print(">>> Initializing Environment...")
|
| 16 |
+
|
| 17 |
+
# 1. Configure KasmVNC (Disable SSL for HF Spaces, Set Port)
|
| 18 |
+
# We write the config file dynamically
|
| 19 |
+
vnc_dir = os.path.join(os.environ['HOME'], '.vnc')
|
| 20 |
+
os.makedirs(vnc_dir, exist_ok=True)
|
| 21 |
+
|
| 22 |
+
yaml_config = """
|
| 23 |
+
network:
|
| 24 |
+
protocol: http
|
| 25 |
+
interface: 0.0.0.0
|
| 26 |
+
websocket_port: 7860
|
| 27 |
+
ssl:
|
| 28 |
+
require_ssl: false
|
| 29 |
+
"""
|
| 30 |
+
with open(os.path.join(vnc_dir, 'kasmvnc.yaml'), 'w') as f:
|
| 31 |
+
f.write(yaml_config)
|
| 32 |
+
|
| 33 |
+
# 2. Start KasmVNC
|
| 34 |
+
# This acts as the X Server (Display :1) AND the Web Server (Port 7860)
|
| 35 |
+
print(">>> Starting KasmVNC on Port 7860...")
|
| 36 |
+
run_bg("vncserver -select-de matchbox -geometry 1280x720 -depth 24 -http_port 7860 -websocket_port 7860 -SecurityTypes None")
|
| 37 |
+
|
| 38 |
+
# Wait for X server to be ready
|
| 39 |
+
time.sleep(3)
|
| 40 |
+
|
| 41 |
+
# 3. Start Window Manager (Matchbox)
|
| 42 |
+
print(">>> Starting Window Manager...")
|
| 43 |
+
run_bg("matchbox-window-manager -use_titlebar no")
|
| 44 |
+
|
| 45 |
+
# 4. Start Brave Browser
|
| 46 |
+
print(">>> Starting Brave Browser...")
|
| 47 |
+
brave_cmd = (
|
| 48 |
+
"brave-browser "
|
| 49 |
+
"--start-maximized "
|
| 50 |
+
"--user-data-dir=/home/user/brave-data "
|
| 51 |
+
"--no-sandbox "
|
| 52 |
+
"--disable-dev-shm-usage "
|
| 53 |
+
"--disable-gpu-shader-disk-cache "
|
| 54 |
+
"--test-type "
|
| 55 |
+
# Use these flags to force software rendering if GPU fails in Space
|
| 56 |
+
"--disable-gpu-compositing "
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Loop to keep the script running and restart Brave if it crashes
|
| 60 |
+
while True:
|
| 61 |
+
try:
|
| 62 |
+
# We use .run() here to block until Brave closes (if it crashes), then loop restarts it
|
| 63 |
+
subprocess.run(brave_cmd, shell=True, env={**os.environ, "DISPLAY": ":1"})
|
| 64 |
+
time.sleep(1)
|
| 65 |
+
except KeyboardInterrupt:
|
| 66 |
+
print(">>> Stopping services...")
|
| 67 |
+
sys.exit(0)
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
main()
|