mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 22:11:14 +02:00
Use "template" to name what we use to create new nodes.
This commit is contained in:
1 parent
21c0a35f77
commit
d7c2e16c21
262 files changed
+16588
-16863
No files matched your search
+104
-106
@@ -28,8 +28,8 @@ import copy
|
||||
|
||||
from ..config import Config
|
||||
from .project import Project
|
||||
from .template import Template
|
||||
from .appliance import Appliance
|
||||
from .appliance_template import ApplianceTemplate
|
||||
from .compute import Compute, ComputeError
|
||||
from .notification import Notification
|
||||
from .symbols import Symbols
|
||||
@@ -59,38 +59,38 @@ class Controller:
|
||||
self._iou_license_settings = {"iourc_content": "",
|
||||
"license_check": True}
|
||||
self._config_loaded = False
|
||||
self._templates = {}
|
||||
self._appliances = {}
|
||||
self._appliance_templates = {}
|
||||
self._appliance_templates_etag = None
|
||||
self._appliances_etag = None
|
||||
|
||||
self._config_file = os.path.join(Config.instance().config_dir, "gns3_controller.conf")
|
||||
log.info("Load controller configuration file {}".format(self._config_file))
|
||||
|
||||
@locking
|
||||
async def download_appliance_templates(self):
|
||||
async def download_appliances(self):
|
||||
|
||||
try:
|
||||
headers = {}
|
||||
if self._appliance_templates_etag:
|
||||
log.info("Checking if appliance templates are up-to-date (ETag {})".format(self._appliance_templates_etag))
|
||||
headers["If-None-Match"] = self._appliance_templates_etag
|
||||
if self._appliances_etag:
|
||||
log.info("Checking if appliances are up-to-date (ETag {})".format(self._appliances_etag))
|
||||
headers["If-None-Match"] = self._appliances_etag
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get('https://api.github.com/repos/GNS3/gns3-registry/contents/appliances', headers=headers) as response:
|
||||
if response.status == 304:
|
||||
log.info("Appliance templates are already up-to-date (ETag {})".format(self._appliance_templates_etag))
|
||||
log.info("Appliances are already up-to-date (ETag {})".format(self._appliances_etag))
|
||||
return
|
||||
elif response.status != 200:
|
||||
raise aiohttp.web.HTTPConflict(text="Could not retrieve appliance templates on GitHub due to HTTP error code {}".format(response.status))
|
||||
raise aiohttp.web.HTTPConflict(text="Could not retrieve appliances on GitHub due to HTTP error code {}".format(response.status))
|
||||
etag = response.headers.get("ETag")
|
||||
if etag:
|
||||
self._appliance_templates_etag = etag
|
||||
self._appliances_etag = etag
|
||||
self.save()
|
||||
json_data = await response.json()
|
||||
appliances_dir = get_resource('appliances')
|
||||
for appliance in json_data:
|
||||
if appliance["type"] == "file":
|
||||
appliance_name = appliance["name"]
|
||||
log.info("Download appliance template file from '{}'".format(appliance["download_url"]))
|
||||
log.info("Download appliance file from '{}'".format(appliance["download_url"]))
|
||||
async with session.get(appliance["download_url"]) as response:
|
||||
if response.status != 200:
|
||||
log.warning("Could not download '{}' due to HTTP error code {}".format(appliance["download_url"], response.status))
|
||||
@@ -106,13 +106,13 @@ class Controller:
|
||||
with open(path, 'wb') as f:
|
||||
f.write(appliance_data)
|
||||
except OSError as e:
|
||||
raise aiohttp.web.HTTPConflict(text="Could not write appliance template file '{}': {}".format(path, e))
|
||||
raise aiohttp.web.HTTPConflict(text="Could not write appliance file '{}': {}".format(path, e))
|
||||
except ValueError as e:
|
||||
raise aiohttp.web.HTTPConflict(text="Could not read appliance templates information from GitHub: {}".format(e))
|
||||
raise aiohttp.web.HTTPConflict(text="Could not read appliances information from GitHub: {}".format(e))
|
||||
|
||||
def load_appliance_templates(self):
|
||||
def load_appliances(self):
|
||||
|
||||
self._appliance_templates = {}
|
||||
self._appliances = {}
|
||||
for directory, builtin in ((get_resource('appliances'), True,), (self.appliances_path(), False,)):
|
||||
if directory and os.path.isdir(directory):
|
||||
for file in os.listdir(directory):
|
||||
@@ -122,99 +122,97 @@ class Controller:
|
||||
appliance_id = uuid.uuid3(uuid.NAMESPACE_URL, path) # Generate UUID from path to avoid change between reboots
|
||||
try:
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
appliance = ApplianceTemplate(appliance_id, json.load(f), builtin=builtin)
|
||||
appliance = Appliance(appliance_id, json.load(f), builtin=builtin)
|
||||
appliance.__json__() # Check if loaded without error
|
||||
if appliance.status != 'broken':
|
||||
self._appliance_templates[appliance.id] = appliance
|
||||
self._appliances[appliance.id] = appliance
|
||||
except (ValueError, OSError, KeyError) as e:
|
||||
log.warning("Cannot load appliance template file '%s': %s", path, str(e))
|
||||
log.warning("Cannot load appliance file '%s': %s", path, str(e))
|
||||
continue
|
||||
|
||||
def add_appliance(self, settings):
|
||||
def add_template(self, settings):
|
||||
"""
|
||||
Adds a new appliance.
|
||||
Adds a new template.
|
||||
|
||||
:param settings: appliance settings
|
||||
:param settings: template settings
|
||||
|
||||
:returns: Appliance object
|
||||
:returns: Template object
|
||||
"""
|
||||
|
||||
appliance_id = settings.get("appliance_id", "")
|
||||
if appliance_id in self._appliances:
|
||||
raise aiohttp.web.HTTPConflict(text="Appliance ID '{}' already exists".format(appliance_id))
|
||||
template_id = settings.get("template_id", "")
|
||||
if template_id in self._templates:
|
||||
raise aiohttp.web.HTTPConflict(text="Template ID '{}' already exists".format(template_id))
|
||||
else:
|
||||
appliance_id = settings.setdefault("appliance_id", str(uuid.uuid4()))
|
||||
template_id = settings.setdefault("template_id", str(uuid.uuid4()))
|
||||
try:
|
||||
appliance = Appliance(appliance_id, settings)
|
||||
template = Template(template_id, settings)
|
||||
except jsonschema.ValidationError as e:
|
||||
message = "JSON schema error adding appliance with JSON data '{}': {}".format(settings, e.message)
|
||||
message = "JSON schema error adding template with JSON data '{}': {}".format(settings, e.message)
|
||||
raise aiohttp.web.HTTPBadRequest(text=message)
|
||||
self._appliances[appliance.id] = appliance
|
||||
self._templates[template.id] = template
|
||||
self.save()
|
||||
self.notification.controller_emit("appliance.created", appliance.__json__())
|
||||
return appliance
|
||||
self.notification.controller_emit("template.created", template.__json__())
|
||||
return template
|
||||
|
||||
def get_appliance(self, appliance_id):
|
||||
def get_template(self, template_id):
|
||||
"""
|
||||
Gets an appliance.
|
||||
Gets a template.
|
||||
|
||||
:param appliance_id: appliance identifier
|
||||
:param template_id: template identifier
|
||||
|
||||
:returns: Appliance object
|
||||
:returns: Template object
|
||||
"""
|
||||
|
||||
appliance = self._appliances.get(appliance_id)
|
||||
if not appliance:
|
||||
raise aiohttp.web.HTTPNotFound(text="Appliance ID {} doesn't exist".format(appliance_id))
|
||||
return appliance
|
||||
template = self._templates.get(template_id)
|
||||
if not template:
|
||||
raise aiohttp.web.HTTPNotFound(text="Template ID {} doesn't exist".format(template_id))
|
||||
return template
|
||||
|
||||
def delete_appliance(self, appliance_id):
|
||||
def delete_template(self, template_id):
|
||||
"""
|
||||
Deletes an appliance.
|
||||
Deletes a template.
|
||||
|
||||
:param appliance_id: appliance identifier
|
||||
:param template_id: template identifier
|
||||
"""
|
||||
|
||||
appliance = self.get_appliance(appliance_id)
|
||||
if appliance.builtin:
|
||||
raise aiohttp.web.HTTPConflict(text="Appliance ID {} cannot be deleted because it is a builtin".format(appliance_id))
|
||||
self._appliances.pop(appliance_id)
|
||||
template = self.get_template(template_id)
|
||||
if template.builtin:
|
||||
raise aiohttp.web.HTTPConflict(text="Template ID {} cannot be deleted because it is a builtin".format(template_id))
|
||||
self._templates.pop(template_id)
|
||||
self.save()
|
||||
self.notification.controller_emit("appliance.deleted", appliance.__json__())
|
||||
self.notification.controller_emit("template.deleted", template.__json__())
|
||||
|
||||
def duplicate_appliance(self, appliance_id):
|
||||
def duplicate_template(self, template_id):
|
||||
"""
|
||||
Duplicates an appliance.
|
||||
Duplicates a template.
|
||||
|
||||
:param appliance_id: appliance identifier
|
||||
:param template_id: template identifier
|
||||
"""
|
||||
|
||||
appliance = self.get_appliance(appliance_id)
|
||||
if appliance.builtin:
|
||||
raise aiohttp.web.HTTPConflict(text="Appliance ID {} cannot be duplicated because it is a builtin".format(appliance_id))
|
||||
appliance_settings = copy.deepcopy(appliance.settings)
|
||||
del appliance_settings["appliance_id"]
|
||||
return self.add_appliance(appliance_settings)
|
||||
template = self.get_template(template_id)
|
||||
if template.builtin:
|
||||
raise aiohttp.web.HTTPConflict(text="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)
|
||||
|
||||
def load_appliances(self):
|
||||
|
||||
#self._appliances = {}
|
||||
def load_templates(self):
|
||||
|
||||
# Add builtins
|
||||
builtins = []
|
||||
builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "cloud"), {"appliance_type": "cloud", "name": "Cloud", "category": 2, "symbol": ":/symbols/cloud.svg"}, builtin=True))
|
||||
builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "nat"), {"appliance_type": "nat", "name": "NAT", "category": 2, "symbol": ":/symbols/cloud.svg"}, builtin=True))
|
||||
builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "vpcs"), {"appliance_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(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "ethernet_switch"), {"appliance_type": "ethernet_switch", "console_type": "telnet", "name": "Ethernet switch", "category": 1, "symbol": ":/symbols/ethernet_switch.svg"}, builtin=True))
|
||||
builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "ethernet_hub"), {"appliance_type": "ethernet_hub", "name": "Ethernet hub", "category": 1, "symbol": ":/symbols/hub.svg"}, builtin=True))
|
||||
builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "frame_relay_switch"), {"appliance_type": "frame_relay_switch", "name": "Frame Relay switch", "category": 1, "symbol": ":/symbols/frame_relay_switch.svg"}, builtin=True))
|
||||
builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "atm_switch"), {"appliance_type": "atm_switch", "name": "ATM switch", "category": 1, "symbol": ":/symbols/atm_switch.svg"}, builtin=True))
|
||||
builtins.append(Template(uuid.uuid3(uuid.NAMESPACE_DNS, "cloud"), {"template_type": "cloud", "name": "Cloud", "category": 2, "symbol": ":/symbols/cloud.svg"}, builtin=True))
|
||||
builtins.append(Template(uuid.uuid3(uuid.NAMESPACE_DNS, "nat"), {"template_type": "nat", "name": "NAT", "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": "telnet", "name": "Ethernet switch", "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", "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", "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", "category": 1, "symbol": ":/symbols/atm_switch.svg"}, builtin=True))
|
||||
|
||||
#FIXME: disable TraceNG
|
||||
#if sys.platform.startswith("win"):
|
||||
# builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "traceng"), {"appliance_type": "traceng", "name": "TraceNG", "default_name_format": "TraceNG-{0}", "category": 2, "symbol": ":/symbols/traceng.svg", "properties": {}}, builtin=True))
|
||||
# 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._appliances[b.id] = b
|
||||
self._templates[b.id] = b
|
||||
|
||||
async def start(self):
|
||||
|
||||
@@ -295,15 +293,15 @@ class Controller:
|
||||
return
|
||||
|
||||
controller_settings = {"computes": [],
|
||||
"appliances": [],
|
||||
"templates": [],
|
||||
"gns3vm": self.gns3vm.__json__(),
|
||||
"iou_license": self._iou_license_settings,
|
||||
"appliance_templates_etag": self._appliance_templates_etag,
|
||||
"appliances_etag": self._appliances_etag,
|
||||
"version": __version__}
|
||||
|
||||
for appliance in self._appliances.values():
|
||||
if not appliance.builtin:
|
||||
controller_settings["appliances"].append(appliance.__json__())
|
||||
for template in self._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":
|
||||
@@ -337,14 +335,14 @@ class Controller:
|
||||
log.critical("Cannot load configuration file '{}': {}".format(self._config_file, e))
|
||||
return []
|
||||
|
||||
# load the appliances
|
||||
if "appliances" in controller_settings:
|
||||
for appliance_settings in controller_settings["appliances"]:
|
||||
# load the templates
|
||||
if "templates" in controller_settings:
|
||||
for template_settings in controller_settings["templates"]:
|
||||
try:
|
||||
appliance = Appliance(appliance_settings.get("appliance_id"), appliance_settings)
|
||||
self._appliances[appliance.id] = appliance
|
||||
template = Template(template_settings.get("template_id"), template_settings)
|
||||
self._templates[template.id] = template
|
||||
except jsonschema.ValidationError as e:
|
||||
message = "Cannot load appliance with JSON data '{}': {}".format(appliance_settings, e.message)
|
||||
message = "Cannot load template with JSON data '{}': {}".format(template_settings, e.message)
|
||||
log.warning(message)
|
||||
continue
|
||||
|
||||
@@ -356,9 +354,9 @@ class Controller:
|
||||
if "iou_license" in controller_settings:
|
||||
self._iou_license_settings = controller_settings["iou_license"]
|
||||
|
||||
self._appliance_templates_etag = controller_settings.get("appliance_templates_etag")
|
||||
self.load_appliance_templates()
|
||||
self._appliances_etag = controller_settings.get("appliances_etag")
|
||||
self.load_appliances()
|
||||
self.load_templates()
|
||||
self._config_loaded = True
|
||||
return controller_settings.get("computes", [])
|
||||
|
||||
@@ -479,37 +477,37 @@ class Controller:
|
||||
|
||||
vms = []
|
||||
for vm in settings.get("Qemu", {}).get("vms", []):
|
||||
vm["appliance_type"] = "qemu"
|
||||
vm["template_type"] = "qemu"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("IOU", {}).get("devices", []):
|
||||
vm["appliance_type"] = "iou"
|
||||
vm["template_type"] = "iou"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("Docker", {}).get("containers", []):
|
||||
vm["appliance_type"] = "docker"
|
||||
vm["template_type"] = "docker"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("Builtin", {}).get("cloud_nodes", []):
|
||||
vm["appliance_type"] = "cloud"
|
||||
vm["template_type"] = "cloud"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("Builtin", {}).get("ethernet_switches", []):
|
||||
vm["appliance_type"] = "ethernet_switch"
|
||||
vm["template_type"] = "ethernet_switch"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("Builtin", {}).get("ethernet_hubs", []):
|
||||
vm["appliance_type"] = "ethernet_hub"
|
||||
vm["template_type"] = "ethernet_hub"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("Dynamips", {}).get("routers", []):
|
||||
vm["appliance_type"] = "dynamips"
|
||||
vm["template_type"] = "dynamips"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("VMware", {}).get("vms", []):
|
||||
vm["appliance_type"] = "vmware"
|
||||
vm["template_type"] = "vmware"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("VirtualBox", {}).get("vms", []):
|
||||
vm["appliance_type"] = "virtualbox"
|
||||
vm["template_type"] = "virtualbox"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("VPCS", {}).get("nodes", []):
|
||||
vm["appliance_type"] = "vpcs"
|
||||
vm["template_type"] = "vpcs"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("TraceNG", {}).get("nodes", []):
|
||||
vm["appliance_type"] = "traceng"
|
||||
vm["template_type"] = "traceng"
|
||||
vms.append(vm)
|
||||
|
||||
for vm in vms:
|
||||
@@ -530,14 +528,14 @@ class Controller:
|
||||
if "symbol" not in vm.keys():
|
||||
vm["symbol"] = ":/symbols/computer.svg"
|
||||
|
||||
vm.setdefault("appliance_id", str(uuid.uuid4()))
|
||||
vm.setdefault("template_id", str(uuid.uuid4()))
|
||||
try:
|
||||
appliance = Appliance(vm["appliance_id"], vm)
|
||||
appliance.__json__() # Check if loaded without error
|
||||
self._appliances[appliance.id] = appliance
|
||||
template = Template(vm["template_id"], vm)
|
||||
template.__json__() # Check if loaded without error
|
||||
self._templates[template.id] = template
|
||||
except KeyError as e:
|
||||
# appliance data is not complete (missing name or type)
|
||||
log.warning("Cannot load appliance template {} ('{}'): missing key {}".format(vm["appliance_id"], vm.get("name", "unknown"), e))
|
||||
# template data is not complete (missing name or type)
|
||||
log.warning("Cannot load template {} ('{}'): missing key {}".format(vm["template_id"], vm.get("name", "unknown"), e))
|
||||
continue
|
||||
|
||||
async def add_compute(self, compute_id=None, name=None, force=False, connect=True, **kwargs):
|
||||
@@ -759,14 +757,6 @@ class Controller:
|
||||
|
||||
return self._projects
|
||||
|
||||
@property
|
||||
def appliance_templates(self):
|
||||
"""
|
||||
:returns: The dictionary of appliances templates managed by GNS3
|
||||
"""
|
||||
|
||||
return self._appliance_templates
|
||||
|
||||
@property
|
||||
def appliances(self):
|
||||
"""
|
||||
@@ -775,6 +765,14 @@ class Controller:
|
||||
|
||||
return self._appliances
|
||||
|
||||
@property
|
||||
def templates(self):
|
||||
"""
|
||||
:returns: The dictionary of templates managed by GNS3
|
||||
"""
|
||||
|
||||
return self._templates
|
||||
|
||||
@property
|
||||
def iou_license(self):
|
||||
"""
|
||||
|
||||
@@ -17,193 +17,34 @@
|
||||
|
||||
import copy
|
||||
import uuid
|
||||
import json
|
||||
import jsonschema
|
||||
|
||||
from gns3server.schemas.cloud_appliance import CLOUD_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.ethernet_switch_appliance import ETHERNET_SWITCH_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.ethernet_hub_appliance import ETHERNET_HUB_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.docker_appliance import DOCKER_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.vpcs_appliance import VPCS_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.traceng_appliance import TRACENG_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.virtualbox_appliance import VIRTUALBOX_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.vmware_appliance import VMWARE_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.iou_appliance import IOU_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.qemu_appliance import QEMU_APPLIANCE_OBJECT_SCHEMA
|
||||
|
||||
from gns3server.schemas.dynamips_appliance import (
|
||||
DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C7200_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C3745_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C3725_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C3600_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C2691_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C2600_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C1700_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA
|
||||
)
|
||||
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Add default values for missing entries in a request, largely taken from jsonschema documentation example
|
||||
# https://python-jsonschema.readthedocs.io/en/latest/faq/#why-doesn-t-my-schema-s-default-property-set-the-default-on-my-instance
|
||||
def extend_with_default(validator_class):
|
||||
|
||||
validate_properties = validator_class.VALIDATORS["properties"]
|
||||
def set_defaults(validator, properties, instance, schema):
|
||||
if jsonschema.Draft4Validator(schema).is_valid(instance):
|
||||
# only add default for the matching sub-schema (e.g. when using 'oneOf')
|
||||
for property, subschema in properties.items():
|
||||
if "default" in subschema:
|
||||
instance.setdefault(property, subschema["default"])
|
||||
|
||||
for error in validate_properties(validator, properties, instance, schema,):
|
||||
yield error
|
||||
|
||||
return jsonschema.validators.extend(
|
||||
validator_class, {"properties" : set_defaults},
|
||||
)
|
||||
|
||||
|
||||
ValidatorWithDefaults = extend_with_default(jsonschema.Draft4Validator)
|
||||
|
||||
ID_TO_CATEGORY = {
|
||||
3: "firewall",
|
||||
2: "guest",
|
||||
1: "switch",
|
||||
0: "router"
|
||||
}
|
||||
|
||||
APPLIANCE_TYPE_TO_SHEMA = {
|
||||
"cloud": CLOUD_APPLIANCE_OBJECT_SCHEMA,
|
||||
"ethernet_hub": ETHERNET_HUB_APPLIANCE_OBJECT_SCHEMA,
|
||||
"ethernet_switch": ETHERNET_SWITCH_APPLIANCE_OBJECT_SCHEMA,
|
||||
"docker": DOCKER_APPLIANCE_OBJECT_SCHEMA,
|
||||
"dynamips": DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"vpcs": VPCS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"traceng": TRACENG_APPLIANCE_OBJECT_SCHEMA,
|
||||
"virtualbox": VIRTUALBOX_APPLIANCE_OBJECT_SCHEMA,
|
||||
"vmware": VMWARE_APPLIANCE_OBJECT_SCHEMA,
|
||||
"iou": IOU_APPLIANCE_OBJECT_SCHEMA,
|
||||
"qemu": QEMU_APPLIANCE_OBJECT_SCHEMA
|
||||
}
|
||||
|
||||
DYNAMIPS_PLATFORM_TO_SHEMA = {
|
||||
"c7200": C7200_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c3745": C3745_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c3725": C3725_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c3600": C3600_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c2691": C2691_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c2600": C2600_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c1700": C1700_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA
|
||||
}
|
||||
|
||||
|
||||
class Appliance:
|
||||
|
||||
def __init__(self, appliance_id, settings, builtin=False):
|
||||
|
||||
def __init__(self, appliance_id, data, builtin=True):
|
||||
if appliance_id is None:
|
||||
self._id = str(uuid.uuid4())
|
||||
elif isinstance(appliance_id, uuid.UUID):
|
||||
self._id = str(appliance_id)
|
||||
else:
|
||||
self._id = appliance_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 "appliance_type" setting in version 2.2
|
||||
if "node_type" in self._settings:
|
||||
self._settings["appliance_type"] = self._settings.pop("node_type")
|
||||
|
||||
# Remove an old IOU setting
|
||||
if self._settings["appliance_type"] == "iou" and "image" in self._settings:
|
||||
del self._settings["image"]
|
||||
|
||||
self._data = data.copy()
|
||||
self._builtin = builtin
|
||||
|
||||
if builtin is False:
|
||||
self.validate_and_apply_defaults(APPLIANCE_TYPE_TO_SHEMA[self.appliance_type])
|
||||
|
||||
if self.appliance_type == "dynamips":
|
||||
# special case for Dynamips to cover all platform types that contain specific settings
|
||||
self.validate_and_apply_defaults(DYNAMIPS_PLATFORM_TO_SHEMA[self._settings["platform"]])
|
||||
if "appliance_id" in self._data:
|
||||
del self._data["appliance_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 appliance_type(self):
|
||||
return self._settings["appliance_type"]
|
||||
|
||||
@property
|
||||
def builtin(self):
|
||||
return self._builtin
|
||||
|
||||
def update(self, **kwargs):
|
||||
|
||||
self._settings.update(kwargs)
|
||||
from gns3server.controller import Controller
|
||||
controller = Controller.instance()
|
||||
controller.notification.controller_emit("appliance.updated", self.__json__())
|
||||
controller.save()
|
||||
|
||||
def validate_and_apply_defaults(self, schema):
|
||||
|
||||
validator = ValidatorWithDefaults(schema)
|
||||
try:
|
||||
validator.validate(self.__json__())
|
||||
except jsonschema.ValidationError as e:
|
||||
message = "JSON schema error {}".format(e.message)
|
||||
log.error(message)
|
||||
log.debug("Input schema: {}".format(json.dumps(schema)))
|
||||
raise
|
||||
def status(self):
|
||||
return self._data["status"]
|
||||
|
||||
def __json__(self):
|
||||
"""
|
||||
Appliance settings.
|
||||
Appliance data (a hash)
|
||||
"""
|
||||
|
||||
settings = self._settings
|
||||
settings.update({"appliance_id": self._id,
|
||||
"builtin": self.builtin})
|
||||
|
||||
if not self.builtin:
|
||||
settings["compute_id"] = self.compute_id
|
||||
|
||||
return settings
|
||||
data = copy.deepcopy(self._data)
|
||||
data["builtin"] = self._builtin
|
||||
return data
|
||||
@@ -1,50 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 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
|
||||
|
||||
|
||||
class ApplianceTemplate:
|
||||
|
||||
def __init__(self, appliance_id, data, builtin=True):
|
||||
if appliance_id is None:
|
||||
self._id = str(uuid.uuid4())
|
||||
elif isinstance(appliance_id, uuid.UUID):
|
||||
self._id = str(appliance_id)
|
||||
else:
|
||||
self._id = appliance_id
|
||||
self._data = data.copy()
|
||||
self._builtin = builtin
|
||||
if "appliance_id" in self._data:
|
||||
del self._data["appliance_id"]
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def status(self):
|
||||
return self._data["status"]
|
||||
|
||||
def __json__(self):
|
||||
"""
|
||||
Appliance data (a hash)
|
||||
"""
|
||||
data = copy.deepcopy(self._data)
|
||||
data["builtin"] = self._builtin
|
||||
return data
|
||||
@@ -38,14 +38,14 @@ class Node:
|
||||
"port_name_format", "first_port_name", "port_segment_size", "ports",
|
||||
"category", "console_auto_start"]
|
||||
|
||||
def __init__(self, project, compute, name, node_id=None, node_type=None, appliance_id=None, **kwargs):
|
||||
def __init__(self, project, compute, name, node_id=None, node_type=None, template_id=None, **kwargs):
|
||||
"""
|
||||
:param project: Project of the node
|
||||
:param compute: Compute where the server will run
|
||||
:param name: Node name
|
||||
:param node_id: UUID of the node (integer)
|
||||
:param node_type: Type of emulator
|
||||
:param appliance_id: Appliance ID used to create this node
|
||||
:param template_id: Template ID used to create this node
|
||||
:param kwargs: Node properties
|
||||
"""
|
||||
|
||||
@@ -70,7 +70,7 @@ class Node:
|
||||
self._command_line = None
|
||||
self._node_directory = None
|
||||
self._status = "stopped"
|
||||
self._appliance_id = appliance_id
|
||||
self._template_id = template_id
|
||||
self._x = 0
|
||||
self._y = 0
|
||||
self._z = 1 # default z value is 1
|
||||
@@ -671,7 +671,7 @@ class Node:
|
||||
"compute_id": str(self._compute.id),
|
||||
"node_id": self._id,
|
||||
"node_type": self._node_type,
|
||||
"appliance_id": self._appliance_id,
|
||||
"template_id": self._template_id,
|
||||
"name": self._name,
|
||||
"console": self._console,
|
||||
"console_type": self._console_type,
|
||||
@@ -693,7 +693,7 @@ class Node:
|
||||
"compute_id": str(self._compute.id),
|
||||
"project_id": self._project.id,
|
||||
"node_id": self._id,
|
||||
"appliance_id": self._appliance_id,
|
||||
"template_id": self._template_id,
|
||||
"node_type": self._node_type,
|
||||
"node_directory": self._node_directory,
|
||||
"name": self._name,
|
||||
|
||||
@@ -476,19 +476,19 @@ class Project:
|
||||
return new_name
|
||||
|
||||
@open_required
|
||||
async def add_node_from_appliance(self, appliance_id, x=0, y=0, compute_id=None):
|
||||
async def add_node_from_template(self, template_id, x=0, y=0, compute_id=None):
|
||||
"""
|
||||
Create a node from an appliance
|
||||
Create a node from a template.
|
||||
"""
|
||||
try:
|
||||
template = copy.deepcopy(self.controller.appliances[appliance_id].settings)
|
||||
template = copy.deepcopy(self.controller.templates[template_id].settings)
|
||||
except KeyError:
|
||||
msg = "Appliance {} doesn't exist".format(appliance_id)
|
||||
msg = "Template {} doesn't exist".format(template_id)
|
||||
log.error(msg)
|
||||
raise aiohttp.web.HTTPNotFound(text=msg)
|
||||
template["x"] = x
|
||||
template["y"] = y
|
||||
node_type = template.pop("appliance_type")
|
||||
node_type = template.pop("template_type")
|
||||
compute = self.controller.get_compute(template.pop("compute_id", compute_id))
|
||||
name = template.pop("name")
|
||||
default_name_format = template.pop("default_name_format", "{name}-{0}")
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 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 json
|
||||
import jsonschema
|
||||
|
||||
from gns3server.schemas.cloud_template import CLOUD_TEMPLATE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.ethernet_switch_template import ETHERNET_SWITCH_TEMPLATE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.ethernet_hub_template import ETHERNET_HUB_TEMPLATE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.docker_template import DOCKER_TEMPLATE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.vpcs_template import VPCS_TEMPLATE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.traceng_template import TRACENG_TEMPLATE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.virtualbox_template import VIRTUALBOX_TEMPLATE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.vmware_template import VMWARE_TEMPLATE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.iou_template import IOU_TEMPLATE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.qemu_template import QEMU_TEMPLATE_OBJECT_SCHEMA
|
||||
|
||||
from gns3server.schemas.dynamips_template import (
|
||||
DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
C7200_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
C3745_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
C3725_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
C3600_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
C2691_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
C2600_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
C1700_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA
|
||||
)
|
||||
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Add default values for missing entries in a request, largely taken from jsonschema documentation example
|
||||
# https://python-jsonschema.readthedocs.io/en/latest/faq/#why-doesn-t-my-schema-s-default-property-set-the-default-on-my-instance
|
||||
def extend_with_default(validator_class):
|
||||
|
||||
validate_properties = validator_class.VALIDATORS["properties"]
|
||||
def set_defaults(validator, properties, instance, schema):
|
||||
if jsonschema.Draft4Validator(schema).is_valid(instance):
|
||||
# only add default for the matching sub-schema (e.g. when using 'oneOf')
|
||||
for property, subschema in properties.items():
|
||||
if "default" in subschema:
|
||||
instance.setdefault(property, subschema["default"])
|
||||
|
||||
for error in validate_properties(validator, properties, instance, schema,):
|
||||
yield error
|
||||
|
||||
return jsonschema.validators.extend(
|
||||
validator_class, {"properties" : set_defaults},
|
||||
)
|
||||
|
||||
|
||||
ValidatorWithDefaults = extend_with_default(jsonschema.Draft4Validator)
|
||||
|
||||
ID_TO_CATEGORY = {
|
||||
3: "firewall",
|
||||
2: "guest",
|
||||
1: "switch",
|
||||
0: "router"
|
||||
}
|
||||
|
||||
TEMPLATE_TYPE_TO_SHEMA = {
|
||||
"cloud": CLOUD_TEMPLATE_OBJECT_SCHEMA,
|
||||
"ethernet_hub": ETHERNET_HUB_TEMPLATE_OBJECT_SCHEMA,
|
||||
"ethernet_switch": ETHERNET_SWITCH_TEMPLATE_OBJECT_SCHEMA,
|
||||
"docker": DOCKER_TEMPLATE_OBJECT_SCHEMA,
|
||||
"dynamips": DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
"vpcs": VPCS_TEMPLATE_OBJECT_SCHEMA,
|
||||
"traceng": TRACENG_TEMPLATE_OBJECT_SCHEMA,
|
||||
"virtualbox": VIRTUALBOX_TEMPLATE_OBJECT_SCHEMA,
|
||||
"vmware": VMWARE_TEMPLATE_OBJECT_SCHEMA,
|
||||
"iou": IOU_TEMPLATE_OBJECT_SCHEMA,
|
||||
"qemu": QEMU_TEMPLATE_OBJECT_SCHEMA
|
||||
}
|
||||
|
||||
DYNAMIPS_PLATFORM_TO_SHEMA = {
|
||||
"c7200": C7200_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
"c3745": C3745_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
"c3725": C3725_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
"c3600": C3600_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
"c2691": C2691_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
"c2600": C2600_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA,
|
||||
"c1700": C1700_DYNAMIPS_TEMPLATE_OBJECT_SCHEMA
|
||||
}
|
||||
|
||||
|
||||
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:
|
||||
self.validate_and_apply_defaults(TEMPLATE_TYPE_TO_SHEMA[self.template_type])
|
||||
|
||||
if self.template_type == "dynamips":
|
||||
# special case for Dynamips to cover all platform types that contain specific settings
|
||||
self.validate_and_apply_defaults(DYNAMIPS_PLATFORM_TO_SHEMA[self._settings["platform"]])
|
||||
|
||||
@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):
|
||||
|
||||
self._settings.update(kwargs)
|
||||
from gns3server.controller import Controller
|
||||
controller = Controller.instance()
|
||||
controller.notification.controller_emit("template.updated", self.__json__())
|
||||
controller.save()
|
||||
|
||||
def validate_and_apply_defaults(self, schema):
|
||||
|
||||
validator = ValidatorWithDefaults(schema)
|
||||
try:
|
||||
validator.validate(self.__json__())
|
||||
except jsonschema.ValidationError as e:
|
||||
message = "JSON schema error {}".format(e.message)
|
||||
log.error(message)
|
||||
log.debug("Input schema: {}".format(json.dumps(schema)))
|
||||
raise
|
||||
|
||||
def __json__(self):
|
||||
"""
|
||||
Template settings.
|
||||
"""
|
||||
|
||||
settings = self._settings
|
||||
settings.update({"template_id": self._id,
|
||||
"builtin": self.builtin})
|
||||
|
||||
if not self.builtin:
|
||||
settings["compute_id"] = self.compute_id
|
||||
|
||||
return settings
|
||||
@@ -159,6 +159,14 @@ def load_topology(path):
|
||||
if topo["revision"] < 9:
|
||||
topo = _convert_2_1_0(topo, path)
|
||||
|
||||
# Version GNS3 2.2 dev (for project created with 2.2dev).
|
||||
# Appliance ID has been repleace by Template ID
|
||||
if topo["revision"] == 9:
|
||||
for node in topo.get("topology", {}).get("nodes", []):
|
||||
if "appliance_id" in node:
|
||||
node["template_id"] = node["appliance_id"]
|
||||
del node["appliance_id"]
|
||||
|
||||
try:
|
||||
_check_topology_schema(topo)
|
||||
except aiohttp.web.HTTPConflict as e:
|
||||
|
||||
Reference in new issue
Block a user