Files
zebra-power/backend/app/database.py

91 lines
3.4 KiB
Python

from sqlalchemy import create_engine, Column, Integer, String, DateTime, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime
import os
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./zebra.db")
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
class Server(Base):
__tablename__ = "servers"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True, nullable=False)
ip_address = Column(String, nullable=False)
mac_address = Column(String, nullable=False)
description = Column(String)
is_online = Column(Boolean, default=False)
last_ping = Column(DateTime)
created_at = Column(DateTime, default=datetime.utcnow)
class ProxmoxCluster(Base):
__tablename__ = "proxmox_clusters"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True, nullable=False)
host = Column(String, nullable=False)
username = Column(String, nullable=False)
password = Column(String, nullable=False)
port = Column(Integer, default=8006)
verify_ssl = Column(Boolean, default=True)
created_at = Column(DateTime, default=datetime.utcnow)
class ProxmoxHost(Base):
__tablename__ = "proxmox_hosts"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True, nullable=False)
description = Column(String)
# Network configuration for WOL
ip_address = Column(String, nullable=False)
mac_address = Column(String, nullable=False)
# Proxmox configuration
proxmox_host = Column(String, nullable=False) # Can be same as ip_address
proxmox_username = Column(String, nullable=False)
proxmox_password = Column(String, nullable=False)
proxmox_port = Column(Integer, default=8006)
verify_ssl = Column(Boolean, default=True)
# Host status
is_online = Column(Boolean, default=False)
last_ping = Column(DateTime)
# Optional shutdown endpoint for host shutdown
shutdown_endpoint = Column(String, nullable=True) # e.g., /api/hosts/shutdown
created_at = Column(DateTime, default=datetime.utcnow)
class ActionLog(Base):
__tablename__ = "action_logs"
id = Column(Integer, primary_key=True, index=True)
action_type = Column(String, nullable=False) # 'wol', 'proxmox', 'server'
target_id = Column(Integer, nullable=True) # server_id, cluster_id, vm_id, etc.
target_name = Column(String, nullable=True) # server name, vm name, etc.
action = Column(String, nullable=False) # 'wake', 'start', 'stop', 'create', 'delete', etc.
timestamp = Column(DateTime, default=datetime.utcnow)
success = Column(Boolean, default=True)
message = Column(String)
details = Column(String, nullable=True) # JSON string for additional data
# Keep WolLog for backward compatibility
class WolLog(Base):
__tablename__ = "wol_logs"
id = Column(Integer, primary_key=True, index=True)
server_id = Column(Integer, nullable=False)
action = Column(String, nullable=False)
timestamp = Column(DateTime, default=datetime.utcnow)
success = Column(Boolean, default=True)
message = Column(String)
def init_db():
Base.metadata.create_all(bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()