From dded8d0853d2b4b97bf8cbfa1392d6ef1437be13 Mon Sep 17 00:00:00 2001 From: Simon Westphahl Date: Thu, 27 Jun 2024 09:28:46 +0200 Subject: [PATCH] Add build-diskimage role Role in order to build a diskimage in a Zuul job. Change-Id: I18756445d41d51b05b3dbb334e43a94637253321 --- doc/source/dib-roles.rst | 1 + roles/build-diskimage/README.rst | 60 +++++++++++++++++ roles/build-diskimage/defaults/main.yaml | 13 ++++ roles/build-diskimage/tasks/main.yaml | 18 ++++++ .../diskimage-builder/build-diskimage.yaml | 17 +++++ zuul-tests.d/diskimage-builder-roles.yaml | 64 +++++++++++++++++++ 6 files changed, 173 insertions(+) create mode 100644 roles/build-diskimage/README.rst create mode 100644 roles/build-diskimage/defaults/main.yaml create mode 100644 roles/build-diskimage/tasks/main.yaml create mode 100644 test-playbooks/diskimage-builder/build-diskimage.yaml diff --git a/doc/source/dib-roles.rst b/doc/source/dib-roles.rst index 7b7b72b4a..ff0876931 100644 --- a/doc/source/dib-roles.rst +++ b/doc/source/dib-roles.rst @@ -2,3 +2,4 @@ Diskimage-Builder Roles ======================= .. zuul:autorole:: ensure-dib +.. zuul:autorole:: build-diskimage diff --git a/roles/build-diskimage/README.rst b/roles/build-diskimage/README.rst new file mode 100644 index 000000000..f05016426 --- /dev/null +++ b/roles/build-diskimage/README.rst @@ -0,0 +1,60 @@ +Role for building images using diskimage-builder. + +Diskimage-builder and diskimage elements can be configured by passing +corresponding settings as environment variables to this role. + +Example: + +.. code-block:: yaml + + roles: + - name: build-diskimage + environment: + ELEMENTS_PATH: /tmp/elements + DIB_PYPI_MIRROR_URL: https://example.com + +**Role variables** + +.. zuul:rolevar:: build_diskimage_command + :default: "{{ ensure_dib_command }}" + + Path to the build-disk-image command. This defaults to + {{ ensure_dib_command }}. as being set by the ensure-dib role. + +.. zuul:rolevar:: build_diskimage_image_name + + Name of the image to build. + +.. zuul:rolevar:: build_diskimage_formats + :type: list + :default: ['qcow2'] + + List of image types to generate. + +.. zuul:rolevar:: build_diskimage_elements + :type: list + :default: ['ubuntu', 'vm'] + + List of elements that should be used when creating the disk image. + +.. zuul:rolevar:: build_diskimage_image_root + :default: "{{ ansible_user_dir }}/dib-images" + + Directory to store the build images. + +.. zuul:rolevar:: build_diskimage_logs_dir + :default: {{ ansible_user_dir }}/zuul-output/logs + + The path where the log output of the diskimage-builder shall be written to + +.. zuul:rolevar:: build_diskimage_retry_limit + :default: 0 + + The number of times the build-diskimage command shall be retried until + successful. + +.. zuul:rolevar:: build_diskimage_retry_delay + :default: 120 + + The number of seconds to wait between retries of the build-diskimage + command. diff --git a/roles/build-diskimage/defaults/main.yaml b/roles/build-diskimage/defaults/main.yaml new file mode 100644 index 000000000..d72daf9ce --- /dev/null +++ b/roles/build-diskimage/defaults/main.yaml @@ -0,0 +1,13 @@ +build_diskimage_command: "{{ ensure_dib_command }}" + +build_diskimage_formats: + - qcow2 + +build_diskimage_elements: + - ubuntu + - vm + +build_diskimage_image_root: "{{ ansible_user_dir }}/dib-images" +build_diskimage_logs_dir: "{{ ansible_user_dir }}/zuul-output/logs" +build_diskimage_retry_limit: 0 +build_diskimage_retry_delay: 120 diff --git a/roles/build-diskimage/tasks/main.yaml b/roles/build-diskimage/tasks/main.yaml new file mode 100644 index 000000000..6a0973941 --- /dev/null +++ b/roles/build-diskimage/tasks/main.yaml @@ -0,0 +1,18 @@ +- name: Ensure DIB image root + file: + path: "{{ build_diskimage_image_root }}" + state: directory + +- name: Build diskimage + command: > + "{{ build_diskimage_command }}" + -o "{{ build_diskimage_image_root }}/{{ build_diskimage_image_name | mandatory }}" + -t "{{ build_diskimage_formats | join(',') }}" + --logfile "{{ build_diskimage_logs_dir }}/diskimage-{{ build_diskimage_image_name }}.log" + {{ build_diskimage_elements | mandatory | join(' ') }} + args: + chdir: "{{ build_diskimage_image_root }}" + register: build_diskimage_result + until: build_diskimage_result is not failed + retries: "{{ build_diskimage_retry_limit }}" + delay: "{{ build_diskimage_retry_delay }}" diff --git a/test-playbooks/diskimage-builder/build-diskimage.yaml b/test-playbooks/diskimage-builder/build-diskimage.yaml new file mode 100644 index 000000000..f8d6f07bd --- /dev/null +++ b/test-playbooks/diskimage-builder/build-diskimage.yaml @@ -0,0 +1,17 @@ +- hosts: all + roles: + - ensure-dib + - role: build-diskimage + build_diskimage_image_name: foo-ubuntu-image + build_diskimage_formats: ["qcow2"] + build_diskimage_elements: ["vm", "ubuntu"] + environment: + DIB_RELEASE: "noble" + tasks: + - name: Gather image file info + stat: + path: "/home/zuul/dib-images/foo-ubuntu-image.qcow2" + register: image_file + - name: Assert image file exists + assert: + that: image_file.stat.exists diff --git a/zuul-tests.d/diskimage-builder-roles.yaml b/zuul-tests.d/diskimage-builder-roles.yaml index f8329b3c6..b29b9bff9 100644 --- a/zuul-tests.d/diskimage-builder-roles.yaml +++ b/zuul-tests.d/diskimage-builder-roles.yaml @@ -57,6 +57,65 @@ - name: ubuntu-noble label: ubuntu-noble +- job: + name: zuul-jobs-test-build-diskimage + description: Test the build-diskimage role + files: + - roles/build-diskimage/.* + - test-playbooks/diskimage-builder/.* + run: test-playbooks/diskimage-builder/build-diskimage.yaml + tags: debuntu-platforms + +- job: + name: zuul-jobs-test-build-diskimage-debian-bookworm + description: Test the build-diskimage role on debian-bookworm + parent: zuul-jobs-test-build-diskimage + tags: auto-generated + nodeset: + nodes: + - name: debian-bookworm + label: debian-bookworm + +- job: + name: zuul-jobs-test-build-diskimage-debian-bullseye + description: Test the build-diskimage role on debian-bullseye + parent: zuul-jobs-test-build-diskimage + tags: auto-generated + nodeset: + nodes: + - name: debian-bullseye + label: debian-bullseye + +- job: + name: zuul-jobs-test-build-diskimage-ubuntu-focal + description: Test the build-diskimage role on ubuntu-focal + parent: zuul-jobs-test-build-diskimage + tags: auto-generated + nodeset: + nodes: + - name: ubuntu-focal + label: ubuntu-focal + +- job: + name: zuul-jobs-test-build-diskimage-ubuntu-jammy + description: Test the build-diskimage role on ubuntu-jammy + parent: zuul-jobs-test-build-diskimage + tags: auto-generated + nodeset: + nodes: + - name: ubuntu-jammy + label: ubuntu-jammy + +- job: + name: zuul-jobs-test-build-diskimage-ubuntu-noble + description: Test the build-diskimage role on ubuntu-noble + parent: zuul-jobs-test-build-diskimage + tags: auto-generated + nodeset: + nodes: + - name: ubuntu-noble + label: ubuntu-noble + - project: check: jobs: &id001 @@ -65,6 +124,11 @@ - zuul-jobs-test-ensure-dib-ubuntu-focal - zuul-jobs-test-ensure-dib-ubuntu-jammy - zuul-jobs-test-ensure-dib-ubuntu-noble + - zuul-jobs-test-build-diskimage-debian-bookworm + - zuul-jobs-test-build-diskimage-debian-bullseye + - zuul-jobs-test-build-diskimage-ubuntu-focal + - zuul-jobs-test-build-diskimage-ubuntu-jammy + - zuul-jobs-test-build-diskimage-ubuntu-noble gate: jobs: *id001 periodic-weekly: