From 67d628b22a37274cdee0342690aff56bfd3d44a9 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Fri, 12 Jul 2019 14:44:41 -0400 Subject: [PATCH] Initial Version Signed-off-by: Daniel J Walsh --- BuildSourceImage.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 7 +++++ 2 files changed, 77 insertions(+) create mode 100755 BuildSourceImage.sh diff --git a/BuildSourceImage.sh b/BuildSourceImage.sh new file mode 100755 index 0000000..4d026cf --- /dev/null +++ b/BuildSourceImage.sh @@ -0,0 +1,70 @@ +#!/bin/sh +# 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. +IMAGE=$1 +SRC_RPM_DIR=SRCRPMS +SRC_IMAGE=$1-src +CONTEXT_DIR=$2 +EXTRA_SRC_DIR=$3 +IMAGE_CTR=$(buildah from ${IMAGE}) +IMAGE_MNT=$(buildah mount ${IMAGE_CTR}) +SRC_CTR=$(buildah from scratch) +SRC_MNT=$(buildah mount ${SRC_CTR}) +# +# From the executable image, get the RELEASEVER of the image +# +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 +# +SRC_RPMS=$(rpm -qa --root ${IMAGE_MNT} --queryformat '%{SOURCERPM}\n' | grep -v '(none)' | sort -u) + +# +# Create directory in source container image for RPMS +# +mkdir -p ${SRC_RPM_DIR} ${SRC_MNT}/RPMS +# +# For each SRC_RPMS used to build the executable image, download the SRC RPM +# and generate a layer in the SRC RPM. +# +(cd ${SRC_RPM_DIR}; +set -x +for i in ${SRC_RPMS}; do + if [ ! -f $i ]; then + RPM=$(echo $i | sed 's/.src.rpm$//g') + dnf download --release $RELEASE --source $RPM || continue + fi + cp $i $SRC_MNT/RPMS + buildah commit $SRC_CTR $i +done +) +# +# If the caller specified a context directory, +# add it to the CONTEXT DIR in SRC IMAGE +# +if [ ! -z "${CONTEXT_DIR}" ]; then + cp -R ${CONTEXT_DIR} ${SRC_MNT}/CONTEXT + buildah commit $SRC_CTR $1_$(echo $(CONTEXT_DIR) | sed 's|/|_|g') +fi + +# +# If the caller specified a extra directory, +# add it to the CONTEXT DIR in SRC IMAGE +# +if [ ! -z "${EXTRA_SRC_DIR}" ]; then + cp -R ${EXTRA_SRC_DIR} ${SRC_MNT}/EXTRA + buildah commit $SRC_CTR $1_$(echo $(EXTRA_SRC_DIR) | sed 's|/|_|g') +fi + +# +# Commit the SRC_CTR TO a SRC_IMAGE +# +buildah commit $SRC_CTR $SRC_IMAGE + +# Push SRC_IMAGE to Registry +# buildah push $SRC_IMAGE REGISTRY_NAME/$SRC_IMAGE + +# Cleanup and remove containers +buildah umount ${SRC_CTR} ${IMAGE_CTR} +buildah rm ${SRC_CTR} ${IMAGE_CTR} diff --git a/README.md b/README.md index 86080ca..14d0dbd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ # BuildSourceImage Tool to build a source image based on an existing OCI image + + +This script uses BUILDAH to build source code container images based on +existing OCI images. + +The goal is to make retrieving the source code used to make a container image +easier for users to obtain, using the standard OCI protocols and image formats.