[Kimchi-devel] [PATCH 3/4] Upgrade Kimchi objectstore content.

pvital at linux.vnet.ibm.com pvital at linux.vnet.ibm.com
Thu Nov 5 18:47:17 UTC 2015


From: Paulo Vital <pvital at linux.vnet.ibm.com>

With the new structure of Wok and Kimchi, the content of the objectstore file
needs to be updated to reflect the new paths of icon and storagepool present
in template's and VM's information.

This patch adds a step to execute an update of these contents in the previous
objectstore file (older than Kimchi 1.5.1) during the startup of Kimchi 2.0.0
(or higher) plugin.

Signed-off-by: Paulo Vital <pvital at linux.vnet.ibm.com>
---
 src/wok/plugins/kimchi/i18n.py  |  1 +
 src/wok/plugins/kimchi/root.py  | 10 ++++++
 src/wok/plugins/kimchi/utils.py | 80 +++++++++++++++++++++++++++++++++++++++--
 3 files changed, 89 insertions(+), 2 deletions(-)

diff --git a/src/wok/plugins/kimchi/i18n.py b/src/wok/plugins/kimchi/i18n.py
index 42a5e16..59b61de 100644
--- a/src/wok/plugins/kimchi/i18n.py
+++ b/src/wok/plugins/kimchi/i18n.py
@@ -262,6 +262,7 @@ messages = {
     "KCHHOST0004E": _("Conflicting flag filters specified."),
 
     "KCHUTILS0003E": _("Unable to choose a virtual machine name"),
+    "KCHUTILS0006E": _("Cannot upgrade objectstore data."),
 
     "KCHVMSTOR0002E": _("Invalid storage type. Types supported: 'cdrom', 'disk'"),
     "KCHVMSTOR0003E": _("The path '%(value)s' is not a valid local/remote path for the device"),
diff --git a/src/wok/plugins/kimchi/root.py b/src/wok/plugins/kimchi/root.py
index 37da8b3..6af053e 100644
--- a/src/wok/plugins/kimchi/root.py
+++ b/src/wok/plugins/kimchi/root.py
@@ -25,6 +25,8 @@ from wok.plugins.kimchi import config, mockmodel, vnc
 from wok.plugins.kimchi.i18n import messages
 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.root import WokRoot
 
 
@@ -62,5 +64,13 @@ class KimchiRoot(WokRoot):
         self.domain = 'kimchi'
         self.messages = messages
 
+        # Some paths or URI's present in the objectstore have changed after
+        # Kimchi 2.0.0 release. Check here if an upgrade in the schema and data
+        # are necessary.
+        if upgrade_objectstore_schema('version'):
+            upgrade_objectstore_data('icon', 'images', 'plugins/kimchi/')
+            upgrade_objectstore_data('storagepool', '/storagepools',
+                                     '/plugins/kimchi')
+
     def get_custom_conf(self):
         return config.KimchiConfig()
diff --git a/src/wok/plugins/kimchi/utils.py b/src/wok/plugins/kimchi/utils.py
index 4462ce6..31def2e 100644
--- a/src/wok/plugins/kimchi/utils.py
+++ b/src/wok/plugins/kimchi/utils.py
@@ -19,14 +19,17 @@
 #
 
 import contextlib
+import json
 import os
 import re
+import sqlite3
 import urllib2
 from httplib import HTTPConnection, HTTPException
 from urlparse import urlparse
 
-from wok.exception import InvalidParameter
-
+from wok.exception import InvalidParameter, OperationFailed
+from wok.plugins.kimchi import config
+from wok.utils import wok_log
 
 MAX_REDIRECTION_ALLOWED = 5
 
@@ -95,3 +98,76 @@ def validate_repo_url(url):
             raise InvalidParameter("WOKUTILS0001E", {'url': url})
     else:
         raise InvalidParameter("KCHREPOS0002E")
+
+
+def get_objectstore_fields():
+    """
+        Return a list with all fields of the objectstore.
+    """
+    conn = sqlite3.connect(config.get_object_store(), timeout=10)
+    cursor = conn.cursor()
+    schema_fields = []
+    sql = "PRAGMA table_info('objects')"
+    cursor.execute(sql)
+    for row in cursor.fetchall():
+        schema_fields.append(row[1])
+    return schema_fields
+
+
+def upgrade_objectstore_schema(field=None):
+    """
+        Add a new column (of type TEXT) in the objectstore schema.
+    """
+    if field is None:
+        wok_log.error("Cannot upgrade objectstore schema.")
+        return False
+
+    if field in get_objectstore_fields():
+        return False
+    try:
+        conn = sqlite3.connect(config.get_object_store(), timeout=10)
+        cursor = conn.cursor()
+        sql = "ALTER TABLE objects ADD COLUMN %s TEXT" % field
+        cursor.execute(sql)
+        wok_log.info("Objectstore schema sucessfully upgraded.")
+        conn.close()
+    except sqlite3.Error, e:
+        if conn:
+            conn.rollback()
+            conn.close()
+        wok_log.error("Cannot upgrade objectstore schema: ", e.args[0])
+        return False
+    return True
+
+
+def upgrade_objectstore_data(item, old_uri, new_uri):
+    """
+        Upgrade the value of a given JSON's item of all Template and VM entries
+        of the objectstore from old_uri to new_uri.
+    """
+    total = 0
+    try:
+        conn = sqlite3.connect(config.get_object_store(), timeout=10)
+        cursor = conn.cursor()
+        sql = "SELECT id, json FROM objects WHERE type='template' OR type='vm'"
+        cursor.execute(sql)
+        for row in cursor.fetchall():
+            # execute update here
+            template = json.loads(row[1])
+            path = (template[item] if item in template else 'none')
+            if path.startswith(old_uri):
+                template[item] = new_uri + path
+                sql = "UPDATE objects SET json=?, version=? WHERE id=?"
+                cursor.execute(sql, (json.dumps(template),
+                                     config.get_kimchi_version(), 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 '%s' entries upgraded in objectstore.", total, item)
-- 
2.4.3




More information about the Kimchi-devel mailing list