256 lines
8.6 KiB
Python
256 lines
8.6 KiB
Python
import html
|
|
|
|
from fastapi import APIRouter, Form
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
from ..catalog import load_apps, save_apps
|
|
from ..config import (
|
|
DEFAULT_APPFACTORY_HOST,
|
|
DEFAULT_GITEA_ORG,
|
|
DELETE_APP_SCRIPT,
|
|
DEPLOY_SCRIPT,
|
|
GENERATE_COMPOSE_SCRIPT,
|
|
NEW_APP_SCRIPT,
|
|
read_env_value,
|
|
)
|
|
from ..shell import run_command
|
|
from ..templates.layout import page, render_result
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_class=HTMLResponse)
|
|
def index():
|
|
apps = load_apps()
|
|
|
|
gitea_url = read_env_value("GITEA_URL", "")
|
|
gitea_org = read_env_value("GITEA_ORG", DEFAULT_GITEA_ORG)
|
|
host = read_env_value("APPFACTORY_HOST", DEFAULT_APPFACTORY_HOST)
|
|
|
|
rows = ""
|
|
|
|
for item in apps:
|
|
app_id = html.escape(item.get("id", ""))
|
|
status = html.escape(item.get("status", ""))
|
|
docs = html.escape(item.get("docs", f"/apps/{app_id}/docs"))
|
|
memory = item.get("memory", "")
|
|
cpus = item.get("cpus", "")
|
|
|
|
http_clone = html.escape(f"git clone {gitea_url}/{gitea_org}/{app_id}.git")
|
|
ssh_clone = html.escape(f"git clone ssh://git@{host}:2222/{gitea_org}/{app_id}.git")
|
|
|
|
memory_options = ""
|
|
for value, label in [
|
|
("", "Default"),
|
|
("256m", "256 MB - small service"),
|
|
("512m", "512 MB - normal service"),
|
|
("1g", "1 GB - larger service"),
|
|
("2g", "2 GB - heavy service"),
|
|
]:
|
|
selected = "selected" if memory == value else ""
|
|
memory_options += f'<option value="{value}" {selected}>{label}</option>'
|
|
|
|
cpu_options = ""
|
|
for value, label in [
|
|
("", "Default"),
|
|
("0.25", "0.25 CPU - very small"),
|
|
("0.50", "0.50 CPU - normal"),
|
|
("1.00", "1 CPU - full core"),
|
|
("2.00", "2 CPU - heavy"),
|
|
]:
|
|
selected = "selected" if cpus == value else ""
|
|
cpu_options += f'<option value="{value}" {selected}>{label}</option>'
|
|
|
|
rows += f"""
|
|
<tr>
|
|
<td>
|
|
<strong>{app_id}</strong><br>
|
|
<span class="muted">/apps/{app_id}</span>
|
|
</td>
|
|
<td><span class="pill">{status}</span></td>
|
|
<td><a href="{docs}">Swagger</a></td>
|
|
<td>
|
|
<form method="post" action="/portal/update-resources">
|
|
<input type="hidden" name="app_id" value="{app_id}">
|
|
<div class="inline-form">
|
|
<select name="memory">{memory_options}</select>
|
|
<select name="cpus">{cpu_options}</select>
|
|
<button type="submit">Apply</button>
|
|
</div>
|
|
<div class="resource-help">
|
|
Memory is RAM limit. CPU is max compute share.
|
|
Example: 0.50 = half CPU core, 1.00 = one full core.
|
|
</div>
|
|
</form>
|
|
</td>
|
|
<td>
|
|
<details>
|
|
<summary>Clone commands</summary>
|
|
<label class="muted">HTTP</label>
|
|
<div class="cmd-row">
|
|
<input readonly value="{http_clone}" id="http-{app_id}">
|
|
<button onclick="copyValue('http-{app_id}')">Copy</button>
|
|
</div>
|
|
|
|
<label class="muted">SSH</label>
|
|
<div class="cmd-row">
|
|
<input readonly value="{ssh_clone}" id="ssh-{app_id}">
|
|
<button onclick="copyValue('ssh-{app_id}')">Copy</button>
|
|
</div>
|
|
</details>
|
|
</td>
|
|
<td>
|
|
<form method="post" action="/portal/delete-app" onsubmit="return confirm('Delete {app_id}? This removes container, image, workspace, catalog entry and Gitea repo.');">
|
|
<input type="hidden" name="app_id" value="{app_id}">
|
|
<button type="submit" class="danger">Delete</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
"""
|
|
|
|
if not rows:
|
|
rows = '<tr><td colspan="6">No apps deployed yet.</td></tr>'
|
|
|
|
return page(
|
|
"Apps",
|
|
f"""
|
|
<div class="grid">
|
|
<div class="card">
|
|
<h2>Apps</h2>
|
|
<p class="muted">Create, deploy, clone, tune resources and delete services.</p>
|
|
<a class="btn" href="/portal/new-app">+ New App</a>
|
|
</div>
|
|
<div class="card">
|
|
<h2>Backups</h2>
|
|
<p class="muted">Create backups and copy restore commands. Restore is intentionally manual and guarded.</p>
|
|
<a class="btn" href="/portal/backups">Manage Backups</a>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<h2>Deployed Apps</h2>
|
|
<table>
|
|
<tr>
|
|
<th>App</th>
|
|
<th>Status</th>
|
|
<th>Docs</th>
|
|
<th>Resources</th>
|
|
<th>Git</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
{rows}
|
|
</table>
|
|
</div>
|
|
""",
|
|
)
|
|
|
|
|
|
@router.get("/new-app", response_class=HTMLResponse)
|
|
def new_app_form():
|
|
return page(
|
|
"New App",
|
|
"""
|
|
<div class="card">
|
|
<h2>Create New App</h2>
|
|
<p class="muted">This creates Gitea repo, webhook, local workspace, initial commit and deploys the service.</p>
|
|
|
|
<form method="post" action="/portal/new-app">
|
|
<p>
|
|
<label>App ID</label><br>
|
|
<input name="app_id" placeholder="gmail-service" required>
|
|
</p>
|
|
|
|
<p>
|
|
<label>App Name</label><br>
|
|
<input name="app_name" placeholder="Gmail Service" required>
|
|
</p>
|
|
|
|
<p>
|
|
<label>Template</label><br>
|
|
<select name="template">
|
|
<option value="python-fastapi">Python FastAPI</option>
|
|
</select>
|
|
</p>
|
|
|
|
<button type="submit">Create App</button>
|
|
</form>
|
|
|
|
<p><a href="/portal">← Back</a></p>
|
|
</div>
|
|
""",
|
|
)
|
|
|
|
|
|
@router.post("/new-app", response_class=HTMLResponse)
|
|
def create_app(app_id: str = Form(...), app_name: str = Form(...), template: str = Form(...)):
|
|
if template != "python-fastapi":
|
|
return HTMLResponse("Unsupported template", status_code=400)
|
|
|
|
create_result = run_command([NEW_APP_SCRIPT, app_id, app_name])
|
|
deploy_result = run_command([DEPLOY_SCRIPT, app_id])
|
|
|
|
status = "OK" if create_result.returncode == 0 and deploy_result.returncode == 0 else "FAILED"
|
|
|
|
return render_result(
|
|
title=f"Create App: {status}",
|
|
back_url="/portal",
|
|
sections=[
|
|
("Create Output", create_result.stdout),
|
|
("Create Error", create_result.stderr),
|
|
("Deploy Output", deploy_result.stdout),
|
|
("Deploy Error", deploy_result.stderr),
|
|
],
|
|
extra_link=f"/apps/{html.escape(app_id)}/docs",
|
|
extra_label="Open Swagger",
|
|
)
|
|
|
|
|
|
@router.post("/delete-app", response_class=HTMLResponse)
|
|
def delete_app(app_id: str = Form(...)):
|
|
result = run_command([DELETE_APP_SCRIPT, app_id])
|
|
status = "OK" if result.returncode == 0 else "FAILED"
|
|
|
|
return render_result(
|
|
title=f"Delete App: {status}",
|
|
back_url="/portal",
|
|
sections=[("Output", result.stdout), ("Error", result.stderr)],
|
|
)
|
|
|
|
|
|
@router.post("/update-resources", response_class=HTMLResponse)
|
|
def update_resources(app_id: str = Form(...), memory: str = Form(""), cpus: str = Form("")):
|
|
apps = load_apps()
|
|
|
|
for item in apps:
|
|
if item.get("id") == app_id:
|
|
memory = memory.strip()
|
|
cpus = cpus.strip()
|
|
|
|
if memory:
|
|
item["memory"] = memory
|
|
else:
|
|
item.pop("memory", None)
|
|
|
|
if cpus:
|
|
item["cpus"] = cpus
|
|
else:
|
|
item.pop("cpus", None)
|
|
|
|
save_apps(apps)
|
|
|
|
compose_result = run_command([GENERATE_COMPOSE_SCRIPT])
|
|
deploy_result = run_command([DEPLOY_SCRIPT, app_id])
|
|
|
|
status = "OK" if compose_result.returncode == 0 and deploy_result.returncode == 0 else "FAILED"
|
|
|
|
return render_result(
|
|
title=f"Update Resources: {status}",
|
|
back_url="/portal",
|
|
sections=[
|
|
("Compose Output", compose_result.stdout),
|
|
("Compose Error", compose_result.stderr),
|
|
("Deploy Output", deploy_result.stdout),
|
|
("Deploy Error", deploy_result.stderr),
|
|
],
|
|
)
|