Refactor template management to use database.

This commit is contained in:
grossmj committed 2021-03-28 11:15:08 +10:30
1 parent b417bc4dec
commit d730c591b3
20 files changed
+1704 -1535

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
#!/usr/bin/env python
#
# Copyright (C) 2021 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from .base import Base
from .users import User
from .templates import (
Template,
CloudTemplate,
DockerTemplate,
DynamipsTemplate,
EthernetHubTemplate,
EthernetSwitchTemplate,
IOUTemplate,
QemuTemplate,
VirtualBoxTemplate,
VMwareTemplate,
VPCSTemplate
)
+87
View File
@@ -0,0 +1,87 @@
#!/usr/bin/env python
#
# Copyright (C) 2020 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import uuid
from fastapi.encoders import jsonable_encoder
from sqlalchemy import Column, DateTime, func, inspect
from sqlalchemy.types import TypeDecorator, CHAR
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.declarative import as_declarative
@as_declarative()
class Base:
def _asdict(self):
return {c.key: getattr(self, c.key)
for c in inspect(self).mapper.column_attrs}
def _asjson(self):
return jsonable_encoder(self._asdict())
class GUID(TypeDecorator):
"""
Platform-independent GUID type.
Uses PostgreSQL's UUID type, otherwise uses
CHAR(32), storing as stringified hex values.
"""
impl = CHAR
def load_dialect_impl(self, dialect):
if dialect.name == 'postgresql':
return dialect.type_descriptor(UUID())
else:
return dialect.type_descriptor(CHAR(32))
def process_bind_param(self, value, dialect):
if value is None:
return value
elif dialect.name == 'postgresql':
return str(value)
else:
if not isinstance(value, uuid.UUID):
return "%.32x" % uuid.UUID(value).int
else:
# hexstring
return "%.32x" % value.int
def process_result_value(self, value, dialect):
if value is None:
return value
else:
if not isinstance(value, uuid.UUID):
value = uuid.UUID(value)
return value
class BaseTable(Base):
__abstract__ = True
created_at = Column(DateTime, default=func.current_timestamp())
updated_at = Column(DateTime,
default=func.current_timestamp(),
onupdate=func.current_timestamp())
def generate_uuid():
return str(uuid.uuid4())
+286
View File
@@ -0,0 +1,286 @@
#!/usr/bin/env python
#
# Copyright (C) 2021 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sqlalchemy import Boolean, Column, String, Integer, ForeignKey, PickleType
from .base import BaseTable, generate_uuid, GUID
class Template(BaseTable):
__tablename__ = "templates"
template_id = Column(GUID, primary_key=True, default=generate_uuid)
name = Column(String, index=True)
category = Column(String)
default_name_format = Column(String)
symbol = Column(String)
builtin = Column(Boolean, default=False)
compute_id = Column(String)
usage = Column(String)
template_type = Column(String)
__mapper_args__ = {
"polymorphic_identity": "templates",
"polymorphic_on": template_type
}
class CloudTemplate(Template):
__tablename__ = "cloud_templates"
template_id = Column(GUID, ForeignKey("templates.template_id"), primary_key=True)
ports_mapping = Column(PickleType)
remote_console_host = Column(String)
remote_console_port = Column(Integer)
remote_console_type = Column(String)
remote_console_http_path = Column(String)
__mapper_args__ = {
"polymorphic_identity": "cloud"
}
class DockerTemplate(Template):
__tablename__ = "docker_templates"
template_id = Column(GUID, ForeignKey("templates.template_id"), primary_key=True)
image = Column(String)
adapters = Column(Integer)
start_command = Column(String)
environment = Column(String)
console_type = Column(String)
aux_type = Column(String)
console_auto_start = Column(Boolean)
console_http_port = Column(Integer)
console_http_path = Column(String)
console_resolution = Column(String)
extra_hosts = Column(String)
extra_volumes = Column(PickleType)
memory = Column(Integer)
cpus = Column(Integer)
custom_adapters = Column(PickleType)
__mapper_args__ = {
"polymorphic_identity": "docker"
}
class DynamipsTemplate(Template):
__tablename__ = "dynamips_templates"
template_id = Column(GUID, ForeignKey("templates.template_id"), primary_key=True)
platform = Column(String)
chassis = Column(String)
image = Column(String)
exec_area = Column(Integer)
mmap = Column(Boolean)
mac_addr = Column(String)
system_id = Column(String)
startup_config = Column(String)
private_config = Column(String)
idlepc = Column(String)
idlemax = Column(Integer)
idlesleep = Column(Integer)
disk0 = Column(Integer)
disk1 = Column(Integer)
auto_delete_disks = Column(Boolean)
console_type = Column(String)
console_auto_start = Column(Boolean)
aux_type = Column(String)
ram = Column(Integer)
nvram = Column(Integer)
npe = Column(String)
midplane = Column(String)
sparsemem = Column(Boolean)
iomem = Column(Integer)
slot0 = Column(String)
slot1 = Column(String)
slot2 = Column(String)
slot3 = Column(String)
slot4 = Column(String)
slot5 = Column(String)
slot6 = Column(String)
wic0 = Column(String)
wic1 = Column(String)
wic2 = Column(String)
__mapper_args__ = {
"polymorphic_identity": "dynamips"
}
class EthernetHubTemplate(Template):
__tablename__ = "ethernet_hub_templates"
template_id = Column(GUID, ForeignKey("templates.template_id"), primary_key=True)
ports_mapping = Column(PickleType)
__mapper_args__ = {
"polymorphic_identity": "ethernet_hub"
}
class EthernetSwitchTemplate(Template):
__tablename__ = "ethernet_switch_templates"
template_id = Column(GUID, ForeignKey("templates.template_id"), primary_key=True)
ports_mapping = Column(PickleType)
console_type = Column(String)
__mapper_args__ = {
"polymorphic_identity": "ethernet_switch"
}
class IOUTemplate(Template):
__tablename__ = "iou_templates"
template_id = Column(GUID, ForeignKey("templates.template_id"), primary_key=True)
path = Column(String)
ethernet_adapters = Column(Integer)
serial_adapters = Column(Integer)
ram = Column(Integer)
nvram = Column(Integer)
use_default_iou_values = Column(Boolean)
startup_config = Column(String)
private_config = Column(String)
l1_keepalives = Column(Boolean)
console_type = Column(String)
console_auto_start = Column(Boolean)
__mapper_args__ = {
"polymorphic_identity": "iou"
}
class QemuTemplate(Template):
__tablename__ = "qemu_templates"
template_id = Column(GUID, ForeignKey("templates.template_id"), primary_key=True)
qemu_path = Column(String)
platform = Column(String)
linked_clone = Column(Boolean)
ram = Column(Integer)
cpus = Column(Integer)
maxcpus = Column(Integer)
adapters = Column(Integer)
adapter_type = Column(String)
mac_address = Column(String)
first_port_name = Column(String)
port_name_format = Column(String)
port_segment_size = Column(Integer)
console_type = Column(String)
console_auto_start = Column(Boolean)
aux_type = Column(String)
boot_priority = Column(String)
hda_disk_image = Column(String)
hda_disk_interface = Column(String)
hdb_disk_image = Column(String)
hdb_disk_interface = Column(String)
hdc_disk_image = Column(String)
hdc_disk_interface = Column(String)
hdd_disk_image = Column(String)
hdd_disk_interface = Column(String)
cdrom_image = Column(String)
initrd = Column(String)
kernel_image = Column(String)
bios_image = Column(String)
kernel_command_line = Column(String)
legacy_networking = Column(Boolean)
replicate_network_connection_state = Column(Boolean)
create_config_disk = Column(Boolean)
on_close = Column(String)
cpu_throttling = Column(Integer)
process_priority = Column(String)
options = Column(String)
custom_adapters = Column(PickleType)
__mapper_args__ = {
"polymorphic_identity": "qemu"
}
class VirtualBoxTemplate(Template):
__tablename__ = "virtualbox_templates"
template_id = Column(GUID, ForeignKey("templates.template_id"), primary_key=True)
vmname = Column(String)
ram = Column(Integer)
linked_clone = Column(Boolean)
adapters = Column(Integer)
use_any_adapter = Column(Boolean)
adapter_type = Column(String)
first_port_name = Column(String)
port_name_format = Column(String)
port_segment_size = Column(Integer)
headless = Column(Boolean)
on_close = Column(String)
console_type = Column(String)
console_auto_start = Column(Boolean)
custom_adapters = Column(PickleType)
__mapper_args__ = {
"polymorphic_identity": "virtualbox"
}
class VMwareTemplate(Template):
__tablename__ = "vmware_templates"
template_id = Column(GUID, ForeignKey("templates.template_id"), primary_key=True)
vmx_path = Column(String)
linked_clone = Column(Boolean)
first_port_name = Column(String)
port_name_format = Column(String)
port_segment_size = Column(Integer)
adapters = Column(Integer)
adapter_type = Column(String)
use_any_adapter = Column(Boolean)
headless = Column(Boolean)
on_close = Column(String)
console_type = Column(String)
console_auto_start = Column(Boolean)
custom_adapters = Column(PickleType)
__mapper_args__ = {
"polymorphic_identity": "vmware"
}
class VPCSTemplate(Template):
__tablename__ = "vpcs_templates"
template_id = Column(GUID, ForeignKey("templates.template_id"), primary_key=True)
base_script_file = Column(String)
console_type = Column(String)
console_auto_start = Column(Boolean, default=False)
__mapper_args__ = {
"polymorphic_identity": "vpcs"
}
+48
View File
@@ -0,0 +1,48 @@
#!/usr/bin/env python
#
# Copyright (C) 2021 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime, func
from sqlalchemy.orm import relationship
from .base import BaseTable, generate_uuid, GUID
class User(BaseTable):
__tablename__ = "users"
user_id = Column(GUID, primary_key=True, default=generate_uuid)
username = Column(String, unique=True, index=True)
email = Column(String, unique=True, index=True)
full_name = Column(String)
hashed_password = Column(String)
is_active = Column(Boolean, default=True)
is_superuser = Column(Boolean, default=False)
# items = relationship("Item", back_populates="owner")
#
#
# class Item(Base):
# __tablename__ = "items"
#
# id = Column(Integer, primary_key=True, index=True)
# title = Column(String, index=True)
# description = Column(String, index=True)
# owner_id = Column(Integer, ForeignKey("users.id"))
#
# owner = relationship("User", back_populates="items")