diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index 1a5836bc..ec51f658 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -22,12 +22,14 @@ import stat import asyncio import aiohttp import socket +import shutil import logging log = logging.getLogger(__name__) from uuid import UUID, uuid4 from ..config import Config +from ..utils.asyncio import wait_run_in_executor from .project_manager import ProjectManager from .nios.nio_udp import NIOUDP @@ -157,9 +159,20 @@ class BaseManager: project = ProjectManager.instance().get_project(project_id) try: - if vm_id: + if vm_id and hasattr(self, "get_legacy_vm_workdir_name"): + # move old project VM files to a new location legacy_id = int(vm_id) - # TODO: support for old projects VM with normal IDs. + project_dir = os.path.dirname(project.path) + project_name = os.path.basename(project_dir) + project_files_dir = os.path.join(project_dir, "{}-files".format(project_name)) + module_path = os.path.join(project_files_dir, self.module_name.lower()) + vm_working_dir = os.path.join(module_path, self.get_legacy_vm_workdir_name(legacy_id)) + vm_id = str(uuid4()) + new_vm_working_dir = os.path.join(project.path, self.module_name.lower(), vm_id) + try: + yield from wait_run_in_executor(shutil.move, vm_working_dir, new_vm_working_dir) + except OSError as e: + raise aiohttp.web.HTTPInternalServerError(text="Could not move VM working directory: {}".format(e)) except ValueError: pass diff --git a/gns3server/modules/virtualbox/__init__.py b/gns3server/modules/virtualbox/__init__.py index 245d8976..346b237c 100644 --- a/gns3server/modules/virtualbox/__init__.py +++ b/gns3server/modules/virtualbox/__init__.py @@ -127,3 +127,14 @@ class VirtualBox(BaseManager): if not extra_data[0].strip() == "Value: yes": vms.append(vmname) return vms + + @staticmethod + def get_legacy_vm_workdir_name(legacy_vm_id): + """ + Returns the name of the legacy working directory name for a VM. + + :param legacy_vm_id: legacy VM identifier (integer) + :returns: working directory name + """ + + return "vm-{}".format(legacy_vm_id) diff --git a/gns3server/modules/vpcs/__init__.py b/gns3server/modules/vpcs/__init__.py index 0741c0ab..826e6fd8 100644 --- a/gns3server/modules/vpcs/__init__.py +++ b/gns3server/modules/vpcs/__init__.py @@ -63,3 +63,14 @@ class VPCS(BaseManager): """ return self._used_mac_ids.get(vm_id, 1) + + @staticmethod + def get_legacy_vm_workdir_name(legacy_vm_id): + """ + Returns the name of the legacy working directory name for a VM. + + :param legacy_vm_id: legacy VM identifier (integer) + :returns: working directory name + """ + + return "pc-{}".format(legacy_vm_id) diff --git a/gns3server/schemas/virtualbox.py b/gns3server/schemas/virtualbox.py index adcfd6eb..b527f6f6 100644 --- a/gns3server/schemas/virtualbox.py +++ b/gns3server/schemas/virtualbox.py @@ -23,10 +23,13 @@ VBOX_CREATE_SCHEMA = { "properties": { "vm_id": { "description": "VirtualBox VM instance identifier", - "type": "string", - "minLength": 36, - "maxLength": 36, - "pattern": "(^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}|\d+)$" + "oneOf": [ + {"type": "string", + "minLength": 36, + "maxLength": 36, + "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"}, + {"type": "integer"} # for legacy projects + ] }, "linked_clone": { "description": "either the VM is a linked clone or not", diff --git a/gns3server/schemas/vpcs.py b/gns3server/schemas/vpcs.py index fe9cc8e8..8ba53064 100644 --- a/gns3server/schemas/vpcs.py +++ b/gns3server/schemas/vpcs.py @@ -28,10 +28,13 @@ VPCS_CREATE_SCHEMA = { }, "vm_id": { "description": "VPCS VM identifier", - "type": "string", - "minLength": 36, - "maxLength": 36, - "pattern": "^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}|\d+)$" + "oneOf": [ + {"type": "string", + "minLength": 36, + "maxLength": 36, + "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"}, + {"type": "integer"} # for legacy projects + ] }, "console": { "description": "console TCP port", diff --git a/gns3server/utils/asyncio.py b/gns3server/utils/asyncio.py index 9b94eaf0..0554593a 100644 --- a/gns3server/utils/asyncio.py +++ b/gns3server/utils/asyncio.py @@ -23,7 +23,7 @@ import asyncio def wait_run_in_executor(func, *args): """ Run blocking code in a different thread and wait - the result. + for the result. :param func: Run this function in a different thread :param args: Parameters of the function