From 3d85bba9d48777f35bf8c16868c6b6bf80cdc696 Mon Sep 17 00:00:00 2001 From: ziajka Date: Mon, 2 Oct 2017 10:41:57 +0200 Subject: [PATCH] Don't create directory structure during json dump. Fixes #2270 --- gns3server/compute/base_node.py | 8 ++++++++ gns3server/compute/builtin/nodes/cloud.py | 2 +- gns3server/compute/docker/docker_vm.py | 2 +- gns3server/compute/iou/iou_vm.py | 2 +- gns3server/compute/project.py | 11 ++++++++++- gns3server/compute/qemu/qemu_vm.py | 2 +- gns3server/compute/virtualbox/virtualbox_vm.py | 2 +- gns3server/compute/vmware/vmware_vm.py | 2 +- gns3server/compute/vpcs/vpcs_vm.py | 2 +- tests/compute/test_project.py | 10 ++++++++++ 10 files changed, 35 insertions(+), 8 deletions(-) diff --git a/gns3server/compute/base_node.py b/gns3server/compute/base_node.py index 543224a2..288c8930 100644 --- a/gns3server/compute/base_node.py +++ b/gns3server/compute/base_node.py @@ -233,6 +233,14 @@ class BaseNode: return self._project.node_working_directory(self) + @property + def working_path(self): + """ + Return the node working path. Doesn't create structure of directories when not present. + """ + + return self._project.node_working_path(self) + @property def temporary_directory(self): if self._temporary_directory is None: diff --git a/gns3server/compute/builtin/nodes/cloud.py b/gns3server/compute/builtin/nodes/cloud.py index 27f82d9f..31796964 100644 --- a/gns3server/compute/builtin/nodes/cloud.py +++ b/gns3server/compute/builtin/nodes/cloud.py @@ -86,7 +86,7 @@ class Cloud(BaseNode): "ports_mapping": self._ports_mapping, "interfaces": host_interfaces, "status": self.status, - "node_directory": self.working_dir + "node_directory": self.working_path } @property diff --git a/gns3server/compute/docker/docker_vm.py b/gns3server/compute/docker/docker_vm.py index a2b17b2c..4fcab715 100644 --- a/gns3server/compute/docker/docker_vm.py +++ b/gns3server/compute/docker/docker_vm.py @@ -114,7 +114,7 @@ class DockerVM(BaseNode): "start_command": self.start_command, "status": self.status, "environment": self.environment, - "node_directory": self.working_dir + "node_directory": self.working_path } def _get_free_display_port(self): diff --git a/gns3server/compute/iou/iou_vm.py b/gns3server/compute/iou/iou_vm.py index b7800b36..890bbc63 100644 --- a/gns3server/compute/iou/iou_vm.py +++ b/gns3server/compute/iou/iou_vm.py @@ -194,7 +194,7 @@ class IOUVM(BaseNode): iou_vm_info = {"name": self.name, "node_id": self.id, - "node_directory": self.working_dir, + "node_directory": self.working_path, "console": self._console, "console_type": "telnet", "status": self.status, diff --git a/gns3server/compute/project.py b/gns3server/compute/project.py index 34401f82..8be8fb1e 100644 --- a/gns3server/compute/project.py +++ b/gns3server/compute/project.py @@ -207,7 +207,7 @@ class Project: :returns: Node working directory """ - workdir = os.path.join(self._path, "project-files", node.manager.module_name.lower(), node.id) + workdir = self.node_working_path(node) if not self._deleted: try: os.makedirs(workdir, exist_ok=True) @@ -215,6 +215,15 @@ class Project: raise aiohttp.web.HTTPInternalServerError(text="Could not create the node working directory: {}".format(e)) return workdir + def node_working_path(self, node): + """ + Returns a node working path for node. It doesn't create structure if not present on system. + :param node: Node instance + :return: Node working path + """ + return os.path.join(self._path, "project-files", node.manager.module_name.lower(), node.id) + + def tmp_working_directory(self): """ A temporary directory. Will be clean at project open and close diff --git a/gns3server/compute/qemu/qemu_vm.py b/gns3server/compute/qemu/qemu_vm.py index d782cbe7..cc7d6be2 100644 --- a/gns3server/compute/qemu/qemu_vm.py +++ b/gns3server/compute/qemu/qemu_vm.py @@ -1626,7 +1626,7 @@ class QemuVM(BaseNode): answer = { "project_id": self.project.id, "node_id": self.id, - "node_directory": self.working_dir + "node_directory": self.working_path } # Qemu has a long list of options. The JSON schema is the single source of information for field in QEMU_OBJECT_SCHEMA["required"]: diff --git a/gns3server/compute/virtualbox/virtualbox_vm.py b/gns3server/compute/virtualbox/virtualbox_vm.py index 79dc0472..8fd3be83 100644 --- a/gns3server/compute/virtualbox/virtualbox_vm.py +++ b/gns3server/compute/virtualbox/virtualbox_vm.py @@ -89,7 +89,7 @@ class VirtualBoxVM(BaseNode): "use_any_adapter": self.use_any_adapter, "linked_clone": self.linked_clone} if self.linked_clone: - json["node_directory"] = self.working_dir + json["node_directory"] = self.working_path else: json["node_directory"] = None return json diff --git a/gns3server/compute/vmware/vmware_vm.py b/gns3server/compute/vmware/vmware_vm.py index d9cb7504..bf5359e6 100644 --- a/gns3server/compute/vmware/vmware_vm.py +++ b/gns3server/compute/vmware/vmware_vm.py @@ -86,7 +86,7 @@ class VMwareVM(BaseNode): "adapter_type": self.adapter_type, "use_any_adapter": self.use_any_adapter, "status": self.status, - "node_directory": self.working_dir, + "node_directory": self.working_path, "linked_clone": self.linked_clone} return json diff --git a/gns3server/compute/vpcs/vpcs_vm.py b/gns3server/compute/vpcs/vpcs_vm.py index 2bf95daf..b832b8e7 100644 --- a/gns3server/compute/vpcs/vpcs_vm.py +++ b/gns3server/compute/vpcs/vpcs_vm.py @@ -127,7 +127,7 @@ class VPCSVM(BaseNode): return {"name": self.name, "node_id": self.id, - "node_directory": self.working_dir, + "node_directory": self.working_path, "status": self.status, "console": self._console, "console_type": "telnet", diff --git a/tests/compute/test_project.py b/tests/compute/test_project.py index 3dd0b3e6..1b349354 100644 --- a/tests/compute/test_project.py +++ b/tests/compute/test_project.py @@ -106,6 +106,16 @@ def test_node_working_directory(tmpdir, node): assert os.path.exists(p.node_working_directory(node)) +def test_node_working_path(tmpdir, node): + directory = Config.instance().get_section_config("Server").get("projects_path") + + with patch("gns3server.compute.project.Project.is_local", return_value=True): + p = Project(project_id=str(uuid4())) + assert p.node_working_path(node) == os.path.join(directory, p.id, 'project-files', node.module_name, node.id) + # after this execution directory structure should not be created + assert not os.path.exists(p.node_working_path(node)) + + def test_project_delete(loop): project = Project(project_id=str(uuid4())) directory = project.path