Support for nested global variables

This commit is contained in:
ziajka 2018-05-09 11:25:55 +02:00
parent 0bcc657bf1
commit 83445214be
2 changed files with 17 additions and 5 deletions

View File

@ -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 = []

View File

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