1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 03:08:14 +00:00

Handle old projects.

This commit is contained in:
grossmj 2015-02-08 18:10:04 -07:00
parent 2a3b37a3bd
commit 0d7d0a05c3
6 changed files with 52 additions and 11 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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