Read apps and resources from database
This commit is contained in:
+16
-44
@@ -35,51 +35,37 @@ def get_apps():
|
||||
).fetchall()
|
||||
|
||||
con.close()
|
||||
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
|
||||
def get_app(app_id: str):
|
||||
def update_app_resources(app_id: str, memory: str, cpus: str):
|
||||
con = get_connection()
|
||||
|
||||
row = con.execute(
|
||||
con.execute(
|
||||
"""
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
language,
|
||||
version,
|
||||
status,
|
||||
COALESCE(memory, '') AS memory,
|
||||
COALESCE(cpus, '') AS cpus,
|
||||
updated_at
|
||||
FROM apps
|
||||
UPDATE apps
|
||||
SET memory = ?,
|
||||
cpus = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
""",
|
||||
(app_id,),
|
||||
).fetchone()
|
||||
(
|
||||
memory or None,
|
||||
cpus or None,
|
||||
app_id,
|
||||
),
|
||||
)
|
||||
|
||||
con.commit()
|
||||
con.close()
|
||||
|
||||
if not row:
|
||||
return None
|
||||
|
||||
return dict(row)
|
||||
|
||||
|
||||
def get_deployments(limit: int = 100):
|
||||
con = get_connection()
|
||||
|
||||
rows = con.execute(
|
||||
"""
|
||||
SELECT
|
||||
id,
|
||||
app_id,
|
||||
kind,
|
||||
status,
|
||||
started_at,
|
||||
finished_at,
|
||||
returncode
|
||||
SELECT id, app_id, kind, status, started_at, finished_at, returncode
|
||||
FROM deployments
|
||||
ORDER BY id DESC
|
||||
LIMIT ?
|
||||
@@ -88,7 +74,6 @@ def get_deployments(limit: int = 100):
|
||||
).fetchall()
|
||||
|
||||
con.close()
|
||||
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
|
||||
@@ -97,16 +82,7 @@ def get_deployment(deployment_id: int):
|
||||
|
||||
row = con.execute(
|
||||
"""
|
||||
SELECT
|
||||
id,
|
||||
app_id,
|
||||
kind,
|
||||
status,
|
||||
started_at,
|
||||
finished_at,
|
||||
returncode,
|
||||
stdout,
|
||||
stderr
|
||||
SELECT id, app_id, kind, status, started_at, finished_at, returncode, stdout, stderr
|
||||
FROM deployments
|
||||
WHERE id = ?
|
||||
""",
|
||||
@@ -114,8 +90,4 @@ def get_deployment(deployment_id: int):
|
||||
).fetchone()
|
||||
|
||||
con.close()
|
||||
|
||||
if not row:
|
||||
return None
|
||||
|
||||
return dict(row)
|
||||
return dict(row) if row else None
|
||||
|
||||
+11
-18
@@ -3,7 +3,6 @@ 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,
|
||||
@@ -13,6 +12,7 @@ from ..config import (
|
||||
NEW_APP_SCRIPT,
|
||||
read_env_value,
|
||||
)
|
||||
from ..db.apps import get_apps, update_app_resources
|
||||
from ..shell import run_command
|
||||
from ..templates.layout import page, render_result
|
||||
|
||||
@@ -21,7 +21,7 @@ router = APIRouter()
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
def index():
|
||||
apps = load_apps()
|
||||
apps = get_apps()
|
||||
|
||||
gitea_url = read_env_value("GITEA_URL", "")
|
||||
gitea_org = read_env_value("GITEA_ORG", DEFAULT_GITEA_ORG)
|
||||
@@ -224,34 +224,27 @@ def delete_app(app_id: str = Form(...)):
|
||||
|
||||
@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)
|
||||
update_app_resources(app_id, memory, cpus)
|
||||
|
||||
catalog_result = run_command(["/tools/generate-catalog.sh"])
|
||||
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"
|
||||
status = (
|
||||
"OK"
|
||||
if catalog_result.returncode == 0 and compose_result.returncode == 0 and deploy_result.returncode == 0
|
||||
else "FAILED"
|
||||
)
|
||||
|
||||
return render_result(
|
||||
title=f"Úprava prostředků: {status}",
|
||||
back_url="/portal",
|
||||
sections=[
|
||||
("Výstup katalogu", catalog_result.stdout),
|
||||
("Chyba katalogu", catalog_result.stderr),
|
||||
("Výstup compose", compose_result.stdout),
|
||||
("Chyba compose", compose_result.stderr),
|
||||
("Výstup nasazení", deploy_result.stdout),
|
||||
|
||||
Reference in New Issue
Block a user