- hosts: all
  name: ironic-inspector-tempest-dsvm-python3
  tasks:

    - name: Ensure workspace directory
      file:
        path: '{{ ansible_user_dir }}/workspace'
        state: directory

    - shell:
        cmd: |
          set -e
          set -x
          cat > clonemap.yaml << EOF
          clonemap:
            - name: openstack-infra/devstack-gate
              dest: devstack-gate
          EOF
          /usr/zuul-env/bin/zuul-cloner -m clonemap.yaml --cache-dir /opt/git \
              git://git.openstack.org \
              openstack-infra/devstack-gate
        executable: /bin/bash
        chdir: '{{ ansible_user_dir }}/workspace'
      environment: '{{ zuul | zuul_legacy_vars }}'

    - shell:
        cmd: |
          cat << 'EOF' >> ironic-extra-vars
            export DEVSTACK_GATE_USE_PYTHON3=True

          EOF
        chdir: '{{ ansible_user_dir }}/workspace'
      environment: '{{ zuul | zuul_legacy_vars }}'

    - shell:
        cmd: |
          set -e
          set -x
          cat << 'EOF' >>"/tmp/dg-local.conf"
          [[local|localrc]]
          disable_service s-account
          disable_service s-container
          disable_service s-object
          disable_service s-proxy

          EOF
        executable: /bin/bash
        chdir: '{{ ansible_user_dir }}/workspace'
      environment: '{{ zuul | zuul_legacy_vars }}'

    - shell:
        cmd: |
          cat << 'EOF' >> ironic-extra-vars
            export DEVSTACK_GATE_TEMPEST_REGEX="Inspector"
          EOF
        chdir: '{{ ansible_user_dir }}/workspace'
      environment: '{{ zuul | zuul_legacy_vars }}'

    - shell:
        cmd: |
          cat << 'EOF' >> ironic-vars-early
            # use tempest plugin
            export DEVSTACK_LOCAL_CONFIG+=$'\n'"TEMPEST_PLUGINS+=' /opt/stack/new/ironic-tempest-plugin'"
            export TEMPEST_CONCURRENCY=1
          EOF
        chdir: '{{ ansible_user_dir }}/workspace'
      environment: '{{ zuul | zuul_legacy_vars }}'

    - shell:
        cmd: |
          cat << 'EOF' >> ironic-extra-vars
          export IRONIC_INSPECTOR_AUTO_DISCOVERY=1
          if [ "$IRONIC_INSPECTOR_AUTO_DISCOVERY" == "1" ]; then
               # discovery test requires sudo for iptables and virsh
               export DEVSTACK_GATE_REMOVE_STACK_SUDO=0
               # enable enroll hook
               export DEVSTACK_LOCAL_CONFIG+=$'\n'"IRONIC_INSPECTOR_NODE_NOT_FOUND_HOOK=enroll"
               # we are deleting node from ironic for simulate node discovery,
               # so inspector has to sync cache asap
               export DEVSTACK_LOCAL_CONFIG+=$'\n'"IRONIC_INSPECTOR_CLEAN_UP_PERIOD=5"
          fi

          # PXE Filter Driver
          export DEVSTACK_LOCAL_CONFIG+=$'\n'"IRONIC_INSPECTOR_DHCP_FILTER=iptables"

          EOF
        chdir: '{{ ansible_user_dir }}/workspace'
      environment: '{{ zuul | zuul_legacy_vars }}'

    - shell:
        cmd: |
          set -e
          set -x
          export PYTHONUNBUFFERED=true
          export DEVSTACK_GATE_TEMPEST=1
          export DEVSTACK_GATE_IRONIC=1
          export DEVSTACK_GATE_IRONIC_INSPECTOR=1
          export DEVSTACK_GATE_NEUTRON=1
          export DEVSTACK_GATE_VIRT_DRIVER=ironic
          export DEVSTACK_GATE_CONFIGDRIVE=1

          export BRANCH_OVERRIDE="{{ zuul.override_checkout | default('default') }}"
          if [ "$BRANCH_OVERRIDE" != "default" ] ; then
              export OVERRIDE_ZUUL_BRANCH=$BRANCH_OVERRIDE
          fi

          export PROJECTS="openstack/ironic openstack/ironic-inspector $PROJECTS"
          export PROJECTS="openstack/ironic-tempest-plugin $PROJECTS"
          export DEVSTACK_LOCAL_CONFIG="enable_plugin ironic git://git.openstack.org/openstack/ironic"
          export DEVSTACK_LOCAL_CONFIG+=$'\n'"enable_plugin ironic-inspector https://git.openstack.org/openstack/ironic-inspector"
          export DEVSTACK_LOCAL_CONFIG+=$'\n'"IRONIC_AUTOMATED_CLEAN_ENABLED=False"

          export DEVSTACK_LOCAL_CONFIG+=$'\n'"IRONIC_BUILD_DEPLOY_RAMDISK=False"
          # IPA requires at least 1 GiB of RAM
          export DEVSTACK_LOCAL_CONFIG+=$'\n'"IRONIC_VM_SPECS_RAM=1024"$'\n'"IRONIC_VM_COUNT=1"

          # Ensure the ironic-vars-EARLY file exists
          touch ironic-vars-early
          # Pull in the EARLY variables injected by the optional builders
          source ironic-vars-early

          # Ensure the ironic-EXTRA-vars file exists
          touch ironic-extra-vars
          # Pull in the EXTRA variables injected by the optional builders
          source ironic-extra-vars

          cp devstack-gate/devstack-vm-gate-wrap.sh ./safe-devstack-vm-gate-wrap.sh
          ./safe-devstack-vm-gate-wrap.sh
        executable: /bin/bash
        chdir: '{{ ansible_user_dir }}/workspace'
      environment: '{{ zuul | zuul_legacy_vars }}'
