Skip to content

Commit d8f0961

Browse files
committed
Pull in containerfile element from DIB with 902366
Since [1] is not merged yet. [1]: https://review.opendev.org/c/openstack/diskimage-builder/+/902366
1 parent 0cc7157 commit d8f0961

File tree

10 files changed

+136
-1
lines changed

10 files changed

+136
-1
lines changed

elements/containerfile/README.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=============
2+
containerfile
3+
=============
4+
5+
Base element for creating images from container files (aka
6+
Dockerfiles).
7+
8+
Usually this element will be called via a more specific distro element
9+
which provides an environment for building a full image. This element
10+
will search active elements for a container file located in
11+
``containerfiles/${DIB_RELEASE}``.
12+
13+
Alternatively, to use this element directly supply the path to a
14+
container file in the environment variable
15+
``DIB_CONTAINERFILE_DOCKERFILE``.
16+
17+
Set ``DIB_CONTAINERFILE_RUNTIME`` to ``docker`` to use Docker for building
18+
images (default is ``podman``).
19+
20+
Set ``DIB_CONTAINERFILE_RUNTIME_ROOT`` to ``1`` to run the runtime
21+
(Docker or ``podman``, per above) as ``root``.
22+
23+
Set ``DIB_CONTAINERFILE_NETWORK_DRIVER`` to a network driver of your choice
24+
(e.g. host) to use it instead of the default bridge during build.
25+
26+
Set ``DIB_CONTAINERFILE_BUILDOPTS`` to pass any other options to build command, e.g. ``--from docker.io/library/ubuntu:jammy --build-arg=HTTP_PROXY=http://10.20.30.2:1234``
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
operating-system
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2015 Hewlett-Packard Development Company, L.P.
4+
# Copyright 2019 Red Hat, INC.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7+
# not use this file except in compliance with the License. You may obtain
8+
# a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
# License for the specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
if [ ${DIB_DEBUG_TRACE:-1} -gt 0 ]; then
19+
set -x
20+
fi
21+
set -eu
22+
set -o pipefail
23+
24+
: "${DIB_CONTAINERFILE_RUNTIME:=podman}"
25+
26+
# Convert the old value which was podman specific
27+
if [[ "${DIB_CONTAINERFILE_PODMAN_ROOT:-0}" != '0' ]]; then
28+
DIB_CONTAINERFILE_RUNTIME_ROOT=1
29+
fi
30+
31+
if [[ -z "${DIB_CONTAINERFILE_NETWORK_DRIVER:-}" ]]; then
32+
DIB_CONTAINERFILE_RUNTIME_NETWORK=""
33+
else
34+
DIB_CONTAINERFILE_RUNTIME_NETWORK="--network ${DIB_CONTAINERFILE_NETWORK_DRIVER:-}"
35+
fi
36+
37+
if [ -f ${TARGET_ROOT}/.extra_settings ] ; then
38+
. ${TARGET_ROOT}/.extra_settings
39+
fi
40+
41+
if [ -z "${DIB_CONTAINERFILE_DOCKERFILE:-}" ]; then
42+
_xtrace=$(set +o | grep xtrace)
43+
set +o xtrace
44+
45+
eval declare -A image_elements=($(get_image_element_array))
46+
47+
for i in "${!image_elements[@]}"; do
48+
element=$i
49+
element_dir=${image_elements[$i]}
50+
51+
containerfile="${element_dir}/containerfiles/${DIB_RELEASE}"
52+
if [ -f "${containerfile}" ]; then
53+
echo "Found container file ${containerfile}"
54+
DIB_CONTAINERFILE_DOCKERFILE="${containerfile}"
55+
break
56+
fi
57+
done
58+
59+
$_xtrace
60+
61+
if [ -z "${DIB_CONTAINERFILE_DOCKERFILE:-}" ]; then
62+
echo "*** DIB_CONTAINERFILE_DOCKERFILE not specified or found!"
63+
exit 1
64+
fi
65+
fi
66+
67+
# Use the image cache directory as the default context, so anything
68+
# there is automatically available for COPY commands.
69+
DIB_CONTAINER_CONTEXT=${DIB_CONTAINER_CONTEXT:-${DIB_IMAGE_CACHE}/containerfile}
70+
71+
mkdir -p $DIB_CONTAINER_CONTEXT
72+
73+
if [[ ${DIB_CONTAINERFILE_RUNTIME_ROOT:-0} -gt 0 ]]; then
74+
_sudo="sudo"
75+
else
76+
_sudo=""
77+
fi
78+
79+
_podman_build_image="dib-tmp-work-image-$RANDOM"
80+
_podman_export_container="dib-tmp-export-$RANDOM"
81+
82+
function podman_cleanup() {
83+
echo "Cleaning up container ${_podman_export_container}"
84+
${_sudo} ${DIB_CONTAINERFILE_RUNTIME} rm ${_podman_export_container} || true
85+
echo "Cleaning up build image ${_podman_build_image}"
86+
${_sudo} ${DIB_CONTAINERFILE_RUNTIME} rmi ${_podman_build_image} || true
87+
}
88+
89+
trap "podman_cleanup" EXIT
90+
91+
${_sudo} ${DIB_CONTAINERFILE_RUNTIME} build ${DIB_CONTAINERFILE_RUNTIME_NETWORK} -t ${_podman_build_image} -f $DIB_CONTAINERFILE_DOCKERFILE ${DIB_CONTAINERFILE_BUILDOPTS:-} $DIB_CONTAINER_CONTEXT
92+
${_sudo} ${DIB_CONTAINERFILE_RUNTIME} run ${DIB_CONTAINERFILE_RUNTIME_NETWORK} --name ${_podman_export_container} -d ${_podman_build_image} /bin/sh
93+
# NOTE(ianw) 2021-11-10 the tar must always be sudo to write out the chroot files
94+
# as other uids
95+
${_sudo} ${DIB_CONTAINERFILE_RUNTIME} export ${_podman_export_container} | sudo tar -C $TARGET_ROOT --numeric-owner -xf -
96+
97+
sudo rm -f ${TARGET_ROOT}/.extra_settings
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Verify we can build an image from a containerfile.
2+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
openstack-ci-mirrors
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# For the openstack-ci-mirrors element
2+
export DISTRO_NAME=ubuntu
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
2+
path="$( dirname $path)"
3+
export DIB_CONTAINERFILE_DOCKERFILE="$path/files/Dockerfile"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM docker.io/library/ubuntu:jammy
2+
RUN touch /testfile
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tar

elements/rocky-container-stackhpc/containerfiles/9-stackhpc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ RUN dnf group install -y 'Minimal Install' --allowerasing && \
1616
dnf install -y findutils util-linux \
1717
https://mirror.stream.centos.org/9-stream/AppStream/x86_64/os/Packages/cloud-init-23.1.1-12.el9.noarch.rpm
1818

19-
RUN rm -f /etc/yum.repos.d/stackhpc.repo && \
19+
RUN rm -f /etc/yum.repos.d/* && \
2020
mv -f /tmp/orig_repos/* /etc/yum.repos.d/ && \
2121
rmdir /tmp/orig_repos
2222

0 commit comments

Comments
 (0)