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
---
virt-sandbox-image/sources/DockerSource.py | 10 ++++++++++
virt-sandbox-image/sources/Source.py | 4 ++++
virt-sandbox-image/virt-sandbox-image.py | 17 +++++++++++++++++
3 files changed, 31 insertions(+)
diff --git a/virt-sandbox-image/sources/DockerSource.py
b/virt-sandbox-image/sources/DockerSource.py
index 1022107..122e595 100644
--- a/virt-sandbox-image/sources/DockerSource.py
+++ b/virt-sandbox-image/sources/DockerSource.py
@@ -45,6 +45,12 @@ class DockerConfParser():
for key,value in volumes.iteritems():
volumelist.append(key)
return volumelist
+ def getEnvs(self):
+ lst = self.json_data['container_config']['Env']
+ if lst is not None and isinstance(lst,list):
+ return lst
+ else:
+ return []
class DockerSource(Source):
@@ -405,5 +411,9 @@ class DockerSource(Source):
configParser = DockerConfParser(configfile)
return configParser.getVolumes()
+ def get_env(self,configfile):
+ configParser = DockerConfParser(configfile)
+ return configParser.getEnvs()
+
def debug(msg):
sys.stderr.write(msg)
diff --git a/virt-sandbox-image/sources/Source.py b/virt-sandbox-image/sources/Source.py
index 8cc508e..c467ce2 100644
--- a/virt-sandbox-image/sources/Source.py
+++ b/virt-sandbox-image/sources/Source.py
@@ -49,3 +49,7 @@ class Source():
@abstractmethod
def get_volume(self,**args):
pass
+
+ @abstractmethod
+ def get_env(self,**args):
+ pass
diff --git a/virt-sandbox-image/virt-sandbox-image.py
b/virt-sandbox-image/virt-sandbox-image.py
index 79f8d8c..285e99c 100755
--- a/virt-sandbox-image/virt-sandbox-image.py
+++ b/virt-sandbox-image/virt-sandbox-image.py
@@ -170,6 +170,20 @@ def run(args):
pass
params.append("--mount")
params.append("host-bind:%s=%s" %(guestPath,hostPath))
+
+ allEnvs = source.get_env(configfile)
+ 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
+
params.append('--')
params.append(commandToRun)
cmd = cmd + params
@@ -256,6 +270,9 @@ def gen_run_args(subparser):
help=_("Network params for running template"))
parser.add_argument("-v","--volume",action="append",
help=_("Volume params for running template"))
+ parser.add_argument("-e","--env",action="append",
+ help=_("Environment params for running template"))
+
parser.set_defaults(func=run)
if __name__ == '__main__':
--
2.1.0