1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-01 04:38:12 +00:00

Base for dedicated appliance management API. Ref https://github.com/GNS3/gns3-server/issues/1427

This commit is contained in:
grossmj 2018-11-11 20:13:58 +08:00
parent 887b32c4bc
commit f0fe9d39fa
8 changed files with 359 additions and 157 deletions

View File

@ -128,71 +128,59 @@ class Controller:
log.warning("Cannot load appliance template file '%s': %s", path, str(e)) log.warning("Cannot load appliance template file '%s': %s", path, str(e))
continue continue
def add_appliance(self, settings):
"""
Adds a new appliance.
:param settings: appliance settings
:returns: Appliance 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))
else:
appliance_id = settings.setdefault("appliance_id", str(uuid.uuid4()))
try:
appliance = Appliance(appliance_id, settings)
appliance.__json__() # Check if loaded without error
except KeyError as e:
# appliance settings is not complete
raise aiohttp.web.HTTPConflict(text="Cannot create new appliance: key '{}' is missing for appliance ID '{}'".format(e, appliance_id))
self._appliances[appliance.id] = appliance
self.save()
return appliance
def get_appliance(self, appliance_id):
"""
Gets an appliance.
:param appliance_id: appliance identifier
:returns: Appliance 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
def delete_appliance(self, appliance_id):
"""
Deletes an appliance.
:param appliance_id: appliance identifier
"""
appliance = self._appliances.get(appliance_id)
if appliance.builtin:
raise aiohttp.web.HTTPConflict(text="Appliance ID {} cannot be deleted because it is builtin".format(appliance_id))
self._appliances.pop(appliance_id)
def load_appliances(self): def load_appliances(self):
self._appliances = {} #self._appliances = {}
vms = []
for vm in self._settings.get("Qemu", {}).get("vms", []):
vm["node_type"] = "qemu"
vms.append(vm)
for vm in self._settings.get("IOU", {}).get("devices", []):
vm["node_type"] = "iou"
vms.append(vm)
for vm in self._settings.get("Docker", {}).get("containers", []):
vm["node_type"] = "docker"
vms.append(vm)
for vm in self._settings.get("Builtin", {}).get("cloud_nodes", []):
vm["node_type"] = "cloud"
vms.append(vm)
for vm in self._settings.get("Builtin", {}).get("ethernet_switches", []):
vm["node_type"] = "ethernet_switch"
vms.append(vm)
for vm in self._settings.get("Builtin", {}).get("ethernet_hubs", []):
vm["node_type"] = "ethernet_hub"
vms.append(vm)
for vm in self._settings.get("Dynamips", {}).get("routers", []):
vm["node_type"] = "dynamips"
vms.append(vm)
for vm in self._settings.get("VMware", {}).get("vms", []):
vm["node_type"] = "vmware"
vms.append(vm)
for vm in self._settings.get("VirtualBox", {}).get("vms", []):
vm["node_type"] = "virtualbox"
vms.append(vm)
for vm in self._settings.get("VPCS", {}).get("nodes", []):
vm["node_type"] = "vpcs"
vms.append(vm)
for vm in self._settings.get("TraceNG", {}).get("nodes", []):
vm["node_type"] = "traceng"
vms.append(vm)
for vm in vms:
# remove deprecated properties
for prop in vm.copy():
if prop in ["enable_remote_console", "use_ubridge", "acpi_shutdown"]:
del vm[prop]
# remove deprecated default_symbol and hover_symbol
# and set symbol if not present
deprecated = ["default_symbol", "hover_symbol"]
if len([prop for prop in vm.keys() if prop in deprecated]) > 0:
if "default_symbol" in vm.keys():
del vm["default_symbol"]
if "hover_symbol" in vm.keys():
del vm["hover_symbol"]
if "symbol" not in vm.keys():
vm["symbol"] = ":/symbols/computer.svg"
vm.setdefault("appliance_id", str(uuid.uuid4()))
try:
appliance = Appliance(vm["appliance_id"], vm)
appliance.__json__() # Check if loaded without error
self._appliances[appliance.id] = appliance
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))
continue
# Add builtins # Add builtins
builtins = [] builtins = []
@ -288,31 +276,34 @@ class Controller:
# We don't save during the loading otherwise we could lost stuff # We don't save during the loading otherwise we could lost stuff
if self._settings is None: if self._settings is None:
return return
data = {
"computes": [], controller_settings = {"computes": [],
"settings": self._settings, "settings": self._settings,
"appliances": [],
"gns3vm": self.gns3vm.__json__(), "gns3vm": self.gns3vm.__json__(),
"appliance_templates_etag": self._appliance_templates_etag, "appliance_templates_etag": self._appliance_templates_etag,
"version": __version__ "version": __version__}
}
for appliance in self._appliances.values():
if not appliance.builtin:
controller_settings["appliances"].append(appliance.__json__())
for compute in self._computes.values():
if compute.id != "local" and compute.id != "vm":
controller_settings["computes"].append({"host": compute.host,
"name": compute.name,
"port": compute.port,
"protocol": compute.protocol,
"user": compute.user,
"password": compute.password,
"compute_id": compute.id})
for c in self._computes.values():
if c.id != "local" and c.id != "vm":
data["computes"].append({
"host": c.host,
"name": c.name,
"port": c.port,
"protocol": c.protocol,
"user": c.user,
"password": c.password,
"compute_id": c.id
})
try: try:
os.makedirs(os.path.dirname(self._config_file), exist_ok=True) os.makedirs(os.path.dirname(self._config_file), exist_ok=True)
with open(self._config_file, 'w+') as f: with open(self._config_file, 'w+') as f:
json.dump(data, f, indent=4) json.dump(controller_settings, f, indent=4)
except OSError as e: except OSError as e:
log.error("Cannnot write configuration file '{}': {}".format(self._config_file, e)) log.error("Cannot write controller configuration file '{}': {}".format(self._config_file, e))
async def _load_controller_settings(self): async def _load_controller_settings(self):
""" """
@ -324,23 +315,37 @@ class Controller:
await self._import_gns3_gui_conf() await self._import_gns3_gui_conf()
self.save() self.save()
with open(self._config_file) as f: with open(self._config_file) as f:
data = json.load(f) controller_settings = json.load(f)
except (OSError, ValueError) as e: except (OSError, ValueError) as e:
log.critical("Cannot load configuration file '{}': {}".format(self._config_file, e)) log.critical("Cannot load configuration file '{}': {}".format(self._config_file, e))
self._settings = {} self._settings = {}
return [] return []
if "settings" in data and data["settings"] is not None: if "settings" in controller_settings and controller_settings["settings"] is not None:
self._settings = data["settings"] self._settings = controller_settings["settings"]
else: else:
self._settings = {} self._settings = {}
if "gns3vm" in data:
self.gns3vm.settings = data["gns3vm"]
self._appliance_templates_etag = data.get("appliance_templates_etag") # load the appliances
if "appliances" in controller_settings:
for appliance_settings in controller_settings["appliances"]:
try:
appliance = Appliance(appliance_settings["appliance_id"], appliance_settings)
appliance.__json__() # Check if loaded without error
self._appliances[appliance.id] = appliance
except KeyError as e:
# appliance data is not complete (missing name or type)
log.warning("Cannot load appliance template {} ('{}'): missing key {}".format(appliance_settings["appliance_id"], appliance_settings.get("name", "unknown"), e))
continue
# load GNS3 VM settings
if "gns3vm" in controller_settings:
self.gns3vm.settings = controller_settings["gns3vm"]
self._appliance_templates_etag = controller_settings.get("appliance_templates_etag")
self.load_appliance_templates() self.load_appliance_templates()
self.load_appliances() self.load_appliances()
return data.get("computes", []) return controller_settings.get("computes", [])
async def load_projects(self): async def load_projects(self):
""" """
@ -416,18 +421,16 @@ class Controller:
config_file = os.path.join(os.path.dirname(self._config_file), "gns3_gui.conf") config_file = os.path.join(os.path.dirname(self._config_file), "gns3_gui.conf")
if os.path.exists(config_file): if os.path.exists(config_file):
with open(config_file) as f: with open(config_file) as f:
data = json.load(f) settings = json.load(f)
server_settings = data.get("Servers", {}) server_settings = settings.get("Servers", {})
for remote in server_settings.get("remote_servers", []): for remote in server_settings.get("remote_servers", []):
try: try:
await self.add_compute( await self.add_compute(host=remote.get("host", "localhost"),
host=remote.get("host", "localhost"),
port=remote.get("port", 3080), port=remote.get("port", 3080),
protocol=remote.get("protocol", "http"), protocol=remote.get("protocol", "http"),
name=remote.get("url"), name=remote.get("url"),
user=remote.get("user"), user=remote.get("user"),
password=remote.get("password") password=remote.get("password"))
)
except aiohttp.web.HTTPConflict: except aiohttp.web.HTTPConflict:
pass # if the server is broken we skip it pass # if the server is broken we skip it
if "vm" in server_settings: if "vm" in server_settings:
@ -458,6 +461,70 @@ class Controller:
"headless": vm_settings.get("headless", False), "headless": vm_settings.get("headless", False),
"vmname": vmname "vmname": vmname
} }
vms = []
for vm in settings.get("Qemu", {}).get("vms", []):
vm["node_type"] = "qemu"
vms.append(vm)
for vm in settings.get("IOU", {}).get("devices", []):
vm["node_type"] = "iou"
vms.append(vm)
for vm in settings.get("Docker", {}).get("containers", []):
vm["node_type"] = "docker"
vms.append(vm)
for vm in settings.get("Builtin", {}).get("cloud_nodes", []):
vm["node_type"] = "cloud"
vms.append(vm)
for vm in settings.get("Builtin", {}).get("ethernet_switches", []):
vm["node_type"] = "ethernet_switch"
vms.append(vm)
for vm in settings.get("Builtin", {}).get("ethernet_hubs", []):
vm["node_type"] = "ethernet_hub"
vms.append(vm)
for vm in settings.get("Dynamips", {}).get("routers", []):
vm["node_type"] = "dynamips"
vms.append(vm)
for vm in settings.get("VMware", {}).get("vms", []):
vm["node_type"] = "vmware"
vms.append(vm)
for vm in settings.get("VirtualBox", {}).get("vms", []):
vm["node_type"] = "virtualbox"
vms.append(vm)
for vm in settings.get("VPCS", {}).get("nodes", []):
vm["node_type"] = "vpcs"
vms.append(vm)
for vm in settings.get("TraceNG", {}).get("nodes", []):
vm["node_type"] = "traceng"
vms.append(vm)
for vm in vms:
# remove deprecated properties
for prop in vm.copy():
if prop in ["enable_remote_console", "use_ubridge", "acpi_shutdown"]:
del vm[prop]
# remove deprecated default_symbol and hover_symbol
# and set symbol if not present
deprecated = ["default_symbol", "hover_symbol"]
if len([prop for prop in vm.keys() if prop in deprecated]) > 0:
if "default_symbol" in vm.keys():
del vm["default_symbol"]
if "hover_symbol" in vm.keys():
del vm["hover_symbol"]
if "symbol" not in vm.keys():
vm["symbol"] = ":/symbols/computer.svg"
vm.setdefault("appliance_id", str(uuid.uuid4()))
try:
appliance = Appliance(vm["appliance_id"], vm)
appliance.__json__() # Check if loaded without error
self._appliances[appliance.id] = appliance
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))
continue
self._settings = {} self._settings = {}
@property @property

