# Copyright 2017, 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,
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Set the delegated task facts
    _db_nova_bin: "{{ hostvars[nova_conductor_setup_host]['nova_bin'] | default(nova_bin) }}"
    _db_nova_system_user_name: "{{ hostvars[nova_conductor_setup_host]['nova_system_user_name'] | default(nova_system_user_name) }}"

# This needs to be done after Compute hosts are added.
- name: Perform a cell_v2 discover
  command: "{{ _db_nova_bin }}/nova-manage cell_v2 discover_hosts{{ (debug | bool) | ternary(' --verbose', '') }}{{ (nova_virt_type == 'ironic') | ternary(' --by-service', '') }}"
  become: yes
  become_user: "{{ _db_nova_system_user_name }}"
  changed_when: false

# The nova-status upgrade check command is typically run after upgrading the
# controller services to new code, but is also OK to run for a greenfield
# install to verify everything is setup correctly. This must run after cell
# mapping setup and online data migrations have run.
# https://docs.openstack.org/nova/latest/cli/nova-status.html
- name: Run nova-status upgrade check to validate a healthy configuration
  command: "{{ _db_nova_bin }}/nova-status upgrade check"
  become: yes
  become_user: "{{ _db_nova_system_user_name }}"
  register: nova_status_upgrade_check
  until: nova_status_upgrade_check is success
  retries: 3
  delay: 15
  # The nova-status upgrade check command has three standard return codes:
  # 0: all checks were successful
  # 1: warning: there might be some checks that require investigation, but
  #    generally will not block an automated install/upgrade; digging into
  #    warnings is useful for debugging post-install/upgrade issues
  # 2: at least one check failed and must stop the install/upgrade because
  #    something was not setup properly
  failed_when: "nova_status_upgrade_check.rc not in [0, 1]"
  changed_when: false