Merge "OS release upgrade / update cells utility"
diff --git a/README.rst b/README.rst
index 06741cd..3abd1fb 100644
--- a/README.rst
+++ b/README.rst
@@ -158,6 +158,16 @@
         barbican:
           enabled: true
 
+Enable cells update:
+
+**Note:** Useful when upgrading Openstack. To update cells to test sync db agains duplicated production database.
+
+.. code-block:: yaml
+
+    nova:
+      controller:
+        update_cells: true
+
 
 Configuring TLS communications
 ------------------------------
diff --git a/_states/novang.py b/_states/novang.py
index a3376e7..4ea4094 100644
--- a/_states/novang.py
+++ b/_states/novang.py
@@ -57,6 +57,28 @@
     return ret
 
 
+def update_cell(name='cell1', transport_url='none:///', db_engine='mysql', db_name='nova_upgrade', db_user='nova', db_password=None, db_address='0.0.0.0'):
+    '''
+    Ensures that the nova cell is setup correctly
+    '''
+    ret = {'name': name,
+           'changes': {},
+           'result': False,
+           'comment': 'Cell "{0}" does not exists'.format(name)}
+    cell_uuid = __salt__['cmd.shell']('nova-manage cell_v2 list_cells 2>&- | grep ' + name + ' | tr -d \"\n\" | awk \'{print $4}\'')
+    if cell_uuid:
+        try:
+            __salt__['cmd.shell']('nova-manage cell_v2 update_cell --cell_uuid ' + cell_uuid + ' --transport-url ' + transport_url + ' --database_connection ' + db_engine + '+pymysql://' + db_user + ':' + db_password + '@' + db_address + '/' + db_name + '?charset=utf8')
+            ret['result'] = True
+            ret['comment'] = 'Cell {0} updated'.format(name)
+            ret['changes'][name] = 'Cell {0} successfuly updated'.format(name)
+        except:
+            ret['result'] = False
+            ret['comment'] = 'Cell {0} not updated'.format(name)
+            ret['changes'][name] = 'Cell {0} failed to be updated'.format(name)
+    return ret
+
+
 def api_db_version_present(name=None, version="20"):
     '''
     Ensures that specific api_db version is present
diff --git a/nova/controller.sls b/nova/controller.sls
index 24bb535..71d8834 100644
--- a/nova/controller.sls
+++ b/nova/controller.sls
@@ -265,6 +265,45 @@
   - require:
     - cmd: nova_controller_syncdb
 
+{%- if controller.get('update_cells') %}
+
+nova_update_cell0:
+  novang.update_cell:
+  - name: "cell0"
+  - db_name: {{ controller.database.name }}_cell0
+  - db_engine: {{ controller.database.engine }}
+  - db_password: {{ controller.database.password }}
+  - db_user: {{ controller.database.user }}
+  - db_address: {{ controller.database.host }}
+  {%- if grains.get('noservices') %}
+  - onlyif: /bin/false
+  {%- endif %}
+
+{%- set rabbit_port = controller.message_queue.get('port', 5671 if controller.message_queue.get('ssl',{}).get('enabled', False) else 5672) %}
+
+nova_update_cell1:
+  novang.update_cell:
+  - name: "cell1"
+  - db_name: {{ controller.database.name }}
+{%- if controller.message_queue.members is defined %}
+  - transport_url = rabbit://{% for member in controller.message_queue.members -%}
+                             {{ controller.message_queue.user }}:{{ controller.message_queue.password }}@{{ member.host }}:{{ member.get('port', rabbit_port) }}
+                             {%- if not loop.last -%},{%- endif -%}
+                         {%- endfor -%}
+                             /{{ controller.message_queue.virtual_host }}
+{%- else %}
+  - transport_url: rabbit://{{ controller.message_queue.user }}:{{ controller.message_queue.password }}@{{ controller.message_queue.host }}:{{ rabbit_port}}/{{ controller.message_queue.virtual_host }}
+{%- endif %}
+  - db_engine: {{ controller.database.engine }}
+  - db_password: {{ controller.database.password }}
+  - db_user: {{ controller.database.user }}
+  - db_address: {{ controller.database.host }}
+  {%- if grains.get('noservices') %}
+  - onlyif: /bin/false
+  {%- endif %}
+
+{%- endif %}
+
 nova_placement_service_mask:
   file.symlink:
    - name: /etc/systemd/system/nova-placement-api.service