2019-07-12 18:44:41 +00:00
|
|
|
#!/bin/sh
|
2019-07-15 19:07:51 +00:00
|
|
|
|
2019-07-12 18:44:41 +00:00
|
|
|
# This script requires an OCI IMAGE Name to pull.
|
|
|
|
# The script generates a SOURCE Image based on the OCI Image
|
|
|
|
# Script must be executed on the same OS or newer as the image.
|
2019-07-31 23:19:47 +00:00
|
|
|
if test $# -lt 2 ; then
|
|
|
|
echo Usage: $(basename $0) IMAGE CONTEXT_DIR [EXTRA_SRC_DIR]
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
export IMAGE=$1
|
|
|
|
export SRC_RPM_DIR=$(pwd)/SRCRPMS
|
|
|
|
export SRC_IMAGE=$1-src
|
|
|
|
export CONTEXT_DIR=$2
|
|
|
|
export EXTRA_SRC_DIR=$3
|
|
|
|
export IMAGE_CTR=$(buildah from ${IMAGE})
|
|
|
|
export IMAGE_MNT=$(buildah mount ${IMAGE_CTR})
|
2019-07-12 18:44:41 +00:00
|
|
|
#
|
|
|
|
# From the executable image, get the RELEASEVER of the image
|
2019-07-15 19:07:51 +00:00
|
|
|
#
|
2019-07-12 18:44:41 +00:00
|
|
|
RELEASE=$(rpm -q --queryformat "%{VERSION}\n" --root $IMAGE_MNT -f /etc/os-release)
|
|
|
|
#
|
|
|
|
# From the executable image, list the SRC RPMS used to build the image
|
2019-07-15 19:07:51 +00:00
|
|
|
#
|
2019-07-15 08:53:50 +00:00
|
|
|
SRC_RPMS=$(rpm -qa --root ${IMAGE_MNT} --queryformat '%{SOURCERPM}\n' | grep -v '^gpg-pubkey' | sort -u)
|
2019-07-15 19:07:51 +00:00
|
|
|
buildah umount ${IMAGE_CTR}
|
|
|
|
buildah rm ${IMAGE_CTR}
|
2019-07-12 18:44:41 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# For each SRC_RPMS used to build the executable image, download the SRC RPM
|
|
|
|
# and generate a layer in the SRC RPM.
|
2019-07-15 19:07:51 +00:00
|
|
|
#
|
2019-07-31 23:19:47 +00:00
|
|
|
mkdir -p ${SRC_RPM_DIR}
|
|
|
|
pushd ${SRC_RPM_DIR} > /dev/null
|
2019-07-15 19:07:51 +00:00
|
|
|
export SRC_CTR=$(buildah from scratch)
|
2019-07-12 18:44:41 +00:00
|
|
|
for i in ${SRC_RPMS}; do
|
|
|
|
if [ ! -f $i ]; then
|
2019-07-15 19:07:51 +00:00
|
|
|
RPM=$(echo $i | sed 's/.src.rpm$//g')
|
|
|
|
dnf download --release $RELEASE --source $RPM || continue
|
2019-07-12 18:44:41 +00:00
|
|
|
fi
|
2019-07-15 19:07:51 +00:00
|
|
|
echo "Adding $i"
|
2019-07-31 23:19:47 +00:00
|
|
|
touch --date=@`rpm -q --qf '%{buildtime}' $i` $i
|
|
|
|
buildah add ${SRC_CTR} $i /RPMS/
|
|
|
|
buildah config --created-by "/bin/sh -c #(nop) ADD file:$(sha256sum $i | cut -f1 -d' ') in /RPMS" ${SRC_CTR}
|
|
|
|
export IMG=$(buildah commit --omit-timestamp --disable-compression --rm ${SRC_CTR})
|
2019-07-15 19:07:51 +00:00
|
|
|
export SRC_CTR=$(buildah from ${IMG})
|
2019-07-12 18:44:41 +00:00
|
|
|
done
|
2019-07-31 23:19:47 +00:00
|
|
|
popd > /dev/null
|
2019-07-12 18:44:41 +00:00
|
|
|
#
|
|
|
|
# If the caller specified a context directory,
|
|
|
|
# add it to the CONTEXT DIR in SRC IMAGE
|
|
|
|
#
|
|
|
|
if [ ! -z "${CONTEXT_DIR}" ]; then
|
2019-07-31 23:19:47 +00:00
|
|
|
CONTEXT_DIR=$(cd ${CONTEXT_DIR}; pwd)
|
|
|
|
buildah add ${SRC_CTR} ${CONTEXT_DIR} /CONTEXT
|
|
|
|
buildah config --created-by "/bin/sh -c #(nop) ADD file:$(cd ${CONTEXT_DIR}; tar cf - . | sha256sum -| cut -f1 -d' ') in /CONTEXT" ${SRC_CTR}
|
|
|
|
export IMG=$(buildah commit --omit-timestamp --rm ${SRC_CTR})
|
2019-07-15 19:07:51 +00:00
|
|
|
export SRC_CTR=$(buildah from ${IMG})
|
2019-07-12 18:44:41 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
#
|
|
|
|
# If the caller specified a extra directory,
|
|
|
|
# add it to the CONTEXT DIR in SRC IMAGE
|
|
|
|
#
|
|
|
|
if [ ! -z "${EXTRA_SRC_DIR}" ]; then
|
2019-07-31 23:19:47 +00:00
|
|
|
buildah add ${SRC_CTR} ${EXTRA_SRC_DIR} /EXTRA
|
|
|
|
buildah config --created-by "/bin/sh -c #(nop) ADD file:$(cd ${EXTRA_SRC_DIR}; tar cf - . | sha256sum -| cut -f1 -d' ') in /CONTEXT" ${SRC_CTR}
|
|
|
|
export IMG=$(buildah commit --omit-timestamp --rm ${EXTRA_SRC_CTR})
|
2019-07-15 19:07:51 +00:00
|
|
|
export SRC_CTR=$(buildah from ${IMG})
|
2019-07-12 18:44:41 +00:00
|
|
|
fi
|
|
|
|
|
2019-07-15 19:07:51 +00:00
|
|
|
# Cleanup and remove source container
|
|
|
|
buildah rm ${SRC_CTR}
|
|
|
|
|
2019-07-12 18:44:41 +00:00
|
|
|
#
|
2019-07-31 23:19:47 +00:00
|
|
|
# Add the final name to our image
|
2019-07-12 18:44:41 +00:00
|
|
|
#
|
2019-07-15 19:07:51 +00:00
|
|
|
buildah tag $IMG $SRC_IMAGE
|
2019-07-12 18:44:41 +00:00
|
|
|
|
|
|
|
# Push SRC_IMAGE to Registry
|
|
|
|
# buildah push $SRC_IMAGE REGISTRY_NAME/$SRC_IMAGE
|