---
- name: Find Cirros partition image
  shell: openstack image list -f value -c Name | grep 'cirros-.*-partition$' | sort | tail -n1
  register: cirros_partition_image_result
  failed_when: cirros_partition_image_result.stdout == ""

- name: Find Cirros disk image
  shell: openstack image list -f value -c Name | grep 'cirros-.*-disk$' | sort | tail -n1
  register: cirros_disk_image_result
  failed_when: cirros_disk_image_result.stdout == ""

- name: Set image facts for Glance image
  set_fact:
    metalsmith_whole_disk_image: "{{ cirros_disk_image_result.stdout }}"
    metalsmith_partition_image: "{{ cirros_partition_image_result.stdout }}"
  when: not (metalsmith_use_http | default(false))

- block:
    - name: Find Cirros UEC image
      shell: openstack image list -f value -c Name | grep 'cirros-.*-uec$' | sort | tail -n1
      register: cirros_uec_image_result
      failed_when: cirros_uec_image_result.stdout == ""

    - name: Get baremetal HTTP endpoint
      shell: |
        source /opt/stack/devstack/openrc admin admin > /dev/null
        iniget /etc/ironic/ironic.conf deploy http_url
      args:
        executable: /bin/bash
      register: baremetal_endpoint_result
      failed_when: baremetal_endpoint_result.stdout == ""

    - name: Copy UEC images directory
      command: >
        cp -r /opt/stack/devstack/files/images/{{ cirros_uec_image_result.stdout }}
            /opt/stack/data/ironic/httpboot/metalsmith
      args:
        creates: /opt/stack/data/ironic/httpboot/metalsmith
      become: yes

    - name: Copy whole disk image
      command: >
        cp /opt/stack/devstack/files/{{ cirros_disk_image_result.stdout }}.img
            /opt/stack/data/ironic/httpboot/metalsmith/
      args:
        creates: /opt/stack/data/ironic/httpboot/metalsmith/{{ cirros_disk_image_result.stdout }}.img
      become: yes

    - name: Copy partition image
      command: >
        cp /opt/stack/data/ironic/{{ cirros_partition_image_result.stdout }}.img
            /opt/stack/data/ironic/httpboot/metalsmith
      args:
        creates: /opt/stack/data/ironic/httpboot/metalsmith/{{ cirros_partition_image_result.stdout }}.img
      become: yes

    - name: Create MD5 checksums file for images
      shell: md5sum cirros-* > CHECKSUMS
      args:
        chdir: /opt/stack/data/ironic/httpboot/metalsmith
      become: yes

    - name: Change ownership of image files
      file:
        path: /opt/stack/data/ironic/httpboot/metalsmith
        state: directory
        owner: "{{ ansible_user }}"
        recurse: yes
        mode: a+r
      become: yes

    - name: Calculate MD5 checksum for HTTP disk image
      shell: |
          md5sum /opt/stack/devstack/files/{{ cirros_disk_image_result.stdout }}.img \
              | awk '{ print $1; }'
      register: cirros_disk_image_checksum_result
      failed_when: cirros_disk_image_checksum_result.stdout == ""

    - name: Set facts for HTTP image
      set_fact:
        metalsmith_partition_image: "{{ baremetal_endpoint_result.stdout}}/metalsmith/{{ cirros_partition_image_result.stdout }}.img"
        metalsmith_partition_kernel_image: "{{ baremetal_endpoint_result.stdout}}/metalsmith/{{ cirros_uec_image_result.stdout | replace('-uec', '-vmlinuz') }}"
        metalsmith_partition_ramdisk_image: "{{ baremetal_endpoint_result.stdout}}/metalsmith/{{ cirros_uec_image_result.stdout | replace('-uec', '-initrd') }}"
        metalsmith_partition_checksum: "{{ baremetal_endpoint_result.stdout}}/metalsmith/CHECKSUMS"
        metalsmith_whole_disk_image: "{{ baremetal_endpoint_result.stdout}}/metalsmith/{{ cirros_disk_image_result.stdout }}.img"
        metalsmith_whole_disk_checksum: "{{ cirros_disk_image_checksum_result.stdout }}"

  when: metalsmith_use_http | default(false)