From 2a14088eac05b51e671c8adb094acc89982bba61 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Mon, 10 May 2021 10:39:24 +1000 Subject: [PATCH] containerfile: automatically search for distro docker files This makes the container file element search the active element list for `containerfiles/${DIB_RELEASE}` for building. This makes it easy to write wrappers for ubuntu/fedora/etc. containerfile elements. Change-Id: I68f1d928e54a70bad76985ddd3e156bb5f978b0d --- .../elements/containerfile/README.rst | 9 ++++++-- .../containerfile/root.d/08-containerfile | 21 ++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/diskimage_builder/elements/containerfile/README.rst b/diskimage_builder/elements/containerfile/README.rst index 3ad02729..5fa878c3 100644 --- a/diskimage_builder/elements/containerfile/README.rst +++ b/diskimage_builder/elements/containerfile/README.rst @@ -5,5 +5,10 @@ containerfile Base element for creating images from container files (aka Dockerfiles). -To use this element, create a container file (Dockerfile), and supply -the path to that file in the environment variable `DIB_CONTAINER_FILE`. +Usually this element will be called via a more specific distro element +which provides an environment for building a full image. This element +will search active elements for a container file located in +`containerfiles/${DIB_RELEASE}`. + +Alternatively, to use this element directly supply the path to a +container file in the environment variable `DIB_CONTAINER_FILE`. diff --git a/diskimage_builder/elements/containerfile/root.d/08-containerfile b/diskimage_builder/elements/containerfile/root.d/08-containerfile index 4ea65728..47de3a34 100755 --- a/diskimage_builder/elements/containerfile/root.d/08-containerfile +++ b/diskimage_builder/elements/containerfile/root.d/08-containerfile @@ -25,7 +25,26 @@ if [ -f ${TARGET_ROOT}/.extra_settings ] ; then . ${TARGET_ROOT}/.extra_settings fi -[ -n "$DIB_CONTAINER_FILE" ] +if [ -z "${DIB_CONTAINER_FILE:-}" ]; then + _xtrace=$(set +o | grep xtrace) + set +o xtrace + + eval declare -A image_elements=($(get_image_element_array)) + + for i in "${!image_elements[@]}"; do + element=$i + element_dir=${image_elements[$i]} + + containerfile="${element_dir}/containerfiles/${DIB_RELEASE}" + if [ -f "${containerfile}" ]; then + echo "Found container file ${containerfile}" + DIB_CONTAINER_FILE="${containerfile}" + break + fi + done + + $_xtrace +fi # Use the image cache directory as the default context, so anything # there is automatically available for COPY commands.