2022-10-31 14:46:32 +00:00
|
|
|
from sqlalchemy import Column, Integer, String, ForeignKey, Table
|
|
|
|
from sqlalchemy.orm import relationship, backref
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
|
2022-11-18 17:24:30 +00:00
|
|
|
Base = declarative_base()
|
2022-10-31 14:46:32 +00:00
|
|
|
|
|
|
|
client_image_table = Table(
|
|
|
|
"client_image",
|
|
|
|
Base.metadata,
|
|
|
|
Column("client_mac", String, ForeignKey("clients.mac_address")),
|
|
|
|
Column("image_id", Integer, ForeignKey("vm_images.image_id"))
|
|
|
|
)
|
|
|
|
|
2022-11-13 21:12:02 +00:00
|
|
|
|
2022-10-31 14:46:32 +00:00
|
|
|
class Client(Base):
|
|
|
|
__tablename__ = "clients"
|
|
|
|
mac_address = Column(String, primary_key=True)
|
2022-11-13 21:12:02 +00:00
|
|
|
ip_address = Column(String(16), nullable=False)
|
|
|
|
hostname = Column(String(100), nullable=False)
|
|
|
|
client_version = Column(String(100), nullable=False)
|
2022-10-31 14:46:32 +00:00
|
|
|
vm_list_on_machine = relationship(
|
2022-11-17 21:45:43 +00:00
|
|
|
"VMImage",
|
2022-11-13 21:12:02 +00:00
|
|
|
secondary=client_image_table,
|
2022-11-18 18:36:07 +00:00
|
|
|
back_populates="clients"
|
2022-10-31 14:46:32 +00:00
|
|
|
)
|
2022-11-13 21:12:02 +00:00
|
|
|
|
2022-10-31 14:46:32 +00:00
|
|
|
def has_vm_installed(self, vm_object):
|
|
|
|
for vm in self.vm_list_on_machine:
|
|
|
|
if vm.image_hash == vm_object.image_hash:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2022-11-18 17:24:30 +00:00
|
|
|
def as_dict(self):
|
|
|
|
return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}
|
|
|
|
|
2022-11-13 21:12:02 +00:00
|
|
|
|
2022-10-31 14:46:32 +00:00
|
|
|
class VMImage(Base):
|
|
|
|
__tablename__ = "vm_images"
|
|
|
|
image_id = Column(Integer, primary_key=True)
|
2022-11-18 17:24:30 +00:00
|
|
|
image_name = Column(String(100), unique=False, nullable=False)
|
2022-11-13 21:12:02 +00:00
|
|
|
image_file = Column(String(500), unique=False, nullable=False)
|
|
|
|
image_version = Column(String(100), nullable=False)
|
2022-11-17 21:45:43 +00:00
|
|
|
image_hash = Column(String(500), nullable=False)
|
2022-11-18 17:24:30 +00:00
|
|
|
image_name_version_combo = Column(String(600), nullable=False, unique=True)
|
2022-10-31 14:46:32 +00:00
|
|
|
clients = relationship(
|
2022-11-17 21:45:43 +00:00
|
|
|
"Client",
|
2022-11-18 18:36:07 +00:00
|
|
|
secondary=client_image_table,
|
|
|
|
back_populates="vm_list_on_machine"
|
2022-11-13 21:12:02 +00:00
|
|
|
)
|
|
|
|
|
2022-11-18 17:24:30 +00:00
|
|
|
def as_dict(self):
|
|
|
|
return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}
|
|
|
|
|
2022-11-13 21:12:02 +00:00
|
|
|
|
|
|
|
class User(Base):
|
|
|
|
__tablename__ = "users"
|
2022-11-17 21:45:43 +00:00
|
|
|
user_id = Column(Integer, primary_key=True, autoincrement=True)
|
2022-11-13 21:12:02 +00:00
|
|
|
username = Column(String)
|
|
|
|
password_hash = Column(String)
|
2022-11-18 17:24:30 +00:00
|
|
|
|
|
|
|
def as_dict(self):
|
|
|
|
return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}
|