homelab_automation/app/crud/docker_container.py
Bruno Charest 984d06a223
Some checks failed
Tests / Backend Tests (Python) (3.10) (push) Has been cancelled
Tests / Backend Tests (Python) (3.11) (push) Has been cancelled
Tests / Backend Tests (Python) (3.12) (push) Has been cancelled
Tests / Frontend Tests (JS) (push) Has been cancelled
Tests / Integration Tests (push) Has been cancelled
Tests / All Tests Passed (push) Has been cancelled
feat: Implement comprehensive database schema with new models, CRUD operations, and documentation for host metrics, Docker management, and terminal sessions, while removing old test files.
2026-03-05 10:16:13 -05:00

212 lines
8.2 KiB
Python

"""CRUD operations for Docker containers."""
from datetime import datetime
from typing import List, Optional
from sqlalchemy import case, delete, func, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.docker_container import DockerContainer
class DockerContainerRepository:
"""Repository for Docker container CRUD operations."""
def __init__(self, session: AsyncSession):
self.session = session
async def get(self, container_db_id: int) -> Optional[DockerContainer]:
"""Get a container by its database ID."""
result = await self.session.execute(
select(DockerContainer).where(DockerContainer.id == container_db_id)
)
return result.scalar_one_or_none()
async def get_by_container_id(self, host_id: str, container_id: str) -> Optional[DockerContainer]:
"""Get a container by host ID and Docker container ID."""
result = await self.session.execute(
select(DockerContainer).where(
DockerContainer.host_id == host_id,
DockerContainer.container_id == container_id
).limit(1)
)
return result.scalars().first()
async def get_by_name(self, host_id: str, name: str) -> Optional[DockerContainer]:
"""Get a container by host ID and container name."""
result = await self.session.execute(
select(DockerContainer).where(
DockerContainer.host_id == host_id,
DockerContainer.name == name
).limit(1)
)
return result.scalars().first()
async def list_by_host(
self,
host_id: str,
state: Optional[str] = None,
compose_project: Optional[str] = None
) -> List[DockerContainer]:
"""List all containers for a host with optional filters."""
query = select(DockerContainer).where(DockerContainer.host_id == host_id)
if state:
query = query.where(DockerContainer.state == state)
if compose_project:
query = query.where(DockerContainer.compose_project == compose_project)
query = query.order_by(DockerContainer.name)
result = await self.session.execute(query)
return list(result.scalars().all())
async def count_by_host(self, host_id: str) -> dict:
"""Count containers by state for a host."""
result = await self.session.execute(
select(
func.count(DockerContainer.id).label('total'),
func.sum(case((DockerContainer.state == 'running', 1), else_=0)).label('running')
).where(DockerContainer.host_id == host_id)
)
row = result.one()
return {
"total": row.total or 0,
"running": row.running or 0
}
async def upsert(
self,
host_id: str,
container_id: str,
name: str,
image: Optional[str] = None,
state: str = "unknown",
status: Optional[str] = None,
health: Optional[str] = None,
created_at: Optional[datetime] = None,
ports: Optional[dict] = None,
labels: Optional[dict] = None,
compose_project: Optional[str] = None
) -> DockerContainer:
"""Create or update a container using SQLite upsert."""
# Determine if we are using MySQL or SQLite
dialect = self.session.bind.dialect.name
values = {
"host_id": host_id,
"container_id": container_id,
"name": name,
"image": image,
"state": state,
"status": status,
"health": health,
"created_at": created_at,
"ports": ports,
"labels": labels,
"compose_project": compose_project,
"last_update_at": datetime.utcnow()
}
if dialect == "mysql":
from sqlalchemy.dialects.mysql import insert as mysql_insert
stmt = mysql_insert(DockerContainer).values(**values)
stmt = stmt.on_duplicate_key_update(
name=stmt.inserted.name,
image=stmt.inserted.image,
state=stmt.inserted.state,
status=stmt.inserted.status,
health=stmt.inserted.health,
created_at=stmt.inserted.created_at,
ports=stmt.inserted.ports,
labels=stmt.inserted.labels,
compose_project=stmt.inserted.compose_project,
last_update_at=stmt.inserted.last_update_at
)
else:
from sqlalchemy.dialects.sqlite import insert as sqlite_insert
stmt = sqlite_insert(DockerContainer).values(**values)
stmt = stmt.on_conflict_do_update(
index_elements=['host_id', 'container_id'],
set_={
'name': stmt.excluded.name,
'image': stmt.excluded.image,
'state': stmt.excluded.state,
'status': stmt.excluded.status,
'health': stmt.excluded.health,
'created_at': stmt.excluded.created_at,
'ports': stmt.excluded.ports,
'labels': stmt.excluded.labels,
'compose_project': stmt.excluded.compose_project,
'last_update_at': stmt.excluded.last_update_at
}
)
await self.session.execute(stmt)
# Return the upserted container
return await self.get_by_container_id(host_id, container_id)
async def delete_by_host(self, host_id: str) -> int:
"""Delete all containers for a host."""
result = await self.session.execute(
delete(DockerContainer).where(DockerContainer.host_id == host_id)
)
return result.rowcount
async def delete_stale(self, host_id: str, current_container_ids: List[str]) -> int:
"""Delete containers that no longer exist on the host."""
if not current_container_ids:
return await self.delete_by_host(host_id)
result = await self.session.execute(
delete(DockerContainer).where(
DockerContainer.host_id == host_id,
DockerContainer.container_id.notin_(current_container_ids)
)
)
return result.rowcount
async def list_all(
self,
state: Optional[str] = None,
compose_project: Optional[str] = None,
health: Optional[str] = None,
host_ids: Optional[List[str]] = None
) -> List[DockerContainer]:
"""List all containers across all hosts with optional filters."""
query = select(DockerContainer)
if state:
query = query.where(DockerContainer.state == state)
if compose_project:
query = query.where(DockerContainer.compose_project == compose_project)
if health:
query = query.where(DockerContainer.health == health)
if host_ids:
query = query.where(DockerContainer.host_id.in_(host_ids))
query = query.order_by(DockerContainer.host_id, DockerContainer.name)
result = await self.session.execute(query)
return list(result.scalars().all())
async def count_all(self) -> dict:
"""Count all containers by state across all hosts."""
result = await self.session.execute(
select(
func.count(DockerContainer.id).label('total'),
func.sum(case((DockerContainer.state == 'running', 1), else_=0)).label('running'),
func.sum(case((DockerContainer.state == 'exited', 1), else_=0)).label('stopped'),
func.sum(case((DockerContainer.state == 'paused', 1), else_=0)).label('paused'),
func.count(func.distinct(DockerContainer.host_id)).label('hosts_count'),
func.max(DockerContainer.last_update_at).label('last_update')
)
)
row = result.one()
return {
"total": row.total or 0,
"running": row.running or 0,
"stopped": row.stopped or 0,
"paused": row.paused or 0,
"hosts_count": row.hosts_count or 0,
"last_update": row.last_update
}