1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-24 15:58:08 +00:00

Basic API for GNS3 VM.

This commit is contained in:
grossmj 2016-07-11 17:01:18 -06:00
parent 27269fb13a
commit 13deecea4e
5 changed files with 246 additions and 67 deletions

View File

@ -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

View File

@ -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.
"""

View File

@ -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))

View File

@ -22,3 +22,4 @@ from .link_handler import LinkHandler
from .server_handler import ServerHandler
from .drawing_handler import DrawingHandler
from .symbol_handler import SymbolHandler
from .gns3_vm_handler import GNS3VMHandler

View File

@ -0,0 +1,103 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 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/>.
from aiohttp.web import HTTPConflict
from gns3server.web.route import Route
from gns3server.controller.vmware_gns3_vm import VMwareGNS3VM
from gns3server.controller.virtualbox_gns3_vm import VirtualBoxGNS3VM
import logging
log = logging.getLogger(__name__)
class GNS3VMHandler:
"""API entry points for GNS3 VM management."""
@Route.get(
r"/gns3vm/{engine}/vms",
parameters={
"engine": "Virtualization engine name"
},
status_codes={
200: "Success",
400: "Invalid request",
},
description="Get all the available VMs for a specific virtualization engine")
def get_vms(request, response):
engine = request.match_info["engine"]
if engine == "vmware":
engine_instance = VMwareGNS3VM.instance()
elif engine == "virtualbox":
engine_instance = VirtualBoxGNS3VM.instance()
else:
raise HTTPConflict(text="Unknown engine: '{}'".format(engine))
vms = yield from engine_instance.list()
response.json(vms)
@Route.get(
r"/gns3vm",
description="Get GNS3 VM settings",
status_codes={
200: "GNS3 VM settings returned"
})
def show(request, response):
gns3_vm = VMwareGNS3VM.instance()
response.json(gns3_vm)
@Route.put(
r"/gns3vm",
description="Update GNS3 VM settings",
#input=GNS3VM_UPDATE_SCHEMA, # TODO: validate settings
status_codes={
200: "GNS3 VM updated"
})
def update(request, response):
gns3_vm = VMwareGNS3VM.instance()
for name, value in request.json.items():
if hasattr(gns3_vm, name) and getattr(gns3_vm, name) != value:
setattr(gns3_vm, name, value)
response.json(gns3_vm)
@Route.post(
r"/gns3vm/start",
status_codes={
200: "Instance started",
400: "Invalid request",
},
description="Start the GNS3 VM"
)
def start(request, response):
gns3_vm = VMwareGNS3VM.instance()
yield from gns3_vm.start()
response.json(gns3_vm)
@Route.post(
r"/gns3vm/stop",
status_codes={
204: "Instance stopped",
400: "Invalid request",
},
description="Stop the GNS3 VM")
def stop(request, response):
gns3_vm = VMwareGNS3VM.instance()
yield from gns3_vm.stop()
response.set_status(204)