[Kimchi-devel] [PATCH 4/5] Create guests asynchronously: Backend

Christy Perez christy at linux.vnet.ibm.com
Wed Feb 25 16:20:54 UTC 2015


Signed-off-by: Christy Perez <christy at linux.vnet.ibm.com>
---
 src/kimchi/control/vms.py |  4 ++--
 src/kimchi/model/vms.py   | 28 +++++++++++++++++++++++++---
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/src/kimchi/control/vms.py b/src/kimchi/control/vms.py
index 5068b7c..8a31dc0 100644
--- a/src/kimchi/control/vms.py
+++ b/src/kimchi/control/vms.py
@@ -17,13 +17,13 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
-from kimchi.control.base import Collection, Resource
+from kimchi.control.base import AsyncCollection, Resource
 from kimchi.control.utils import internal_redirect, UrlSubNode
 from kimchi.control.vm import sub_nodes
 
 
 @UrlSubNode('vms', True)
-class VMs(Collection):
+class VMs(AsyncCollection):
     def __init__(self, model):
         super(VMs, self).__init__(model)
         self.resource = VM
diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index f49bc88..0ac3a44 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -84,6 +84,7 @@ def __init__(self, **kargs):
         self.guests_stats_thread = BackgroundTask(GUESTS_STATS_INTERVAL,
                                                   self._update_guests_stats)
         self.guests_stats_thread.start()
+        self.task = TaskModel(**kargs)
 
     def _update_guests_stats(self):
         vm_list = self.get_list()
@@ -185,7 +186,6 @@ def _get_disk_io_rate(self, vm_uuid, dom, seconds):
                                'diskWrKB': diskWrKB})
 
     def create(self, params):
-        conn = self.conn.get()
         t_name = template_name_from_uri(params['template'])
         vm_uuid = str(uuid.uuid4())
         vm_list = self.get_list()
@@ -207,6 +207,26 @@ def create(self, params):
 
         t.validate()
 
+        taskid = add_task(u'/vms/%s' % name, 'create',
+                          self._create_task, self.objstore,
+                          {'vm_uuid': vm_uuid, 'template': t, 'name': name})
+
+        return self.task.lookup(taskid)
+
+    def _create_task(self, cb, params):
+        """
+        params: A dict with the following values:
+            - vm_uuid: The UUID of the VM being created
+            - template: The template being used to create the VM
+            - name: The name for the new VM
+        """
+
+        vm_uuid = params['vm_uuid']
+        t = params['template']
+        name = params['name']
+        conn = self.conn.get()
+
+        cb('Storing VM icon')
         # Store the icon for displaying later
         icon = t.info.get('icon')
         if icon:
@@ -221,6 +241,7 @@ def create(self, params):
 
         # If storagepool is SCSI, volumes will be LUNs and must be passed by
         # the user from UI or manually.
+        cb('Provisioning storage for new VM')
         vol_list = []
         if t._get_storage_type() not in ["iscsi", "scsi"]:
             vol_list = t.fork_vm_storage(vm_uuid)
@@ -233,6 +254,7 @@ def create(self, params):
                           graphics=graphics,
                           volumes=vol_list)
 
+        cb('Defining new VM')
         try:
             conn.defineXML(xml.encode('utf-8'))
         except libvirt.libvirtError as e:
@@ -243,10 +265,10 @@ def create(self, params):
             raise OperationFailed("KCHVM0007E", {'name': name,
                                                  'err': e.get_error_message()})
 
+        cb('Updating VM metadata')
         VMModel.vm_update_os_metadata(VMModel.get_vm(name, self.conn), t.info,
                                       self.caps.metadata_support)
-
-        return name
+        cb('OK', True)
 
     def get_list(self):
         return self.get_vms(self.conn)
-- 
2.1.0




More information about the Kimchi-devel mailing list