[sandbox PATCH v2 19/19] Image: Add custom environment support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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   | 19 +++++++++++++++++++
 3 files changed, 33 insertions(+)

diff --git a/virt-sandbox-image/sources/DockerSource.py b/virt-sandbox-image/sources/DockerSource.py
index 44bc238..54b68b9 100644
--- a/virt-sandbox-image/sources/DockerSource.py
+++ b/virt-sandbox-image/sources/DockerSource.py
@@ -48,6 +48,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):
     default_index_server = "index.docker.io"
@@ -411,5 +417,9 @@ class DockerSource(Source):
         configParser = DockerConfParser(configfile)
         return configParser.getVolumes()
 
+    def get_environment(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 6898c15..ad82986 100644
--- a/virt-sandbox-image/sources/Source.py
+++ b/virt-sandbox-image/sources/Source.py
@@ -53,3 +53,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 b12b99b..25c8dfa 100755
--- a/virt-sandbox-image/virt-sandbox-image.py
+++ b/virt-sandbox-image/virt-sandbox-image.py
@@ -118,10 +118,12 @@ def run(args):
             cmd.append("-c")
             cmd.append(args.connect)
         params = ['-m','host-image:/=%s,format=%s' %(diskfile,format)]
+
         networkArgs = args.network
         if networkArgs is not None:
             params.append('-N')
             params.append(networkArgs)
+
         allVolumes = source.get_volume(configfile)
         volumeArgs = args.volume
         if volumeArgs is not None:
@@ -141,6 +143,20 @@ def run(args):
                 pass
             params.append("--mount")
             params.append("host-bind:%s=%s" %(guestPath,hostPath))
+
+        allEnvs = source.get_environment(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
@@ -215,6 +231,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

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]