---
- hosts: all
  vars:
    kolla_inventory_path: "{{ zuul.project.src_dir }}/inventory"
    nodepool_rdo_proxy: "{{ zuul_site_mirror_fqdn }}:8080/rdo"
    nodepool_pypi_mirror: "http://{{ zuul_site_mirror_fqdn }}:8080/pypi/simple"
    nodepool_mirror_host: "{{ zuul_site_mirror_fqdn }}"
    nodepool_npmjs_proxy: "http://{{ zuul_site_mirror_fqdn }}:8080/registry.npmjs/"
    nodepool_elastic_proxy: "http://{{ zuul_site_mirror_fqdn }}:8080/elastic/"
    nodepool_grafana_proxy: "http://{{ zuul_site_mirror_fqdn }}:8080/grafana/"
    nodepool_opendaylight_proxy: "http://{{ zuul_site_mirror_fqdn }}:8080/opendaylight/"
    nodepool_oraclelinux_proxy: "http://{{ zuul_site_mirror_fqdn }}:8080/oraclelinux/"
  tasks:
    - name: generate kolla inventory file
      copy:
        dest: "{{ kolla_inventory_path }}"
        content: |
          {% for host in hostvars %}
          {{ host }} ansible_host={{ hostvars[host]['ansible_host'] }} ansible_user={{ hostvars[host]['ansible_user'] }}
          {% endfor %}

    - name: setup logging
      shell:
        cmd: |
          mkdir logs
          ln -s $(pwd)/logs /tmp/logs
          mkdir -p /tmp/logs/{ansible,build,kolla,kolla_configs,system_logs}
        executable: /bin/bash
        chdir: "{{ zuul.project.src_dir }}"

    - name: copy setup script
      copy:
        src: "{{ zuul.executor.work_root }}/{{ zuul.project.src_dir }}/tools/setup_{{ ansible_os_family }}.sh"
        dest: /tmp/setup.sh
        mode: 0755

    - name: run node setup script
      shell: /tmp/setup.sh
      become: true

    - name: changing permission of Docker socket to 666
      file:
        path: /run/docker.sock
        mode: 666
      become: true

    - name: Ensure /etc/kolla exists
      file:
        path: /etc/kolla
        state: directory
        mode: 0777
      become: true

    - name: Template kolla-build.conf
      template:
        src: "{{ zuul.executor.work_root }}/{{ zuul.project.src_dir }}/tests/templates/kolla-build.conf.j2"
        dest: /etc/kolla/kolla-build.conf

    - name: Template override
      template:
        src: "{{ zuul.executor.work_root }}/{{ zuul.project.src_dir }}/tests/templates/template_overrides.j2"
        dest: /etc/kolla/template_overrides.j2

    - shell:
        cmd: |
          set -e
          set -x

          export ACTION={{ action }}
          export BASE_DISTRO={{ base_distro }}
          export INSTALL_TYPE={{ install_type }}
          export IN_PROJECT_JOBS=1
          tools/gate_run.sh
        executable: /bin/bash
        chdir: "{{ zuul.project.src_dir }}"
      environment: '{{ zuul | zuul_legacy_vars }}'