View File

@ -18,8 +18,6 @@
import copy import copy
import uuid import uuid
# Convert old GUI category to text category
ID_TO_CATEGORY = { ID_TO_CATEGORY = {
3: "firewall", 3: "firewall",
2: "guest", 2: "guest",
@ -30,7 +28,7 @@ ID_TO_CATEGORY = {
class Appliance: class Appliance:
def __init__(self, appliance_id, data, builtin=False): def __init__(self, appliance_id, settings, builtin=False):
if appliance_id is None: if appliance_id is None:
self._id = str(uuid.uuid4()) self._id = str(uuid.uuid4())
@ -38,18 +36,30 @@ class Appliance:
self._id = str(appliance_id) self._id = str(appliance_id)
else: else:
self._id = appliance_id self._id = appliance_id
self._data = data.copy()
if "appliance_id" in self._data: self._settings = copy.deepcopy(settings)
del self._data["appliance_id"]
# Version of the gui before 2.1 use linked_base # Version of the gui before 2.1 use linked_base
# and the server linked_clone # and the server linked_clone
if "linked_base" in self._data: if "linked_base" in self.settings:
linked_base = self._data.pop("linked_base") linked_base = self._settings.pop("linked_base")
if "linked_clone" not in self._data: if "linked_clone" not in self._settings:
self._data["linked_clone"] = linked_base self._settings["linked_clone"] = linked_base
if data["node_type"] == "iou" and "image" in data:
del self._data["image"] # 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")
# Remove an old IOU setting
if settings["node_type"] == "iou" and "image" in settings:
del self._settings["image"]
self._builtin = builtin self._builtin = builtin
@property @property
@ -57,16 +67,25 @@ class Appliance:
return self._id return self._id
@property @property
def data(self): def settings(self):
return copy.deepcopy(self._data) return self._settings
@settings.setter
def settings(self, settings):
self._settings.update(settings)
@property @property
def name(self): def name(self):
return self._data["name"] return self._settings["name"]
@property @property
def compute_id(self): def compute_id(self):
return self._data.get("server") return self._settings["compute_id"]
@property
def node_type(self):
return self._settings["node_type"]
@property @property
def builtin(self): def builtin(self):
@ -74,21 +93,15 @@ class Appliance:
def __json__(self): def __json__(self):
""" """
Appliance data (a hash) Appliance settings.
""" """
try: settings = self._settings
category = ID_TO_CATEGORY[self._data["category"]] settings.update({"appliance_id": self._id,
except KeyError: "default_name_format": settings.get("default_name_format", "{name}-{0}"),
category = self._data["category"] "symbol": settings.get("symbol", ":/symbols/computer.svg"),
"builtin": self.builtin})
return { if not self.builtin:
"appliance_id": self._id, settings["compute_id"] = self.compute_id
"node_type": self._data["node_type"],
"name": self._data["name"], return settings
"default_name_format": self._data.get("default_name_format", "{name}-{0}"),
"category": category,
"symbol": self._data.get("symbol", ":/symbols/computer.svg"),
"compute_id": self.compute_id,
"builtin": self._builtin,
"platform": self._data.get("platform", None)
}

View File

@ -271,8 +271,7 @@ class Node:
if self._label is None: if self._label is None:
# Apply to label user style or default # Apply to label user style or default
try: try:
style = qt_font_to_style( style = qt_font_to_style(self._project.controller.settings["GraphicsView"]["default_label_font"],
self._project.controller.settings["GraphicsView"]["default_label_font"],
self._project.controller.settings["GraphicsView"]["default_label_color"]) self._project.controller.settings["GraphicsView"]["default_label_color"])
except KeyError: except KeyError:
style = "font-size: 10;font-familly: Verdana" style = "font-size: 10;font-familly: Verdana"

View File

@ -465,7 +465,7 @@ class Project:
Create a node from an appliance Create a node from an appliance
""" """
try: try:
template = self.controller.appliances[appliance_id].data template = self.controller.appliances[appliance_id].settings
except KeyError: except KeyError:
msg = "Appliance {} doesn't exist".format(appliance_id) msg = "Appliance {} doesn't exist".format(appliance_id)
log.error(msg) log.error(msg)
@ -473,12 +473,13 @@ class Project:
template["x"] = x template["x"] = x
template["y"] = y template["y"] = y
node_type = template.pop("node_type") node_type = template.pop("node_type")
compute = self.controller.get_compute(template.pop("server", compute_id)) compute = self.controller.get_compute(template.pop("compute_id", compute_id))
name = template.pop("name") name = template.pop("name")
default_name_format = template.pop("default_name_format", "{name}-{0}") default_name_format = template.pop("default_name_format", "{name}-{0}")
name = default_name_format.replace("{name}", name) name = default_name_format.replace("{name}", name)
node_id = str(uuid.uuid4()) node_id = str(uuid.uuid4())
node = await self.add_node(compute, name, node_id, node_type=node_type, appliance_id=appliance_id, **template) template.pop("builtin") # not needed to add a node
node = await self.add_node(compute, name, node_id, node_type=node_type, **template)
return node return node
@open_required @open_required

View File

@ -20,6 +20,11 @@ from gns3server.controller import Controller
from gns3server.schemas.node import NODE_OBJECT_SCHEMA from gns3server.schemas.node import NODE_OBJECT_SCHEMA
from gns3server.schemas.appliance import APPLIANCE_USAGE_SCHEMA from gns3server.schemas.appliance import APPLIANCE_USAGE_SCHEMA
from gns3server.schemas.appliance import (
APPLIANCE_OBJECT_SCHEMA,
APPLIANCE_UPDATE_SCHEMA,
APPLIANCE_CREATE_SCHEMA
)
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -42,6 +47,74 @@ class ApplianceHandler:
controller.load_appliance_templates() controller.load_appliance_templates()
response.json([c for c in controller.appliance_templates.values()]) response.json([c for c in controller.appliance_templates.values()])
@Route.post(
r"/appliances",
description="Create a new appliance",
status_codes={
201: "Appliance created",
400: "Invalid request"
},
input=APPLIANCE_CREATE_SCHEMA,
output=APPLIANCE_OBJECT_SCHEMA)
def create(request, response):
controller = Controller.instance()
appliance = controller.add_appliance(request.json)
response.set_status(201)
response.json(appliance)
@Route.get(
r"/appliances/{appliance_id}",
status_codes={
200: "Appliance found",
400: "Invalid request",
404: "Appliance doesn't exist"
},
description="Get an appliance",
output=APPLIANCE_OBJECT_SCHEMA)
def get(request, response):
controller = Controller.instance()
appliance = controller.get_appliance(request.match_info["appliance_id"])
response.set_status(200)
response.json(appliance)
@Route.put(
r"/appliances/{appliance_id}",
status_codes={
200: "Appliance updated",
400: "Invalid request",
404: "Appliance doesn't exist"
},
description="Update an appliance",
input=APPLIANCE_UPDATE_SCHEMA,
output=APPLIANCE_OBJECT_SCHEMA)
def update(request, response):
controller = Controller.instance()
appliance = controller.get_appliance(request.match_info["appliance_id"])
#TODO: update appliance!
#appliance.settings = request.json
response.set_status(200)
response.json(appliance)
@Route.delete(
r"/appliances/{appliance_id}",
parameters={
"appliance_id": "Node UUID"
},
status_codes={
204: "Appliance deleted",
400: "Invalid request",
404: "Appliance doesn't exist"
},
description="Delete an appliance")
def delete(request, response):
controller = Controller.instance()
controller.delete_appliance(request.match_info["appliance_id"])
response.set_status(204)
@Route.get( @Route.get(
r"/appliances", r"/appliances",
description="List of appliance", description="List of appliance",
@ -50,6 +123,8 @@ class ApplianceHandler:
}) })
def list(request, response): def list(request, response):
#old_etag = request.headers.get('If-None-Match', '')
#print("ETAG => ", old_etag)
controller = Controller.instance() controller = Controller.instance()
response.json([c for c in controller.appliances.values()]) response.json([c for c in controller.appliances.values()])
@ -58,11 +133,11 @@ class ApplianceHandler:
description="Create a node from an appliance", description="Create a node from an appliance",
parameters={ parameters={
"project_id": "Project UUID", "project_id": "Project UUID",
"appliance_id": "Appliance template UUID" "appliance_id": "Appliance UUID"
}, },
status_codes={ status_codes={
201: "Node created", 201: "Node created",
404: "The project or template doesn't exist" 404: "The project or appliance doesn't exist"
}, },
input=APPLIANCE_USAGE_SCHEMA, input=APPLIANCE_USAGE_SCHEMA,
output=NODE_OBJECT_SCHEMA) output=NODE_OBJECT_SCHEMA)

