---
# Build and install overcloud host disk images for the seed host's ironic
# service.

- name: Ensure overcloud host disk images are built and installed
  hosts: seed
  tags:
    - overcloud-host-image-build
  vars:
    overcloud_host_image_force_rebuild: False
  tasks:
    - block:
        - name: Validate overcloud host disk image configuration
          assert:
            that:
              - overcloud_dib_host_images is sequence
              - overcloud_dib_host_images | selectattr('name', 'undefined') | list | length == 0
              - overcloud_dib_host_images | selectattr('elements', 'undefined') | list | length == 0
            msg: "overcloud_dib_host_images set to invalid value"

        - name: Ensure overcloud host disk images are built
          include_role:
            name: stackhpc.os-images
          vars:
            os_images_venv: "{{ virtualenv_path }}/overcloud-host-image-dib"
            os_images_package_state: latest
            os_images_upper_constraints_file: "{{ overcloud_dib_upper_constraints_file }}"
            os_images_cache: "{{ image_cache_path }}"
            os_images_common: ""
            os_images_list: "{{ overcloud_dib_host_images }}"
            os_images_git_elements: "{{ overcloud_dib_git_elements }}"
            os_images_upload: False
            os_images_force_rebuild: "{{ overcloud_host_image_force_rebuild }}"

        - name: Copy overcloud host disk images into /httpboot
          copy:
            src: "{{ image_cache_path }}/{{ image.name }}/{{ image.name }}.{{ image.type | default('qcow2') }}"
            dest: "/var/lib/docker/volumes/bifrost_httpboot/_data/{{ image.name }}.{{ image.type | default('qcow2') }}"
            remote_src: True
          with_items: "{{ overcloud_dib_host_images }}"
          loop_control:
            loop_var: image
          become: True
      when: overcloud_dib_build_host_images | bool
