ocata upgrade fix

Change-Id: I43e421ac1c356aa6df6125a2038b353072114a4c
diff --git a/_states/novang.py b/_states/novang.py
index 3433d16..d5bcb35 100644
--- a/_states/novang.py
+++ b/_states/novang.py
@@ -68,13 +68,14 @@
     api_db_version = __salt__['cmd.shell']('nova-manage api_db version 2>/dev/null')
     try:
         api_db_version = int(api_db_version)
+        version = int(version)
     except:
         # nova is not installed
         ret = _no_change('api_db --version', None, test=True)
         return ret
     if api_db_version < version:
         try:
-            __salt__['cmd.shell']('nova-manage api_db sync --version ' + version)
+            __salt__['cmd.shell']('nova-manage api_db sync --version ' + str(version))
             ret['result'] = True
             ret['comment'] = 'Nova-manage api_db sync --version {0} was successfuly executed'.format(version)
             ret['changes']['api_db'] = 'api_db sync --version {0}'.format(version)
@@ -96,6 +97,7 @@
     db_version = __salt__['cmd.shell']('nova-manage db version 2>/dev/null')
     try:
         db_version = int(db_version)
+        version = int(version)
     except:
         # nova is not installed
         ret = _no_change('db --version', None, test=True)
@@ -103,7 +105,7 @@
 
     if db_version < version:
         try:
-            __salt__['cmd.shell']('nova-manage db sync --version ' + version)
+            __salt__['cmd.shell']('nova-manage db sync --version ' + str(version))
             ret['result'] = True
             ret['comment'] = 'Nova-manage db sync --version {0} was successfuly executed'.format(version)
             ret['changes']['db'] = 'db sync --version {0}'.format(version)
@@ -113,6 +115,37 @@
             ret['changes']['db'] = 'Failed to execute db sync --version {0}'.format(version)
     return ret
 
+def online_data_migrations_present(name=None, api_db_version="20", db_version="334"):
+    '''
+    Ensures that online_data_migrations are enforced if specific version of api_db and db is present
+    '''
+    ret = {'name': 'online_data_migrations',
+           'changes': {},
+           'result': True,
+           'comment': 'Current api_db version != {0} a db version != {1}.'.format(api_db_version, db_version)}
+    cur_api_db_version = __salt__['cmd.shell']('nova-manage api_db version 2>/dev/null')
+    cur_db_version = __salt__['cmd.shell']('nova-manage db version 2>/dev/null')
+    try:
+        cur_api_db_version = int(cur_api_db_version)
+        cur_db_version = int(cur_db_version)
+        api_db_version = int(api_db_version)
+        db_version = int(db_version)
+    except:
+        # nova is not installed
+        ret = _no_change('online_data_migrations', None, test=True)
+        return ret
+    if cur_api_db_version == api_db_version and cur_db_version == db_version:
+        try:
+            __salt__['cmd.shell']('nova-manage db online_data_migrations')
+            ret['result'] = True
+            ret['comment'] = 'nova-manage db online_data_migrations was successfuly executed'
+            ret['changes']['online_data_migrations'] = 'online_data_migrations on api_db version {0} and db version {1}'.format(api_db_version, db_version)
+        except:
+            ret['result'] = False
+            ret['comment'] = 'Error while executing nova-manage db online_data_migrations'
+            ret['changes']['online_data_migrations'] = 'Failed to execute online_data_migrations on api_db version {0} and db version {1}'.format(api_db_version, db_version)
+    return ret
+
 def quota_present(tenant_name, profile, name=None, **kwargs):
     '''
     Ensures that the nova quota exists
diff --git a/nova/controller.sls b/nova/controller.sls
index 664cb2b..85a3ee7 100644
--- a/nova/controller.sls
+++ b/nova/controller.sls
@@ -137,6 +137,16 @@
   - require:
     - file: /etc/nova/nova.conf
 
+{#- the following db online_data_migrations executes only if the current db version == 334 && api_db version == 20 #}
+
+online_data_migrations_for_apidb20_and_db334:
+  novang.online_data_migrations_present:
+  - api_db_version: "20"
+  - db_version: "334"
+  - require:
+    - novang: nova_controller_api_db_sync_version_20
+    - novang: nova_controller_db_sync_version_334
+
 nova_controller_map_cell0:
   cmd.run:
   - name: nova-manage cell_v2 map_cell0