Merge remote-tracking branch 'origin/2.0' into 2.0

Conflicts:
	gns3server/controller/project.py
	tests/compute/test_project.py
	tests/controller/test_project.py
This commit is contained in:
grossmj 2016-05-11 11:41:45 -06:00
commit 483431438a
17 changed files with 208 additions and 53 deletions

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
gns3server/version.py merge=ours

View File

@ -1,5 +1,15 @@
# Change Log
## 1.5.0a2 10/05/2016
* Fix distribution on PyPi
## 1.5.0a1 10/05/2016
* Rebase Qcow2 disks when starting a VM if needed
* Docker support
* import / export portable projects (.gns3project)
## 1.4.6 28/04/2016
* More robust save/restore for VirtualBox linked clone VM hard disks.
@ -57,7 +67,7 @@
* Fix error when setting Qemu VM boot to 'cd' (HDD or CD/DVD-ROM)
* Fixed the VMware default VM location on Windows, so that it doesn't assume the "Documents" folder is within the %USERPROFILE% folder, and also support Windows Server's folder (which is "My Virtual Machines" instead of "Virtual Machines").
* Improve dynamips startup_config dump
* Dump environnement to server debug log
* Dump environment to server debug log
* Fix usage of qemu 0.10 on Windows
* Show hostname when the hostname is missing in the iourc.txt

View File

@ -4,6 +4,7 @@ include INSTALL
include LICENSE
include MANIFEST.in
include tox.ini
include requirements.txt
recursive-include tests *
recursive-exclude docs *
recursive-include gns3server *

View File

@ -29,6 +29,7 @@ from .port_manager import PortManager
from .notification_manager import NotificationManager
from ..config import Config
from ..utils.asyncio import wait_run_in_executor
from ..utils.path import check_path_allowed, get_default_project_directory
import logging
@ -62,7 +63,7 @@ class Project:
self._used_udp_ports = set()
if path is None:
location = self._config().get("project_directory", self._get_default_project_directory())
location = get_default_project_directory()
path = os.path.join(location, self._id)
try:
os.makedirs(path, exist_ok=True)
@ -94,22 +95,6 @@ class Project:
return self._config().getboolean("local", False)
@classmethod
def _get_default_project_directory(cls):
"""
Return the default location for the project directory
depending of the operating system
"""
server_config = Config.instance().get_section_config("Server")
path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects"))
path = os.path.normpath(path)
try:
os.makedirs(path, exist_ok=True)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
return path
@property
def id(self):
@ -122,6 +107,7 @@ class Project:
@path.setter
def path(self, path):
check_path_allowed(path)
if hasattr(self, "_path"):
if path != self._path and self.is_local() is False:
@ -416,8 +402,7 @@ class Project:
At startup drop old temporary project. After a crash for example
"""
config = Config.instance().get_section_config("Server")
directory = config.get("project_directory", cls._get_default_project_directory())
directory = get_default_project_directory()
if os.path.exists(directory):
for project in os.listdir(directory):
path = os.path.join(directory, project)

View File

@ -91,14 +91,35 @@ class Controller:
"""
Add a server to the dictionnary of computes controlled by GNS3
:param compute_id: Id of the compute node
:param kwargs: See the documentation of Compute
"""
# We dissallow to create from the outside the
if compute_id == 'local':
return self._createLocalCompute()
if compute_id not in self._computes:
compute = Compute(compute_id=compute_id, controller=self, **kwargs)
self._computes[compute_id] = compute
self.save()
return self._computes[compute_id]
def _createLocalCompute(self):
"""
Create the local compute node. It's the controller itself
"""
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", ""))
return self._computes["local"]
@property
def computes(self):
"""
@ -113,6 +134,8 @@ class Controller:
try:
return self._computes[compute_id]
except KeyError:
if compute_id == "local":
return self._createLocalCompute()
raise aiohttp.web.HTTPNotFound(text="Compute ID {} doesn't exist".format(compute_id))
@asyncio.coroutine

View File

