"""Docker Container model for Homelab Automation.""" from __future__ import annotations 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, 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[Optional[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[Optional[str]] = mapped_column(String(255), nullable=True) # Up 2 hours, Exited (0) 5 minutes ago health: Mapped[Optional[str]] = mapped_column(String(20), nullable=True) # healthy/unhealthy/starting/none created_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True) ports: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True) labels: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True) compose_project: Mapped[Optional[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"" 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, }