---
# Copyright 2015, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: "Newton: Set venv path"
  hosts: "keystone_all[0]:glance_all[0]:cinder_all[0]:nova_api_os_compute[0]:neutron_server[0]:heat_all[0]:ironic_conductor[0]:aodh_all[0]"
  user: root
  vars:
    file_name: "{{ venv_tar_location | basename }}"
  tasks:
    - name: "Newton: Set venv fact"
      set_fact:
        venv_path: "/opt/{{ file_name.split('.tgz')[0] }}"

- name: "Newton: Keystone DB Migrations"
  hosts: "keystone_all[0]"
  user: root
  tasks:
    - name: "Newton: Perform a Keystone DB sync"
      command: "{{ venv_path }}/bin/keystone-manage db_sync"

- name: "Newton: Glance DB Migrations"
  hosts: "glance_all[0]"
  user: root
  tasks:
    - name: "Newton: Perform a Glance DB sync"
      command: "{{ venv_path }}/bin/glance-manage db_sync"

- name: "Newton: Cinder DB Migrations"
  hosts: "cinder_all[0]"
  user: root
  tasks:
    - name: "Newton: Check the service list"
      shell: mysql -Be 'select `binary`,`host` from services;' cinder | awk '{if (NR!=1) {print $1, $2}}'
      args:
        executable: /bin/bash
      changed_when: false
      delegate_to: "{{ groups['utility_all'][0] }}"
      register: cinder_service_list
    - name: "Newton: Perform a cinder DB sync"
      command: "{{ venv_path }}/bin/cinder-manage db sync"
    - name: "Newton: Perform service purge"
      shell: "{{ venv_path }}/bin/cinder-manage service remove {{ item }}"
      args:
        executable: /bin/bash
      with_items: "{{ cinder_service_list.stdout_lines }}"

- name: "Newton: Nova DB Migrations"
  hosts: "nova_api_os_compute[0]"
  user: root
  tasks:
    - name: "Newton: Run nova null uuid checks"
      command: "{{ venv_path }}/bin/nova-manage db null_instance_uuid_scan --delete"
    - name: "Newton: Perform a Nova DB sync"
      command: "{{ venv_path }}/bin/nova-manage db sync"
    - name: "Newton: Perform Nova online data migrations"
      command: "{{ venv_path }}/bin/nova-manage db online_data_migrations"

- name: "Newton: Stop Neutron Server"
  hosts: "neutron_server"
  user: root
  tasks:
    - name: "Newton: Stop Neutron server"
      service:
        name: "neutron-server"
        state: stopped
        pattern: "neutron-server"

- name: "Newton: Neutron DB Migrations"
  hosts: "neutron_server[0]"
  user: root
  vars:
    neutron_plugins:
      ml2.lxb:
        plugin_ini: "plugins/ml2/ml2_conf.ini"
      ml2.ovs:
        plugin_ini: "plugins/ml2/ml2_conf.ini"
      ml2.ovs.dvr:
        plugin_ini: "plugins/ml2/ml2_conf.ini"
      ml2.calico:
        plugin_ini: "plugins/ml2/ml2_conf.ini"
      plumgrid:
        plugin_ini: "plugins/plumgrid/plumgrid.ini"
      nuage:
        plugin_ini: "plugins/nuage/nuage.ini"
    neutron_system_user_name: "neutron"
    neutron_db_revision: "heads"
    neutron_db_plugin: "/etc/neutron/{{ neutron_plugins[neutron_plugin_type | default('ml2.lxb')]['plugin_ini'] }}"
    neutron_db_config: "/etc/neutron/neutron.conf"
  tasks:
    - name: "Newton: Perform a Neutron DB Upgrade"
      command: "{{ venv_path }}/bin/neutron-db-manage --config-file {{ neutron_db_config }} --config-file {{ neutron_db_plugin }} upgrade {{ neutron_db_revision }}"
      sudo: yes
      sudo_user: "{{ neutron_system_user_name }}"

- name: "Newton: Heat DB Migrations"
  hosts: "heat_all[0]"
  user: root
  tasks:
    - name: "Newton: Perform a Heat DB sync"
      command: "{{ venv_path }}/bin/heat-manage db_sync"

- name: "Newton: Horizon DB drop"
  hosts: "utility_all[0]"
  user: root
  tasks:
    - name: "Newton: Drop horizon DB - It will be recreated later"
      command: "mysql --unbuffered -sNL -e 'drop database {{ horizon_galera_database | default('dash') }}'"
      register: drop_db
      failed_when: false
      changed_when: drop_db.rc == 0

- name: "Newton: Aodh DB Migrations"
  hosts: "aodh_all[0]"
  user: root
  tasks:
    - name: "Newton: Perform a Aodh DB sync"
      command: "{{ aodh_bin }}/aodh-dbsync"

- name: "Newton: Ironic DB Migrations"
  hosts: "ironic_conductor[0]"
  user: root
  tasks:
    - name: "Newton: Update database schema"
      command: "{{ venv_path }}/ironic-dbsync upgrade"