---
- name: Get cluster version
  kubernetes.core.k8s_info:
    api_version: config.openshift.io/v1
    kind: ClusterVersion
    name: version
    kubeconfig: /auth/kubeconfig
  register: cluster_version

- name: Set OCP version fact
  ansible.builtin.set_fact:
    ocp_version: "{{ cluster_version.resources[0].status.desired.version }}"

- name: Create temporary directory for s390x artifacts
  ansible.builtin.tempfile:
    state: directory
    suffix: s390x
  register: temp_dir

- name: Debug temp directory
  ansible.builtin.debug:
    msg: "Using temporary directory: {{ temp_dir.path }}"

- name: ensure container config dir
  file:
    path: /root/.config/containers
    state: directory
    mode: '0700'

- name: create container auth file
  copy:
    dest: /root/.config/containers/auth.json
    content: |
      {
        "auths": {
          "quay.io": {
            "auth": "{{ (((ocp_pull_secret | string | from_json).auths['quay.io'].auth | b64decode))| b64encode }}"
          }
        }
      }
    mode: '0600'

- name: Extract s390x release tools
  ansible.builtin.command:
    cmd: >-
      oc adm release extract
      --command=openshift-install
      quay.io/openshift-release-dev/ocp-release:{{ ocp_version }}-s390x 
      --to={{ temp_dir.path }}
    chdir: "{{ temp_dir.path }}"
  register: extract_result
  changed_when: extract_result.rc == 0

- name: Create directory for ISO
  ansible.builtin.file:
    path: "{{ temp_dir.path }}"
    state: directory
    mode: '0755'

- name: Get release info
  ansible.builtin.command:
    cmd: >-
      oc adm release info 
      quay.io/openshift-release-dev/ocp-release:{{ ocp_version }}-s390x
  register: release_info_all
  changed_when: false

- name: Debug release info
  ansible.builtin.debug:
    msg: "{{ release_info_all.stdout }}"

- name: Get RHCOS image
  ansible.builtin.command:
    cmd: >-
      oc adm release info 
      quay.io/openshift-release-dev/ocp-release:{{ ocp_version }}-s390x
      --image-for=installer-artifacts
  register: release_info
  changed_when: false

- name: Extract RHCOS ISO from release
  ansible.builtin.command:
    cmd: >-
      oc image extract {{ release_info.stdout }}
      --path /artifacts/rhcos-live.s390x.iso:{{ temp_dir.path }}/rhcos-live.s390x.iso
      --registry-config /root/.config/containers/auth.json
    chdir: "{{ temp_dir.path }}"
  register: iso_extract_result
  changed_when: iso_extract_result.rc == 0

- name: Extract openshift-install
  ansible.builtin.unarchive:
    src: "{{ temp_dir.path }}/openshift-install-linux-s390x.tar.gz"
    dest: "{{ temp_dir.path }}"
    remote_src: yes

- name: Install coreos-installer
  ansible.builtin.package:
    name: coreos-installer
    state: present
  become: true

- name: List extracted files
  ansible.builtin.find:
    paths: "{{ temp_dir.path }}"
    recurse: yes
  register: extracted_files

- name: Debug extracted files
  ansible.builtin.debug:
    msg: "Extracted files: {{ extracted_files.files | map(attribute='path') | list }}"

- name: Wait for RHCOS ISO to be available
  ansible.builtin.wait_for:
    path: "{{ temp_dir.path }}/rhcos-live.s390x.iso"
    timeout: 300  # 5 minutes should be enough

- name: Get cluster infrastructure details
  kubernetes.core.k8s_info:
    api_version: config.openshift.io/v1
    kind: Infrastructure
    name: cluster
    kubeconfig: /auth/kubeconfig
  register: cluster_infrastructure

- name: Generate worker ignition config
  ansible.builtin.command:
    cmd: "./openshift-install create worker-ignition"
    chdir: "{{ temp_dir.path }}"
    environment:
      OPENSHIFT_INSTALL_RELEASE_IMAGE_OVERRIDE: "quay.io/openshift-release-dev/ocp-release:{{ ocp_version }}-s390x"

- name: Create ISO for s390x nodes
  ansible.builtin.command:
    cmd: >-
      ./coreos-installer iso customize
      --dest-ignition {{ temp_dir.path }}/worker.ign
      --output {{ temp_dir.path }}/s390x-worker.iso
      {{ temp_dir.path }}/rhcos-live.s390x.iso
  register: customize_result
  changed_when: customize_result.rc == 0
  failed_when: customize_result.rc != 0 