@ -51,6 +51,7 @@ class Compute:
self._controller = controller
self._setAuth(user, password)
self._session = aiohttp.ClientSession()
self._version = None
# If the compute is local but the compute id is local
# it's a configuration issue
@ -71,6 +72,20 @@ class Compute:
else:
self._auth = None
@property
def version(self):
"""
:returns: Version of compute node (string or None if not connected)
"""
return self._version
@property
def connected(self):
"""
:returns: True if compute node is connected
"""
return self._connected
@property
def id(self):
"""
@ -160,6 +175,7 @@ class Compute:
if "version" not in response.json:
raise aiohttp.web.HTTPConflict(text="The server {} is not a GNS3 server".format(self._id))
self._version = response.json["version"]
if parse_version(__version__)[:2] != parse_version(response.json["version"])[:2]:
raise aiohttp.web.HTTPConflict(text="The server {} versions are not compatible {} != {}".format(self._id, __version__, response.json["version"]))

View File

@ -27,6 +27,7 @@ from .node import Node
from .udp_link import UDPLink
from ..notification_queue import NotificationQueue
from ..config import Config
from ..utils.path import check_path_allowed, get_default_project_directory
class Project:
@ -50,10 +51,8 @@ class Project:
raise aiohttp.web.HTTPBadRequest(text="{} is not a valid UUID".format(project_id))
self._id = project_id
#TODO: Security check if not locale
if path is None:
location = self._config().get("project_directory", self._get_default_project_directory())
path = os.path.join(location, self._id)
path = os.path.join(get_default_project_directory(), self._id)
self.path = path
self._temporary = temporary
@ -62,6 +61,9 @@ class Project:
self._links = {}
self._listeners = set()
# Create the project on demand on the compute node
self._project_created_on_compute = set()
@property
def name(self):
return self._name
@ -80,6 +82,7 @@ class Project:
@path.setter
def path(self, path):
check_path_allowed(path)
try:
os.makedirs(path, exist_ok=True)
except OSError as e:
@ -105,7 +108,6 @@ class Project:
@asyncio.coroutine
def add_compute(self, compute):
self._computes.add(compute)
yield from compute.post("/projects", self)
@asyncio.coroutine
def add_node(self, compute, node_id, **kwargs):
@ -116,6 +118,9 @@ class Project:
"""
if node_id not in self._nodes:
node = Node(self, compute, node_id=node_id, **kwargs)
if compute not in self._project_created_on_compute:
yield from compute.post("/projects", self)
self._project_created_on_compute.add(compute)
yield from node.create()
self._nodes[node.id] = node
return node

View File

@ -28,11 +28,21 @@ in futur GNS3 versions.
<h2>Computes</h2>
<table border="1">
<tr>
<th>ID</td>
<th>ID</th>
<th>Version</th>
<th>Connected</th>
<th>Protocol</th>
<th>Host</th>
<th>Port</th>
</tr>
{% for compute in controller.computes.values() %}
<tr>
<td>{{compute.id}}</td>
<td>{{compute.version}}</td>
<td>{{compute.connected}}</td>
<td>{{compute.protocol}}</td>
<td>{{compute.host}}</td>
<td>{{compute.port}}</td>
</tr>
{% endfor %}
</table>

55
gns3server/utils/path.py Normal file
View File

@ -0,0 +1,55 @@
#!/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 os
import aiohttp
from ..config import Config
def get_default_project_directory():
"""
Return the default location for the project directory
depending of the operating system
"""
server_config = Config.instance().get_section_config("Server")
path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects"))
path = os.path.normpath(path)
try:
os.makedirs(path, exist_ok=True)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
return path
def check_path_allowed(path):
"""
If the server is non local raise an error if
the path is outside project directories
Raise a 403 in case of error
"""
config = Config.instance().get_section_config("Server")
project_directory = get_default_project_directory()
if len(os.path.commonprefix([project_directory, path])) == len(project_directory):
return
if "local" in config and config.getboolean("local") is False:
raise aiohttp.web.HTTPForbidden(text="The path is not allowed")

View File

@ -69,10 +69,10 @@ def test_clean_tmp_directory(async_run):
def test_path(tmpdir):
directory = Config.instance().get_section_config("Server").get("project_directory")
directory = Config.instance().get_section_config("Server").get("projects_path")
with patch("gns3server.compute.project.Project.is_local", return_value=True):
with patch("gns3server.compute.project.Project._get_default_project_directory", return_value=directory):
with patch("gns3server.utils.path.get_default_project_directory", return_value=directory):
p = Project(project_id=str(uuid4()))
assert p.path == os.path.join(directory, p.id)
assert os.path.exists(os.path.join(directory, p.id))
@ -123,8 +123,8 @@ def test_json(tmpdir):
assert p.__json__() == {"name": p.name, "project_id": p.id, "temporary": False}
def test_vm_working_directory(tmpdir, node):
directory = Config.instance().get_section_config("Server").get("project_directory")
def test_node_working_directory(tmpdir, vm):
directory = Config.instance().get_section_config("Server").get("projects_path")
with patch("gns3server.compute.project.Project.is_local", return_value=True):
p = Project(project_id=str(uuid4()))
@ -210,16 +210,6 @@ def test_project_close_temporary_project(loop, manager):
loop.run_until_complete(asyncio.async(project.close()))
assert os.path.exists(directory) is False
def test_get_default_project_directory(monkeypatch):
monkeypatch.undo()
project = Project(project_id=str(uuid4()))
path = os.path.normpath(os.path.expanduser("~/GNS3/projects"))
assert project._get_default_project_directory() == path
assert os.path.exists(path)
def test_clean_project_directory(tmpdir):
# A non anonymous project with uuid.
@ -237,7 +227,7 @@ def test_clean_project_directory(tmpdir):
with open(str(tmp), 'w+') as f:
f.write("1")
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
Project.clean_project_directory()
assert os.path.exists(str(project1))
@ -247,7 +237,7 @@ def test_clean_project_directory(tmpdir):
def test_list_files(tmpdir, loop):
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
project = Project(project_id=str(uuid4()))
path = project.path
os.makedirs(os.path.join(path, "vm-1", "dynamips"))

