|
import os |
|
import gradio as gr |
|
import threading |
|
from flask import Flask, request |
|
from psycopg2 import connect, sql |
|
import psycopg2.extras |
|
|
|
|
|
DB_NAME = os.getenv("DB_NAME") |
|
DB_USER = os.getenv("DB_USER") |
|
DB_PASSWORD = os.getenv("DB_PASSWORD") |
|
DB_HOST = os.getenv("DB_HOST") |
|
DB_PORT = os.getenv("DB_PORT") |
|
APP_PASSWORD = os.getenv("APP_PASSWORD") |
|
|
|
|
|
def get_db_connection(): |
|
return connect( |
|
dbname=DB_NAME, |
|
user=DB_USER, |
|
password=DB_PASSWORD, |
|
host=DB_HOST, |
|
port=DB_PORT |
|
) |
|
|
|
|
|
server_threads = {} |
|
|
|
|
|
def create_server(name, port): |
|
app = Flask(name) |
|
|
|
@app.route("/") |
|
def home(): |
|
return f"Server {name} is running on port {port}!" |
|
|
|
return app |
|
|
|
|
|
def start_server(name, port): |
|
if name in server_threads: |
|
return f"Server {name} is already running!" |
|
|
|
app = create_server(name, port) |
|
|
|
def run(): |
|
app.run(host="0.0.0.0", port=port, debug=False, use_reloader=False) |
|
|
|
thread = threading.Thread(target=run, daemon=True) |
|
thread.start() |
|
server_threads[name] = thread |
|
|
|
|
|
conn = get_db_connection() |
|
with conn.cursor() as cursor: |
|
cursor.execute( |
|
"INSERT INTO servers (name, port, status) VALUES (%s, %s, %s) ON CONFLICT (name) DO UPDATE SET status = EXCLUDED.status", |
|
(name, port, "running") |
|
) |
|
conn.commit() |
|
conn.close() |
|
|
|
return f"Server {name} started on port {port}!" |
|
|
|
|
|
def stop_server(name): |
|
if name not in server_threads: |
|
return f"Server {name} is not running!" |
|
|
|
|
|
server_threads.pop(name) |
|
return f"Server {name} stopped!" |
|
|
|
|
|
def manage_servers(password, action, name, port): |
|
if password != APP_PASSWORD: |
|
return "Invalid password!" |
|
if action == "start": |
|
return start_server(name, port) |
|
elif action == "stop": |
|
return stop_server(name) |
|
else: |
|
return "Invalid action!" |
|
|
|
|
|
def execute_db_command(password, command): |
|
if password != APP_PASSWORD: |
|
return "Invalid password!" |
|
|
|
|
|
conn = get_db_connection() |
|
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) |
|
|
|
try: |
|
|
|
cursor.execute(command) |
|
result = cursor.fetchall() |
|
conn.commit() |
|
except Exception as e: |
|
result = f"Error executing command: {e}" |
|
finally: |
|
conn.close() |
|
|
|
return result |
|
|
|
|
|
def get_db_stats(): |
|
conn = get_db_connection() |
|
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) |
|
|
|
|
|
cursor.execute(""" |
|
SELECT |
|
db_name, |
|
SUM(range_size) AS total_size_bytes |
|
FROM crdb_internal.database_statistics |
|
WHERE db_name = current_database() |
|
GROUP BY db_name; |
|
""") |
|
db_stats = cursor.fetchone() |
|
|
|
conn.close() |
|
if db_stats: |
|
return f"Database {db_stats['db_name']} size: {db_stats['total_size_bytes']} bytes" |
|
else: |
|
return "Unable to fetch database size" |
|
|
|
|
|
with gr.Blocks() as server_manager: |
|
gr.Markdown("# ZeroCPU Server Manager") |
|
|
|
|
|
with gr.Row(): |
|
password = gr.Textbox(label="Password", type="password") |
|
with gr.Row(): |
|
action = gr.Radio(["start", "stop"], label="Action") |
|
name = gr.Textbox(label="Server Name") |
|
port = gr.Number(label="Port") |
|
with gr.Row(): |
|
submit = gr.Button("Submit") |
|
output = gr.Textbox(label="Output") |
|
|
|
|
|
submit.click( |
|
manage_servers, |
|
inputs=[password, action, name, port], |
|
outputs=output |
|
) |
|
|
|
|
|
with gr.Row(): |
|
command_input = gr.Textbox(label="Database Command", placeholder="Enter SQL command") |
|
command_submit = gr.Button("Execute Command") |
|
command_output = gr.Textbox(label="Command Output") |
|
|
|
|
|
command_submit.click( |
|
execute_db_command, |
|
inputs=[password, command_input], |
|
outputs=command_output |
|
) |
|
|
|
|
|
with gr.Row(): |
|
db_stats_output = gr.Textbox(label="Database Stats") |
|
gr.Button("Show Database Stats").click( |
|
get_db_stats, |
|
outputs=db_stats_output |
|
) |
|
|
|
|
|
gr.Markdown("ZeroCPU is active") |
|
|
|
server_manager.launch() |
|
|