View File

@ -76,7 +76,7 @@ class NodeHandler:
400: "Invalid request", 400: "Invalid request",
404: "Node doesn't exist" 404: "Node doesn't exist"
}, },
description="Update a node instance", description="Get a node",
output=NODE_OBJECT_SCHEMA) output=NODE_OBJECT_SCHEMA)
def get_node(request, response): def get_node(request, response):
project = Controller.instance().get_project(request.match_info["project_id"]) project = Controller.instance().get_project(request.match_info["project_id"])
@ -84,7 +84,6 @@ class NodeHandler:
response.set_status(200) response.set_status(200)
response.json(node) response.json(node)
@Route.put( @Route.put(
r"/projects/{project_id}/nodes/{node_id}", r"/projects/{project_id}/nodes/{node_id}",
status_codes={ status_codes={

View File

@ -15,6 +15,54 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import copy
from .node import NODE_TYPE_SCHEMA
APPLIANCE_OBJECT_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "A template object",
"type": "object",
"properties": {
"appliance_id": {
"description": "Appliance UUID from which the node has been created. Read only",
"type": ["null", "string"],
"minLength": 36,
"maxLength": 36,
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
},
"compute_id": {
"description": "Compute identifier",
"type": "string"
},
"node_type": NODE_TYPE_SCHEMA,
"name": {
"description": "Appliance name",
"type": "string",
"minLength": 1,
},
"default_name_format": {
"description": "Default name format",
"type": "string",
"minLength": 1,
},
"symbol": {
"description": "Symbol of the appliance",
"type": "string",
"minLength": 1
},
},
"additionalProperties": True, #TODO: validate all properties
"required": ["appliance_id", "compute_id", "node_type", "name", "default_name_format", "symbol"]
}
APPLIANCE_CREATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA)
# these properties are not required to create an appliance
APPLIANCE_CREATE_SCHEMA["required"].remove("appliance_id")
APPLIANCE_CREATE_SCHEMA["required"].remove("compute_id")
APPLIANCE_CREATE_SCHEMA["required"].remove("default_name_format")
APPLIANCE_CREATE_SCHEMA["required"].remove("symbol")
APPLIANCE_UPDATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA)
APPLIANCE_USAGE_SCHEMA = { APPLIANCE_USAGE_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#", "$schema": "http://json-schema.org/draft-04/schema#",