View File

@ -203,7 +203,7 @@ def run_around_tests(monkeypatch, port_manager, controller, config):
port_manager._instance = port_manager
os.makedirs(os.path.join(tmppath, 'projects'))
config.set("Server", "project_directory", os.path.join(tmppath, 'projects'))
config.set("Server", "projects_path", os.path.join(tmppath, 'projects'))
config.set("Server", "images_path", os.path.join(tmppath, 'images'))
config.set("Server", "auth", False)
config.set("Server", "controller", True)
@ -216,7 +216,7 @@ def run_around_tests(monkeypatch, port_manager, controller, config):
# Force turn off KVM because it's not available on CI
config.set("Qemu", "enable_kvm", False)
monkeypatch.setattr("gns3server.compute.project.Project._get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects'))
monkeypatch.setattr("gns3server.utils.path.get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects'))
# Force sys.platform to the original value. Because it seem not be restore correctly at each tests
sys.platform = sys.original_platform

View File

@ -86,6 +86,7 @@ def test_compute_httpQueryNotConnected(compute, async_run):
mock.assert_any_call("GET", "https://example.com:84/v2/compute/version", headers={'content-type': 'application/json'}, data=None, auth=None)
mock.assert_any_call("POST", "https://example.com:84/v2/compute/projects", data='{"a": "b"}', headers={'content-type': 'application/json'}, auth=None)
assert compute._connected
assert compute.version == __version__
def test_compute_httpQueryNotConnectedInvalidVersion(compute, async_run):

View File

@ -108,6 +108,15 @@ def test_getCompute(controller, async_run):
assert controller.getCompute("dsdssd")
def test_addComputeLocal(controller, controller_config_path, async_run):
"""
The local node is the controller itself you can not change the informations
"""
Config.instance().set("Server", "local", True)
async_run(controller.addCompute("local", host="example.org"))
assert controller.getCompute("local").host == "localhost"
def test_addProject(controller, async_run):
uuid1 = str(uuid.uuid4())
uuid2 = str(uuid.uuid4())
@ -138,7 +147,6 @@ def test_addProject_with_compute(controller, async_run):
controller._computes = {"test1": compute}
project1 = async_run(controller.addProject(project_id=uuid1))
compute.post.assert_called_with("/projects", project1)
def test_getProject(controller, async_run):

View File

@ -43,9 +43,9 @@ def test_json(tmpdir):
def test_path(tmpdir):
directory = Config.instance().get_section_config("Server").get("project_directory")
directory = Config.instance().get_section_config("Server").get("projects_path")
with patch("gns3server.compute.project.Project._get_default_project_directory", return_value=directory):
with patch("gns3server.utils.path.get_default_project_directory", return_value=directory):
p = Project(project_id=str(uuid4()))
assert p.path == os.path.join(directory, p.id)
assert os.path.exists(os.path.join(directory, p.id))
@ -68,6 +68,12 @@ def test_captures_directory(tmpdir):
assert p.captures_directory == str(tmpdir / "project-files" / "captures")
assert os.path.exists(p.captures_directory)
def test_add_compute(async_run):
compute = MagicMock()
project = Project()
async_run(project.addCompute(compute))
assert compute in project._computes
def test_addVM(async_run):
compute = MagicMock()
@ -79,11 +85,12 @@ def test_addVM(async_run):
vm = async_run(project.add_node(compute, None, name="test", node_type="vpcs", properties={"startup_config": "test.cfg"}))
compute.post.assert_called_with('/projects/{}/vpcs/nodes'.format(project.id),
data={'node_id': vm.id,
'console_type': 'telnet',
'startup_config': 'test.cfg',
'name': 'test'})
compute.post.assert_any_call('/projects/{}/vpcs/nodes'.format(project.id),
data={'node_id': node.id,
'console_type': 'telnet',
'startup_config': 'test.cfg',
'name': 'test'})
assert compute in project._project_created_on_compute
def test_getVM(async_run):

View File

@ -201,7 +201,7 @@ def test_close_project_invalid_uuid(http_compute):
def test_get_file(http_compute, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
with open(os.path.join(project.path, "hello"), "w+") as f:
@ -220,7 +220,7 @@ def test_get_file(http_compute, tmpdir):
def test_stream_file(http_compute, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
with open(os.path.join(project.path, "hello"), "w+") as f:

42
tests/utils/test_path.py Normal file
View File

@ -0,0 +1,42 @@
#!/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 os
import pytest
import aiohttp
from gns3server.utils.path import check_path_allowed, get_default_project_directory
def test_check_path_allowed(config, tmpdir):
config.set("Server", "local", False)
config.set("Server", "projects_path", str(tmpdir))
with pytest.raises(aiohttp.web.HTTPForbidden):
check_path_allowed("/private")
config.set("Server", "local", True)
check_path_allowed(str(tmpdir / "hello" / "world"))
check_path_allowed("/private")
def test_get_default_project_directory(config):
config.clear()
path = os.path.normpath(os.path.expanduser("~/GNS3/projects"))
assert get_default_project_directory() == path
assert os.path.exists(path)