From cdc778c159c3528feed4e2bdb592f521e2d99aac Mon Sep 17 00:00:00 2001 From: grossmj Date: Fri, 18 Jul 2014 18:04:21 -0600 Subject: [PATCH] Fixes issue with VirtualBox integration on Windows. --- gns3server/modules/virtualbox/__init__.py | 6 --- gns3server/modules/virtualbox/pipe_proxy.py | 3 +- .../modules/virtualbox/virtualbox_vm.py | 48 +++++++++++-------- gns3server/modules/vpcs/vpcs_device.py | 1 - 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/gns3server/modules/virtualbox/__init__.py b/gns3server/modules/virtualbox/__init__.py index c8d7d7ec..d7e0f1f9 100644 --- a/gns3server/modules/virtualbox/__init__.py +++ b/gns3server/modules/virtualbox/__init__.py @@ -32,12 +32,6 @@ from .vboxwrapper_client import VboxWrapperClient from .nios.nio_udp import NIO_UDP from ..attic import find_unused_port -if sys.platform.startswith("win"): - # automatically generate the Typelib wrapper - import win32com - win32com.client.gencache.is_readonly = False - win32com.client.gencache.GetGeneratePath() - from .schemas import VBOX_CREATE_SCHEMA from .schemas import VBOX_DELETE_SCHEMA from .schemas import VBOX_UPDATE_SCHEMA diff --git a/gns3server/modules/virtualbox/pipe_proxy.py b/gns3server/modules/virtualbox/pipe_proxy.py index 5a5b8710..619a48fd 100644 --- a/gns3server/modules/virtualbox/pipe_proxy.py +++ b/gns3server/modules/virtualbox/pipe_proxy.py @@ -142,8 +142,7 @@ class PipeProxy(threading.Thread): # For some reason, windows likes to send "cr/lf" when you send a "cr". # Strip that so we don't get a double prompt. - #data = string.replace(data, chr(13) + chr(10), chr(13)) - data = data.replace(bytearray([13, 10]), bytes(13)) + data = data.replace(b"\r\n", b"\n") self.write_to_pipe(data) except Exception as msg: diff --git a/gns3server/modules/virtualbox/virtualbox_vm.py b/gns3server/modules/virtualbox/virtualbox_vm.py index 3614872d..3c4153c0 100644 --- a/gns3server/modules/virtualbox/virtualbox_vm.py +++ b/gns3server/modules/virtualbox/virtualbox_vm.py @@ -26,6 +26,7 @@ import tempfile import re import time import socket +import subprocess from .pipe_proxy import PipeProxy from .virtualbox_error import VirtualBoxError @@ -92,8 +93,6 @@ class VirtualBoxVM(object): self._host = host self._command = [] self._vboxwrapper = vboxwrapper - - self._host = "127.0.0.1" self._started = False self._console_start_port_range = console_start_port_range self._console_end_port_range = console_end_port_range @@ -483,7 +482,7 @@ class VirtualBoxVM(object): except OSError as e: raise VirtualBoxError("Could not open the pipe {}: {}".format(pipe_name, e)) self._serial_pipe_thread = PipeProxy(self._vmname, msvcrt.get_osfhandle(self._serial_pipe.fileno()), self._host, self._console) - self._serial_pipe_thread.setDaemon(True) + #self._serial_pipe_thread.setDaemon(True) self._serial_pipe_thread.start() else: try: @@ -492,7 +491,7 @@ class VirtualBoxVM(object): except OSError as e: raise VirtualBoxError("Could not connect to the pipe {}: {}".format(pipe_name, e)) self._serial_pipe_thread = PipeProxy(self._vmname, self._serial_pipe, self._host, self._console) - self._serial_pipe_thread.setDaemon(True) + #self._serial_pipe_thread.setDaemon(True) self._serial_pipe_thread.start() def stop(self): @@ -503,11 +502,32 @@ class VirtualBoxVM(object): if self._vboxwrapper: self._vboxwrapper.send('vbox stop "{}"'.format(self._name)) else: + + if self._serial_pipe_thread: + self._serial_pipe_thread.stop() + self._serial_pipe_thread.join(1) + if self._serial_pipe_thread.isAlive(): + log.warn("Serial pire thread is still alive!") + self._serial_pipe_thread = None + + if self._serial_pipe: + if sys.platform.startswith('win'): + win32file.CloseHandle(msvcrt.get_osfhandle(self._serial_pipe.fileno())) + else: + self._serial_pipe.close() + self._serial_pipe = None + try: - progress = self._session.console.powerDown() - # wait for VM to actually go down - progress.waitForCompletion(-1) - log.info("VM is stopping with {}% completed".format(self.vmname, progress.percent)) + if sys.platform.startswith('win') and "VBOX_INSTALL_PATH" in os.environ: + # work around VirtualBox bug #9239 + vboxmanage_path = os.path.join(os.environ["VBOX_INSTALL_PATH"], "VBoxManage.exe") + command = '"{}" controlvm "{}" poweroff'.format(vboxmanage_path, self._vmname) + subprocess.call(command, timeout=3) + else: + progress = self._session.console.powerDown() + # wait for VM to actually go down + progress.waitForCompletion(3000) + log.info("VM is stopping with {}% completed".format(self.vmname, progress.percent)) self._lock_machine() for adapter_id in range(0, len(self._ethernet_adapters)): @@ -520,18 +540,6 @@ class VirtualBoxVM(object): # This can happen, if user manually kills VBox VM. log.warn("could not stop VM for {}: {}".format(self._vmname, e)) return - finally: - if self._serial_pipe_thread: - self._serial_pipe_thread.stop() - self._serial_pipe_thread.join() - self._serial_pipe_thread = None - - if self._serial_pipe: - if sys.platform.startswith('win'): - win32file.CloseHandle(msvcrt.get_osfhandle(self._serial_pipe.fileno())) - else: - self._serial_pipe.close() - self._serial_pipe = None def suspend(self): """ diff --git a/gns3server/modules/vpcs/vpcs_device.py b/gns3server/modules/vpcs/vpcs_device.py index 3b0fdb2c..33d5d8e0 100644 --- a/gns3server/modules/vpcs/vpcs_device.py +++ b/gns3server/modules/vpcs/vpcs_device.py @@ -93,7 +93,6 @@ class VPCSDevice(object): self._command = [] self._process = None self._vpcs_stdout_file = "" - self._host = "127.0.0.1" self._started = False self._console_start_port_range = console_start_port_range self._console_end_port_range = console_end_port_range