2013-12-22 00:42:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2015-02-10 01:24:13 +00:00
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
2013-12-22 00:42:33 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Interface for Dynamips hypervisor management module ("hypervisor")
|
|
|
|
http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L46
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
2015-03-06 01:00:17 +00:00
|
|
|
import time
|
2013-12-22 00:42:33 +00:00
|
|
|
import logging
|
2015-02-10 01:24:13 +00:00
|
|
|
import asyncio
|
|
|
|
|
2013-12-22 00:42:33 +00:00
|
|
|
from .dynamips_error import DynamipsError
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
class DynamipsHypervisor:
|
2015-02-13 13:43:28 +00:00
|
|
|
|
2013-12-22 00:42:33 +00:00
|
|
|
"""
|
|
|
|
Creates a new connection to a Dynamips server (also called hypervisor)
|
|
|
|
|
2014-03-02 22:20:03 +00:00
|
|
|
:param working_dir: working directory
|
2013-12-22 00:42:33 +00:00
|
|
|
:param host: the hostname or ip address string of the Dynamips server
|
|
|
|
:param port: the tcp port integer (defaults to 7200)
|
|
|
|
:param timeout: timeout integer for how long to wait for a response to commands sent to the
|
|
|
|
hypervisor (defaults to 30 seconds)
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Used to parse Dynamips response codes
|
|
|
|
error_re = re.compile(r"""^2[0-9]{2}-""")
|
|
|
|
success_re = re.compile(r"""^1[0-9]{2}\s{1}""")
|
|
|
|
|
2014-03-02 22:20:03 +00:00
|
|
|
def __init__(self, working_dir, host, port=7200, timeout=30.0):
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
self._host = host
|
|
|
|
self._port = port
|
|
|
|
|
|
|
|
self._devices = []
|
2014-03-02 22:20:03 +00:00
|
|
|
self._working_dir = working_dir
|
2013-12-22 00:42:33 +00:00
|
|
|
self._version = "N/A"
|
2014-05-28 12:26:20 +00:00
|
|
|
self._timeout = timeout
|
2013-12-22 00:42:33 +00:00
|
|
|
self._uuid = None
|
2015-02-10 01:24:13 +00:00
|
|
|
self._reader = None
|
|
|
|
self._writer = None
|
2015-03-20 09:21:27 +00:00
|
|
|
self._io_lock = asyncio.Lock()
|
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
@asyncio.coroutine
|
2015-03-06 01:00:17 +00:00
|
|
|
def connect(self, timeout=10):
|
2013-12-22 00:42:33 +00:00
|
|
|
"""
|
|
|
|
Connects to the hypervisor.
|
|
|
|
"""
|
|
|
|
|
2014-04-12 00:33:42 +00:00
|
|
|
# connect to a local address by default
|
|
|
|
# if listening to all addresses (IPv4 or IPv6)
|
|
|
|
if self._host == "0.0.0.0":
|
|
|
|
host = "127.0.0.1"
|
|
|
|
elif self._host == "::":
|
|
|
|
host = "::1"
|
|
|
|
else:
|
|
|
|
host = self._host
|
|
|
|
|
2015-03-06 01:00:17 +00:00
|
|
|
begin = time.time()
|
|
|
|
connection_success = False
|
|
|
|
last_exception = None
|
|
|
|
while time.time() - begin < timeout:
|
|
|
|
yield from asyncio.sleep(0.01)
|
2015-03-01 21:25:09 +00:00
|
|
|
try:
|
2015-03-21 01:19:49 +00:00
|
|
|
self._reader, self._writer = yield from asyncio.open_connection(host, self._port)
|
2015-03-01 21:25:09 +00:00
|
|
|
except OSError as e:
|
2015-03-06 01:00:17 +00:00
|
|
|
last_exception = e
|
|
|
|
continue
|
|
|
|
connection_success = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if not connection_success:
|
|
|
|
raise DynamipsError("Couldn't connect to hypervisor on {}:{} :{}".format(host, self._port, last_exception))
|
|
|
|
else:
|
|
|
|
log.info("Connected to Dynamips hypervisor after {:.4f} seconds".format(time.time() - begin))
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
try:
|
2015-02-10 01:24:13 +00:00
|
|
|
version = yield from self.send("hypervisor version")
|
|
|
|
self._version = version[0].split("-", 1)[0]
|
2013-12-22 00:42:33 +00:00
|
|
|
except IndexError:
|
|
|
|
self._version = "Unknown"
|
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
self._uuid = yield from self.send("hypervisor uuid")
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2014-05-02 21:38:52 +00:00
|
|
|
# this forces to send the working dir to Dynamips
|
2015-02-10 01:24:13 +00:00
|
|
|
yield from self.set_working_dir(self._working_dir)
|
2014-05-02 21:38:52 +00:00
|
|
|
|
2013-12-22 00:42:33 +00:00
|
|
|
@property
|
|
|
|
def version(self):
|
|
|
|
"""
|
|
|
|
Returns Dynamips version.
|
|
|
|
|
|
|
|
:returns: version string
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._version
|
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
@asyncio.coroutine
|
2013-12-22 00:42:33 +00:00
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
Closes the connection to this hypervisor (but leave it running).
|
|
|
|
"""
|
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
yield from self.send("hypervisor close")
|
2015-03-21 01:19:49 +00:00
|
|
|
self._writer.close()
|
|
|
|
self._reader, self._writer = None
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
@asyncio.coroutine
|
2013-12-22 00:42:33 +00:00
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Stops this hypervisor (will no longer run).
|
|
|
|
"""
|
|
|
|
|
2015-03-21 01:19:49 +00:00
|
|
|
try:
|
|
|
|
# try to properly stop the hypervisor
|
|
|
|
yield from self.send("hypervisor stop")
|
|
|
|
except DynamipsError:
|
|
|
|
pass
|
|
|
|
try:
|
2015-03-27 09:08:03 +00:00
|
|
|
if self._writer is not None:
|
|
|
|
yield from self._writer.drain()
|
|
|
|
self._writer.close()
|
2015-03-21 01:19:49 +00:00
|
|
|
except OSError as e:
|
|
|
|
log.debug("Stopping hypervisor {}:{} {}".format(self._host, self._port, e))
|
|
|
|
self._reader = self._writer = None
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
@asyncio.coroutine
|
2013-12-22 00:42:33 +00:00
|
|
|
def reset(self):
|
|
|
|
"""
|
|
|
|
Resets this hypervisor (used to get an empty configuration).
|
|
|
|
"""
|
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
yield from self.send("hypervisor reset")
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def set_working_dir(self, working_dir):
|
2013-12-22 00:42:33 +00:00
|
|
|
"""
|
2014-01-31 23:31:34 +00:00
|
|
|
Sets the working directory for this hypervisor.
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
:param working_dir: path to the working directory
|
|
|
|
"""
|
|
|
|
|
|
|
|
# encase working_dir in quotes to protect spaces in the path
|
2015-02-27 23:51:17 +00:00
|
|
|
yield from self.send('hypervisor working_dir "{}"'.format(working_dir))
|
2013-12-22 00:42:33 +00:00
|
|
|
self._working_dir = working_dir
|
2015-02-10 01:24:13 +00:00
|
|
|
log.debug("Working directory set to {}".format(self._working_dir))
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
@property
|
|
|
|
def working_dir(self):
|
2013-12-22 00:42:33 +00:00
|
|
|
"""
|
2015-02-10 01:24:13 +00:00
|
|
|
Returns current working directory
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
:returns: path to the working directory
|
2013-12-22 00:42:33 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
return self._working_dir
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def uuid(self):
|
|
|
|
"""
|
|
|
|
Returns this hypervisor UUID.
|
|
|
|
|
|
|
|
:Returns: uuid string
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._uuid
|
|
|
|
|
|
|
|
@property
|
|
|
|
def devices(self):
|
|
|
|
"""
|
|
|
|
Returns the list of devices managed by this hypervisor instance.
|
|
|
|
|
2014-02-28 04:50:46 +00:00
|
|
|
:returns: a list of device instances
|
2013-12-22 00:42:33 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
return self._devices
|
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
@property
|
|
|
|
def port(self):
|
|
|
|
"""
|
|
|
|
Returns the port used to start the hypervisor.
|
|
|
|
|
|
|
|
:returns: port number (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._port
|
|
|
|
|
|
|
|
@port.setter
|
|
|
|
def port(self, port):
|
|
|
|
"""
|
|
|
|
Sets the port used to start the hypervisor.
|
|
|
|
|
|
|
|
:param port: port number (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._port = port
|
|
|
|
|
2013-12-22 00:42:33 +00:00
|
|
|
@property
|
|
|
|
def host(self):
|
|
|
|
"""
|
2015-02-10 01:24:13 +00:00
|
|
|
Returns the host (binding) used to start the hypervisor.
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
:returns: host/address (string)
|
2013-12-22 00:42:33 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
return self._host
|
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
@host.setter
|
|
|
|
def host(self, host):
|
2013-12-22 00:42:33 +00:00
|
|
|
"""
|
2015-02-10 01:24:13 +00:00
|
|
|
Sets the host (binding) used to start the hypervisor.
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
:param host: host/address (string)
|
2013-12-22 00:42:33 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
self._host = host
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2015-02-10 01:24:13 +00:00
|
|
|
@asyncio.coroutine
|
2013-12-22 00:42:33 +00:00
|
|
|
def send(self, command):
|
|
|
|
"""
|
|
|
|
Sends commands to this hypervisor.
|
|
|
|
|
|
|
|
:param command: a Dynamips hypervisor command
|
|
|
|
|
|
|
|
:returns: results as a list
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Dynamips responses are of the form:
|
|
|
|
# 1xx yyyyyy\r\n
|
|
|
|
# 1xx yyyyyy\r\n
|
|
|
|
# ...
|
|
|
|
# 100-yyyy\r\n
|
|
|
|
# or
|
|
|
|
# 2xx-yyyy\r\n
|
|
|
|
#
|
|
|
|
# Where 1xx is a code from 100-199 for a success or 200-299 for an error
|
|
|
|
# The result might be multiple lines and might be less than the buffer size
|
|
|
|
# but still have more data. The only thing we know for sure is the last line
|
|
|
|
# will begin with '100-' or a '2xx-' and end with '\r\n'
|
|
|
|
|
2015-03-20 09:21:27 +00:00
|
|
|
with (yield from self._io_lock):
|
|
|
|
if self._writer is None or self._reader is None:
|
|
|
|
raise DynamipsError("Not connected")
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
try:
|
2015-03-20 09:21:27 +00:00
|
|
|
command = command.strip() + '\n'
|
|
|
|
log.debug("sending {}".format(command))
|
|
|
|
self._writer.write(command.encode())
|
2015-03-29 01:23:01 +00:00
|
|
|
yield from self._writer.drain()
|
2014-04-06 17:37:34 +00:00
|
|
|
except OSError as e:
|
2015-03-20 09:21:27 +00:00
|
|
|
raise DynamipsError("Lost communication with {host}:{port} :{error}, Dynamips process running: {run}"
|
2014-12-19 22:47:12 +00:00
|
|
|
.format(host=self._host, port=self._port, error=e, run=self.is_running()))
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2015-03-20 09:21:27 +00:00
|
|
|
# Now retrieve the result
|
|
|
|
data = []
|
2013-12-22 00:42:33 +00:00
|
|
|
buf = ''
|
2015-03-20 09:21:27 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2015-03-29 01:23:01 +00:00
|
|
|
try:
|
|
|
|
line = yield from self._reader.readline()
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
# task has been canceled but continue to read
|
|
|
|
# any remaining data sent by the hypervisor
|
|
|
|
continue
|
|
|
|
if not line:
|
2015-03-20 09:21:27 +00:00
|
|
|
raise DynamipsError("No data returned from {host}:{port}, Dynamips process running: {run}"
|
|
|
|
.format(host=self._host, port=self._port, run=self.is_running()))
|
2015-03-29 01:23:01 +00:00
|
|
|
buf += line.decode()
|
2015-03-20 09:21:27 +00:00
|
|
|
except OSError as e:
|
2015-03-21 01:19:49 +00:00
|
|
|
raise DynamipsError("Lost communication with {host}:{port} :{error}, Dynamips process running: {run}"
|
2015-03-20 09:21:27 +00:00
|
|
|
.format(host=self._host, port=self._port, error=e, run=self.is_running()))
|
|
|
|
|
|
|
|
# If the buffer doesn't end in '\n' then we can't be done
|
|
|
|
try:
|
|
|
|
if buf[-1] != '\n':
|
|
|
|
continue
|
|
|
|
except IndexError:
|
|
|
|
raise DynamipsError("Could not communicate with {host}:{port}, Dynamips process running: {run}"
|
|
|
|
.format(host=self._host, port=self._port, run=self.is_running()))
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2015-03-20 09:21:27 +00:00
|
|
|
data += buf.split('\r\n')
|
|
|
|
if data[-1] == '':
|
2013-12-22 00:42:33 +00:00
|
|
|
data.pop()
|
2015-03-20 09:21:27 +00:00
|
|
|
buf = ''
|
|
|
|
|
|
|
|
# Does it contain an error code?
|
|
|
|
if self.error_re.search(data[-1]):
|
|
|
|
raise DynamipsError(data[-1][4:])
|
|
|
|
|
|
|
|
# Or does the last line begin with '100-'? Then we are done!
|
|
|
|
if data[-1][:4] == '100-':
|
|
|
|
data[-1] = data[-1][4:]
|
|
|
|
if data[-1] == 'OK':
|
|
|
|
data.pop()
|
|
|
|
break
|
|
|
|
|
|
|
|
# Remove success responses codes
|
|
|
|
for index in range(len(data)):
|
|
|
|
if self.success_re.search(data[index]):
|
|
|
|
data[index] = data[index][4:]
|
|
|
|
|
|
|
|
log.debug("returned result {}".format(data))
|
|
|
|
return data
|