- name: Change the status message
  become: yes
  shell:
    cmd: |
      set -e
      if [ -f "{{ rally_results_dir }}/status.txt" ]; then
        MSG=`cat {{ rally_results_dir }}/status.txt`
      else
        MSG="Unexpected error had happened before Rally Task start. Check ARA for more details."
      fi

      if [ "$MSG" != "OK" ]; then
        sed -i -- "s/var errorMessage = null\;/var errorMessage = \'$MSG\';/g" {{ zuul.projects['opendev.org/openstack/rally-openstack'].src_dir }}/tests/ci/pages/task-index.html
      fi

- name: Check the existence of results dir
  stat:
    path: '{{ rally_results_dir }}'
  register: results_dir_stat

- name: Gzip custom plugins
  become: yes
  become_user: stack
  shell: "tar -czf {{ rally_results_dir }}/plugins.tar.gz ./plugins"
  args:
    chdir: '{{ rally_home_dir }}'
  when: results_dir_stat.stat.exists == True

- name: Copy Rally results
  become: yes
  synchronize:
    src: '{{ rally_results_dir }}'
    dest: '{{ zuul.executor.log_root }}'
    mode: pull
    copy_links: true
    verify_host: true
    rsync_opts:
      - --include=/**
      - --include=*/
      - --exclude=*
      - --prune-empty-dirs
  when: results_dir_stat.stat.exists == True

- name: Put index.html in the right place
  become: yes
  synchronize:
    src: "{{ zuul.projects['opendev.org/openstack/rally-openstack'].src_dir }}/tests/ci/pages/task-index.html"
    dest: '{{ zuul.executor.log_root }}/index.html'
    mode: pull
    copy_links: true
    verify_host: true
    rsync_opts:
      - --include=/**
      - --include=*/
      - --exclude=*
      - --prune-empty-dirs

- name: Return artifact to Zuul
  zuul_return:
    data:
      zuul:
        artifacts:
          - name: "Rally CI report"
            url: "index.html"
