1
0
mirror of https://github.com/GNS3/gns3-server synced 2025-01-23 14:31:03 +00:00

Attempt to fix error when loading wmi module. Fixes #1712

This commit is contained in:
grossmj 2020-01-23 19:04:17 +08:00
parent ab4b391a02
commit fb4ece5c67

View File

@ -21,9 +21,6 @@ import asyncio
import psutil import psutil
import ipaddress import ipaddress
if sys.platform.startswith("win"):
import wmi
from .base_gns3_vm import BaseGNS3VM from .base_gns3_vm import BaseGNS3VM
from .gns3_vm_error import GNS3VMError from .gns3_vm_error import GNS3VMError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -47,6 +44,7 @@ class HyperVGNS3VM(BaseGNS3VM):
self._conn = None self._conn = None
self._vm = None self._vm = None
self._management = None self._management = None
self._wmi = None
def _check_requirements(self): def _check_requirements(self):
""" """
@ -64,8 +62,12 @@ class HyperVGNS3VM(BaseGNS3VM):
raise GNS3VMError("Hyper-V with nested virtualization is only supported on Windows 10 Anniversary Update (build 10.0.14393) or later") raise GNS3VMError("Hyper-V with nested virtualization is only supported on Windows 10 Anniversary Update (build 10.0.14393) or later")
try: try:
conn = wmi.WMI() import pythoncom
except wmi.x_wmi as e: pythoncom.CoInitialize()
import wmi
self._wmi = wmi
conn = self._wmi.WMI()
except self._wmi.x_wmi as e:
raise GNS3VMError("Could not connect to WMI: {}".format(e)) raise GNS3VMError("Could not connect to WMI: {}".format(e))
if not conn.Win32_ComputerSystem()[0].HypervisorPresent: if not conn.Win32_ComputerSystem()[0].HypervisorPresent:
@ -86,8 +88,8 @@ class HyperVGNS3VM(BaseGNS3VM):
self._check_requirements() self._check_requirements()
try: try:
self._conn = wmi.WMI(namespace=r"root\virtualization\v2") self._conn = self._wmi.WMI(namespace=r"root\virtualization\v2")
except wmi.x_wmi as e: except self._wmi.x_wmi as e:
raise GNS3VMError("Could not connect to WMI: {}".format(e)) raise GNS3VMError("Could not connect to WMI: {}".format(e))
if not self._conn.Msvm_VirtualSystemManagementService(): if not self._conn.Msvm_VirtualSystemManagementService():
@ -185,7 +187,7 @@ class HyperVGNS3VM(BaseGNS3VM):
for vm in self._conn.Msvm_ComputerSystem(): for vm in self._conn.Msvm_ComputerSystem():
if vm.ElementName != self._management.SystemName: if vm.ElementName != self._management.SystemName:
vms.append({"vmname": vm.ElementName}) vms.append({"vmname": vm.ElementName})
except wmi.x_wmi as e: except self._wmi.x_wmi as e:
raise GNS3VMError("Could not list Hyper-V VMs: {}".format(e)) raise GNS3VMError("Could not list Hyper-V VMs: {}".format(e))
return vms return vms
@ -194,7 +196,7 @@ class HyperVGNS3VM(BaseGNS3VM):
Gets the WMI object. Gets the WMI object.
""" """
return wmi.WMI(moniker=path.replace('\\', '/')) return self._wmi.WMI(moniker=path.replace('\\', '/'))
async def _set_state(self, state): async def _set_state(self, state):
""" """