diff --git a/gns3server/compute/docker/docker_vm.py b/gns3server/compute/docker/docker_vm.py index 7318b755..66e5d6f7 100644 --- a/gns3server/compute/docker/docker_vm.py +++ b/gns3server/compute/docker/docker_vm.py @@ -325,15 +325,20 @@ class DockerVM(BaseNode): # Give the information to the container the list of volume path mounted params["Env"].append("GNS3_VOLUMES={}".format(":".join(self._volumes))) - if self.project.variables: - for var in self.project.variables: - params["Env"].append("{}={}".format(var["name"], var.get('value', ''))) + variables = self.project.variables + if not variables: + variables = [] + + for var in variables: + formatted = self._format_env(variables, var.get('value', '')) + params["Env"].append("{}={}".format(var["name"], formatted)) if self._environment: for e in self._environment.strip().split("\n"): e = e.strip() if not e.startswith("GNS3_"): - params["Env"].append(e) + formatted = self._format_env(variables, e) + params["Env"].append(formatted) if self._console_type == "vnc": yield from self._start_vnc() @@ -352,6 +357,11 @@ class DockerVM(BaseNode): name=self._name, id=self._id)) return True + def _format_env(self, variables, env): + for variable in variables: + env = env.replace('${' + variable["name"] + '}', variable.get("value", "")) + return env + def _format_extra_hosts(self, extra_hosts): lines = [h.strip() for h in self._extra_hosts.split("\n") if h.strip() != ""] hosts = [] diff --git a/tests/compute/docker/test_docker_vm.py b/tests/compute/docker/test_docker_vm.py index 1fb6012b..f51e8c9c 100644 --- a/tests/compute/docker/test_docker_vm.py +++ b/tests/compute/docker/test_docker_vm.py @@ -259,7 +259,8 @@ def test_create_with_project_variables(loop, project, manager): project.variables = [ {"name": "VAR1"}, - {"name": "VAR2", "value": "VAL1"} + {"name": "VAR2", "value": "VAL1"}, + {"name": "VAR3", "value": "2x${VAR2}"} ] with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]): @@ -269,6 +270,7 @@ def test_create_with_project_variables(loop, project, manager): called_kwargs = mock.call_args[1] assert "VAR1=" in called_kwargs["data"]["Env"] assert "VAR2=VAL1" in called_kwargs["data"]["Env"] + assert "VAR3=2xVAL1" in called_kwargs["data"]["Env"] project.variables = None