Skip to content

Commit

Permalink
Support for sendings settings for the GNS3VM from the GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-duponchelle committed Aug 25, 2016
1 parent 6cad685 commit 21b99ad
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 173 deletions.
34 changes: 19 additions & 15 deletions gns3server/controller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from .symbols import Symbols
from ..version import __version__
from .topology import load_topology
from .gns3vm import GNS3VM


import logging
log = logging.getLogger(__name__)
Expand All @@ -40,6 +42,7 @@ def __init__(self):
self._computes = {}
self._projects = {}
self._notification = Notification(self)
self.gns3vm = GNS3VM(self)
self.symbols = Symbols()
# Store settings shared by the different GUI will be replace by dedicated API later
self._settings = {}
Expand All @@ -56,22 +59,26 @@ def __init__(self):
self._config_file = os.path.join(config_path, "gns3_controller.conf")
log.info("Load controller configuration file {}".format(self._config_file))

if server_config.getboolean("local", False) is True:
self._computes["local"] = Compute(compute_id="local",
controller=self,
protocol=server_config.get("protocol", "http"),
host=server_config.get("host", "localhost"),
port=server_config.getint("port", 3080),
user=server_config.get("user", ""),
password=server_config.get("password", ""))

@asyncio.coroutine
def start(self):
log.info("Start controller")
server_config = Config.instance().get_section_config("Server")
self._computes["local"] = Compute(compute_id="local",
controller=self,
protocol=server_config.get("protocol", "http"),
host=server_config.get("host", "localhost"),
port=server_config.getint("port", 3080),
user=server_config.get("user", ""),
password=server_config.get("password", ""))
yield from self.load()

@asyncio.coroutine
def stop(self):
log.info("Stop controller")
for compute in self._computes.values():
yield from compute.close()
self._computes = {}
self._projects = {}

def save(self):
"""
Expand All @@ -80,6 +87,7 @@ def save(self):
data = {
"computes": [],
"settings": self._settings,
"gns3vm": self.gns3vm.__json__(),
"version": __version__
}

Expand Down Expand Up @@ -115,6 +123,8 @@ def load(self):
return
if "settings" in data:
self._settings = data["settings"]
if "gns3vm" in data:
self.gns3vm.settings = data["gns3vm"]

for c in data["computes"]:
yield from self.add_compute(**c)
Expand Down Expand Up @@ -234,12 +244,6 @@ def delete_compute(self, compute_id):
self.save()
self.notification.emit("compute.deleted", compute.__json__())

@asyncio.coroutine
def close(self):
log.info("Close controller")
for compute in self._computes.values():
yield from compute.close()

@property
def notification(self):
"""
Expand Down
101 changes: 101 additions & 0 deletions gns3server/controller/gns3vm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/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/>.

import sys
import asyncio

from .vmware_gns3_vm import VMwareGNS3VM
from .virtualbox_gns3_vm import VirtualBoxGNS3VM


class GNS3VM:
"""
Proxy between the controller and the GNS3 VM engine
"""

def __init__(self, controller, settings={}):
self._controller = controller
# Keep instance of the loaded engines
self._engines = {}
self._settings = {
"vmname": None,
"auto_stop": False,
"headless": False,
"enable": False,
"engine": "vmware"
}
self._settings.update(settings)

def engine_list(self):
"""
:returns: Return list of engines supported by GNS3 for the GNS3VM
"""
virtualbox_informations = {
"engine_id": "virtualbox",
"name": "VirtualBox",
"description": "VirtualBox doesn't support nested virtualization, this means running Qemu based VM could be very slow."
}
vmware_informations = {
"engine_id": "vmware",
"description": "VMware is the recommended choice for best performances."
}
if sys.platform.startswith("darwin"):
vmware_informations["name"] = "VMware Fusion"
else:
vmware_informations["name"] = "VMware Workstation / Player"
return [
vmware_informations,
virtualbox_informations
]

@property
def settings(self):
return self._settings

@settings.setter
def settings(self, val):
self._settings.update(val)
self._controller.save()

def _get_engine(self, engine):
"""
Load an engine
"""
if engine in self._engines:
return self._engines[engine]

