Use aiosqlite and add service for templates

This commit is contained in:
grossmj committed 2021-03-28 21:17:29 +10:30
1 parent d730c591b3
commit 0fea3f969e
14 files changed
+290 -230

No files matched your search

+3 -3
View File
@@ -27,14 +27,14 @@ from sqlalchemy.ext.declarative import as_declarative
@as_declarative()
class Base:
def _asdict(self):
def asdict(self):
return {c.key: getattr(self, c.key)
for c in inspect(self).mapper.column_attrs}
def _asjson(self):
def asjson(self):
return jsonable_encoder(self._asdict())
return jsonable_encoder(self.asdict())
class GUID(TypeDecorator):
+21 -11
View File
@@ -37,7 +37,7 @@ class Template(BaseTable):
__mapper_args__ = {
"polymorphic_identity": "templates",
"polymorphic_on": template_type
"polymorphic_on": template_type,
}
@@ -53,7 +53,8 @@ class CloudTemplate(Template):
remote_console_http_path = Column(String)
__mapper_args__ = {
"polymorphic_identity": "cloud"
"polymorphic_identity": "cloud",
"polymorphic_load": "selectin"
}
@@ -79,7 +80,8 @@ class DockerTemplate(Template):
custom_adapters = Column(PickleType)
__mapper_args__ = {
"polymorphic_identity": "docker"
"polymorphic_identity": "docker",
"polymorphic_load": "selectin"
}
@@ -124,7 +126,8 @@ class DynamipsTemplate(Template):
wic2 = Column(String)
__mapper_args__ = {
"polymorphic_identity": "dynamips"
"polymorphic_identity": "dynamips",
"polymorphic_load": "selectin"
}
@@ -136,7 +139,8 @@ class EthernetHubTemplate(Template):
ports_mapping = Column(PickleType)
__mapper_args__ = {
"polymorphic_identity": "ethernet_hub"
"polymorphic_identity": "ethernet_hub",
"polymorphic_load": "selectin"
}
@@ -149,7 +153,8 @@ class EthernetSwitchTemplate(Template):
console_type = Column(String)
__mapper_args__ = {
"polymorphic_identity": "ethernet_switch"
"polymorphic_identity": "ethernet_switch",
"polymorphic_load": "selectin"
}
@@ -171,7 +176,8 @@ class IOUTemplate(Template):
console_auto_start = Column(Boolean)
__mapper_args__ = {
"polymorphic_identity": "iou"
"polymorphic_identity": "iou",
"polymorphic_load": "selectin"
}
@@ -219,7 +225,8 @@ class QemuTemplate(Template):
custom_adapters = Column(PickleType)
__mapper_args__ = {
"polymorphic_identity": "qemu"
"polymorphic_identity": "qemu",
"polymorphic_load": "selectin"
}
@@ -244,7 +251,8 @@ class VirtualBoxTemplate(Template):
custom_adapters = Column(PickleType)
__mapper_args__ = {
"polymorphic_identity": "virtualbox"
"polymorphic_identity": "virtualbox",
"polymorphic_load": "selectin"
}
@@ -268,7 +276,8 @@ class VMwareTemplate(Template):
custom_adapters = Column(PickleType)
__mapper_args__ = {
"polymorphic_identity": "vmware"
"polymorphic_identity": "vmware",
"polymorphic_load": "selectin"
}
@@ -282,5 +291,6 @@ class VPCSTemplate(Template):
console_auto_start = Column(Boolean, default=False)
__mapper_args__ = {
"polymorphic_identity": "vpcs"
"polymorphic_identity": "vpcs",
"polymorphic_load": "selectin"
}
-2
View File
@@ -16,7 +16,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sqlalchemy.ext.asyncio import AsyncSession
from gns3server.controller import Controller
class BaseRepository:
@@ -24,4 +23,3 @@ class BaseRepository:
def __init__(self, db_session: AsyncSession) -> None:
self._db_session = db_session
self._controller = Controller.instance()
+15 -152
View File
@@ -15,11 +15,8 @@
# 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 uuid import UUID
from typing import List
from fastapi.encoders import jsonable_encoder
from typing import List, Union
from sqlalchemy import select, update, delete
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm.session import make_transient
@@ -29,29 +26,6 @@ from .base import BaseRepository
import gns3server.db.models as models
from gns3server import schemas
TEMPLATE_TYPE_TO_SHEMA = {
"cloud": schemas.CloudTemplate,
"ethernet_hub": schemas.EthernetHubTemplate,
"ethernet_switch": schemas.EthernetSwitchTemplate,
"docker": schemas.DockerTemplate,
"dynamips": schemas.DynamipsTemplate,
"vpcs": schemas.VPCSTemplate,
"virtualbox": schemas.VirtualBoxTemplate,
"vmware": schemas.VMwareTemplate,
"iou": schemas.IOUTemplate,
"qemu": schemas.QemuTemplate
}
DYNAMIPS_PLATFORM_TO_SHEMA = {
"c7200": schemas.C7200DynamipsTemplate,
"c3745": schemas.C3745DynamipsTemplate,
"c3725": schemas.C3725DynamipsTemplate,
"c3600": schemas.C3600DynamipsTemplate,
"c2691": schemas.C2691DynamipsTemplate,
"c2600": schemas.C2600DynamipsTemplate,
"c1700": schemas.C1700DynamipsTemplate
}
TEMPLATE_TYPE_TO_MODEL = {
"cloud": models.CloudTemplate,
"docker": models.DockerTemplate,
@@ -65,82 +39,6 @@ TEMPLATE_TYPE_TO_MODEL = {
"vpcs": models.VPCSTemplate
}
# built-in templates have their compute_id set to None to tell clients to select a compute
BUILTIN_TEMPLATES = [
{
"template_id": uuid.uuid3(uuid.NAMESPACE_DNS, "cloud"),
"template_type": "cloud",
"name": "Cloud",
"default_name_format": "Cloud{0}",
"category": "guest",
"symbol": ":/symbols/cloud.svg",
"compute_id": None,
"builtin": True
},
{
"template_id": uuid.uuid3(uuid.NAMESPACE_DNS, "nat"),
"template_type": "nat",
"name": "NAT",
"default_name_format": "NAT{0}",
"category": "guest",
"symbol": ":/symbols/cloud.svg",
"compute_id": None,
"builtin": True
},
{
"template_id": uuid.uuid3(uuid.NAMESPACE_DNS, "vpcs"),
"template_type": "vpcs",
"name": "VPCS",
"default_name_format": "PC{0}",
"category": "guest",
"symbol": ":/symbols/vpcs_guest.svg",
"base_script_file": "vpcs_base_config.txt",
"compute_id": None,
"builtin": True
},
{
"template_id": uuid.uuid3(uuid.NAMESPACE_DNS, "ethernet_switch"),
"template_type": "ethernet_switch",
"name": "Ethernet switch",
"console_type": "none",
"default_name_format": "Switch{0}",
"category": "switch",
"symbol": ":/symbols/ethernet_switch.svg",
"compute_id": None,
"builtin": True
},
{
"template_id": uuid.uuid3(uuid.NAMESPACE_DNS, "ethernet_hub"),
"template_type": "ethernet_hub",
"name": "Ethernet hub",
"default_name_format": "Hub{0}",
"category": "switch",
"symbol": ":/symbols/hub.svg",
"compute_id": None,
"builtin": True
},
{
"template_id": uuid.uuid3(uuid.NAMESPACE_DNS, "frame_relay_switch"),
"template_type": "frame_relay_switch",
"name": "Frame Relay switch",
"default_name_format": "FRSW{0}",
"category": "switch",
"symbol": ":/symbols/frame_relay_switch.svg",
"compute_id": None,
"builtin": True
},
{
"template_id": uuid.uuid3(uuid.NAMESPACE_DNS, "atm_switch"),
"template_type": "atm_switch",
"name": "ATM switch",
"default_name_format": "ATMSW{0}",
"category": "switch",
"symbol": ":/symbols/atm_switch.svg",
"compute_id": None,
"builtin": True
},
]
class TemplatesRepository(BaseRepository):
@@ -148,58 +46,31 @@ class TemplatesRepository(BaseRepository):
super().__init__(db_session)
def get_builtin_template(self, template_id: UUID) -> dict:
for builtin_template in BUILTIN_TEMPLATES:
if builtin_template["template_id"] == template_id:
return jsonable_encoder(builtin_template)
async def get_template(self, template_id: UUID) -> dict:
async def get_template(self, template_id: UUID) -> Union[None, models.Template]:
query = select(models.Template).where(models.Template.template_id == template_id)
result = (await self._db_session.execute(query)).scalars().first()
if result:
return result._asjson()
else:
return self.get_builtin_template(template_id)
result = await self._db_session.execute(query)
return result.scalars().first()
async def get_templates(self) -> List[dict]:
async def get_templates(self) -> List[models.Template]:
templates = []
query = select(models.Template)
result = await self._db_session.execute(query)
for db_template in result.scalars().all():
templates.append(db_template._asjson())
for builtin_template in BUILTIN_TEMPLATES:
templates.append(jsonable_encoder(builtin_template))
return templates
return result.scalars().all()
async def create_template(self, template_create: schemas.TemplateCreate) -> dict:
async def create_template(self, template_type: str, template_settings: dict) -> models.Template:
# get the default template settings
template_settings = jsonable_encoder(template_create, exclude_unset=True)
template_schema = TEMPLATE_TYPE_TO_SHEMA[template_create.template_type]
template_settings_with_defaults = template_schema.parse_obj(template_settings)
settings = template_settings_with_defaults.dict()
if template_create.template_type == "dynamips":
# special case for Dynamips to cover all platform types that contain specific settings
dynamips_template_schema = DYNAMIPS_PLATFORM_TO_SHEMA[settings["platform"]]
dynamips_template_settings_with_defaults = dynamips_template_schema.parse_obj(template_settings)
settings = dynamips_template_settings_with_defaults.dict()
model = TEMPLATE_TYPE_TO_MODEL[template_create.template_type]
db_template = model(**settings)
model = TEMPLATE_TYPE_TO_MODEL[template_type]
db_template = model(**template_settings)
self._db_session.add(db_template)
await self._db_session.commit()
await self._db_session.refresh(db_template)
template = db_template._asjson()
self._controller.notification.controller_emit("template.created", template)
return template
return db_template
async def update_template(
self,
template_id: UUID,
template_update: schemas.TemplateUpdate) -> dict:
template_update: schemas.TemplateUpdate) -> schemas.Template:
update_values = template_update.dict(exclude_unset=True)
@@ -209,22 +80,16 @@ class TemplatesRepository(BaseRepository):
await self._db_session.execute(query)
await self._db_session.commit()
template = await self.get_template(template_id)
if template:
self._controller.notification.controller_emit("template.updated", template)
return template
return await self.get_template(template_id)
async def delete_template(self, template_id: UUID) -> bool:
query = delete(models.Template).where(models.Template.template_id == template_id)
result = await self._db_session.execute(query)
await self._db_session.commit()
if result.rowcount > 0:
self._controller.notification.controller_emit("template.deleted", {"template_id": str(template_id)})
return True
return False
return result.rowcount > 0
async def duplicate_template(self, template_id: UUID) -> dict:
async def duplicate_template(self, template_id: UUID) -> schemas.Template:
query = select(models.Template).where(models.Template.template_id == template_id)
db_template = (await self._db_session.execute(query)).scalars().first()
@@ -238,6 +103,4 @@ class TemplatesRepository(BaseRepository):
self._db_session.add(db_template)
await self._db_session.commit()
await self._db_session.refresh(db_template)
template = db_template._asjson()
self._controller.notification.controller_emit("template.created", template)
return template
return db_template
+1 -1
View File
@@ -31,7 +31,7 @@ log = logging.getLogger(__name__)
async def connect_to_db(app: FastAPI) -> None:
db_path = os.path.join(Config.instance().config_dir, "gns3_controller.db")
db_url = os.environ.get("GNS3_DATABASE_URI", f"sqlite+pysqlite:///{db_path}")
db_url = os.environ.get("GNS3_DATABASE_URI", f"sqlite+aiosqlite:///{db_path}")
engine = create_async_engine(db_url, connect_args={"check_same_thread": False}, future=True)
try:
async with engine.begin() as conn: