diff --git a/docs/api/examples/post_vpcs.txt b/docs/api/examples/post_vpcs.txt index d5007ba4..1e529a62 100644 --- a/docs/api/examples/post_vpcs.txt +++ b/docs/api/examples/post_vpcs.txt @@ -20,5 +20,5 @@ X-ROUTE: /vpcs "name": "PC TEST 1", "project_uuid": "a1e920ca-338a-4e9f-b363-aa607b09dd80", "script_file": null, - "uuid": "934f0745-0824-451e-8ad6-db0877dbf387" + "uuid": "21e9a130-450e-41e4-8864-e5c83ba7aa80" } diff --git a/gns3server/modules/attic.py b/gns3server/modules/attic.py index 6331317b..98e2413b 100644 --- a/gns3server/modules/attic.py +++ b/gns3server/modules/attic.py @@ -25,6 +25,7 @@ import struct import socket import stat import time +import aiohttp import logging log = logging.getLogger(__name__) diff --git a/gns3server/modules/base_vm.py b/gns3server/modules/base_vm.py index 001d5b1e..e1e19d1d 100644 --- a/gns3server/modules/base_vm.py +++ b/gns3server/modules/base_vm.py @@ -87,7 +87,7 @@ class BaseVM: Return VM working directory """ - return self._project.vm_working_directory(self._uuid) + return self._project.vm_working_directory(self.module_name, self._uuid) def create(self): """ diff --git a/gns3server/modules/port_manager.py b/gns3server/modules/port_manager.py index 31a38a4e..48d7e8af 100644 --- a/gns3server/modules/port_manager.py +++ b/gns3server/modules/port_manager.py @@ -148,9 +148,9 @@ class PortManager: continue raise HTTPConflict(text="Could not find a free port between {} and {} on host {}, last exception: {}".format(start_port, - end_port, - host, - last_exception)) + end_port, + host, + last_exception)) def get_free_console_port(self): """ diff --git a/gns3server/modules/project.py b/gns3server/modules/project.py index 9d98519c..c924a9d5 100644 --- a/gns3server/modules/project.py +++ b/gns3server/modules/project.py @@ -45,11 +45,9 @@ class Project: if location is None: self._location = tempfile.mkdtemp() - self._path = os.path.join(self._location, self._uuid, "vms") + self._path = os.path.join(self._location, self._uuid) try: - os.makedirs(self._path) - except FileExistsError: - pass + os.makedirs(os.path.join(self._path, 'vms'), exist_ok=True) except OSError as e: raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e)) @@ -68,22 +66,23 @@ class Project: return self._path - def vm_working_directory(self, vm_uuid): + def vm_working_directory(self, module, vm_uuid): """ Return a working directory for a specific VM. If the directory doesn't exist, the directory is created. + :param module: The module name (vpcs, dynamips...) :param vm_uuid: VM UUID """ - path = os.path.join(self._path, "vms", vm_uuid) + p = os.path.join(self._path, "vms", module, vm_uuid) try: - os.makedirs(self._path) + os.makedirs(p, exist_ok=True) except FileExistsError: pass except OSError as e: raise aiohttp.web.HTTPInternalServerError(text="Could not create VM working directory: {}".format(e)) - return path + return p def __json__(self): diff --git a/gns3server/modules/virtualbox/virtualbox_vm.py b/gns3server/modules/virtualbox/virtualbox_vm.py index 9a61f76b..772beedc 100644 --- a/gns3server/modules/virtualbox/virtualbox_vm.py +++ b/gns3server/modules/virtualbox/virtualbox_vm.py @@ -33,7 +33,6 @@ import shutil from .virtualbox_error import VirtualBoxError from ..adapters.ethernet_adapter import EthernetAdapter -from ..attic import find_unused_port from .telnet_server import TelnetServer from ..base_vm import BaseVM @@ -105,7 +104,6 @@ class VirtualBoxVM(BaseVM): # create the device own working directory self.working_dir = working_dir_path - if linked_clone: if vbox_id and os.path.isdir(os.path.join(self.working_dir, self._vmname)): vbox_file = os.path.join(self.working_dir, self._vmname, self._vmname + ".vbox") diff --git a/gns3server/modules/vpcs/vpcs_vm.py b/gns3server/modules/vpcs/vpcs_vm.py index 00ad5a73..c92f703c 100644 --- a/gns3server/modules/vpcs/vpcs_vm.py +++ b/gns3server/modules/vpcs/vpcs_vm.py @@ -43,6 +43,7 @@ log = logging.getLogger(__name__) class VPCSVM(BaseVM): + module_name = 'vpcs' """ VPCS vm implementation. diff --git a/tests/api/base.py b/tests/api/base.py index 63607498..e8fac3f8 100644 --- a/tests/api/base.py +++ b/tests/api/base.py @@ -151,11 +151,12 @@ def server(request, loop): port = _get_unused_port() host = "localhost" app = web.Application() + port_manager = PortManager("127.0.0.1", False) for method, route, handler in Route.get_routes(): app.router.add_route(method, route, handler) for module in MODULES: instance = module.instance() - instance.port_manager = PortManager("127.0.0.1", False) + instance.port_manager = port_manager srv = loop.create_server(app.make_handler(), host, port) srv = loop.run_until_complete(srv) diff --git a/tests/modules/test_project.py b/tests/modules/test_project.py index 4cfdc764..cb980b76 100644 --- a/tests/modules/test_project.py +++ b/tests/modules/test_project.py @@ -47,5 +47,5 @@ def test_json(tmpdir): def test_vm_working_directory(tmpdir): p = Project(location=str(tmpdir)) - assert os.path.exists(p.vm_working_directory('00010203-0405-0607-0809-0a0b0c0d0e0f')) - assert os.path.exists(os.path.join(str(tmpdir), p.uuid, 'vms', '00010203-0405-0607-0809-0a0b0c0d0e0f')) + assert os.path.exists(p.vm_working_directory('vpcs', '00010203-0405-0607-0809-0a0b0c0d0e0f')) + assert os.path.exists(os.path.join(str(tmpdir), p.uuid, 'vms', 'vpcs', '00010203-0405-0607-0809-0a0b0c0d0e0f'))