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

When we remove a VM, the VM is removed from the project.

This commit is contained in:
Julien Duponchelle 2015-01-23 14:34:50 +01:00
parent 3f5c2390cd
commit 7bf121c6da
4 changed files with 24 additions and 7 deletions

View File

@ -142,6 +142,7 @@ class BaseManager:
uuid = str(uuid4())
vm = self._VM_CLASS(name, uuid, project, self, *args, **kwargs)
project.add_vm(vm)
if asyncio.iscoroutinefunction(vm.create):
yield from vm.create()
else:

View File

@ -27,7 +27,6 @@ class BaseVM:
self._uuid = uuid
self._project = project
self._manager = manager
project.add_vm(self)
log.debug("{module}: {name} [{uuid}] initialized".format(module=self.manager.module_name,
name=self.name,

View File

@ -95,6 +95,7 @@ class Project:
:param vm: An instance of VM
"""
self.remove_vm(vm)
self._vms_to_destroy.add(vm)
def __json__(self):
@ -106,14 +107,25 @@ class Project:
def add_vm(self, vm):
"""
Add a VM to the project. In theory this should be called by
the VM initializer.
Add a VM to the project.
In theory this should be called by the VM manager.
:params vm: A VM instance
"""
self._vms.add(vm)
def remove_vm(self, vm):
"""
Remove a VM from the project.
In theory this should be called by the VM manager.
:params vm: A VM instance
"""
if vm in self._vms:
self._vms.remove(vm)
def close(self):
"""Close the project, but keep informations on disk"""
@ -128,6 +140,7 @@ class Project:
directory = self.vm_working_directory(vm)
if os.path.exists(directory):
shutil.rmtree(directory)
self.remove_vm(vm)
def delete(self):
"""Remove project from disk"""

View File

@ -68,14 +68,17 @@ def test_vm_working_directory(tmpdir, vm):
def test_mark_vm_for_destruction(tmpdir, vm):
p = Project(location=str(tmpdir))
p.mark_vm_for_destruction(vm)
assert len(p._vms_to_destroy) == 1
project = Project(location=str(tmpdir))
project.add_vm(vm)
project.mark_vm_for_destruction(vm)
assert len(project._vms_to_destroy) == 1
assert len(project.vms) == 0
def test_commit(tmpdir, manager):
project = Project(location=str(tmpdir))
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
project.add_vm(vm)
directory = project.vm_working_directory(vm)
project.mark_vm_for_destruction(vm)
assert len(project._vms_to_destroy) == 1
@ -83,6 +86,7 @@ def test_commit(tmpdir, manager):
project.commit()
assert len(project._vms_to_destroy) == 0
assert os.path.exists(directory) is False
assert len(project.vms) == 0
def test_project_delete(tmpdir):
@ -95,8 +99,8 @@ def test_project_delete(tmpdir):
def test_project_add_vm(tmpdir, manager):
project = Project(location=str(tmpdir))
# The VM initalizer call the add_vm method
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
project.add_vm(vm)
assert len(project.vms) == 1