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

- name: Extract s390x release tools
  ansible.builtin.command:
    cmd: "oc adm release extract --tools quay.io/openshift-release-dev/ocp-release:{{ ocp_version }}-s390x"
    chdir: "{{ temp_dir.path }}"

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

- name: Generate worker ignition config
  ansible.builtin.command:
    cmd: "openshift-install create worker-ignition"
    chdir: "{{ temp_dir.path }}"

- 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

- name: Configure LPAR using HMC
  community.ibm_zos_core.zhmc_partition:  # This is a hypothetical module name
    hmc_host: "{{ hmc_host }}"
    hmc_auth: "{{ hmc_auth }}"
    cpc: "{{ cpc_name }}"
    name: "{{ inventory_hostname }}"
    state: present
    properties:
      memory: "{{ memory_mb }}"
      processor_count: "{{ cpu_count }}"
      boot_device: cdrom

- name: Mount ISO to LPAR
  community.ibm_zos_core.zhmc_virtual_media:  # This is a hypothetical module name
    hmc_host: "{{ hmc_host }}"
    hmc_auth: "{{ hmc_auth }}"
    cpc: "{{ cpc_name }}"
    partition: "{{ inventory_hostname }}"
    iso_path: "{{ temp_dir.path }}/s390x-worker.iso"
    state: mounted

- name: Activate LPAR
  community.ibm_zos_core.zhmc_partition:  # This is a hypothetical module name
    hmc_host: "{{ hmc_host }}"
    hmc_auth: "{{ hmc_auth }}"
    cpc: "{{ cpc_name }}"
    name: "{{ inventory_hostname }}"
    state: active

- name: Wait for node to join cluster
  kubernetes.core.k8s_info:
    api_version: v1
    kind: Node
    name: "{{ inventory_hostname }}"
  register: node_status
  until: node_status.resources | length > 0
  retries: 60
  delay: 30
