[Kimchi-devel] [PATCH 7/9 - v7] Add script to upgrade objectstore to support new Template disks schema

Rodrigo Trujillo rodrigo.trujillo at linux.vnet.ibm.com
Wed Dec 2 18:38:09 UTC 2015


In Kimchi 2.0, Templates recorded in objectstore will not have
'storagepool' anymore. Instead, each disk in disks field will have a new
entry:
"pool": {"name": ..., "type": ...}
This change is necessary in order to support old objectstore in new
Kimchi version, which has lots of changes in Template backend.

Signed-off-by: Rodrigo Trujillo <rodrigo.trujillo at linux.vnet.ibm.com>
---
 root.py  |  2 ++
 utils.py | 47 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/root.py b/src/wok/plugins/kimchi/root.py
index 6af053e..4fbe0fc 100644
--- a/root.py
+++ b/root.py
@@ -27,6 +27,7 @@ from wok.plugins.kimchi.control import sub_nodes
 from wok.plugins.kimchi.model import model as kimchiModel
 from wok.plugins.kimchi.utils import upgrade_objectstore_data
 from wok.plugins.kimchi.utils import upgrade_objectstore_schema
+from wok.plugins.kimchi.utils import upgrade_objectstore_template_disks
 from wok.root import WokRoot
 
 
@@ -71,6 +72,7 @@ class KimchiRoot(WokRoot):
             upgrade_objectstore_data('icon', 'images', 'plugins/kimchi/')
             upgrade_objectstore_data('storagepool', '/storagepools',
                                      '/plugins/kimchi')
+            upgrade_objectstore_template_disks(self.model.conn)
 
     def get_custom_conf(self):
         return config.KimchiConfig()
diff --git a/utils.py b/src/wok/plugins/kimchi/utils.py
index a5f5e97..0997faa 100644
--- a/utils.py
+++ b/utils.py
@@ -30,6 +30,7 @@ from urlparse import urlparse
 from wok.exception import InvalidParameter, OperationFailed
 from wok.plugins.kimchi import config
 from wok.utils import wok_log
+from wok.xmlutils.utils import xpath_get_text
 
 MAX_REDIRECTION_ALLOWED = 5
 
@@ -171,3 +172,49 @@ def upgrade_objectstore_data(item, old_uri, new_uri):
         if conn:
             conn.close()
         wok_log.info("%d '%s' entries upgraded in objectstore.", total, item)
+
+
+def upgrade_objectstore_template_disks(libv_conn):
+    """
+        Upgrade the value of a given JSON's item of all Templates.
+        Removes 'storagepool' entry and adds
+        'pool: { name: ..., type: ... }'
+    """
+    total = 0
+    try:
+        conn = sqlite3.connect(config.get_object_store(), timeout=10)
+        cursor = conn.cursor()
+        sql = "SELECT id, json FROM objects WHERE type='template'"
+        cursor.execute(sql)
+        for row in cursor.fetchall():
+            template = json.loads(row[1])
+
+            # Get pool info
+            pool_uri = template['storagepool']
+            pool_name = pool_name_from_uri(pool_uri)
+            pool = libv_conn.get().storagePoolLookupByName(
+                pool_name.encode("utf-8"))
+            pool_type = xpath_get_text(pool.XMLDesc(0), "/pool/@type")[0]
+
+            # Update json
+            new_disks = []
+            for disk in template['disks']:
+                disk['pool'] = {'name': pool_uri,
+                                'type': pool_type}
+                new_disks.append(disk)
+            template['disks'] = new_disks
+            del template['storagepool']
+
+            sql = "UPDATE objects SET json=? WHERE id=?"
+            cursor.execute(sql, (json.dumps(template), row[0]))
+            conn.commit()
+            total += 1
+    except sqlite3.Error, e:
+        if conn:
+            conn.rollback()
+        raise OperationFailed("KCHUTILS0006E")
+        wok_log.error("Error while upgrading objectstore data:", e.args[0])
+    finally:
+        if conn:
+            conn.close()
+        wok_log.info("%d 'template' entries upgraded in objectstore.", total)
-- 
2.1.0




More information about the Kimchi-devel mailing list