2013-12-22 00:42:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Represents a Dynamips hypervisor and starts/stops the associated Dynamips process.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
2014-01-31 23:31:34 +00:00
|
|
|
import time
|
2013-12-22 00:42:33 +00:00
|
|
|
import subprocess
|
2014-04-10 00:39:24 +00:00
|
|
|
import tempfile
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
from .dynamips_hypervisor import DynamipsHypervisor
|
2014-03-02 22:20:03 +00:00
|
|
|
from .dynamips_error import DynamipsError
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2014-03-02 22:20:03 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Hypervisor(DynamipsHypervisor):
|
|
|
|
"""
|
|
|
|
Hypervisor.
|
|
|
|
|
|
|
|
:param path: path to Dynamips executable
|
2014-03-02 22:20:03 +00:00
|
|
|
:param working_dir: working directory
|
2013-12-22 00:42:33 +00:00
|
|
|
:param port: port for this hypervisor
|
|
|
|
:param host: host/address for this hypervisor
|
|
|
|
"""
|
|
|
|
|
|
|
|
_instance_count = 0
|
|
|
|
|
2014-03-02 22:20:03 +00:00
|
|
|
def __init__(self, path, working_dir, host, port):
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2014-03-02 22:20:03 +00:00
|
|
|
DynamipsHypervisor.__init__(self, working_dir, host, port)
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
# create an unique ID
|
|
|
|
self._id = Hypervisor._instance_count
|
|
|
|
Hypervisor._instance_count += 1
|
|
|
|
|
|
|
|
self._path = path
|
|
|
|
self._command = []
|
|
|
|
self._process = None
|
2014-03-02 22:20:03 +00:00
|
|
|
self._stdout_file = ""
|
2014-03-20 00:48:42 +00:00
|
|
|
self._started = False
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
# settings used the load-balance hypervisors
|
|
|
|
# (for the hypervisor manager)
|
|
|
|
self._memory_load = 0
|
|
|
|
self._ios_image_ref = ""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
"""
|
|
|
|
Returns the unique ID for this hypervisor.
|
|
|
|
|
|
|
|
:returns: id (integer)
|
|
|
|
"""
|
|
|
|
|
2014-05-28 12:26:20 +00:00
|
|
|
return self._id
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2014-03-20 00:48:42 +00:00
|
|
|
@property
|
|
|
|
def started(self):
|
|
|
|
"""
|
|
|
|
Returns either this hypervisor has been started or not.
|
|
|
|
|
|
|
|
:returns: boolean
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._started
|
|
|
|
|
2013-12-22 00:42:33 +00:00
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
"""
|
|
|
|
Returns the path to the Dynamips executable.
|
|
|
|
|
|
|
|
:returns: path to Dynamips
|
|
|
|
"""
|
|
|
|
|
2014-05-28 12:26:20 +00:00
|
|
|
return self._path
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
@path.setter
|
|
|
|
def path(self, path):
|
|
|
|
"""
|
2014-01-31 23:31:34 +00:00
|
|
|
Sets the path to the Dynamips executable.
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
:param path: path to Dynamips
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._path = path
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port(self):
|
|
|
|
"""
|
|
|
|
Returns the port used to start the Dynamips hypervisor.
|
|
|
|
|
|
|
|
:returns: port number (integer)
|
|
|
|
"""
|
|
|
|
|
2014-05-28 12:26:20 +00:00
|
|
|
return self._port
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
@port.setter
|
|
|
|
def port(self, port):
|
|
|
|
"""
|
2014-01-31 23:31:34 +00:00
|
|
|
Sets the port used to start the Dynamips hypervisor.
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
:param port: port number (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._port = port
|
|
|
|
|
|
|
|
@property
|
|
|
|
def host(self):
|
|
|
|
"""
|
|
|
|
Returns the host (binding) used to start the Dynamips hypervisor.
|
|
|
|
|
|
|
|
:returns: host/address (string)
|
|
|
|
"""
|
|
|
|
|
2014-05-28 12:26:20 +00:00
|
|
|
return self._host
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
@host.setter
|
|
|
|
def host(self, host):
|
|
|
|
"""
|
2014-01-31 23:31:34 +00:00
|
|
|
Sets the host (binding) used to start the Dynamips hypervisor.
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
:param host: host/address (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._host = host
|
|
|
|
|
|
|
|
@property
|
|
|
|
def image_ref(self):
|
|
|
|
"""
|
|
|
|
Returns the reference IOS image name
|
|
|
|
(used by the hypervisor manager for load-balancing purposes).
|
|
|
|
|
|
|
|
:returns: image reference name
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._ios_image_ref
|
|
|
|
|
|
|
|
@image_ref.setter
|
|
|
|
def image_ref(self, ios_image_name):
|
|
|
|
"""
|
2014-01-31 23:31:34 +00:00
|
|
|
Sets the reference IOS image name
|
2013-12-22 00:42:33 +00:00
|
|
|
(used by the hypervisor manager for load-balancing purposes).
|
|
|
|
|
|
|
|
:param ios_image_name: image reference name
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._ios_image_ref = ios_image_name
|
|
|
|
|
|
|
|
def increase_memory_load(self, memory):
|
|
|
|
"""
|
|
|
|
Increases the memory load of this hypervisor.
|
|
|
|
(used by the hypervisor manager for load-balancing purposes).
|
|
|
|
|
|
|
|
:param memory: amount of RAM (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._memory_load += memory
|
|
|
|
|
|
|
|
def decrease_memory_load(self, memory):
|
|
|
|
"""
|
|
|
|
Decreases the memory load of this hypervisor.
|
|
|
|
(used by the hypervisor manager for load-balancing purposes).
|
|
|
|
|
|
|
|
:param memory: amount of RAM (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._memory_load -= memory
|
|
|
|
|
|
|
|
@property
|
|
|
|
def memory_load(self):
|
|
|
|
"""
|
|
|
|
Returns the memory load of this hypervisor.
|
|
|
|
(used by the hypervisor manager for load-balancing purposes).
|
|
|
|
|
|
|
|
:returns: amount of RAM (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._memory_load
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""
|
|
|
|
Starts the Dynamips hypervisor process.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._command = self._build_command()
|
|
|
|
try:
|
2014-03-02 22:20:03 +00:00
|
|
|
log.info("starting Dynamips: {}".format(self._command))
|
2014-04-10 00:39:24 +00:00
|
|
|
with tempfile.NamedTemporaryFile(delete=False) as fd:
|
|
|
|
self._stdout_file = fd.name
|
|
|
|
log.info("Dynamips process logging to {}".format(fd.name))
|
2014-03-02 22:20:03 +00:00
|
|
|
self._process = subprocess.Popen(self._command,
|
|
|
|
stdout=fd,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
cwd=self._working_dir)
|
|
|
|
log.info("Dynamips started PID={}".format(self._process.pid))
|
2014-03-20 00:48:42 +00:00
|
|
|
self._started = True
|
2014-11-23 00:45:04 +00:00
|
|
|
except subprocess.SubprocessError as e:
|
2014-03-02 22:20:03 +00:00
|
|
|
log.error("could not start Dynamips: {}".format(e))
|
|
|
|
raise DynamipsError("could not start Dynamips: {}".format(e))
|
2013-12-22 00:42:33 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Stops the Dynamips hypervisor process.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.is_running():
|
2014-01-31 23:31:34 +00:00
|
|
|
DynamipsHypervisor.stop(self)
|
2014-03-02 22:20:03 +00:00
|
|
|
log.info("stopping Dynamips PID={}".format(self._process.pid))
|
2014-03-11 21:45:04 +00:00
|
|
|
try:
|
|
|
|
# give some time for the hypervisor to properly stop.
|
|
|
|
# time to delete UNIX NIOs for instance.
|
|
|
|
time.sleep(0.01)
|
|
|
|
self._process.terminate()
|
|
|
|
self._process.wait(1)
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
self._process.kill()
|
2014-05-28 12:26:20 +00:00
|
|
|
if self._process.poll() is None:
|
2014-03-11 21:45:04 +00:00
|
|
|
log.warn("Dynamips process {} is still running".format(self._process.pid))
|
2013-12-22 00:42:33 +00:00
|
|
|
|
2014-04-10 00:39:24 +00:00
|
|
|
if self._stdout_file and os.access(self._stdout_file, os.W_OK):
|
|
|
|
try:
|
|
|
|
os.remove(self._stdout_file)
|
|
|
|
except OSError as e:
|
|
|
|
log.warning("could not delete temporary Dynamips log file: {}".format(e))
|
2014-03-20 00:48:42 +00:00
|
|
|
self._started = False
|
|
|
|
|
2013-12-22 00:42:33 +00:00
|
|
|
def read_stdout(self):
|
|
|
|
"""
|
|
|
|
Reads the standard output of the Dynamips process.
|
|
|
|
Only use when the process has been stopped or has crashed.
|
|
|
|
"""
|
|
|
|
|
2014-03-02 22:20:03 +00:00
|
|
|
output = ""
|
2014-04-10 00:39:24 +00:00
|
|
|
if self._stdout_file and os.access(self._stdout_file, os.R_OK):
|
2014-03-02 22:20:03 +00:00
|
|
|
try:
|
2014-05-29 18:59:13 +00:00
|
|
|
with open(self._stdout_file, errors="replace") as file:
|
2014-03-02 22:20:03 +00:00
|
|
|
output = file.read()
|
2014-04-02 22:10:59 +00:00
|
|
|
except OSError as e:
|
2014-03-02 22:20:03 +00:00
|
|
|
log.warn("could not read {}: {}".format(self._stdout_file, e))
|
2013-12-22 00:42:33 +00:00
|
|
|
return output
|
|
|
|
|
|
|
|
def is_running(self):
|
|
|
|
"""
|
|
|
|
Checks if the process is running
|
|
|
|
|
|
|
|
:returns: True or False
|
|
|
|
"""
|
|
|
|
|
2014-05-28 12:26:20 +00:00
|
|
|
if self._process and self._process.poll() is None:
|
2013-12-22 00:42:33 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _build_command(self):
|
|
|
|
"""
|
|
|
|
Command to start the Dynamips hypervisor process.
|
|
|
|
(to be passed to subprocess.Popen())
|
|
|
|
"""
|
|
|
|
|
|
|
|
command = [self._path]
|
|
|
|
command.extend(["-N1"]) # use instance IDs for filenames
|
2014-05-13 23:17:26 +00:00
|
|
|
command.extend(["-l", "dynamips_log_{}.txt".format(self._port)]) # log file
|
2014-04-16 18:43:59 +00:00
|
|
|
if self._host != "0.0.0.0" and self._host != "::":
|
2014-05-13 23:17:26 +00:00
|
|
|
command.extend(["-H", "{}:{}".format(self._host, self._port)])
|
2013-12-22 00:42:33 +00:00
|
|
|
else:
|
2014-04-16 18:43:59 +00:00
|
|
|
command.extend(["-H", str(self._port)])
|
2013-12-22 00:42:33 +00:00
|
|
|
return command
|