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/>.
|
|
|
|
|
|
|
|
|
|
|
|
import asyncio
|
2016-03-11 14:49:28 +00:00
|
|
|
import copy
|
2016-03-10 20:51:29 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
class Node:
|
2016-03-11 14:06:14 +00:00
|
|
|
|
2016-05-18 09:59:25 +00:00
|
|
|
def __init__(self, project, compute, node_id=None, node_type=None, name=None, console=None, console_type=None, properties={}):
|
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
|
|
|
|
:param node_id: UUID of the node (integer)
|
|
|
|
:param node_type: Type of emulator
|
|
|
|
:param name: Name of the node
|
2016-03-10 20:51:29 +00:00
|
|
|
:param console: TCP port of the console
|
|
|
|
:param console_type: Type of the console (telnet, vnc, serial..)
|
2016-05-11 17:35:36 +00:00
|
|
|
:param properties: Emulator specific properties of the node
|
2016-03-10 20:51:29 +00:00
|
|
|
"""
|
|
|
|
|
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._name = name
|
|
|
|
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-03-10 20:51:29 +00:00
|
|
|
self._console = console
|
|
|
|
self._console_type = console_type
|
2016-03-11 14:06:14 +00:00
|
|
|
self._properties = properties
|
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"
|
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-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
|
|
|
|
|
|
|
|
@property
|
|
|
|
def console_type(self):
|
|
|
|
return self._console_type
|
|
|
|
|
|
|
|
@property
|
|
|
|
def properties(self):
|
|
|
|
return self._properties
|
|
|
|
|
|
|
|
@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-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
|
|
|
|
response = yield from self._compute.post("/projects/{}/{}/nodes".format(self._project.id, self._node_type), data=data)
|
2016-05-18 14:12:13 +00:00
|
|
|
self.parse_node_response(response.json)
|
2016-04-18 15:36:38 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def update(self, name=None, console=None, console_type="telnet", properties={}):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Update the node on the compute server
|
2016-04-18 15:36:38 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
:param node_id: UUID of the node
|
|
|
|
:param node_type: Type of emulator
|
|
|
|
:param name: Name of the node
|
2016-04-18 15:36:38 +00:00
|
|
|
:param console: TCP port of the console
|
|
|
|
:param console_type: Type of the console (telnet, vnc, serial..)
|
2016-05-11 17:35:36 +00:00
|
|
|
:param properties: Emulator specific properties of the node
|
2016-04-18 15:36:38 +00:00
|
|
|
|
|
|
|
"""
|
2016-04-18 16:56:03 +00:00
|
|
|
if name:
|
|
|
|
self._name = name
|
|
|
|
if console:
|
|
|
|
self._console = console
|
|
|
|
if console_type:
|
|
|
|
self._console_type = console_type
|
|
|
|
if properties != {}:
|
|
|
|
self._properties = properties
|
2016-04-18 15:36:38 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
data = self._node_data()
|
2016-04-18 16:56:03 +00:00
|
|
|
response = yield from self.put(None, data=data)
|
2016-05-18 14:12:13 +00:00
|
|
|
self.parse_node_response(response.json)
|
2016-04-18 15:36:38 +00:00
|
|
|
|
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
|
|
|
"""
|
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":
|
|
|
|
self._name = value
|
|
|
|
elif key in ["node_id", "project_id"]:
|
2016-04-18 15:36:38 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self._properties[key] = value
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
def _node_data(self):
|
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-04-18 15:36:38 +00:00
|
|
|
"""
|
2016-03-11 14:49:28 +00:00
|
|
|
data = copy.copy(self._properties)
|
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
|
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()):
|
|
|
|
if data[key] is None:
|
|
|
|
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
|
|
|
|
def start(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Start a node
|
2016-04-12 16:02:36 +00:00
|
|
|
"""
|
2016-04-14 10:22:10 +00:00
|
|
|
yield from self.post("/start")
|
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-04-14 10:22:10 +00:00
|
|
|
yield from self.post("/stop")
|
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-04-14 10:22:10 +00:00
|
|
|
yield from self.post("/suspend")
|
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-04-18 15:36:38 +00:00
|
|
|
yield from self.post("/reload")
|
2016-04-18 14:57:02 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-04-15 15:57:06 +00:00
|
|
|
def post(self, path, data=None):
|
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-05-11 17:35:36 +00:00
|
|
|
return (yield from self._compute.post("/projects/{}/{}/nodes/{}{}".format(self._project.id, self._node_type, self._id, path), data=data))
|
2016-04-15 15:57:06 +00:00
|
|
|
else:
|
2016-05-11 17:35:36 +00:00
|
|
|
return (yield from self._compute.post("/projects/{}/{}/nodes/{}{}".format(self._project.id, self._node_type, self._id, path)))
|
2016-03-11 19:13:52 +00:00
|
|
|
|
2016-03-14 16:40:27 +00:00
|
|
|
@asyncio.coroutine
|
2016-04-18 15:36:38 +00:00
|
|
|
def put(self, path, data=None):
|
|
|
|
"""
|
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-04-18 16:56:03 +00:00
|
|
|
return (yield from self._compute.put(path, data=data))
|
2016-04-18 15:36:38 +00:00
|
|
|
else:
|
2016-04-18 16:56:03 +00:00
|
|
|
return (yield from self._compute.put(path))
|
2016-04-18 15:36:38 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def delete(self, path=None):
|
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-05-11 17:35:36 +00:00
|
|
|
return (yield from self._compute.delete("/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
|
|
|
return (yield from self._compute.delete("/projects/{}/{}/nodes/{}{}".format(self._project.id, self._node_type, self._id, path)))
|
2016-03-14 16:40:27 +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-03-10 20:51:29 +00:00
|
|
|
def __json__(self):
|
|
|
|
return {
|
2016-04-15 15:57:06 +00:00
|
|
|
"compute_id": 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,
|
|
|
|
"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,
|
|
|
|
"console_type": self._console_type,
|
2016-05-12 08:39:50 +00:00
|
|
|
"command_line": self._command_line,
|
2016-05-12 17:15:46 +00:00
|
|
|
"properties": self._properties,
|
|
|
|
"status": self._status
|
2016-03-10 20:51:29 +00:00
|
|
|
}
|