From 16405ff92842b4a3583edae00590c3fc760a05c9 Mon Sep 17 00:00:00 2001
From: Jiri Stransky <jistr@redhat.com>
Date: Mon, 3 Sep 2018 12:32:53 +0200
Subject: [PATCH] Run online data migrations

We always run DB sync in deploy_tasks, ensuring that the database is
up to date. We should follow up with online data migrations
too.

Doing this via docker_config has 2 purposes:

* We can easily ensure this happens in a container with the right
  config files mounted.

* We can even apply this via a minor update. This is important because
  we'll have to backport this all the way to Pike and apply it there
  using Pike containers, before upgrading to Queens containers.

There's an additional issue to consider: In Puppet service variant we
ran the online migrations for release X before upgrading to X+1, but
the proposed Docker variant migrations for X run with upgrade to
X. This means that when switching from non-containerized to
containerized, we'll need to run migrations twice, to correctly switch
between the aforementioned approaches.

Change-Id: I2eb6c7c42d7e7aea4a78a892790e42bc5371f792
Closes-Bug: #1790474
---
 docker/services/README.rst      |  2 +-
 docker/services/cinder-api.yaml | 37 +++++++++++++++++++++++++++++++++
 docker/services/ironic-api.yaml | 33 +++++++++++++++++++++++++++++
 docker/services/nova-api.yaml   | 25 ++++++++++++++++++++++
 4 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/docker/services/README.rst b/docker/services/README.rst
index 2eb5646dea..3969adcbf6 100644
--- a/docker/services/README.rst
+++ b/docker/services/README.rst
@@ -120,7 +120,7 @@ Steps correlate to the following:
      a) step 4 baremetal
      b) step 4 containers
      c) Keystone containers post initialization (tenant,service,endpoint creation)
-   5) Service activation (Pacemaker)
+   5) Service activation (Pacemaker), online data migration
      a) step 5 baremetal
      b) step 5 containers
 
diff --git a/docker/services/cinder-api.yaml b/docker/services/cinder-api.yaml
index 29032e3d23..a710130775 100644
--- a/docker/services/cinder-api.yaml
+++ b/docker/services/cinder-api.yaml
@@ -180,6 +180,26 @@ outputs:
                       - ''
             environment:
               - KOLLA_CONFIG_STRATEGY=COPY_ALWAYS
+        step_5:
+          cinder_api_online_migrations:
+            start_order: 2
+            image: *cinder_api_image
+            net: host
+            privileged: false
+            detach: false
+            user: root
+            volumes:
+              list_concat:
+                - {get_attr: [ContainersCommon, volumes]}
+                -
+                  - /var/lib/config-data/cinder/etc/my.cnf.d/tripleo.cnf:/etc/my.cnf.d/tripleo.cnf:ro
+                  - /var/lib/config-data/cinder/etc/cinder/:/etc/cinder/:ro
+                  - /var/log/containers/cinder:/var/log/cinder
+                  - /var/log/containers/httpd/cinder-api:/var/log/httpd
+            command:
+              - '/usr/bin/bootstrap_host_exec'
+              - 'cinder_api'
+              - "su cinder -s /bin/bash -c 'cinder-manage db online_data_migrations'"
           cinder_api_cron:
             image: *cinder_api_image
             net: host
@@ -229,6 +249,11 @@ outputs:
         - when: step|int == 0
           tags: common
           block:
+            - name: get bootstrap nodeid
+              command: hiera -c /etc/puppet/hiera.yaml bootstrap_nodeid
+              register: bootstrap_node
+            - name: set is_bootstrap_node fact
+              set_fact: is_bootstrap_node={{bootstrap_node.stdout|lower == ansible_hostname|lower}}
             - name: Check is cinder_api is deployed
               command: systemctl is-enabled openstack-cinder-api
               ignore_errors: True
@@ -240,6 +265,18 @@ outputs:
               shell: systemctl is-active --quiet openstack-cinder-api
               when: cinder_api_enabled|bool
               tags: validation
+            # NOTE: In puppet service variant, we ran release N online
+            # migrations before upgrade to release N+1. In docker
+            # service variant, we run release N online migrations
+            # after upgrade to release N. This means that during
+            # switch from non-containerized to containerized, we need
+            # to run both of these.
+            - name: Online data migration for Cinder before switching to containers
+              tags: pre-upgrade
+              when:
+                - is_bootstrap_node|bool
+                - cinder_api_enabled|bool
+              command: cinder-manage db online_data_migrations
         - when: step|int == 2
           block:
             - name: Stop and disable cinder_api service (pre-upgrade not under httpd)
