diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec79470 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +*.pyc +.pytest_cache/ +.env diff --git a/app/db/apps.py b/app/db/apps.py new file mode 100644 index 0000000..8e63b9a --- /dev/null +++ b/app/db/apps.py @@ -0,0 +1,107 @@ +from app.db.database import get_connection + + +def get_apps(): + con = get_connection() + + rows = con.execute( + """ + SELECT + id, + name, + language, + version, + status, + COALESCE(memory, '') AS memory, + COALESCE(cpus, '') AS cpus, + updated_at + FROM apps + ORDER BY id + """ + ).fetchall() + + con.close() + + return [dict(row) for row in rows] + + +def get_app(app_id: str): + con = get_connection() + + row = con.execute( + """ + SELECT + id, + name, + language, + version, + status, + COALESCE(memory, '') AS memory, + COALESCE(cpus, '') AS cpus, + updated_at + FROM apps + WHERE id = ? + """, + (app_id,), + ).fetchone() + + 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 + FROM deployments + ORDER BY id DESC + LIMIT ? + """, + (limit,), + ).fetchall() + + con.close() + + return [dict(row) for row in rows] + + +def get_deployment(deployment_id: int): + con = get_connection() + + row = con.execute( + """ + SELECT + id, + app_id, + kind, + status, + started_at, + finished_at, + returncode, + stdout, + stderr + FROM deployments + WHERE id = ? + """, + (deployment_id,), + ).fetchone() + + con.close() + + if not row: + return None + + return dict(row) diff --git a/app/db/database.py b/app/db/database.py new file mode 100644 index 0000000..4d54c36 --- /dev/null +++ b/app/db/database.py @@ -0,0 +1,10 @@ +import sqlite3 +from pathlib import Path + +DB_FILE = Path("/opt/appfactory/data/appfactory/appfactory.db") + + +def get_connection(): + con = sqlite3.connect(DB_FILE) + con.row_factory = sqlite3.Row + return con