---
- name: Reset checkout to clean state  # noqa command-instead-of-module
  ansible.builtin.shell:
    cmd: "git clean -dfx; git reset --hard"
    chdir: "{{ xla_build_dir }}/"
  when: openxla_version == 'git'

- name: Configure default OpenXLA settings
  ansible.builtin.template:
    src: "tf_configure.bazelrc"
    dest: "{{ xla_build_dir }}/.tf_configure.bazelrc"
    mode: 0400
    force: true

- name: (Python {{ py_ver }}) copy upper constraints
  local_action:
    module: ansible.builtin.template
    src: "upper-constraints.txt"
    dest: "{{ build_dir }}/configs/ldcg-python-openxla/upper-constraints.txt"
    mode: 0644  # needs to be readable in container as well
    force: true

- name: (Python {{ py_ver }}) build Python versioned container
  community.docker.docker_image:
    name: "xla-multipython-py{{ py_ver }}-{{ image_prefix }}"
    build:
      path: "{{ build_dir }}/configs/ldcg-python-openxla/"
      args:
        tag_prefix: "{{ image_prefix }}"
        py_major_minor_version: "{{ py_ver }}"
    source: build
    force_source: true

- name: (Python {{ py_ver }}) start build container
  community.docker.docker_container:
    name: "xla-py{{ py_ver }}"
    image: "xla-multipython-py{{ py_ver }}-{{ image_prefix }}"
    state: started
    restart: true
    volumes:
      - "{{ xla_build_dir }}:/tf/xla"
      - "{{ build_dir }}/bazel-cache:/tf/bazel-cache"
    detach: true
    user: buildslave
    working_dir: "/tf/xla"
    network_mode: host
    etc_hosts:
      ldcg-aarch64-02: 172.17.0.1
    command: "sleep infinity"
    timeout: 6000

- name: (Python {{ py_ver }}) create buildslave bazel cache dir
  community.docker.docker_container_exec:
    container: "xla-py{{ py_ver }}"
    command: |
      install -d -m0777 .cache/bazel/_bazel_buildslave
    chdir: "/home/buildslave/"

- name: (Python {{ py_ver }}) create link for bazel cache
  community.docker.docker_container_exec:
    container: "xla-py{{ py_ver }}"
    command: |
      ln -sf /tf/bazel-cache bazel/_bazel_buildslave/cache
    chdir: "/home/buildslave/.cache/"

- name: (Python {{ py_ver }}) create link for bazelisk cache
  community.docker.docker_container_exec:
    container: "xla-py{{ py_ver }}"
    command: |
      ln -sf /tf/bazel-cache bazelisk
    chdir: "/home/buildslave/.cache/"

- name: (Python {{ py_ver }}) create link for pip cache
  community.docker.docker_container_exec:
    container: "xla-py{{ py_ver }}"
    command: |
      ln -sf /tf/cache pip
    chdir: "/home/buildslave/.cache/"

- name: (Python {{ py_ver }}) collect list of installed Python packages for build
  community.docker.docker_container_exec:
    container: "xla-py{{ py_ver }}"
    command: "/usr/local/bin/python3 -m pip list -v"
    chdir: "/tf/xla"
  register: pip_list_output

- name: Show list of installed Python packages for build
  ansible.builtin.debug:
    var: pip_list_output.stdout

- name: (Python {{ py_ver }}) build OpenXLA {{ openxla_version }}
  community.docker.docker_container_exec:
    container: "xla-py{{ py_ver }}"
    command: "bazel build -- //xla/... -//xla/service/gpu/..."
    chdir: "/tf/xla"

- name: (Python {{ py_ver }}) create script to run tests
  ansible.builtin.template:
    src: "run-tests.sh.j2"
    dest: "{{ xla_build_dir }}/run-tests.sh"
    mode: 0755
    force: true

- name: (Python {{ py_ver }}) run OpenXLA {{ openxla_version }} tests
  community.docker.docker_container_exec:
    container: "xla-py{{ py_ver }}"
    command: "bash run-tests.sh"
    chdir: "/tf/xla"
  register: test_output
  ignore_errors: true

- name: (Python {{ py_ver }}) Extract number of tests run
  ansible.builtin.set_fact:
    ut_tests_line: "{{ test_output.stdout | regex_search('^Executed [0-9]+ out of [0-9]+ tests: [0-9]+ tests pass.*', multiline=True) }}"

- name: (Python {{ py_ver }}) Show number of tests run
  ansible.builtin.debug:
    var: ut_tests_line

- name: (Python {{ py_ver }}) Extract failed tests
  ansible.builtin.set_fact:
    tests_failed: "{{ test_output.stderr | regex_findall('FAIL: .*', multiline=True) }}"
    tests_flaky: "{{ test_output.stdout | regex_findall('.*FLAKY.*', multiline=True) }}"

- name: (Python {{ py_ver }}) Show which tests are flaky
  ansible.builtin.debug:
    var: tests_flaky

- name: (Python {{ py_ver }}) Show which tests failed
  ansible.builtin.debug:
    var: tests_failed

- name: (Python {{ py_ver }}) stop build container
  community.docker.docker_container:
    name: "xla-py{{ py_ver }}"
    image: "xla-multipython-py{{ py_ver }}-{{ image_prefix }}"
    state: absent
    timeout: 6000
