From: Eren Yagdiran <erenyagdiran(a)gmail.com>
Any custom key=value pair can be used as a custom environment variable
in virt-sandbox-image.
e.g virt-sandbox-image run ubuntu /var/lib/libvirt/templates -c lxc:/// -i /bin/bash -e
key1=val1
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
libvirt-sandbox/image/cli.py | 16 ++++++++++++++++
libvirt-sandbox/image/sources/DockerSource.py | 11 +++++++++++
libvirt-sandbox/image/sources/Source.py | 10 ++++++++++
3 files changed, 37 insertions(+)
diff --git a/libvirt-sandbox/image/cli.py b/libvirt-sandbox/image/cli.py
index 30e2558..c17b577 100755
--- a/libvirt-sandbox/image/cli.py
+++ b/libvirt-sandbox/image/cli.py
@@ -129,6 +129,19 @@ def run(args):
params.append('-N')
params.append(networkArgs)
+ allEnvs = source.get_env(args.template, args.template_dir)
+ envArgs = args.env
+ if envArgs is not None:
+ allEnvs = allEnvs + envArgs
+ for env in allEnvs:
+ envsplit = env.split("=")
+ envlen = len(envsplit)
+ if envlen == 2:
+ params.append("--env")
+ params.append(env)
+ else:
+ pass
+
cmd = cmd + params + ['--'] + commandToRun
subprocess.call(cmd)
os.unlink(diskfile)
@@ -222,6 +235,9 @@ def gen_run_args(subparser):
help=_("command arguments to run"))
parser.add_argument("-N","--network",
help=_("Network params for running template"))
+ parser.add_argument("-e","--env",action="append",
+ help=_("Environment params for running template"))
+
parser.set_defaults(func=run)
def main():
diff --git a/libvirt-sandbox/image/sources/DockerSource.py
b/libvirt-sandbox/image/sources/DockerSource.py
index 31c1d80..4455198 100644
--- a/libvirt-sandbox/image/sources/DockerSource.py
+++ b/libvirt-sandbox/image/sources/DockerSource.py
@@ -38,6 +38,12 @@ class DockerConfParser():
return self.json_data['config']['Cmd']
def getEntrypoint(self):
return self.json_data['config']['Entrypoint']
+ def getEnvs(self):
+ lst = self.json_data['config']['Env']
+ if lst is not None and isinstance(lst,list):
+ return lst
+ else:
+ return []
class DockerSource(Source):
@@ -388,5 +394,10 @@ class DockerSource(Source):
else:
return entrypoint + cmd
+ def get_env(self, templatename, templatedir):
+ configfile, diskfile = self._get_template_data(templatename, templatedir)
+ configParser = DockerConfParser(configfile)
+ return configParser.getEnvs()
+
def debug(msg):
sys.stderr.write(msg)
diff --git a/libvirt-sandbox/image/sources/Source.py
b/libvirt-sandbox/image/sources/Source.py
index a5d3844..8a21f90 100644
--- a/libvirt-sandbox/image/sources/Source.py
+++ b/libvirt-sandbox/image/sources/Source.py
@@ -95,3 +95,13 @@ class Source():
of a template.
"""
pass
+
+ @abstractmethod
+ def get_env(self,templatename, templatedir):
+ """
+ :param templatename: name of the template image to download
+ :param templatedir: local directory path in which to find template
+
+ Get the dict of environment variables to set
+ """
+ pass
--
2.4.3