- name: Lookup deployment UUID
  set_fact:
    deployment_uuid: "{{ lookup('file', tripleo_role_name ~ '/' ~ ansible_hostname | lower ~ '/' ~ item) | from_yaml | json_query(item ~ '.id')}}"

- name: Lookup deployment group
  set_fact:
    deployment_group: "{{ lookup('file', tripleo_role_name ~ '/' ~ ansible_hostname | lower ~ '/' ~ item) | from_yaml | json_query(item ~ '.group')}}"


- name: Hiera check and diff mode
  block:

    - name: Create hiera check-mode directory
      file:
        path: /etc/puppet/check-mode/hieradata
        state: directory
      check_mode: no

    - name: Create deployed check-mode directory
      file:
        path: /var/lib/heat-config/check-mode
        state: directory
      check_mode: no

    - name: Create tripleo-config-download check-mode directory
      file:
        path: /var/lib/heat-config/tripleo-config-download/check-mode
        state: directory
      check_mode: no

    - name: "Render deployment file for {{ item }} for check-mode"
      copy:
        content: "[ {{ lookup('file', tripleo_role_name ~ '/' ~ ansible_hostname | lower ~ '/' ~ item) | from_yaml | json_query(item) }} ]"
        dest: "/var/lib/heat-config/tripleo-config-download/check-mode/{{ item ~ '-' ~ deployment_uuid }}"
      become: true
      check_mode: no

    - name: Run hiera deployment for check mode
      shell: |
        rm -f /var/lib/heat-config/check-mode/{{ deployment_uuid }}.json
        /usr/libexec/os-refresh-config/configure.d/55-heat-config
        exit $(jq .deploy_status_code /var/lib/heat-config/check-mode/{{ deployment_uuid }}.notify.json)
      args:
        warn: false
      become: true
      environment:
        HEAT_SHELL_CONFIG: /var/lib/heat-config/tripleo-config-download/check-mode/{{ item ~ '-' ~ deployment_uuid }}
        HEAT_PUPPET_HIERA_DATADIR: /etc/puppet/check-mode/hieradata
        HEAT_HIERA_CONFIG: /etc/puppet/check-mode/hiera.yaml
        HEAT_CONFIG_DEPLOYED: /var/lib/heat-config/check-mode
      check_mode: no
      ignore_errors: yes

    - name: List hieradata files for check mode
      find:
        path: /etc/puppet/check-mode/hieradata
      register: hieradata_files
      check_mode: no

    - name: diff hieradata changes for check mode
      command:
        diff -uN {{ hieradata_item.path | regex_replace('check-mode', '') }} {{ hieradata_item.path }}
      with_items: "{{ hieradata_files.files }}"
      check_mode: no
      register: diff_results
      changed_when: diff_results.rc == 1
      loop_control:
        loop_var: hieradata_item
        label: "{{ hieradata_item.path }}"
      failed_when: false
      when: ansible_diff_mode

    - name: diff hieradata changes for check mode
      debug:
        var: item.stdout_lines
      with_items: "{{ diff_results.results }}"
      changed_when: item.rc == 1
      loop_control:
        label: "{{ item._ansible_item_label }}"
      when: ansible_diff_mode

    - name: hiera.yaml changes for check mode
      command:
        diff -uN /etc/puppet/hiera.yaml /etc/puppet/check-mode/hiera.yaml
      check_mode: no
      register: diff_results
      changed_when: diff_results.rc == 1
      failed_when: false

    - name: diff hiera.yaml changes for check mode
      debug:
        var: diff_results.stdout_lines
      changed_when: diff_results.rc == 1
      when: ansible_diff_mode

  ignore_errors: yes
  when:
    - deployment_group == 'hiera'
    - ansible_check_mode


- name: "Render deployment file for {{ item }}"
  copy:
    content: "[ {{ lookup('file', tripleo_role_name ~ '/' ~ ansible_hostname | lower ~ '/' ~ item) | from_yaml | json_query(item) }} ]"
    dest: "/var/lib/heat-config/tripleo-config-download/{{ item ~ '-' ~ deployment_uuid }}"
  become: true

- name: "Check if deployed file exists for {{ item }}"
  stat:
    path: /var/lib/heat-config/deployed/{{ deployment_uuid }}.json
  register: deployed_file_stat

- name: "Check previous deployment rc for {{ item }}"
  shell: |
    exit $(jq .deploy_status_code /var/lib/heat-config/deployed/{{ deployment_uuid }}.notify.json)
  register: previous_deployment_result
  ignore_errors: true
  failed_when: false
  when: deployed_file_stat.stat.exists

- name: "Remove deployed file for {{ item }} when previous deployment failed"
  file:
    path: /var/lib/heat-config/deployed/{{ deployment_uuid }}.json
    state: absent
  become: true
  when:
    - not ansible_check_mode
    - deployed_file_stat.stat.exists
    - previous_deployment_result.rc != 0

- name: "Force remove deployed file for {{ item }}"
  file:
    path: /var/lib/heat-config/deployed/{{ deployment_uuid }}.json
    state: absent
  become: true
  when: (force | bool)

- name: Set fact for async_deployment
  set_fact:
    use_async_deployment: "{{ (async_deployment | default(False)) or (item == 'NetworkDeployment') }}"

- name: "Run deployment {{ item }}"
  shell: |
    /usr/libexec/os-refresh-config/configure.d/55-heat-config
    exit $(jq .deploy_status_code /var/lib/heat-config/deployed/{{ deployment_uuid }}.notify.json)
  become: true
  environment:
    HEAT_SHELL_CONFIG: /var/lib/heat-config/tripleo-config-download/{{ item ~ '-' ~ deployment_uuid }}
  register: deployment_sync_result
  ignore_errors: yes
  when: not use_async_deployment

- name: "Run async deployment {{ item }}"
  shell: |
    /usr/libexec/os-refresh-config/configure.d/55-heat-config
    exit $(jq .deploy_status_code /var/lib/heat-config/deployed/{{ deployment_uuid }}.notify.json)
  become: true
  environment:
    HEAT_SHELL_CONFIG: /var/lib/heat-config/tripleo-config-download/{{ item ~ '-' ~ deployment_uuid }}
  register: deployment_async_result
  ignore_errors: yes
  when: use_async_deployment
  async: "{{ async_timeout | default(300) }}"
  poll: "{{ async_poll | default(3) }}"

- name: "Output for sync deployment {{ item }}"
  debug:
    msg:
      - stderr: "{{ deployment_sync_result.stderr.split('\n') }}"
      - status_code: "{{ deployment_sync_result.rc }}"
  tags:
    - output
  failed_when: deployment_sync_result.rc != 0
  when: not ansible_check_mode and not use_async_deployment

- name: "Output for async deployment {{ item }}"
  debug:
    msg:
      - stderr: "{{ deployment_async_result.stderr.split('\n') }}"
      - status_code: "{{ deployment_async_result.rc }}"
  tags:
    - output
  failed_when: deployment_async_result.rc != 0
  when: not ansible_check_mode and use_async_deployment

- name: "Check-mode for Run deployment {{ item }} (changed status indicates deployment would run)"
  stat:
    path: /var/lib/heat-config/deployed/{{ deployment_uuid }}.json
  become: true
  register: deploy_exists
  changed_when: not deploy_exists.stat.exists
  when: ansible_check_mode
  ignore_errors: yes
