2016-03-10 20:51:29 +00:00
|
|
|
#!/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/>.
|
|
|
|
|
2016-08-26 12:09:18 +00:00
|
|
|
import aiohttp
|
2016-03-10 20:51:29 +00:00
|
|
|
import asyncio
|
2017-03-20 18:14:07 +00:00
|
|
|
import html
|
2016-03-11 14:49:28 +00:00
|
|
|
import copy
|
2016-03-10 20:51:29 +00:00
|
|
|
import uuid
|
2016-06-07 17:38:01 +00:00
|
|
|
import os
|
|
|
|
|
2016-11-11 09:38:59 +00:00
|
|
|
from .compute import ComputeConflict, ComputeError
|
2016-09-13 07:47:22 +00:00
|
|
|
from .ports.port_factory import PortFactory, StandardPortFactory, DynamipsPortFactory
|
2016-06-07 17:38:01 +00:00
|
|
|
from ..utils.images import images_directories
|
2016-08-17 15:12:23 +00:00
|
|
|
from ..utils.qt import qt_font_to_style
|
2016-03-10 20:51:29 +00:00
|
|
|
|
2016-06-09 14:47:58 +00:00
|
|
|
|
2016-06-15 13:12:38 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
class Node:
|
2016-06-09 14:47:58 +00:00
|
|
|
# This properties are used only on controller and are not forwarded to the compute
|
2016-09-13 07:47:22 +00:00
|
|
|
CONTROLLER_ONLY_PROPERTIES = ["x", "y", "z", "width", "height", "symbol", "label", "console_host",
|
2017-05-16 08:22:55 +00:00
|
|
|
"port_name_format", "first_port_name", "port_segment_size", "ports",
|
2018-04-04 14:31:35 +00:00
|
|
|
"category", "console_auto_start"]
|
2016-03-11 14:06:14 +00:00
|
|
|
|
2018-08-09 10:45:33 +00:00
|
|
|
def __init__(self, project, compute, name, node_id=None, node_type=None, appliance_id=None, **kwargs):
|
2016-03-10 20:51:29 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
:param project: Project of the node
|
|
|
|
:param compute: Compute server where the server will run
|
2016-06-11 23:31:30 +00:00
|
|
|
:param name: Node name
|
2016-05-11 17:35:36 +00:00
|
|
|
:param node_id: UUID of the node (integer)
|
|
|
|
:param node_type: Type of emulator
|
2018-08-09 10:45:33 +00:00
|
|
|
:param appliance_id: Appliance ID used to create this node
|
2016-06-09 14:47:58 +00:00
|
|
|
:param kwargs: Node properties
|
2016-03-10 20:51:29 +00:00
|
|
|
"""
|
|
|
|
|
2016-07-11 13:36:52 +00:00
|
|
|
assert node_type
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
if node_id is None:
|
2016-03-10 20:51:29 +00:00
|
|
|
self._id = str(uuid.uuid4())
|
|
|
|
else:
|
2016-05-11 17:35:36 +00:00
|
|
|
self._id = node_id
|
2016-03-10 20:51:29 +00:00
|
|
|
|
|
|
|
self._project = project
|
2016-04-15 15:57:06 +00:00
|
|
|
self._compute = compute
|
2016-05-11 17:35:36 +00:00
|
|
|
self._node_type = node_type
|
2016-06-09 14:47:58 +00:00
|
|
|
|
2016-08-17 15:12:23 +00:00
|
|
|
self._label = None
|
2016-11-22 15:05:00 +00:00
|
|
|
self._links = set()
|
2016-07-18 17:09:08 +00:00
|
|
|
self._name = None
|
|
|
|
self.name = name
|
2016-06-09 14:47:58 +00:00
|
|
|
self._console = None
|
|
|
|
self._console_type = None
|
2017-06-27 09:11:07 +00:00
|
|
|
self._properties = None
|
2016-05-12 08:39:50 +00:00
|
|
|
self._command_line = None
|
|
|
|
self._node_directory = None
|
2016-05-12 17:15:46 +00:00
|
|
|
self._status = "stopped"
|
2018-08-09 10:45:33 +00:00
|
|
|
self._appliance_id = appliance_id
|
2016-06-09 14:47:58 +00:00
|
|
|
self._x = 0
|
|
|
|
self._y = 0
|
2018-08-08 07:33:10 +00:00
|
|
|
self._z = 1 # default z value is 1
|
2016-09-15 12:51:40 +00:00
|
|
|
self._ports = None
|
2016-08-17 09:58:19 +00:00
|
|
|
self._symbol = None
|
2018-04-02 15:27:12 +00:00
|
|
|
self._custom_adapters = []
|
2016-09-13 07:47:22 +00:00
|
|
|
if node_type == "iou":
|
|
|
|
self._port_name_format = "Ethernet{segment0}/{port0}"
|
|
|
|
self._port_by_adapter = 4
|
|
|
|
self.port_segment_size = 4
|
|
|
|
else:
|
|
|
|
self._port_name_format = "Ethernet{0}"
|
|
|
|
self._port_by_adapter = 1
|
|
|
|
self._port_segment_size = 0
|
|
|
|
self._first_port_name = None
|
2018-04-04 14:31:35 +00:00
|
|
|
self._console_auto_start = False
|
2016-08-17 09:58:19 +00:00
|
|
|
|
|
|
|
# This properties will be recompute
|
2018-01-12 10:34:37 +00:00
|
|
|
ignore_properties = ("width", "height", "hover_symbol")
|
2017-06-27 09:11:07 +00:00
|
|
|
self.properties = kwargs.pop('properties', {})
|
|
|
|
|
2016-09-13 07:47:22 +00:00
|
|
|
# Update node properties with additional elements
|
2016-06-09 14:47:58 +00:00
|
|
|
for prop in kwargs:
|
2016-08-17 09:58:19 +00:00
|
|
|
if prop not in ignore_properties:
|
2017-05-16 08:22:55 +00:00
|
|
|
if hasattr(self, prop):
|
|
|
|
try:
|
|
|
|
setattr(self, prop, kwargs[prop])
|
|
|
|
except AttributeError as e:
|
|
|
|
log.critical("Can't set attribute %s", prop)
|
|
|
|
raise e
|
|
|
|
else:
|
2017-05-16 08:49:45 +00:00
|
|
|
if prop not in self.CONTROLLER_ONLY_PROPERTIES and kwargs[prop] is not None and kwargs[prop] != "":
|
|
|
|
self.properties[prop] = kwargs[prop]
|
2016-08-17 09:58:19 +00:00
|
|
|
|
|
|
|
if self._symbol is None:
|
2018-01-12 10:34:37 +00:00
|
|
|
# compatibility with old node templates
|
|
|
|
if "default_symbol" in self.properties:
|
|
|
|
default_symbol = self.properties.pop("default_symbol")
|
|
|
|
if default_symbol.endswith("normal.svg"):
|
|
|
|
self.symbol = default_symbol[:-11] + ".svg"
|
|
|
|
else:
|
|
|
|
self.symbol = default_symbol
|
|
|
|
else:
|
2018-01-12 14:46:48 +00:00
|
|
|
self.symbol = ":/symbols/computer.svg"
|
2016-03-10 20:51:29 +00:00
|
|
|
|
2017-07-20 15:29:42 +00:00
|
|
|
def is_always_running(self):
|
|
|
|
"""
|
|
|
|
:returns: Boolean True if the node is always running
|
|
|
|
like ethernet switch
|
|
|
|
"""
|
|
|
|
return self.node_type not in (
|
|
|
|
"qemu", "docker", "dynamips",
|
|
|
|
"vpcs", "vmware", "virtualbox",
|
2018-03-12 10:57:13 +00:00
|
|
|
"iou", "traceng")
|
2017-07-20 15:29:42 +00:00
|
|
|
|
2016-03-10 20:51:29 +00:00
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self._id
|
|
|
|
|
2016-05-12 17:15:46 +00:00
|
|
|
@property
|
|
|
|
def status(self):
|
|
|
|
return self._status
|
|
|
|
|
2016-03-15 10:32:10 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
|
|
|
|
2016-06-09 14:47:58 +00:00
|
|
|
@name.setter
|
2016-06-11 23:31:30 +00:00
|
|
|
def name(self, new_name):
|
2016-07-18 17:30:38 +00:00
|
|
|
self._name = self._project.update_node_name(self, new_name)
|
2016-06-09 14:47:58 +00:00
|
|
|
# The text in label need to be always the node name
|
2016-08-17 15:12:23 +00:00
|
|
|
if self.label and self._label["text"] != self._name:
|
|
|
|
self._label["text"] = self._name
|
|
|
|
self._label["x"] = None # Center text
|
2016-06-09 14:47:58 +00:00
|
|
|
|
2016-03-10 20:51:29 +00:00
|
|
|
@property
|
2016-05-11 17:35:36 +00:00
|
|
|
def node_type(self):
|
|
|
|
return self._node_type
|
2016-03-10 20:51:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def console(self):
|
|
|
|
return self._console
|
|
|
|
|
2016-06-09 14:47:58 +00:00
|
|
|
@console.setter
|
|
|
|
def console(self, val):
|
|
|
|
self._console = val
|
|
|
|
|
2016-03-10 20:51:29 +00:00
|
|
|
@property
|
|
|
|
def console_type(self):
|
|
|
|
return self._console_type
|
|
|
|
|
2016-06-09 14:47:58 +00:00
|
|
|
@console_type.setter
|
|
|
|
def console_type(self, val):
|
|
|
|
self._console_type = val
|
|
|
|
|
2018-04-04 14:31:35 +00:00
|
|
|
@property
|
|
|
|
def console_auto_start(self):
|
|
|
|
return self._console_auto_start
|
|
|
|
|
|
|
|
@console_auto_start.setter
|
|
|
|
def console_auto_start(self, val):
|
|
|
|
self._console_auto_start = val
|
|
|
|
|
2016-03-10 20:51:29 +00:00
|
|
|
@property
|
|
|
|
def properties(self):
|
|
|
|
return self._properties
|
|
|
|
|
2016-06-09 14:47:58 +00:00
|
|
|
@properties.setter
|
|
|
|
def properties(self, val):
|
|
|
|
self._properties = val
|
|
|
|
|
2017-02-02 18:13:47 +00:00
|
|
|
def _base_config_file_content(self, path):
|
|
|
|
if not os.path.isabs(path):
|
|
|
|
path = os.path.join(self.project.controller.configs_path(), path)
|
|
|
|
try:
|
|
|
|
with open(path) as f:
|
|
|
|
return f.read()
|
|
|
|
except (PermissionError, OSError):
|
|
|
|
return None
|
|
|
|
|
2016-03-10 20:51:29 +00:00
|
|
|
@property
|
|
|
|
def project(self):
|
|
|
|
return self._project
|
|
|
|
|
2016-03-11 19:13:52 +00:00
|
|
|
@property
|
2016-04-15 15:57:06 +00:00
|
|
|
def compute(self):
|
|
|
|
return self._compute
|
2016-03-11 19:13:52 +00:00
|
|
|
|
2016-03-15 10:32:10 +00:00
|
|
|
@property
|
|
|
|
def host(self):
|
|
|
|
"""
|
|
|
|
:returns: Domain or ip for console connection
|
|
|
|
"""
|
2016-04-15 15:57:06 +00:00
|
|
|
return self._compute.host
|
2016-03-15 10:32:10 +00:00
|
|
|
|
2016-09-15 12:51:40 +00:00
|
|
|
@property
|
|
|
|
def ports(self):
|
|
|
|
if self._ports is None:
|
|
|
|
self._list_ports()
|
|
|
|
return self._ports
|
|
|
|
|
2016-06-09 14:47:58 +00:00
|
|
|
@property
|
|
|
|
def x(self):
|
|
|
|
return self._x
|
|
|
|
|
|
|
|
@x.setter
|
|
|
|
def x(self, val):
|
|
|
|
self._x = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def y(self):
|
|
|
|
return self._y
|
|
|
|
|
|
|
|
@y.setter
|
|
|
|
def y(self, val):
|
|
|
|
self._y = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def z(self):
|
|
|
|
return self._z
|
|
|
|
|
|
|
|
@z.setter
|
|
|
|
def z(self, val):
|
|
|
|
self._z = val
|
|
|
|
|
2016-07-01 12:37:59 +00:00
|
|
|
@property
|
|
|
|
def width(self):
|
|
|
|
return self._width
|
|
|
|
|
|
|
|
@property
|
|
|
|
def height(self):
|
|
|
|
return self._height
|
|
|
|
|
2016-06-09 14:47:58 +00:00
|
|
|
@property
|
|
|
|
def symbol(self):
|
|
|
|
return self._symbol
|
|
|
|
|
|
|
|
@symbol.setter
|
|
|
|
def symbol(self, val):
|
2018-06-06 09:32:51 +00:00
|
|
|
|
2016-10-20 07:44:24 +00:00
|
|
|
if val is None:
|
|
|
|
val = ":/symbols/computer.svg"
|
|
|
|
|
2016-09-15 16:21:39 +00:00
|
|
|
# No abs path, fix them (bug of 1.X)
|
2017-07-27 09:39:25 +00:00
|
|
|
try:
|
|
|
|
if not val.startswith(":") and os.path.abspath(val):
|
|
|
|
val = os.path.basename(val)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2016-09-15 16:21:39 +00:00
|
|
|
|
2016-06-09 14:47:58 +00:00
|
|
|
self._symbol = val
|
2016-08-17 09:58:19 +00:00
|
|
|
try:
|
|
|
|
self._width, self._height, filetype = self._project.controller.symbols.get_size(val)
|
2018-06-06 09:32:51 +00:00
|
|
|
except (ValueError, OSError) as e:
|
|
|
|
log.error("Could not write symbol: {}".format(e))
|
|
|
|
# If symbol is invalid we replace it by the default
|
2016-08-17 09:58:19 +00:00
|
|
|
self.symbol = ":/symbols/computer.svg"
|
2016-08-17 15:12:23 +00:00
|
|
|
if self._label is None:
|
|
|
|
# Apply to label user style or default
|
|
|
|
try:
|
|
|
|
style = qt_font_to_style(
|
|
|
|
self._project.controller.settings["GraphicsView"]["default_label_font"],
|
|
|
|
self._project.controller.settings["GraphicsView"]["default_label_color"])
|
|
|
|
except KeyError:
|
|
|
|
style = "font-size: 10;font-familly: Verdana"
|
|
|
|
|
|
|
|
self._label = {
|
|
|
|
"y": round(self._height / 2 + 10) * -1,
|
2017-03-20 18:14:07 +00:00
|
|
|
"text": html.escape(self._name),
|
2016-08-17 15:12:23 +00:00
|
|
|
"style": style,
|
|
|
|
"x": None, # None: mean the client should center it
|
|
|
|
"rotation": 0
|
|
|
|
}
|
2016-06-09 14:47:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def label(self):
|
|
|
|
return self._label
|
|
|
|
|
|
|
|
@label.setter
|
|
|
|
def label(self, val):
|
|
|
|
# The text in label need to be always the node name
|
|
|
|
val["text"] = self._name
|
|
|
|
self._label = val
|
|
|
|
|
2016-09-13 07:47:22 +00:00
|
|
|
@property
|
|
|
|
def port_name_format(self):
|
|
|
|
return self._port_name_format
|
|
|
|
|
|
|
|
@port_name_format.setter
|
|
|
|
def port_name_format(self, val):
|
|
|
|
self._port_name_format = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port_segment_size(self):
|
|
|
|
return self._port_segment_size
|
|
|
|
|
|
|
|
@port_segment_size.setter
|
|
|
|
def port_segment_size(self, val):
|
|
|
|
self._port_segment_size = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def first_port_name(self):
|
|
|
|
return self._first_port_name
|
|
|
|
|
|
|
|
@first_port_name.setter
|
|
|
|
def first_port_name(self, val):
|
|
|
|
self._first_port_name = val
|
|
|
|
|
2018-04-02 15:27:12 +00:00
|
|
|
@property
|
|
|
|
def custom_adapters(self):
|
|
|
|
return self._custom_adapters
|
|
|
|
|
|
|
|
@custom_adapters.setter
|
|
|
|
def custom_adapters(self, val):
|
|
|
|
self._custom_adapters = val
|
|
|
|
|
2016-11-22 15:05:00 +00:00
|
|
|
def add_link(self, link):
|
|
|
|
"""
|
|
|
|
A link is connected to the node
|
|
|
|
"""
|
|
|
|
self._links.add(link)
|
|
|
|
|
|
|
|
def remove_link(self, link):
|
|
|
|
"""
|
|
|
|
A link is connected to the node
|
|
|
|
"""
|
|
|
|
self._links.remove(link)
|
|
|
|
|
|
|
|
@property
|
2018-06-06 13:46:44 +00:00
|
|
|
def links(self):
|
2016-11-22 15:05:00 +00:00
|
|
|
return self._links
|
|
|
|
|
2016-03-10 20:51:29 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def create(self):
|
2016-04-18 15:36:38 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Create the node on the compute server
|
2016-04-18 15:36:38 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
data = self._node_data()
|
|
|
|
data["node_id"] = self._id
|
2016-10-03 20:31:34 +00:00
|
|
|
if self._node_type == "docker":
|
|
|
|
timeout = None
|
2018-04-02 15:27:12 +00:00
|
|
|
|
2016-10-04 08:56:38 +00:00
|
|
|
else:
|
2017-12-21 07:46:20 +00:00
|
|
|
timeout = 1200
|
2016-06-07 17:38:01 +00:00
|
|
|
trial = 0
|
|
|
|
while trial != 6:
|
|
|
|
try:
|
2016-10-03 20:31:34 +00:00
|
|
|
response = yield from self._compute.post("/projects/{}/{}/nodes".format(self._project.id, self._node_type), data=data, timeout=timeout)
|
2016-06-07 17:38:01 +00:00
|
|
|
except ComputeConflict as e:
|
2016-06-09 14:47:58 +00:00
|
|
|
if e.response.get("exception") == "ImageMissingError":
|
2016-06-07 17:38:01 +00:00
|
|
|
res = yield from self._upload_missing_image(self._node_type, e.response["image"])
|
|
|
|
if not res:
|
|
|
|
raise e
|
2016-07-12 16:16:50 +00:00
|
|
|
else:
|
|
|
|
raise e
|
2016-06-07 17:38:01 +00:00
|
|
|
else:
|
2016-11-22 15:05:00 +00:00
|
|
|
yield from self.parse_node_response(response.json)
|
2016-06-07 17:38:01 +00:00
|
|
|
return True
|
|
|
|
trial += 1
|
2016-04-18 15:36:38 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-06-09 14:47:58 +00:00
|
|
|
def update(self, **kwargs):
|
2016-04-18 15:36:38 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Update the node on the compute server
|
2016-04-18 15:36:38 +00:00
|
|
|
|
2016-06-09 14:47:58 +00:00
|
|
|
:param kwargs: Node properties
|
2016-04-18 15:36:38 +00:00
|
|
|
"""
|
|
|
|
|
2016-06-09 14:47:58 +00:00
|
|
|
# When updating properties used only on controller we don't need to call the compute
|
|
|
|
update_compute = False
|
|
|
|
|
2016-06-30 16:29:17 +00:00
|
|
|
old_json = self.__json__()
|
|
|
|
|
2016-06-13 16:46:30 +00:00
|
|
|
compute_properties = None
|
2016-06-09 14:47:58 +00:00
|
|
|
# Update node properties with additional elements
|
|
|
|
for prop in kwargs:
|
|
|
|
if getattr(self, prop) != kwargs[prop]:
|
|
|
|
if prop not in self.CONTROLLER_ONLY_PROPERTIES:
|
|
|
|
update_compute = True
|
2016-06-13 16:46:30 +00:00
|
|
|
|
2018-06-09 14:47:36 +00:00
|
|
|
# We update properties on the compute and wait for the answer from the compute node
|
2016-06-13 16:46:30 +00:00
|
|
|
if prop == "properties":
|
2016-06-14 10:04:23 +00:00
|
|
|
compute_properties = kwargs[prop]
|
2016-06-13 16:46:30 +00:00
|
|
|
else:
|
|
|
|
setattr(self, prop, kwargs[prop])
|
2016-06-09 14:47:58 +00:00
|
|
|
|
2018-04-02 15:27:12 +00:00
|
|
|
if compute_properties and "custom_adapters" in compute_properties:
|
|
|
|
# we need to check custom adapters to update the custom port names
|
|
|
|
self.custom_adapters = compute_properties["custom_adapters"]
|
2016-09-15 12:51:40 +00:00
|
|
|
self._list_ports()
|
2016-06-09 14:47:58 +00:00
|
|
|
if update_compute:
|
2016-06-13 16:46:30 +00:00
|
|
|
data = self._node_data(properties=compute_properties)
|
2016-06-09 14:47:58 +00:00
|
|
|
response = yield from self.put(None, data=data)
|
2016-11-22 15:05:00 +00:00
|
|
|
yield from self.parse_node_response(response.json)
|
2018-03-24 11:25:09 +00:00
|
|
|
elif old_json != self.__json__():
|
|
|
|
# We send notif only if object has changed
|
2018-08-16 14:31:56 +00:00
|
|
|
self.project.controller.notification.project_emit("node.updated", self.__json__())
|
2016-06-14 10:04:23 +00:00
|
|
|
self.project.dump()
|
2016-04-18 15:36:38 +00:00
|
|
|
|
2016-11-22 15:05:00 +00:00
|
|
|
@asyncio.coroutine
|
2016-05-18 14:12:13 +00:00
|
|
|
def parse_node_response(self, response):
|
2016-04-18 15:36:38 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Update the object with the remote node object
|
2016-04-18 15:36:38 +00:00
|
|
|
"""
|
2018-03-24 11:11:21 +00:00
|
|
|
|
2016-05-18 14:12:13 +00:00
|
|
|
for key, value in response.items():
|
2016-04-18 15:36:38 +00:00
|
|
|
if key == "console":
|
|
|
|
self._console = value
|
2016-05-12 08:39:50 +00:00
|
|
|
elif key == "node_directory":
|
|
|
|
self._node_directory = value
|
|
|
|
elif key == "command_line":
|
|
|
|
self._command_line = value
|
2016-05-18 14:12:13 +00:00
|
|
|
elif key == "status":
|
|
|
|
self._status = value
|
|
|
|
elif key == "console_type":
|
|
|
|
self._console_type = value
|
|
|
|
elif key == "name":
|
2016-07-18 17:30:38 +00:00
|
|
|
self.name = value
|
2017-02-02 18:13:47 +00:00
|
|
|
elif key in ["node_id", "project_id", "console_host",
|
|
|
|
"startup_config_content",
|
|
|
|
"private_config_content",
|
|
|
|
"startup_script"]:
|
|
|
|
if key in self._properties:
|
|
|
|
del self._properties[key]
|
2016-04-18 15:36:38 +00:00
|
|
|
else:
|
|
|
|
self._properties[key] = value
|
2016-09-15 12:51:40 +00:00
|
|
|
self._list_ports()
|
2016-11-22 15:05:00 +00:00
|
|
|
for link in self._links:
|
|
|
|
yield from link.node_updated(self)
|
2016-04-18 15:36:38 +00:00
|
|
|
|
2016-06-13 16:46:30 +00:00
|
|
|
def _node_data(self, properties=None):
|
2016-04-18 15:36:38 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Prepare node data to send to the remote controller
|
2016-06-13 16:46:30 +00:00
|
|
|
|
|
|
|
:param properties: If properties is None use actual property otherwise use the parameter
|
2016-04-18 15:36:38 +00:00
|
|
|
"""
|
2016-06-13 16:46:30 +00:00
|
|
|
if properties:
|
|
|
|
data = copy.copy(properties)
|
|
|
|
else:
|
|
|
|
data = copy.copy(self._properties)
|
2017-02-02 18:13:47 +00:00
|
|
|
# We replace the startup script name by the content of the file
|
|
|
|
mapping = {
|
|
|
|
"base_script_file": "startup_script",
|
|
|
|
"startup_config": "startup_config_content",
|
|
|
|
"private_config": "private_config_content",
|
|
|
|
}
|
|
|
|
for k, v in mapping.items():
|
|
|
|
if k in list(self._properties.keys()):
|
|
|
|
data[v] = self._base_config_file_content(self._properties[k])
|
|
|
|
del data[k]
|
|
|
|
del self._properties[k] # We send the file only one time
|
2016-03-11 14:06:14 +00:00
|
|
|
data["name"] = self._name
|
2016-05-15 17:23:14 +00:00
|
|
|
if self._console:
|
2016-05-18 03:22:18 +00:00
|
|
|
# console is optional for builtin nodes
|
2016-05-15 17:23:14 +00:00
|
|
|
data["console"] = self._console
|
2016-05-18 09:59:25 +00:00
|
|
|
if self._console_type:
|
2016-05-15 17:23:14 +00:00
|
|
|
data["console_type"] = self._console_type
|
2018-04-02 15:27:12 +00:00
|
|
|
if self.custom_adapters:
|
|
|
|
data["custom_adapters"] = self.custom_adapters
|
2016-03-15 09:45:05 +00:00
|
|
|
|
2016-05-16 16:32:29 +00:00
|
|
|
# None properties are not be send. Because it can mean the emulator doesn't support it
|
2016-03-15 09:45:05 +00:00
|
|
|
for key in list(data.keys()):
|
2016-06-09 14:47:58 +00:00
|
|
|
if data[key] is None or data[key] is {} or key in self.CONTROLLER_ONLY_PROPERTIES:
|
2016-03-15 09:45:05 +00:00
|
|
|
del data[key]
|
2016-04-18 15:36:38 +00:00
|
|
|
return data
|
2016-03-15 09:45:05 +00:00
|
|
|
|
2016-04-18 15:36:38 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def destroy(self):
|
|
|
|
yield from self.delete()
|
2016-03-10 20:51:29 +00:00
|
|
|
|
2016-04-12 16:02:36 +00:00
|
|
|
@asyncio.coroutine
|
2018-03-27 09:58:49 +00:00
|
|
|
def start(self, data=None):
|
2016-04-12 16:02:36 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Start a node
|
2016-04-12 16:02:36 +00:00
|
|
|
"""
|
2016-11-03 09:40:00 +00:00
|
|
|
try:
|
2016-11-21 17:16:50 +00:00
|
|
|
# For IOU we need to send the licence everytime
|
|
|
|
if self.node_type == "iou":
|
|
|
|
try:
|
|
|
|
licence = self._project.controller.settings["IOU"]["iourc_content"]
|
|
|
|
except KeyError:
|
|
|
|
raise aiohttp.web.HTTPConflict(text="IOU licence is not configured")
|
|
|
|
yield from self.post("/start", timeout=240, data={"iourc_content": licence})
|
|
|
|
else:
|
2018-03-27 09:58:49 +00:00
|
|
|
yield from self.post("/start", data=data, timeout=240)
|
2016-11-03 09:40:00 +00:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
raise aiohttp.web.HTTPRequestTimeout(text="Timeout when starting {}".format(self._name))
|
2016-04-12 16:02:36 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def stop(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Stop a node
|
2016-04-12 16:02:36 +00:00
|
|
|
"""
|
2016-08-26 12:09:18 +00:00
|
|
|
try:
|
2016-12-15 20:57:59 +00:00
|
|
|
yield from self.post("/stop", timeout=240, dont_connect=True)
|
2016-11-04 10:40:55 +00:00
|
|
|
# We don't care if a node is down at this step
|
2017-05-16 17:28:47 +00:00
|
|
|
except (ComputeError, aiohttp.ClientError, aiohttp.web.HTTPError):
|
2016-08-26 12:09:18 +00:00
|
|
|
pass
|
2016-11-04 10:40:55 +00:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
raise aiohttp.web.HTTPRequestTimeout(text="Timeout when stopping {}".format(self._name))
|
2016-04-12 16:02:36 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def suspend(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Suspend a node
|
2016-04-12 16:02:36 +00:00
|
|
|
"""
|
2016-11-07 16:34:51 +00:00
|
|
|
try:
|
|
|
|
yield from self.post("/suspend", timeout=240)
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
raise aiohttp.web.HTTPRequestTimeout(text="Timeout when reloading {}".format(self._name))
|
2016-04-12 16:02:36 +00:00
|
|
|
|
2016-03-11 19:13:52 +00:00
|
|
|
@asyncio.coroutine
|
2016-04-18 14:57:02 +00:00
|
|
|
def reload(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Suspend a node
|
2016-04-18 14:57:02 +00:00
|
|
|
"""
|
2016-11-07 16:34:51 +00:00
|
|
|
try:
|
|
|
|
yield from self.post("/reload", timeout=240)
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
raise aiohttp.web.HTTPRequestTimeout(text="Timeout when reloading {}".format(self._name))
|
2016-04-18 14:57:02 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-11-07 16:34:51 +00:00
|
|
|
def post(self, path, data=None, **kwargs):
|
2016-03-11 19:13:52 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
HTTP post on the node
|
2016-03-11 19:13:52 +00:00
|
|
|
"""
|
2016-04-15 15:57:06 +00:00
|
|
|
if data:
|
2016-11-07 16:34:51 +00:00
|
|
|
return (yield from self._compute.post("/projects/{}/{}/nodes/{}{}".format(self._project.id, self._node_type, self._id, path), data=data, **kwargs))
|
2016-04-15 15:57:06 +00:00
|
|
|
else:
|
2016-11-07 16:34:51 +00:00
|
|
|
return (yield from self._compute.post("/projects/{}/{}/nodes/{}{}".format(self._project.id, self._node_type, self._id, path), **kwargs))
|
2016-03-11 19:13:52 +00:00
|
|
|
|
2016-03-14 16:40:27 +00:00
|
|
|
@asyncio.coroutine
|
2016-11-07 16:34:51 +00:00
|
|
|
def put(self, path, data=None, **kwargs):
|
2016-04-18 15:36:38 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
HTTP post on the node
|
2016-04-18 15:36:38 +00:00
|
|
|
"""
|
2016-04-18 16:56:03 +00:00
|
|
|
if path is None:
|
2016-05-11 17:35:36 +00:00
|
|
|
path = "/projects/{}/{}/nodes/{}".format(self._project.id, self._node_type, self._id)
|
2016-04-18 16:56:03 +00:00
|
|
|
else:
|
2016-05-11 17:35:36 +00:00
|
|
|
path = "/projects/{}/{}/nodes/{}{}".format(self._project.id, self._node_type, self._id, path)
|
2016-04-18 15:36:38 +00:00
|
|
|
if data:
|
2016-11-07 16:34:51 +00:00
|
|
|
return (yield from self._compute.put(path, data=data, **kwargs))
|
2016-04-18 15:36:38 +00:00
|
|
|
else:
|
2016-11-07 16:34:51 +00:00
|
|
|
return (yield from self._compute.put(path, **kwargs))
|
2016-04-18 15:36:38 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-11-07 16:34:51 +00:00
|
|
|
def delete(self, path=None, **kwargs):
|
2016-03-14 16:40:27 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
HTTP post on the node
|
2016-03-14 16:40:27 +00:00
|
|
|
"""
|
2016-04-18 16:56:03 +00:00
|
|
|
if path is None:
|
2016-11-07 16:34:51 +00:00
|
|
|
return (yield from self._compute.delete("/projects/{}/{}/nodes/{}".format(self._project.id, self._node_type, self._id), **kwargs))
|
2016-04-18 16:56:03 +00:00
|
|
|
else:
|
2016-11-07 16:34:51 +00:00
|
|
|
return (yield from self._compute.delete("/projects/{}/{}/nodes/{}{}".format(self._project.id, self._node_type, self._id, path), **kwargs))
|
2016-03-14 16:40:27 +00:00
|
|
|
|
2016-06-07 17:38:01 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def _upload_missing_image(self, type, img):
|
|
|
|
"""
|
|
|
|
Search an image on local computer and upload it to remote compute
|
|
|
|
if the image exists
|
|
|
|
"""
|
|
|
|
for directory in images_directories(type):
|
|
|
|
image = os.path.join(directory, img)
|
|
|
|
if os.path.exists(image):
|
2018-08-16 14:31:56 +00:00
|
|
|
self.project.controller.notification.project_emit("log.info", {"message": "Uploading missing image {}".format(img)})
|
2017-07-27 09:42:10 +00:00
|
|
|
try:
|
|
|
|
with open(image, 'rb') as f:
|
|
|
|
yield from self._compute.post("/{}/images/{}".format(self._node_type, os.path.basename(img)), data=f, timeout=None)
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPConflict(text="Can't upload {}: {}".format(image, str(e)))
|
2018-08-16 14:31:56 +00:00
|
|
|
self.project.controller.notification.project_emit("log.info", {"message": "Upload finished for {}".format(img)})
|
2016-06-07 17:38:01 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-05-19 14:21:35 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def dynamips_auto_idlepc(self):
|
|
|
|
"""
|
|
|
|
Compute the idle PC for a dynamips node
|
|
|
|
"""
|
|
|
|
return (yield from self._compute.get("/projects/{}/{}/nodes/{}/auto_idlepc".format(self._project.id, self._node_type, self._id), timeout=240)).json
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def dynamips_idlepc_proposals(self):
|
|
|
|
"""
|
|
|
|
Compute a list of potential idle PC
|
|
|
|
"""
|
|
|
|
return (yield from self._compute.get("/projects/{}/{}/nodes/{}/idlepc_proposals".format(self._project.id, self._node_type, self._id), timeout=240)).json
|
|
|
|
|
2016-09-15 12:51:40 +00:00
|
|
|
def get_port(self, adapter_number, port_number):
|
|
|
|
"""
|
|
|
|
Return the port for this adapter_number and port_number
|
2018-03-12 06:38:50 +00:00
|
|
|
or returns None if the port is not found
|
2016-09-15 12:51:40 +00:00
|
|
|
"""
|
|
|
|
for port in self.ports:
|
|
|
|
if port.adapter_number == adapter_number and port.port_number == port_number:
|
|
|
|
return port
|
2018-03-12 06:38:50 +00:00
|
|
|
return None
|
2016-09-15 12:51:40 +00:00
|
|
|
|
2016-09-13 07:47:22 +00:00
|
|
|
def _list_ports(self):
|
|
|
|
"""
|
|
|
|
Generate the list of port display in the client
|
|
|
|
if the compute has sent a list we return it (use by
|
2018-04-02 15:27:12 +00:00
|
|
|
node where you can not personalize the port naming).
|
2016-09-13 07:47:22 +00:00
|
|
|
"""
|
2016-09-15 12:51:40 +00:00
|
|
|
self._ports = []
|
2016-09-13 07:47:22 +00:00
|
|
|
# Some special cases
|
|
|
|
if self._node_type == "atm_switch":
|
2016-10-26 10:03:54 +00:00
|
|
|
atm_port = set()
|
|
|
|
# Mapping is like {"1:0:100": "10:0:200"}
|
2016-11-03 12:35:44 +00:00
|
|
|
for source, dest in self._properties["mappings"].items():
|
2016-10-26 10:03:54 +00:00
|
|
|
atm_port.add(int(source.split(":")[0]))
|
|
|
|
atm_port.add(int(dest.split(":")[0]))
|
|
|
|
atm_port = sorted(atm_port)
|
|
|
|
for port in atm_port:
|
|
|
|
self._ports.append(PortFactory("{}".format(port), 0, 0, port, "atm"))
|
2016-09-15 12:51:40 +00:00
|
|
|
return
|
2016-10-26 10:03:54 +00:00
|
|
|
|
2016-09-13 07:47:22 +00:00
|
|
|
elif self._node_type == "frame_relay_switch":
|
2016-10-26 09:59:16 +00:00
|
|
|
frame_relay_port = set()
|
|
|
|
# Mapping is like {"1:101": "10:202"}
|
2016-11-03 12:35:44 +00:00
|
|
|
for source, dest in self._properties["mappings"].items():
|
2016-10-26 09:59:16 +00:00
|
|
|
frame_relay_port.add(int(source.split(":")[0]))
|
|
|
|
frame_relay_port.add(int(dest.split(":")[0]))
|
|
|
|
frame_relay_port = sorted(frame_relay_port)
|
|
|
|
for port in frame_relay_port:
|
|
|
|
self._ports.append(PortFactory("{}".format(port), 0, 0, port, "frame_relay"))
|
2016-09-15 12:51:40 +00:00
|
|
|
return
|
2016-09-13 07:47:22 +00:00
|
|
|
elif self._node_type == "dynamips":
|
2016-11-03 12:35:44 +00:00
|
|
|
self._ports = DynamipsPortFactory(self._properties)
|
2016-09-15 12:51:40 +00:00
|
|
|
return
|
2017-01-11 17:38:28 +00:00
|
|
|
elif self._node_type == "docker":
|
|
|
|
for adapter_number in range(0, self._properties["adapters"]):
|
2018-04-02 15:27:12 +00:00
|
|
|
custom_adapter_settings = {}
|
|
|
|
for custom_adapter in self.custom_adapters:
|
|
|
|
if custom_adapter["adapter_number"] == adapter_number:
|
|
|
|
custom_adapter_settings = custom_adapter
|
|
|
|
break
|
|
|
|
port_name = "eth{}".format(adapter_number)
|
|
|
|
port_name = custom_adapter_settings.get("port_name", port_name)
|
|
|
|
self._ports.append(PortFactory(port_name, 0, adapter_number, 0, "ethernet", short_name="eth{}".format(adapter_number)))
|
2017-01-09 15:48:42 +00:00
|
|
|
elif self._node_type in ("ethernet_switch", "ethernet_hub"):
|
|
|
|
# Basic node we don't want to have adapter number
|
2016-09-23 08:56:37 +00:00
|
|
|
port_number = 0
|
2016-11-03 12:35:44 +00:00
|
|
|
for port in self._properties["ports_mapping"]:
|
2017-01-09 15:48:42 +00:00
|
|
|
self._ports.append(PortFactory(port["name"], 0, 0, port_number, "ethernet", short_name="e{}".format(port_number)))
|
|
|
|
port_number += 1
|
2018-03-12 10:57:13 +00:00
|
|
|
elif self._node_type in ("vpcs", "traceng"):
|
2017-01-09 15:48:42 +00:00
|
|
|
self._ports.append(PortFactory("Ethernet0", 0, 0, 0, "ethernet", short_name="e0"))
|
|
|
|
elif self._node_type in ("cloud", "nat"):
|
|
|
|
port_number = 0
|
|
|
|
for port in self._properties["ports_mapping"]:
|
|
|
|
self._ports.append(PortFactory(port["name"], 0, 0, port_number, "ethernet", short_name=port["name"]))
|
2016-09-23 08:56:37 +00:00
|
|
|
port_number += 1
|
2016-09-13 07:47:22 +00:00
|
|
|
else:
|
2018-04-02 15:27:12 +00:00
|
|
|
self._ports = StandardPortFactory(self._properties, self._port_by_adapter, self._first_port_name, self._port_name_format, self._port_segment_size, self._custom_adapters)
|
2016-09-13 07:47:22 +00:00
|
|
|
|
2016-04-21 10:14:09 +00:00
|
|
|
def __repr__(self):
|
2016-05-11 17:35:36 +00:00
|
|
|
return "<gns3server.controller.Node {} {}>".format(self._node_type, self._name)
|
2016-04-21 10:14:09 +00:00
|
|
|
|
2016-07-05 14:07:05 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
if not isinstance(other, Node):
|
|
|
|
return False
|
|
|
|
return self.id == other.id and other.project.id == self.project.id
|
|
|
|
|
2016-06-15 13:12:38 +00:00
|
|
|
def __json__(self, topology_dump=False):
|
|
|
|
"""
|
|
|
|
:param topology_dump: Filter to keep only properties require for saving on disk
|
|
|
|
"""
|
2018-04-02 15:27:12 +00:00
|
|
|
|
2016-06-15 13:12:38 +00:00
|
|
|
if topology_dump:
|
|
|
|
return {
|
|
|
|
"compute_id": str(self._compute.id),
|
|
|
|
"node_id": self._id,
|
|
|
|
"node_type": self._node_type,
|
2018-08-09 10:45:33 +00:00
|
|
|
"appliance_id": self._appliance_id,
|
2016-06-15 13:12:38 +00:00
|
|
|
"name": self._name,
|
|
|
|
"console": self._console,
|
|
|
|
"console_type": self._console_type,
|
2018-04-04 14:31:35 +00:00
|
|
|
"console_auto_start": self._console_auto_start,
|
2017-02-02 18:13:47 +00:00
|
|
|
"properties": self._properties,
|
2016-06-15 13:12:38 +00:00
|
|
|
"label": self._label,
|
2016-07-01 12:37:59 +00:00
|
|
|
"x": self._x,
|
|
|
|
"y": self._y,
|
|
|
|
"z": self._z,
|
|
|
|
"width": self._width,
|
|
|
|
"height": self._height,
|
2016-09-13 07:47:22 +00:00
|
|
|
"symbol": self._symbol,
|
|
|
|
"port_name_format": self._port_name_format,
|
|
|
|
"port_segment_size": self._port_segment_size,
|
2018-04-02 15:27:12 +00:00
|
|
|
"first_port_name": self._first_port_name,
|
|
|
|
"custom_adapters": self._custom_adapters
|
2016-06-15 13:12:38 +00:00
|
|
|
}
|
2016-03-10 20:51:29 +00:00
|
|
|
return {
|
2016-06-14 10:04:23 +00:00
|
|
|
"compute_id": str(self._compute.id),
|
2016-03-11 14:06:14 +00:00
|
|
|
"project_id": self._project.id,
|
2016-05-11 17:35:36 +00:00
|
|
|
"node_id": self._id,
|
2018-08-09 10:45:33 +00:00
|
|
|
"appliance_id": self._appliance_id,
|
2016-05-11 17:35:36 +00:00
|
|
|
"node_type": self._node_type,
|
2016-05-12 08:39:50 +00:00
|
|
|
"node_directory": self._node_directory,
|
2016-03-10 20:51:29 +00:00
|
|
|
"name": self._name,
|
|
|
|
"console": self._console,
|
2016-10-26 12:43:47 +00:00
|
|
|
"console_host": str(self._compute.console_host),
|
2016-03-10 20:51:29 +00:00
|
|
|
"console_type": self._console_type,
|
2018-04-04 14:31:35 +00:00
|
|
|
"console_auto_start": self._console_auto_start,
|
2016-05-12 08:39:50 +00:00
|
|
|
"command_line": self._command_line,
|
2017-02-02 18:13:47 +00:00
|
|
|
"properties": self._properties,
|
2016-06-09 14:47:58 +00:00
|
|
|
"status": self._status,
|
|
|
|
"label": self._label,
|
2016-07-01 12:37:59 +00:00
|
|
|
"x": self._x,
|
|
|
|
"y": self._y,
|
|
|
|
"z": self._z,
|
|
|
|
"width": self._width,
|
|
|
|
"height": self._height,
|
2016-09-13 07:47:22 +00:00
|
|
|
"symbol": self._symbol,
|
|
|
|
"port_name_format": self._port_name_format,
|
|
|
|
"port_segment_size": self._port_segment_size,
|
|
|
|
"first_port_name": self._first_port_name,
|
2018-04-02 15:27:12 +00:00
|
|
|
"custom_adapters": self._custom_adapters,
|
2016-09-15 12:51:40 +00:00
|
|
|
"ports": [port.__json__() for port in self.ports]
|
2016-03-10 20:51:29 +00:00
|
|
|
}
|