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