[Kimchi-devel] [PATCH 3/4] storagevolume: Download remote images to a storage pool

Crístian Viana vianac at linux.vnet.ibm.com
Wed Aug 27 03:36:29 UTC 2014


In order to simplify the creation of a remote image in Kimchi, the user
will be able to provide a remote URL and the Kimchi server will download
it and put it on a storage pool.

Download a remote image to a storage pool

Signed-off-by: Crístian Viana <vianac at linux.vnet.ibm.com>
---
 src/kimchi/model/storagevolumes.py | 49 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/src/kimchi/model/storagevolumes.py b/src/kimchi/model/storagevolumes.py
index bd9dbc9..dc19f4f 100644
--- a/src/kimchi/model/storagevolumes.py
+++ b/src/kimchi/model/storagevolumes.py
@@ -18,6 +18,7 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
 import os
+import urllib2
 
 import libvirt
 
@@ -27,7 +28,7 @@ from kimchi.exception import InvalidOperation, InvalidParameter, IsoFormatError
 from kimchi.exception import MissingParameter, NotFoundError, OperationFailed
 from kimchi.isoinfo import IsoImage
 from kimchi.model.storagepools import StoragePoolModel
-from kimchi.utils import kimchi_log
+from kimchi.utils import add_task, kimchi_log
 from kimchi.model.tasks import TaskModel
 from kimchi.model.vms import VMsModel, VMModel
 from kimchi.vmdisks import get_vm_disk, get_vm_disk_list
@@ -108,6 +109,52 @@ class StorageVolumesModel(object):
 
         return name
 
+    def _create_volume_with_url(self, pool_name, params):
+        def _download(cb, params):
+            pool = params['pool']
+            target = params['target']
+            url = params['url']
+
+            chunk_size = 4096  # 4 kiB
+
+            response = volume = None
+            try:
+                response = urllib2.urlopen(url)
+                volume = open(target, 'w')
+
+                while True:
+                    chunk_data = response.read(chunk_size)
+                    if not chunk_data:
+                        break
+
+                    volume.write(chunk_data)
+            except Exception, e:
+                raise OperationFailed('KCHVOL0007E', {'name': name,
+                                                      'pool': pool_name,
+                                                      'err': e.message})
+            finally:
+                if response is not None:
+                    response.close()
+
+                if volume is not None:
+                    volume.close()
+
+            cb('OK', True)
+            pool.refresh()
+
+        name = params['name']
+        url = params['url']
+
+        pool_model = StoragePoolModel(conn=self.conn, objstore=self.objstore)
+        pool = pool_model.lookup(pool_name)
+
+        download_params = {'pool': StoragePoolModel.get_storagepool(pool_name,
+                                                                    self.conn),
+                           'target': os.path.join(pool['path'], name),
+                           'url': url}
+        taskid = add_task('', _download, self.objstore, download_params)
+        return self.task.lookup(taskid)
+
     def get_list(self, pool_name):
         pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
         if not pool.isActive():
-- 
1.9.3




More information about the Kimchi-devel mailing list