diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index 641ed790..aef1579a 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -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: diff --git a/gns3server/modules/base_vm.py b/gns3server/modules/base_vm.py index 42deb98f..52c003b2 100644 --- a/gns3server/modules/base_vm.py +++ b/gns3server/modules/base_vm.py @@ -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, diff --git a/gns3server/modules/project.py b/gns3server/modules/project.py index a3e0319f..4e8c61df 100644 --- a/gns3server/modules/project.py +++ b/gns3server/modules/project.py @@ -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""" diff --git a/tests/modules/test_project.py b/tests/modules/test_project.py index 2956adaf..d26488ac 100644 --- a/tests/modules/test_project.py +++ b/tests/modules/test_project.py @@ -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