---

# Delete the scaled node
- name: Check the overcloud heat stack-list state
  shell: >
    source {{ working_dir }}/stackrc;
    heat stack-list
  register: heat_stack_list_result

- name: Verify the overcloud is in a complete state
  fail: msg='Overcloud heat stack is not in a complete state'
  when: heat_stack_list_result.stdout.find('COMPLETE') == -1

- name: Register uuid of original {{ node_to_scale }} node
  shell: >
    source {{ working_dir }}/stackrc;
    nova list | grep -m 1 {{ node_to_scale }} | sed -e 's/|//g' | awk '{print $1}'
  register: node_id_to_delete

- name: Register the Name of the original {{ node_to_scale }} node
  shell: >
    source {{ working_dir }}/stackrc;
    nova list | grep -m 1 {{ node_to_scale }} | sed -e 's/|//g' | awk '{print $2}'
  register: node_name_to_delete

- name: Display node name to be deleted
  debug: msg={{ node_name_to_delete.stdout }}

- name: Copy delete node script to undercloud
  template:
    src: delete-node.j2
    dest: "{{ working_dir }}/delete-node.sh"
    mode: 0755

- name: Delete node by id
  shell: >
    cat {{ working_dir }}/delete-node.sh;
    set -o pipefail &&
    {{ working_dir }}/delete-node.sh 2>&1 {{ timestamper_cmd }} >
    delete_node_scale_console.log;

# Verify the delete was successful
- name: Poll heat stack-list to determine when node delete is complete
  shell: >
    source {{ working_dir }}/stackrc;
    heat stack-list
  register: heat_stack_list_result
  until: heat_stack_list_result.stdout.find("COMPLETE") != -1
  retries: 20
  delay: 90

- name: Determine the post scale node count
  shell: >
    source {{ working_dir }}/stackrc;
    nova list | grep {{ node_to_scale }} | cut -f2- -d':' | wc -l
  register: post_scale_node_count

- name: Remove deleted hosts from the host file
  shell: >
    sed -i '/{{ node_name_to_delete.stdout }}/d' {{ local_working_dir }}/hosts
  delegate_to: localhost

- name: Check that post delete node count is correct
  fail:
    msg: "Overcloud nova list does not show expected number of {{ node_to_scale }} services"
  when: post_scale_node_count.stdout|int != (initial_scale_value | int)
