[sandbox PATCH v4 16/21] Image: Add Volume Support

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

 



Volumes let user to map host-paths into sandbox. Docker containers
need volumes for data persistence.
---
 virt-sandbox-image/sources/DockerSource.py | 12 ++++++++++++
 virt-sandbox-image/sources/Source.py       |  4 ++++
 virt-sandbox-image/virt-sandbox-image.py   | 22 ++++++++++++++++++++++
 3 files changed, 38 insertions(+)

diff --git a/virt-sandbox-image/sources/DockerSource.py b/virt-sandbox-image/sources/DockerSource.py
index 87fbcf3..1022107 100644
--- a/virt-sandbox-image/sources/DockerSource.py
+++ b/virt-sandbox-image/sources/DockerSource.py
@@ -28,6 +28,7 @@ import traceback
 import os
 import subprocess
 import shutil
+import collections
 
 class DockerConfParser():
 
@@ -37,6 +38,13 @@ class DockerConfParser():
     def getRunCommand(self):
         cmd = self.json_data['container_config']['Cmd'][2]
         return cmd[cmd.index('"') + 1:cmd.rindex('"')]
+    def getVolumes(self):
+        volumes = self.json_data['container_config']['Volumes']
+        volumelist = []
+        if isinstance(volumes,collections.Iterable):
+          for key,value in volumes.iteritems():
+            volumelist.append(key)
+        return volumelist
 
 class DockerSource(Source):
 
@@ -393,5 +401,9 @@ class DockerSource(Source):
         commandToRun = configParser.getRunCommand()
         return commandToRun
 
+    def get_volume(self,configfile):
+        configParser = DockerConfParser(configfile)
+        return configParser.getVolumes()
+
 def debug(msg):
     sys.stderr.write(msg)
diff --git a/virt-sandbox-image/sources/Source.py b/virt-sandbox-image/sources/Source.py
index 9a3da59..8cc508e 100644
--- a/virt-sandbox-image/sources/Source.py
+++ b/virt-sandbox-image/sources/Source.py
@@ -45,3 +45,7 @@ class Source():
     @abstractmethod
     def get_disk(self,**args):
       pass
+
+    @abstractmethod
+    def get_volume(self,**args):
+      pass
diff --git a/virt-sandbox-image/virt-sandbox-image.py b/virt-sandbox-image/virt-sandbox-image.py
index 058738a..79f8d8c 100755
--- a/virt-sandbox-image/virt-sandbox-image.py
+++ b/virt-sandbox-image/virt-sandbox-image.py
@@ -132,6 +132,7 @@ def check_connect(connectstr):
 
 def run(args):
     try:
+        global storage_dir
         if args.connect is not None:
             check_connect(args.connect)
         source = dynamic_source_loader(args.source)
@@ -150,6 +151,25 @@ def run(args):
         if networkArgs is not None:
             params.append('-N')
             params.append(networkArgs)
+        allVolumes = source.get_volume(configfile)
+        volumeArgs = args.volume
+        if volumeArgs is not None:
+            allVolumes = allVolumes + volumeArgs
+        for volume in allVolumes:
+            volumeSplit = volume.split(":")
+            volumelen = len(volumeSplit)
+            if volumelen == 2:
+                hostPath = volumeSplit[0]
+                guestPath = volumeSplit[1]
+            elif volumelen == 1:
+                guestPath = volumeSplit[0]
+                hostPath = storage_dir + guestPath
+                if not os.path.exists(hostPath):
+                    os.makedirs(hostPath)
+            else:
+                pass
+            params.append("--mount")
+            params.append("host-bind:%s=%s" %(guestPath,hostPath))
         params.append('--')
         params.append(commandToRun)
         cmd = cmd + params
@@ -234,6 +254,8 @@ def gen_run_args(subparser):
                         help=_("Igniter command for image"))
     parser.add_argument("-n","--network",
                         help=_("Network params for running template"))
+    parser.add_argument("-v","--volume",action="append",
+                        help=_("Volume 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]