mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Basic API for GNS3 VM.
This commit is contained in:
1 parent
27269fb13a
commit
13deecea4e
5 files changed
+246
-67
No files matched your search
@@ -20,14 +20,28 @@ import asyncio
|
||||
|
||||
class BaseGNS3VM:
|
||||
|
||||
def __init__(self, vmname, port):
|
||||
def __init__(self):
|
||||
|
||||
self._vmname = vmname
|
||||
self._vmname = None
|
||||
self._ip_address = None
|
||||
self._port = port
|
||||
self._port = 3080
|
||||
self._headless = False
|
||||
self._vcpus = 1
|
||||
self._ram = 1024
|
||||
self._running = False
|
||||
|
||||
def __json__(self):
|
||||
|
||||
settings = {"vmname": self._vmname,
|
||||
"ip_address": self._ip_address,
|
||||
"port": self._port,
|
||||
"headless": self._headless,
|
||||
"vcpus": self._vcpus,
|
||||
"ram": self._ram,
|
||||
"engine": self._engine}
|
||||
|
||||
return settings
|
||||
|
||||
@property
|
||||
def vmname(self):
|
||||
"""
|
||||
@@ -128,6 +142,64 @@ class BaseGNS3VM:
|
||||
|
||||
self._headless = value
|
||||
|
||||
@property
|
||||
def vcpus(self):
|
||||
"""
|
||||
Returns the number of allocated vCPUs.
|
||||
|
||||
:returns: number of vCPUs.
|
||||
"""
|
||||
|
||||
return self._vcpus
|
||||
|
||||
@vcpus.setter
|
||||
def vcpus(self, new_vcpus):
|
||||
"""
|
||||
Sets the number of allocated vCPUs.
|
||||
|
||||
:param new_vcpus: new number of vCPUs.
|
||||
"""
|
||||
|
||||
self._vcpus = new_vcpus
|
||||
|
||||
@property
|
||||
def ram(self):
|
||||
"""
|
||||
Returns the amount of allocated RAM.
|
||||
|
||||
:returns: number of vCPUs.
|
||||
"""
|
||||
|
||||
return self._ram
|
||||
|
||||
@ram.setter
|
||||
def ram(self, new_ram):
|
||||
"""
|
||||
Sets the the amount of allocated RAM.
|
||||
|
||||
:param new_ram: new amount of RAM.
|
||||
"""
|
||||
|
||||
self._ram = new_ram
|
||||
|
||||
@property
|
||||
def engine(self):
|
||||
"""
|
||||
Returns the engine (virtualization technology used to run the GNS3 VM).
|
||||
|
||||
:returns: engine name
|
||||
"""
|
||||
|
||||
return self._engine
|
||||
|
||||
@asyncio.coroutine
|
||||
def list(self):
|
||||
"""
|
||||
List all VMs
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
@asyncio.coroutine
|
||||
def start(self):
|
||||
"""
|
||||
@@ -136,7 +208,6 @@ class BaseGNS3VM:
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def stop(self, force=False):
|
||||
"""
|
||||
@@ -145,26 +216,14 @@ class BaseGNS3VM:
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
@asyncio.coroutine
|
||||
def set_vcpus(self, vcpus):
|
||||
@classmethod
|
||||
def instance(cls):
|
||||
"""
|
||||
Set the number of vCPU cores for the GNS3 VM.
|
||||
Singleton to return only one instance of BaseGNS3VM.
|
||||
|
||||
:param vcpus: number of vCPU cores
|
||||
|
||||
:returns: boolean
|
||||
:returns: instance of BaseGNS3VM
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
@asyncio.coroutine
|
||||
def set_ram(self, ram):
|
||||
"""
|
||||
Set the RAM amount for the GNS3 VM.
|
||||
|
||||
:param ram: amount of memory
|
||||
|
||||
:returns: boolean
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
if not hasattr(cls, "_instance") or cls._instance is None:
|
||||
cls._instance = cls()
|
||||
return cls._instance
|
||||
@@ -32,16 +32,18 @@ log = logging.getLogger(__name__)
|
||||
|
||||
class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
|
||||
def __init__(self, vmname, port):
|
||||
def __init__(self):
|
||||
|
||||
super().__init__(vmname, port)
|
||||
super().__init__()
|
||||
self._engine = "virtualbox"
|
||||
self._virtualbox_manager = VirtualBox()
|
||||
|
||||
@asyncio.coroutine
|
||||
def _execute(self, subcommand, args, timeout=60):
|
||||
|
||||
try:
|
||||
return (yield from self._virtualbox_manager.execute(subcommand, args, timeout))
|
||||
result = yield from self._virtualbox_manager.execute(subcommand, args, timeout)
|
||||
return (''.join(result))
|
||||
except VirtualBoxError as e:
|
||||
raise GNS3VMError("Error while executing VBoxManage command: {}".format(e))
|
||||
|
||||
@@ -138,6 +140,14 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
return True
|
||||
return False
|
||||
|
||||
@asyncio.coroutine
|
||||
def list(self):
|
||||
"""
|
||||
List all VirtualBox VMs
|
||||
"""
|
||||
|
||||
return (yield from self._virtualbox_manager.list_vms())
|
||||
|
||||
@asyncio.coroutine
|
||||
def start(self):
|
||||
"""
|
||||
@@ -221,7 +231,7 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
raise GNS3VMError("Not IP address could be found in the GNS3 VM for eth{}".format(hostonly_interface_number - 1))
|
||||
|
||||
@asyncio.coroutine
|
||||
def stop(self, force=False):
|
||||
def stop(self):
|
||||
"""
|
||||
Stops the GNS3 VM.
|
||||
"""
|
||||
|
||||
@@ -31,31 +31,68 @@ log = logging.getLogger(__name__)
|
||||
|
||||
class VMwareGNS3VM(BaseGNS3VM):
|
||||
|
||||
def __init__(self, vmname, port, vmx_path=None):
|
||||
def __init__(self):
|
||||
|
||||
super().__init__(vmname, port)
|
||||
self._vmx_path = vmx_path
|
||||
super().__init__()
|
||||
self._engine = "vmware"
|
||||
self._vmware_manager = VMware()
|
||||
self._vmx_path = None
|
||||
|
||||
@asyncio.coroutine
|
||||
def _execute(self, subcommand, args, timeout=60):
|
||||
|
||||
try:
|
||||
return (yield from self._vmware_manager.execute(subcommand, args, timeout))
|
||||
result = yield from self._vmware_manager.execute(subcommand, args, timeout)
|
||||
return (''.join(result))
|
||||
except VMwareError as e:
|
||||
raise GNS3VMError("Error while executing VMware command: {}".format(e))
|
||||
|
||||
@asyncio.coroutine
|
||||
def _set_vcpus_ram(self, vcpus, ram):
|
||||
"""
|
||||
Set the number of vCPU cores and amount of RAM for the GNS3 VM.
|
||||
|
||||
:param vcpus: number of vCPU cores
|
||||
:param ram: amount of RAM
|
||||
"""
|
||||
|
||||
try:
|
||||
pairs = VMware.parse_vmware_file(self._vmx_path)
|
||||
pairs["numvcpus"] = str(vcpus)
|
||||
pairs["memsize"] = str(ram)
|
||||
VMware.write_vmx_file(self._vmx_path, pairs)
|
||||
log.info("GNS3 VM vCPU count set to {} and RAM amount set to {}".format(vcpus, ram))
|
||||
except OSError as e:
|
||||
raise GNS3VMError('Could not read/write VMware VMX file "{}": {}'.format(self._vmx_path, e))
|
||||
|
||||
@asyncio.coroutine
|
||||
def list(self):
|
||||
"""
|
||||
List all VMware VMs
|
||||
"""
|
||||
|
||||
return (yield from self._vmware_manager.list_vms())
|
||||
|
||||
@asyncio.coroutine
|
||||
def start(self):
|
||||
"""
|
||||
Starts the GNS3 VM.
|
||||
"""
|
||||
|
||||
vms = yield from self.list()
|
||||
for vm in vms:
|
||||
if vm["vmname"] == self.vmname:
|
||||
self._vmx_path = vm["vmx_path"]
|
||||
break
|
||||
|
||||
# check we have a valid VMX file path
|
||||
if not self._vmx_path:
|
||||
raise GNS3VMError("GNS3 VM is not configured", True)
|
||||
raise GNS3VMError("GNS3 VM is not configured")
|
||||
if not os.path.exists(self._vmx_path):
|
||||
raise GNS3VMError("VMware VMX file {} doesn't exist".format(self._vmx_path), True)
|
||||
raise GNS3VMError("VMware VMX file {} doesn't exist".format(self._vmx_path))
|
||||
|
||||
# set the number of vCPUs and amount of RAM # FIXME
|
||||
# yield from self._set_vcpus_ram(self.vcpus, self.ram)
|
||||
|
||||
# start the VM
|
||||
args = [self._vmx_path]
|
||||
@@ -67,6 +104,7 @@ class VMwareGNS3VM(BaseGNS3VM):
|
||||
|
||||
# check if the VMware guest tools are installed
|
||||
vmware_tools_state = yield from self._execute("checkToolsState", [self._vmx_path])
|
||||
print(vmware_tools_state)
|
||||
if vmware_tools_state not in ("installed", "running"):
|
||||
raise GNS3VMError("VMware tools are not installed in {}".format(self.vmname))
|
||||
|
||||
@@ -76,45 +114,13 @@ class VMwareGNS3VM(BaseGNS3VM):
|
||||
log.info("GNS3 VM IP address set to {}".format(guest_ip_address))
|
||||
|
||||
@asyncio.coroutine
|
||||
def stop(self, force=False):
|
||||
def stop(self):
|
||||
"""
|
||||
Stops the GNS3 VM.
|
||||
"""
|
||||
|
||||
if self._vmx_path is None:
|
||||
raise GNS3VMError("No vm path configured, can't stop the VM")
|
||||
raise GNS3VMError("No VMX path configured, can't stop the VM")
|
||||
yield from self._execute("stop", [self._vmx_path, "soft"])
|
||||
log.info("GNS3 VM has been stopped")
|
||||
self.running = False
|
||||
|
||||
@asyncio.coroutine
|
||||
def set_vcpus(self, vcpus):
|
||||
"""
|
||||
Set the number of vCPU cores for the GNS3 VM.
|
||||
|
||||
:param vcpus: number of vCPU cores
|
||||
"""
|
||||
|
||||
try:
|
||||
pairs = VMware.parse_vmware_file(self._vmx_path)
|
||||
pairs["numvcpus"] = str(vcpus)
|
||||
VMware.write_vmx_file(self._vmx_path, pairs)
|
||||
log.info("GNS3 VM vCPU count set to {}".format(vcpus))
|
||||
except OSError as e:
|
||||
raise GNS3VMError('Could not read/write VMware VMX file "{}": {}'.format(self._vmx_path, e))
|
||||
|
||||
@asyncio.coroutine
|
||||
def set_ram(self, ram):
|
||||
"""
|
||||
Set the RAM amount for the GNS3 VM.
|
||||
|
||||
:param ram: amount of memory
|
||||
"""
|
||||
|
||||
try:
|
||||
pairs = VMware.parse_vmware_file(self._vmx_path)
|
||||
pairs["memsize"] = str(ram)
|
||||
VMware.write_vmx_file(self._vmx_path, pairs)
|
||||
log.info("GNS3 VM RAM amount set to {}".format(ram))
|
||||
except OSError as e:
|
||||
raise GNS3VMError('Could not read/write VMware VMX file "{}": {}'.format(self._vmx_path, e))
|
||||
Reference in new issue
Block a user