- name: Create Rally home directory
  become: True
  become_user: stack
  file:
    path: '{{ rally_home_dir }}'
    state: directory
    owner: stack
    group: stack

- name: Create directory for Rally results
  become: True
  become_user: stack
  file:
    path: '{{ rally_results_dir }}'
    state: directory
    owner: stack
    group: stack

- name: Create directory for OSProfiler reports
  become: True
  become_user: stack
  file:
    path: '{{ rally_results_dir }}/{{ RALLY_OSPROFILER_CHART }}'
    state: directory
    owner: stack
    group: stack

- name: Extend Rally config with
  become: True
  become_user: stack
  shell:
    executable: /bin/bash
    cmd: |
      echo "[openstack]" >> /etc/rally/rally.conf
      echo "osprofiler_chart_mode={{ RALLY_OSPROFILER_CHART }}" >> /etc/rally/rally.conf

- name: Create a directory for custom plugins
  become: True
  become_user: stack
  file:
    path: '{{ rally_home_dir }}/plugins'
    state: directory
    owner: stack
    group: stack

- name: Check the existence of custom plugins
  stat:
    path: '{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/rally-jobs/plugins'
  register: custom_plugins_stat

- name: Copy custom plugins, if they are presented
  become: True
  become_user: stack
  command: cp -r {{ custom_plugins_stat.stat.path }} {{ rally_home_dir }}/
  when: custom_plugins_stat.stat.exists == True

- name: Create a directory for extra files
  become: True
  become_user: stack
  file:
    path: '{{ rally_home_dir }}/extra'
    state: directory
    owner: stack
    group: stack

- name: Check the existence of extra files
  stat:
    path: '{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/rally-jobs/extra'
  register: extra_files_stat

- name: Copy extra files, if they are presented
  become: True
  become_user: stack
  command: cp -r {{ extra_files_stat.stat.path }} {{ rally_home_dir }}/
  when: extra_files_stat.stat.exists == True

- name: Check the existence of fake image
  stat:
    path: '{{ rally_fake_image_path }}'
  register: fake_image_stat

- name: Create a fake image
  become: True
  become_user: stack
  file:
    path: '{{ rally_fake_image_path }}'
    state: touch
  when: fake_image_stat.stat.exists == False

- name: List glance, cinder, neutron resources
  become: True
  become_user: stack
  shell:
    executable: /bin/bash
    cmd: |
      set -e

      rally deployment use --deployment devstack

      . {{ rally_home_dir }}/openrc admin admin

      OPENSTACK_SERVICES=$(openstack service list)
      if [[ $OPENSTACK_SERVICES == *"glance"* ]]; then
          openstack image list
      fi
      if [[ $OPENSTACK_SERVICES == *"cinder"* ]]; then
          openstack volume list --all-projects
      fi
      if [[ $OPENSTACK_SERVICES == *"neutron"* ]]; then
          openstack network list
      fi

- name: Print Rally environment config
  become: True
  become_user: stack
  command: "rally env show --only-spec"

- include_tasks: prepare-env-with-existing-users.yaml
  when: rally_use_existing_users == True

- name: Check Environment works
  become: True
  become_user: stack
  command: "rally --debug env check"

- name: Print Environment info
  become: True
  become_user: stack
  command: "rally env info"

- name: Create nova flavor
  become: True
  become_user: stack
  shell:
    executable: /bin/sh
    cmd: |
      set -e

      . {{ rally_home_dir }}/openrc admin admin

      if rally deployment check | grep 'nova' | grep 'Available' > /dev/null;
      then
          openstack flavor create --id 42 --ram 64 --disk 0 --vcpus 1 m1.nano
      fi

- name: Check the existence of rally_task
  stat:
    path: '{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/{{ rally_task }}'
  register: rally_task_file_stat

- name: Fail if Rally task file is missed.
  fail:
    msg: "'{{ rally_task }}' Rally task file is missed."
  when: rally_task_file_stat.stat.exists != True

- name: Copy task file
  become: True
  become_user: stack
  command: cp "{{ rally_task_file_stat.stat.path }}" "{{ rally_home_dir }}/task.yaml"
  when: rally_task_file_stat.stat.exists == True

- name: Check the existence of task_args_file
  stat:
    path: '{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/{{ rally_task_args_file }}'
  register: task_args_file_stat

- name: Copy task_args_file
  become: True
  become_user: stack
  command: cp "{{ task_args_file_stat.stat.path }}" "{{ rally_home_dir }}/task_args_file.yaml"
  when: task_args_file_stat.stat.exists == True

- name: Create an empty task_args_file
  become: True
  become_user: stack
  command: echo "{}" > "{{ rally_home_dir }}/task_args_file.yaml"
  when: task_args_file_stat.stat.exists == False
