2013-12-22 02:42:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2015-02-10 03:24:13 +02:00
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
2013-12-22 02:42:33 +02: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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Represents a Dynamips hypervisor and starts/stops the associated Dynamips process.
|
|
|
|
"""
|
|
|
|
|
2016-05-21 23:43:10 +03:00
|
|
|
import sys
|
2013-12-22 02:42:33 +02:00
|
|
|
import os
|
|
|
|
import subprocess
|
2015-02-10 03:24:13 +02:00
|
|
|
import asyncio
|
2013-12-22 02:42:33 +02:00
|
|
|
|
2015-07-20 07:55:10 +03:00
|
|
|
from gns3server.utils.asyncio import wait_for_process_termination
|
2013-12-22 02:42:33 +02:00
|
|
|
from .dynamips_hypervisor import DynamipsHypervisor
|
2014-03-03 00:20:03 +02:00
|
|
|
from .dynamips_error import DynamipsError
|
2013-12-22 02:42:33 +02:00
|
|
|
|
2014-03-03 00:20:03 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2013-12-22 02:42:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Hypervisor(DynamipsHypervisor):
|
2015-02-13 15:43:28 +02:00
|
|
|
|
2013-12-22 02:42:33 +02:00
|
|
|
"""
|
|
|
|
Hypervisor.
|
|
|
|
|
|
|
|
:param path: path to Dynamips executable
|
2014-03-03 00:20:03 +02:00
|
|
|
:param working_dir: working directory
|
2013-12-22 02:42:33 +02:00
|
|
|
:param host: host/address for this hypervisor
|
2015-03-02 03:53:03 +02:00
|
|
|
:param port: port for this hypervisor
|
|
|
|
:param console_host: host/address for console connections
|
2013-12-22 02:42:33 +02:00
|
|
|
"""
|
|
|
|
|
2015-02-17 06:30:31 +02:00
|
|
|
_instance_count = 1
|
2013-12-22 02:42:33 +02:00
|
|
|
|
2015-03-02 03:53:03 +02:00
|
|
|
def __init__(self, path, working_dir, host, port, console_host):
|
2013-12-22 02:42:33 +02:00
|
|
|
|
2015-04-08 20:17:34 +03:00
|
|
|
super().__init__(working_dir, host, port)
|
2013-12-22 02:42:33 +02:00
|
|
|
|
|
|
|
# create an unique ID
|
|
|
|
self._id = Hypervisor._instance_count
|
|
|
|
Hypervisor._instance_count += 1
|
|
|
|
|
2015-03-02 03:53:03 +02:00
|
|
|
self._console_host = console_host
|
2013-12-22 02:42:33 +02:00
|
|
|
self._path = path
|
|
|
|
self._command = []
|
|
|
|
self._process = None
|
2014-03-03 00:20:03 +02:00
|
|
|
self._stdout_file = ""
|
2014-03-20 02:48:42 +02:00
|
|
|
self._started = False
|
2013-12-22 02:42:33 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
"""
|
|
|
|
Returns the unique ID for this hypervisor.
|
|
|
|
|
|
|
|
:returns: id (integer)
|
|
|
|
"""
|
|
|
|
|
2014-05-28 15:26:20 +03:00
|
|
|
return self._id
|
2013-12-22 02:42:33 +02:00
|
|
|
|
2015-03-04 17:01:56 +02:00
|
|
|
@property
|
|
|
|
def process(self):
|
|
|
|
"""
|
|
|
|
Returns the subprocess of the Hypervisor
|
|
|
|
|
|
|
|
:returns: subprocess
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._process
|
|
|
|
|
2014-03-20 02:48:42 +02:00
|
|
|
@property
|
|
|
|
def started(self):
|
|
|
|
"""
|
|
|
|
Returns either this hypervisor has been started or not.
|
|
|
|
|
|
|
|
:returns: boolean
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._started
|
|
|
|
|
2013-12-22 02:42:33 +02:00
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
"""
|
|
|
|
Returns the path to the Dynamips executable.
|
|
|
|
|
|
|
|
:returns: path to Dynamips
|
|
|
|
"""
|
|
|
|
|
2014-05-28 15:26:20 +03:00
|
|
|
return self._path
|
2013-12-22 02:42:33 +02:00
|
|
|
|
|
|
|
@path.setter
|
|
|
|
def path(self, path):
|
|
|
|
"""
|
2014-02-01 01:31:34 +02:00
|
|
|
Sets the path to the Dynamips executable.
|
2013-12-22 02:42:33 +02:00
|
|
|
|
|
|
|
:param path: path to Dynamips
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._path = path
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def start(self):
|
2013-12-22 02:42:33 +02:00
|
|
|
"""
|
|
|
|
Starts the Dynamips hypervisor process.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._command = self._build_command()
|
2016-05-21 23:43:10 +03:00
|
|
|
env = os.environ.copy()
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
# add the Npcap directory to $PATH to force Dynamips to use npcap DLL instead of Winpcap (if installed)
|
|
|
|
system_root = os.path.join(os.path.expandvars("%SystemRoot%"), "System32", "Npcap")
|
|
|
|
if os.path.isdir(system_root):
|
|
|
|
env["PATH"] = system_root + ';' + env["PATH"]
|
2013-12-22 02:42:33 +02:00
|
|
|
try:
|
2015-02-10 03:24:13 +02:00
|
|
|
log.info("Starting Dynamips: {}".format(self._command))
|
2016-03-19 19:32:15 +02:00
|
|
|
self._stdout_file = os.path.join(self.working_dir, "dynamips_i{}_stdout.txt".format(self._id))
|
|
|
|
log.info("Dynamips process logging to {}".format(self._stdout_file))
|
|
|
|
with open(self._stdout_file, "w", encoding="utf-8") as fd:
|
2018-10-15 13:05:49 +03:00
|
|
|
self._process = await asyncio.create_subprocess_exec(*self._command,
|
2015-02-10 03:24:13 +02:00
|
|
|
stdout=fd,
|
|
|
|
stderr=subprocess.STDOUT,
|
2016-05-21 23:43:10 +03:00
|
|
|
cwd=self._working_dir,
|
|
|
|
env=env)
|
2015-02-10 03:24:13 +02:00
|
|
|
log.info("Dynamips process started PID={}".format(self._process.pid))
|
2014-03-20 02:48:42 +02:00
|
|
|
self._started = True
|
2014-12-11 21:15:24 +02:00
|
|
|
except (OSError, subprocess.SubprocessError) as e:
|
2015-02-10 03:24:13 +02:00
|
|
|
log.error("Could not start Dynamips: {}".format(e))
|
|
|
|
raise DynamipsError("Could not start Dynamips: {}".format(e))
|
2013-12-22 02:42:33 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def stop(self):
|
2013-12-22 02:42:33 +02:00
|
|
|
"""
|
|
|
|
Stops the Dynamips hypervisor process.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.is_running():
|
2015-02-10 03:24:13 +02:00
|
|
|
log.info("Stopping Dynamips process PID={}".format(self._process.pid))
|
2018-10-15 13:05:49 +03:00
|
|
|
await DynamipsHypervisor.stop(self)
|
2015-02-10 03:24:13 +02:00
|
|
|
# give some time for the hypervisor to properly stop.
|
|
|
|
# time to delete UNIX NIOs for instance.
|
2018-10-15 13:05:49 +03:00
|
|
|
await asyncio.sleep(0.01)
|
2014-03-11 23:45:04 +02:00
|
|
|
try:
|
2018-10-15 13:05:49 +03:00
|
|
|
await wait_for_process_termination(self._process, timeout=3)
|
2015-02-10 03:24:13 +02:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
if self._process.returncode is None:
|
2018-03-15 09:17:39 +02:00
|
|
|
log.warning("Dynamips process {} is still running... killing it".format(self._process.pid))
|
2015-09-04 01:18:27 +03:00
|
|
|
try:
|
|
|
|
self._process.kill()
|
|
|
|
except OSError as e:
|
|
|
|
log.error("Cannot stop the Dynamips process: {}".format(e))
|
|
|
|
if self._process.returncode is None:
|
2018-03-15 09:17:39 +02:00
|
|
|
log.warning('Dynamips hypervisor with PID={} is still running'.format(self._process.pid))
|
2013-12-22 02:42:33 +02:00
|
|
|
|
2014-04-10 03:39:24 +03: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 02:48:42 +02:00
|
|
|
self._started = False
|
|
|
|
|
2013-12-22 02:42:33 +02: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-03 00:20:03 +02:00
|
|
|
output = ""
|
2014-04-10 03:39:24 +03:00
|
|
|
if self._stdout_file and os.access(self._stdout_file, os.R_OK):
|
2014-03-03 00:20:03 +02:00
|
|
|
try:
|
2015-04-25 20:58:34 +03:00
|
|
|
with open(self._stdout_file, "rb") as file:
|
|
|
|
output = file.read().decode("utf-8", errors="replace")
|
2014-04-03 01:10:59 +03:00
|
|
|
except OSError as e:
|
2018-03-15 09:17:39 +02:00
|
|
|
log.warning("could not read {}: {}".format(self._stdout_file, e))
|
2013-12-22 02:42:33 +02:00
|
|
|
return output
|
|
|
|
|
|
|
|
def is_running(self):
|
|
|
|
"""
|
|
|
|
Checks if the process is running
|
|
|
|
|
|
|
|
:returns: True or False
|
|
|
|
"""
|
|
|
|
|
2015-02-10 03:24:13 +02:00
|
|
|
if self._process and self._process.returncode is None:
|
2013-12-22 02:42:33 +02: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
|
2015-02-17 06:30:31 +02:00
|
|
|
command.extend(["-l", "dynamips_i{}_log.txt".format(self._id)]) # log file
|
2023-01-04 12:21:17 +02:00
|
|
|
if not sys.platform.startswith("win"):
|
2023-01-02 09:26:59 +02:00
|
|
|
command.extend(["-H", "{}:{}".format(self._host, self._port), "--console-binding-addr", self._console_host])
|
2013-12-22 02:42:33 +02:00
|
|
|
else:
|
2014-04-16 21:43:59 +03:00
|
|
|
command.extend(["-H", str(self._port)])
|
2023-01-02 09:26:59 +02:00
|
|
|
|
2013-12-22 02:42:33 +02:00
|
|
|
return command
|