homelab_automation/app/models/docker_container.py
Bruno Charest 817f8b4ee7
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 Homelab Automation API v2, introducing a new dashboard, comprehensive backend models, and API routes.
2026-03-06 09:31:08 -05:00

61 lines
2.7 KiB
Python

"""Docker Container model for Homelab Automation."""
from datetime import datetime
from typing import Optional
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, JSON, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql import func
from .database import Base
class DockerContainer(Base):
"""Model representing a Docker container on a host."""
__tablename__ = "docker_containers"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
host_id: Mapped[str] = mapped_column(String(50), ForeignKey("hosts.id", ondelete="CASCADE"), nullable=False)
container_id: Mapped[str] = mapped_column(String(64), nullable=False)
name: Mapped[str] = mapped_column(String(255), nullable=False)
image: Mapped[str] = mapped_column(String(255), nullable=True)
state: Mapped[str] = mapped_column(String(20), nullable=False, default="unknown") # running/exited/paused/created/dead
status: Mapped[str] = mapped_column(String(255), nullable=True) # Up 2 hours, Exited (0) 5 minutes ago
health: Mapped[str] = mapped_column(String(20), nullable=True) # healthy/unhealthy/starting/none
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=True)
ports: Mapped[dict] = mapped_column(JSON, nullable=True)
labels: Mapped[dict] = mapped_column(JSON, nullable=True)
compose_project: Mapped[str] = mapped_column(String(255), nullable=True) # com.docker.compose.project
last_update_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, server_default=func.now(), onupdate=func.now()
)
# Relationship to host
host: Mapped["Host"] = relationship("Host", back_populates="docker_containers")
__table_args__ = (
UniqueConstraint('host_id', 'container_id', name='uq_docker_containers_host_container'),
{"sqlite_autoincrement": True},
)
def __repr__(self) -> str: # pragma: no cover
return f"<DockerContainer id={self.id} name={self.name} state={self.state}>"
def to_dict(self) -> dict:
"""Convert to dictionary for API responses."""
return {
"id": self.id,
"host_id": self.host_id,
"container_id": self.container_id,
"name": self.name,
"image": self.image,
"state": self.state,
"status": self.status,
"health": self.health,
"created_at": self.created_at.isoformat() if self.created_at else None,
"ports": self.ports,
"labels": self.labels,
"compose_project": self.compose_project,
"last_update_at": self.last_update_at.isoformat() if self.last_update_at else None,
}