- hosts: compute
  name: setup RDO Trunk repositories
  vars:
    delorean_default: "https://trunk.rdoproject.org/centos7-master/current-passed-ci/delorean.repo"
    delorean_deps_default: "https://trunk.rdoproject.org/centos7-master/delorean-deps.repo"
  tasks:
    - name: Fetch DLRN repo
      get_url:
       url: "{{ delorean | default(delorean_default) }}"
       dest: /etc/yum.repos.d/delorean.repo
       mode: 0640
      become: true

    - name: Fetch DLRN dependency repo
      get_url:
       url: "{{ delorean_deps | default(delorean_deps_default) }}"
       dest: /etc/yum.repos.d/delorean-deps.repo
       mode: 0640
      become: true

    - name: Update installed packages from RDO repos
      package:
        name: '*'
        state: latest
      become: true

    - name: Disable selinux in Compute node
      shell:
        cmd: |
          setenforce 0
      become: true
      when: not selinux_enforcing|default("true")|bool

- hosts: controller
  name: packstack-ssh-preparation-1
  tasks:
    - name: Generate SSH key on controller
      shell: ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ""
      args:
        creates: /root/.ssh/id_rsa
      become: true

    - name: Fetch SSH key
      shell: cat /root/.ssh/id_rsa.pub
      become: true
      register: ssh_key

    - name: Deploy SSH key on controller
      authorized_key:
        user: root
        state: present
        key: "{{ ssh_key.stdout }}"
      become: true

    - name: Set SSH key fact to be used by the compute node
      set_fact:
        sshkey: "{{ ssh_key.stdout }}"

- hosts: compute
  name: packstack-ssh-preparation-2
  tasks:
    - name: Deploy SSH key on compute node
      authorized_key:
        user: root
        state: present
        key: "{{ hostvars['controller'].sshkey }}"
      become: true

- hosts: controller
  name: packstack-multinode
  tasks:
    - name: Run Packstack
      shell:
        cmd: |
          set -e
          set -x
          export SCENARIO='{{ scenario }}'
          export DELOREAN='{{ delorean | default('') }}'
          export DELOREAN_DEPS='{{ delorean_deps | default('') }}'
          export CONTROLLER_NODE="{{ hostvars['controller'].nodepool.public_ipv4 }}"
          export COMPUTE_NODE="{{ hostvars['compute'].nodepool.public_ipv4 }}"
          export SELINUX_ENFORCING='{{ selinux_enforcing | default('true') }}'
          ./run_tests.sh
        executable: /bin/bash
        chdir: '{{ ansible_user_dir }}/workspace/packstack'
