[Kimchi-devel] [PATCH v2][Kimchi 2/4] Add function to update 'memory' in objectstore

Rodrigo Trujillo rodrigo.trujillo at linux.vnet.ibm.com
Sun Feb 21 04:14:37 UTC 2016


This patch adds a new function to update objectstore. Changing 'memory':
XXX by 'memory': {'current': XXX, 'maxmemory': YYY}. Then old templates
will have new format, avoiding errors.
The rule is use default value of maxmemory provided by osinfo, however,
if memory['current'] is greater, then maxmemory = memory['current']

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

diff --git a/root.py b/root.py
index b5aa78b..56eda12 100644
--- a/root.py
+++ b/root.py
@@ -26,6 +26,7 @@ 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_memory
 from wok.plugins.kimchi.utils import upgrade_objectstore_template_disks
 from wok.root import WokRoot
 from wok.utils import upgrade_objectstore_schema
@@ -74,5 +75,8 @@ class Kimchi(WokRoot):
                                      '/plugins/kimchi')
             upgrade_objectstore_template_disks(self.model.conn)
 
+        # Upgrade memory data, if necessary
+        upgrade_objectstore_memory()
+
     def get_custom_conf(self):
         return config.KimchiConfig()
diff --git a/utils.py b/utils.py
index 5a3f209..132a3c6 100644
--- a/utils.py
+++ b/utils.py
@@ -29,6 +29,7 @@ from urlparse import urlparse
 
 from wok.exception import InvalidParameter, OperationFailed
 from wok.plugins.kimchi import config
+from wok.plugins.kimchi.osinfo import get_template_default
 from wok.utils import wok_log
 from wok.xmlutils.utils import xpath_get_text
 
@@ -178,3 +179,48 @@ def upgrade_objectstore_template_disks(libv_conn):
         if conn:
             conn.close()
         wok_log.info("%d 'template' entries upgraded in objectstore.", total)
+
+
+def upgrade_objectstore_memory():
+    """
+        Upgrade the value of a given JSON's item of all Templates.
+        Changes 'memory': XXX by 'memory': {'current': XXXX,
+                                            'maxmemory': XXXX}
+    """
+    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 memory info
+            memory = template['memory']
+            # New memory is a dictionary with 'current' and 'maxmemory'
+            if type(memory) is not dict:
+                maxmem = get_template_default('modern',
+                                              'memory').get('maxmemory')
+                if maxmem < memory:
+                    maxmem = memory
+                template['memory'] = {'current': memory,
+                                      'maxmemory': maxmem}
+            else:
+                continue
+
+            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()
+        if total > 0:
+            wok_log.info(
+                "%d 'template' memory entries upgraded in objectstore.", total)
-- 
2.1.0




More information about the Kimchi-devel mailing list