if engine == "vmware":
self._engines["vmware"] = VMwareGNS3VM()
return self._engines["vmware"]
elif engine == "virtualbox":
self._engines["virtualbox"] = VirtualBoxGNS3VM()
return self._engines["virtualbox"]
raise NotImplementedError("The engine {} for the GNS3 VM is not supported".format(engine))

def __json__(self):
return self._settings

@asyncio.coroutine
def list(self, engine):
"""
List VMS for an engine
"""
engine = self._get_engine(engine)
vms = []
for vm in (yield from engine.list()):
vms.append({"vmname": vm["vmname"]})
return vms
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
# 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 .gns3_vm_error import GNS3VMError

import os
import sys
import json
import asyncio
import psutil

Expand All @@ -32,7 +27,6 @@ class BaseGNS3VM:
def __init__(self):

self._vmname = None
self._auto_start = False
self._auto_stop = False
self._ip_address = None
self._port = 3080
Expand All @@ -41,12 +35,6 @@ def __init__(self):
self._ram = 1024
self._running = False

if sys.platform.startswith("win"):
config_path = os.path.join(os.path.expandvars("%APPDATA%"), "GNS3")
else:
config_path = os.path.join(os.path.expanduser("~"), ".config", "GNS3")
self._config_file = os.path.join(config_path, "gns3_vm.conf")

# limit the number of vCPUs to the number of physical cores (hyper thread CPUs are excluded)
# because this is likely to degrade performances.
self._vcpus = psutil.cpu_count(logical=False)
Expand All @@ -56,51 +44,6 @@ def __init__(self):
ram -= ram % 4
self._ram = ram

self.load()

def __json__(self):

settings = {"vmname": self._vmname,
"ip_address": self._ip_address,
"port": self._port,
"headless": self._headless,
"vcpus": self._vcpus,
"ram": self._ram,
"auto_start": self._auto_start,
"auto_stop": self._auto_stop,
"engine": self._engine}

return settings

def load(self):
"""
Reload the GNS3 VM configuration from disk
"""

if not os.path.exists(self._config_file):
self.save()
try:
with open(self._config_file) as f:
data = json.load(f)
except OSError as e:
log.critical("Cannot load %s: %s", self._config_file, str(e))
return
if "gns3vm" in data:
for name, value in data["gns3vm"].items():
if hasattr(self, name) and getattr(self, name) != value:
log.debug("GNS3 VM: set {} to {}".format(name, value))
setattr(self, name, value)

def save(self):
"""
Save the GNS3 VM configuration on disk
"""

data = {"gns3vm": self.__json__()}
os.makedirs(os.path.dirname(self._config_file), exist_ok=True)
with open(self._config_file, 'w+') as f:
json.dump(data, f, indent=4)

@property
def vmname(self):
"""
Expand Down Expand Up @@ -241,30 +184,10 @@ def ram(self, new_ram):

self._ram = new_ram

@property
def auto_start(self):
"""
Returns whether the VM should automatically be started when GNS3 is launched
:returns: boolean
"""

return self._auto_start

@auto_start.setter
def auto_start(self, new_auto_start):
"""
Set whether the VM should automatically be started when GNS3 is launched
:param new_auto_start: boolean
"""

self._auto_start = new_auto_start

@property
def auto_stop(self):
"""
Returns whether the VM should automatically be started when GNS3 is launched
Returns whether the VM should automatically be stopped when GNS3 quit
:returns: boolean
"""
Expand All @@ -274,7 +197,7 @@ def auto_stop(self):
@auto_stop.setter
def auto_stop(self, new_auto_stop):
"""
Set whether the VM should automatically be stopped when GNS3 is launched
Set whether the VM should automatically be stopped when GNS3 quit
:param new_auto_stop: boolean
"""
Expand Down Expand Up @@ -314,15 +237,3 @@ def stop(self, force=False):
"""

raise NotImplementedError

@classmethod
def instance(cls):
"""
Singleton to return only one instance of BaseGNS3VM.
:returns: instance of BaseGNS3VM
"""

if not hasattr(cls, "_instance") or cls._instance is None:
cls._instance = cls()
return cls._instance
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .base_gns3_vm import BaseGNS3VM
from .gns3_vm_error import GNS3VMError

from ..compute.virtualbox import (
from ...compute.virtualbox import (
VirtualBox,
VirtualBoxError
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def start(self):

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

Expand Down
Loading

0 comments on commit 21b99ad

Please sign in to comment.