mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-04 06:21:15 +02:00
Refactor template management to use database.
This commit is contained in:
1 parent
b417bc4dec
commit
d730c591b3
20 files changed
+1704
-1535
No files matched your search
@@ -25,10 +25,8 @@ import asyncio
|
||||
|
||||
from ..config import Config
|
||||
from .project import Project
|
||||
from .template import Template
|
||||
from .appliance import Appliance
|
||||
from .appliance_manager import ApplianceManager
|
||||
from .template_manager import TemplateManager
|
||||
from .compute import Compute, ComputeError
|
||||
from .notification import Notification
|
||||
from .symbols import Symbols
|
||||
@@ -55,7 +53,6 @@ class Controller:
|
||||
self.gns3vm = GNS3VM(self)
|
||||
self.symbols = Symbols()
|
||||
self._appliance_manager = ApplianceManager()
|
||||
self._template_manager = TemplateManager()
|
||||
self._iou_license_settings = {"iourc_content": "",
|
||||
"license_check": True}
|
||||
self._config_loaded = False
|
||||
@@ -208,10 +205,6 @@ class Controller:
|
||||
"appliances_etag": self._appliance_manager.appliances_etag,
|
||||
"version": __version__}
|
||||
|
||||
for template in self._template_manager.templates.values():
|
||||
if not template.builtin:
|
||||
controller_settings["templates"].append(template.__json__())
|
||||
|
||||
for compute in self._computes.values():
|
||||
if compute.id != "local" and compute.id != "vm":
|
||||
controller_settings["computes"].append({"host": compute.host,
|
||||
@@ -259,7 +252,6 @@ class Controller:
|
||||
|
||||
self._appliance_manager.appliances_etag = controller_settings.get("appliances_etag")
|
||||
self._appliance_manager.load_appliances()
|
||||
self._template_manager.load_templates(controller_settings.get("templates"))
|
||||
self._config_loaded = True
|
||||
return controller_settings.get("computes", [])
|
||||
|
||||
@@ -546,14 +538,6 @@ class Controller:
|
||||
|
||||
return self._appliance_manager
|
||||
|
||||
@property
|
||||
def template_manager(self):
|
||||
"""
|
||||
:returns: Template Manager instance
|
||||
"""
|
||||
|
||||
return self._template_manager
|
||||
|
||||
@property
|
||||
def iou_license(self):
|
||||
"""
|
||||
|
||||
@@ -612,7 +612,6 @@ class Node:
|
||||
if the image exists
|
||||
"""
|
||||
|
||||
print("UPLOAD MISSING IMAGE")
|
||||
for directory in images_directories(type):
|
||||
image = os.path.join(directory, img)
|
||||
if os.path.exists(image):
|
||||
|
||||
@@ -500,16 +500,11 @@ class Project:
|
||||
return new_name
|
||||
|
||||
@open_required
|
||||
async def add_node_from_template(self, template_id, x=0, y=0, name=None, compute_id=None):
|
||||
async def add_node_from_template(self, template, x=0, y=0, name=None, compute_id=None):
|
||||
"""
|
||||
Create a node from a template.
|
||||
"""
|
||||
try:
|
||||
template = copy.deepcopy(self.controller.template_manager.templates[template_id].settings)
|
||||
except KeyError:
|
||||
msg = "Template {} doesn't exist".format(template_id)
|
||||
log.error(msg)
|
||||
raise ControllerNotFoundError(msg)
|
||||
|
||||
template["x"] = x
|
||||
template["y"] = y
|
||||
node_type = template.pop("template_type")
|
||||
|
||||
@@ -1,168 +0,0 @@
|
||||
#!/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 copy
|
||||
import uuid
|
||||
|
||||
from pydantic import ValidationError
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from gns3server import schemas
|
||||
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
ID_TO_CATEGORY = {
|
||||
3: "firewall",
|
||||
2: "guest",
|
||||
1: "switch",
|
||||
0: "router"
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
class Template:
|
||||
|
||||
def __init__(self, template_id, settings, builtin=False):
|
||||
|
||||
if template_id is None:
|
||||
self._id = str(uuid.uuid4())
|
||||
elif isinstance(template_id, uuid.UUID):
|
||||
self._id = str(template_id)
|
||||
else:
|
||||
self._id = template_id
|
||||
|
||||
self._settings = copy.deepcopy(settings)
|
||||
|
||||
# Version of the gui before 2.1 use linked_base
|
||||
# and the server linked_clone
|
||||
if "linked_base" in self.settings:
|
||||
linked_base = self._settings.pop("linked_base")
|
||||
if "linked_clone" not in self._settings:
|
||||
self._settings["linked_clone"] = linked_base
|
||||
|
||||
# Convert old GUI category to text category
|
||||
try:
|
||||
self._settings["category"] = ID_TO_CATEGORY[self._settings["category"]]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
# The "server" setting has been replaced by "compute_id" setting in version 2.2
|
||||
if "server" in self._settings:
|
||||
self._settings["compute_id"] = self._settings.pop("server")
|
||||
|
||||
# The "node_type" setting has been replaced by "template_type" setting in version 2.2
|
||||
if "node_type" in self._settings:
|
||||
self._settings["template_type"] = self._settings.pop("node_type")
|
||||
|
||||
# Remove an old IOU setting
|
||||
if self._settings["template_type"] == "iou" and "image" in self._settings:
|
||||
del self._settings["image"]
|
||||
|
||||
self._builtin = builtin
|
||||
|
||||
if builtin is False:
|
||||
try:
|
||||
template_schema = TEMPLATE_TYPE_TO_SHEMA[self.template_type]
|
||||
template_settings_with_defaults = template_schema.parse_obj(self.__json__())
|
||||
self._settings = jsonable_encoder(template_settings_with_defaults.dict())
|
||||
if self.template_type == "dynamips":
|
||||
# special case for Dynamips to cover all platform types that contain specific settings
|
||||
dynamips_template_schema = DYNAMIPS_PLATFORM_TO_SHEMA[self._settings["platform"]]
|
||||
dynamips_template_settings_with_defaults = dynamips_template_schema.parse_obj(self.__json__())
|
||||
self._settings = jsonable_encoder(dynamips_template_settings_with_defaults.dict())
|
||||
except ValidationError as e:
|
||||
print(e) #TODO: handle errors
|
||||
raise
|
||||
|
||||
log.debug('Template "{name}" [{id}] loaded'.format(name=self.name, id=self._id))
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def settings(self):
|
||||
return self._settings
|
||||
|
||||
@settings.setter
|
||||
def settings(self, settings):
|
||||
self._settings.update(settings)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._settings["name"]
|
||||
|
||||
@property
|
||||
def compute_id(self):
|
||||
return self._settings["compute_id"]
|
||||
|
||||
@property
|
||||
def template_type(self):
|
||||
return self._settings["template_type"]
|
||||
|
||||
@property
|
||||
def builtin(self):
|
||||
return self._builtin
|
||||
|
||||
def update(self, **kwargs):
|
||||
|
||||
from gns3server.controller import Controller
|
||||
controller = Controller.instance()
|
||||
Controller.instance().check_can_write_config()
|
||||
self._settings.update(kwargs)
|
||||
controller.notification.controller_emit("template.updated", self.__json__())
|
||||
controller.save()
|
||||
|
||||
def __json__(self):
|
||||
"""
|
||||
Template settings.
|
||||
"""
|
||||
|
||||
settings = self._settings
|
||||
settings.update({"template_id": self._id,
|
||||
"builtin": self.builtin})
|
||||
|
||||
if self.builtin:
|
||||
# builin templates have compute_id set to None to tell clients
|
||||
# to select a compute
|
||||
settings["compute_id"] = None
|
||||
else:
|
||||
settings["compute_id"] = self.compute_id
|
||||
|
||||
return settings
|
||||
@@ -1,148 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2019 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 copy
|
||||
import uuid
|
||||
import pydantic
|
||||
|
||||
from .controller_error import ControllerError, ControllerNotFoundError
|
||||
from .template import Template
|
||||
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TemplateManager:
|
||||
"""
|
||||
Manages templates.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self._templates = {}
|
||||
|
||||
@property
|
||||
def templates(self):
|
||||
"""
|
||||
:returns: The dictionary of templates managed by GNS3
|
||||
"""
|
||||
|
||||
return self._templates
|
||||
|
||||
def load_templates(self, template_settings=None):
|
||||
"""
|
||||
Loads templates from controller settings.
|
||||
"""
|
||||
|
||||
if template_settings:
|
||||
for template_settings in template_settings:
|
||||
try:
|
||||
template = Template(template_settings.get("template_id"), template_settings)
|
||||
self._templates[template.id] = template
|
||||
except pydantic.ValidationError as e:
|
||||
message = "Cannot load template with JSON data '{}': {}".format(template_settings, e)
|
||||
log.warning(message)
|
||||
continue
|
||||
|
||||
# Add builtins
|
||||
builtins = []
|
||||
builtins.append(Template(uuid.uuid3(uuid.NAMESPACE_DNS, "cloud"), {"template_type": "cloud", "name": "Cloud", "default_name_format": "Cloud{0}", "category": 2, "symbol": ":/symbols/cloud.svg"}, builtin=True))
|
||||
builtins.append(Template(uuid.uuid3(uuid.NAMESPACE_DNS, "nat"), {"template_type": "nat", "name": "NAT", "default_name_format": "NAT{0}", "category": 2, "symbol": ":/symbols/cloud.svg"}, builtin=True))
|
||||
builtins.append(Template(uuid.uuid3(uuid.NAMESPACE_DNS, "vpcs"), {"template_type": "vpcs", "name": "VPCS", "default_name_format": "PC{0}", "category": 2, "symbol": ":/symbols/vpcs_guest.svg", "properties": {"base_script_file": "vpcs_base_config.txt"}}, builtin=True))
|
||||
builtins.append(Template(uuid.uuid3(uuid.NAMESPACE_DNS, "ethernet_switch"), {"template_type": "ethernet_switch", "console_type": "none", "name": "Ethernet switch", "default_name_format": "Switch{0}", "category": 1, "symbol": ":/symbols/ethernet_switch.svg"}, builtin=True))
|
||||
builtins.append(Template(uuid.uuid3(uuid.NAMESPACE_DNS, "ethernet_hub"), {"template_type": "ethernet_hub", "name": "Ethernet hub", "default_name_format": "Hub{0}", "category": 1, "symbol": ":/symbols/hub.svg"}, builtin=True))
|
||||
builtins.append(Template(uuid.uuid3(uuid.NAMESPACE_DNS, "frame_relay_switch"), {"template_type": "frame_relay_switch", "name": "Frame Relay switch", "default_name_format": "FRSW{0}", "category": 1, "symbol": ":/symbols/frame_relay_switch.svg"}, builtin=True))
|
||||
builtins.append(Template(uuid.uuid3(uuid.NAMESPACE_DNS, "atm_switch"), {"template_type": "atm_switch", "name": "ATM switch", "default_name_format": "ATMSW{0}", "category": 1, "symbol": ":/symbols/atm_switch.svg"}, builtin=True))
|
||||
|
||||
#FIXME: disable TraceNG
|
||||
#if sys.platform.startswith("win"):
|
||||
# builtins.append(Template(uuid.uuid3(uuid.NAMESPACE_DNS, "traceng"), {"template_type": "traceng", "name": "TraceNG", "default_name_format": "TraceNG-{0}", "category": 2, "symbol": ":/symbols/traceng.svg", "properties": {}}, builtin=True))
|
||||
for b in builtins:
|
||||
self._templates[b.id] = b
|
||||
|
||||
def add_template(self, settings):
|
||||
"""
|
||||
Adds a new template.
|
||||
|
||||
:param settings: template settings
|
||||
|
||||
:returns: Template object
|
||||
"""
|
||||
|
||||
template_id = settings.get("template_id", "")
|
||||
if template_id in self._templates:
|
||||
raise ControllerError("Template ID '{}' already exists".format(template_id))
|
||||
else:
|
||||
template_id = settings.setdefault("template_id", str(uuid.uuid4()))
|
||||
try:
|
||||
template = Template(template_id, settings)
|
||||
except pydantic.ValidationError as e:
|
||||
message = "JSON schema error adding template with JSON data '{}': {}".format(settings, e)
|
||||
raise ControllerError(message)
|
||||
|
||||
from . import Controller
|
||||
Controller.instance().check_can_write_config()
|
||||
self._templates[template.id] = template
|
||||
Controller.instance().save()
|
||||
Controller.instance().notification.controller_emit("template.created", template.__json__())
|
||||
return template
|
||||
|
||||
def get_template(self, template_id):
|
||||
"""
|
||||
Gets a template.
|
||||
|
||||
:param template_id: template identifier
|
||||
|
||||
:returns: Template object
|
||||
"""
|
||||
|
||||
template = self._templates.get(template_id)
|
||||
if not template:
|
||||
raise ControllerNotFoundError("Template ID {} doesn't exist".format(template_id))
|
||||
return template
|
||||
|
||||
def delete_template(self, template_id):
|
||||
"""
|
||||
Deletes a template.
|
||||
|
||||
:param template_id: template identifier
|
||||
"""
|
||||
|
||||
template = self.get_template(template_id)
|
||||
if template.builtin:
|
||||
raise ControllerError("Template ID {} cannot be deleted because it is a builtin".format(template_id))
|
||||
from . import Controller
|
||||
Controller.instance().check_can_write_config()
|
||||
self._templates.pop(template_id)
|
||||
Controller.instance().save()
|
||||
Controller.instance().notification.controller_emit("template.deleted", template.__json__())
|
||||
|
||||
def duplicate_template(self, template_id):
|
||||
"""
|
||||
Duplicates a template.
|
||||
|
||||
:param template_id: template identifier
|
||||
"""
|
||||
|
||||
template = self.get_template(template_id)
|
||||
if template.builtin:
|
||||
raise ControllerError("Template ID {} cannot be duplicated because it is a builtin".format(template_id))
|
||||
template_settings = copy.deepcopy(template.settings)
|
||||
del template_settings["template_id"]
|
||||
return self.add_template(template_settings)
|
||||
|
||||
|
||||
Reference in new issue
Block a user