diff --git a/docker/services/ironic-api.yaml b/docker/services/ironic-api.yaml
index ae9c66abdf..762b75c7a3 100644
--- a/docker/services/ironic-api.yaml
+++ b/docker/services/ironic-api.yaml
@@ -139,6 +139,22 @@ outputs:
                   - /var/log/containers/httpd/ironic-api:/var/log/httpd
             environment:
               - KOLLA_CONFIG_STRATEGY=COPY_ALWAYS
+        step_5:
+          ironic_online_migrations:
+            start_order: 2
+            image: *ironic_api_image
+            net: host
+            privileged: false
+            detach: false
+            user: root
+            volumes:
+              list_concat:
+                - {get_attr: [ContainersCommon, volumes]}
+                -
+                  - /var/lib/config-data/ironic_api/etc/ironic:/etc/ironic:ro
+                  - /var/log/containers/ironic:/var/log/ironic
+                  - /var/log/containers/httpd/ironic-api:/var/log/httpd
+            command: "/usr/bin/bootstrap_host_exec ironic_api su ironic -s /bin/bash -c 'ironic-dbsync --config-file /etc/ironic/ironic.conf online_data_migrations'"
       host_prep_tasks:
         - name: create persistent logs directory
           file:
@@ -158,6 +174,11 @@ outputs:
         - when: step|int == 0
           tags: common
           block:
+            - name: get bootstrap nodeid
+              command: hiera -c /etc/puppet/hiera.yaml bootstrap_nodeid
+              register: bootstrap_node
+            - name: set is_bootstrap_node fact
+              set_fact: is_bootstrap_node={{bootstrap_node.stdout|lower == ansible_hostname|lower}}
             - name: Check if ironic_api is deployed
               command: systemctl is-enabled --quiet openstack-ironic-api
               ignore_errors: True
@@ -192,6 +213,18 @@ outputs:
               when:
                 - ironic_httpd_enabled|bool
                 - httpd_running|bool
+            # NOTE: In puppet service variant, we ran release N online
+            # migrations before upgrade to release N+1. In docker
+            # service variant, we run release N online migrations
+            # after upgrade to release N. This means that during
+            # switch from non-containerized to containerized, we need
+            # to run both of these.
+            - name: Online data migration for Ironic before switching to containers
+              tags: pre-upgrade
+              when:
+                - is_bootstrap_node|bool
+                - (ironic_httpd_enabled|bool and httpd_running|bool) or ironic_api_enabled|bool
+              command: ironic-dbsync --config-file /etc/ironic/ironic.conf online_data_migrations
         - when: step|int == 2
           block:
             - name: Stop and disable ironic_api service
diff --git a/docker/services/nova-api.yaml b/docker/services/nova-api.yaml
index 53a364a543..4ee6071ee8 100644
--- a/docker/services/nova-api.yaml
+++ b/docker/services/nova-api.yaml
@@ -323,6 +323,14 @@ outputs:
                   - ''
                   - - 'TRIPLEO_DEPLOY_IDENTIFIER='
                     - {get_param: DeployIdentifier}
+          nova_online_migrations:
+            start_order: 2
+            image: *nova_api_image
+            net: host
+            detach: false
+            volumes: *nova_api_bootstrap_volumes
+            user: root
+            command: "/usr/bin/bootstrap_host_exec nova_api su nova -s /bin/bash -c '/usr/bin/nova-manage db online_data_migrations'"
       metadata_settings:
         get_attr: [NovaApiBase, role_data, metadata_settings]
       host_prep_tasks: {get_attr: [NovaApiLogging, host_prep_tasks]}
@@ -330,6 +338,11 @@ outputs:
         - when: step|int == 0
           tags: common
           block:
+            - name: get bootstrap nodeid
+              command: hiera -c /etc/puppet/hiera.yaml bootstrap_nodeid
+              register: bootstrap_node
+            - name: set is_bootstrap_node fact
+              set_fact: is_bootstrap_node={{bootstrap_node.stdout|lower == ansible_hostname|lower}}
             - name: Check if nova_api is deployed
               command: systemctl is-enabled --quiet openstack-nova-api
               ignore_errors: True
@@ -361,6 +374,18 @@ outputs:
               when:
                 - nova_api_httpd_enabled|bool
                 - httpd_running|bool
+            # NOTE: In puppet service variant, we ran release N online
+            # migrations before upgrade to release N+1. In docker
+            # service variant, we run release N online migrations
+            # after upgrade to release N. This means that during
+            # switch from non-containerized to containerized, we need
+            # to run both of these.
+            - name: Online data migration for Nova before switching to containers
+              tags: pre-upgrade
+              when:
+                - is_bootstrap_node|bool
+                - (nova_api_httpd_enabled|bool and httpd_running|bool) or nova_api_enabled|bool
+              command: nova-manage db online_data_migrations
         - when: step|int == 2
           block:
             - name: Stop and disable nova_api service