madiator-docker-runpod/official-templates/better-ai-launcher/app/utils/filebrowser_utils.py

46 lines
1.7 KiB
Python

import subprocess
import time
import requests
from requests.exceptions import Timeout
FILEBROWSER_PORT = 8181
filebrowser_process = None
def configure_filebrowser():
try:
subprocess.run('filebrowser config init', shell=True, check=True)
subprocess.run('filebrowser config set --auth.method=noauth', shell=True, check=True)
subprocess.run('filebrowser config set --baseurl /fileapp', shell=True, check=True)
subprocess.run('filebrowser config set --root /workspace', shell=True, check=True)
subprocess.run('filebrowser users add admin admin', shell=True, check=True)
print("File Browser configured successfully.")
return True
except subprocess.CalledProcessError as e:
print(f"Error configuring File Browser: {e}")
return False
def start_filebrowser():
global filebrowser_process
if filebrowser_process is None or filebrowser_process.poll() is not None:
filebrowser_process = subprocess.Popen(['filebrowser', '-r', '/workspace', '-a', '0.0.0.0', '-p', str(FILEBROWSER_PORT), '--baseurl', '/fileapp'])
time.sleep(2) # Give it a moment to start
return filebrowser_process.poll() is None
return False
def stop_filebrowser():
global filebrowser_process
if filebrowser_process and filebrowser_process.poll() is None:
filebrowser_process.terminate()
filebrowser_process.wait(timeout=10)
filebrowser_process = None
return True
return False
def get_filebrowser_status():
try:
response = requests.get('http://localhost:7222/fileapp/', timeout=5)
return 'running' if response.status_code == 200 else 'stopped'
except Timeout:
return 'timeout'
except Exception:
return 'unknown'