File kiwi_metainfo_helper of Package obs-service-kiwi_metainfo_helper.38628

#!/bin/bash
# Not using -o pipefail, as SIGPIPE is annoying to deal with
set -eu
shopt -s nullglob

if [ "${BUILD_DIST+x}" != "x" ]; then
	echo "Not running in an OBS build container"
	exit 1
fi

BUILD_DATA="${BUILD_DIST/.dist/.data}"
if [ -e "${BUILD_DATA}" ]; then
	. "${BUILD_DATA}"
	BUILD_ARCH="${BUILD_ARCH%%:*}"

	# The build script renames the recipe (to strip _service:foo:), but doesn't update .data
	RECIPEFILE="${RECIPEFILE##*:}"

	if [ "${RECIPEFILE##*.}" != "kiwi" ] && [[ ! "${RECIPEFILE}" =~ ^Dockerfile.* ]] && [ "${RECIPEFILE}" != "Chart.yaml" ]; then
		echo "Recipe ${RECIPEFILE} is neither Dockerfile, kiwi recipe nor helm chart - exiting"
		exit 0
	fi

	files=("${RECIPEFILE}")

	# process also the config.sh script in Kiwi builds
	if [ "${RECIPEFILE##*.}" == "kiwi" ] && [ -f "config.sh" ]; then
		files+=("config.sh")
	fi
else
	echo "Warning: No build data found - chroot build?"
	DISTURL="local"
	RELEASE=0
	BUILD_ARCH="noarch"

	# Guess the build recipe
	files=(*.kiwi Dockerfile* Chart.yaml*)
	if [ "${#files}" -eq 0 ]; then
		echo "No kiwi recipe, Dockerfile or helm chart found - exiting"
		exit 0
	fi

	# process also the config.sh script in Kiwi builds
	for kiwi_file in *.kiwi; do 
		if [ -f "$kiwi_file" ]; then
			if [ -f "config.sh" ]; then
				files+=("config.sh")
			fi
			break
		fi
	done
fi

# generate %SOURCEURL% based on DISTURL with a special case for build.suse.de
prj=$(echo ${DISTURL} | cut -d/ -f4)
localpath=$(echo ${DISTURL} | cut -d/ -f6-)
rev=$(echo ${localpath} | cut -d- -f1)
packagename=$(echo ${localpath} | cut -d- -f2- | cut -d: -f1)
if [[ "${DISTURL}" == obs://build.suse.de/* ]]; then
	SOURCEURL="https://sources.suse.com/${prj}/${packagename}/${rev}/"
	SOURCEURL_PLACEHOLDER="${SOURCEURL}\\1"
else
	SOURCEURL="https://$(echo ${DISTURL} | cut -d/ -f3)/package/show/${prj}/${packagename}?rev=${rev}"
	SOURCEURL_PLACEHOLDER="https://$(echo ${DISTURL} | cut -d/ -f3)/public/source/${prj}/${packagename}/\\1?rev=${rev}"
fi

# Print all rpm files which contain os-release
find_release_rpms() {
	find ./repos -name \*-release\*.rpm | while read rpm; do
		if rpm -qlp "${rpm}" | grep -qE '^(/etc/os-release|/usr/lib/os-release)$'; then
			echo "${rpm}"
		fi
	done
}

if grep -q "%OS_" ${files[@]}; then
	# Needs os-release, search for RPMs
	relpkgs=($(find_release_rpms))

	if [ ${#relpkgs[@]} -lt 1 ]; then
		echo "No release package found, but recipe uses %OS_*% placeholders"
		exit 1
	fi

	if [ ${#relpkgs[@]} -gt 1 ]; then
		echo "Multiple release packages found, don't know which os-release to use"
		exit 1
	fi

	# Extract the content
	tempdir=$(mktemp -d)
	trap "rm -r ${tempdir}" EXIT
	rpm2cpio "${relpkgs[0]}" | (cd "${tempdir}" && cpio -id --quiet)

	# And source it
	[ -f "${tempdir}/usr/lib/os-release" ] && . "${tempdir}/usr/lib/os-release"
	[ -f "${tempdir}/etc/os-release" ] && . "${tempdir}/etc/os-release"

	VERSION="${VERSION:-}"
	VERSION_NO_DASH="${VERSION/-/ }"
	# Special case for SLE X "SP 0", make sure it has .0
	VERSION_ID_SP="${VERSION_ID}"
	[[ "${VERSION_ID_SP%}" == *"."* ]] || VERSION_ID_SP="${VERSION_ID}.0"

	# Provide various modified spellings of PRETTY_NAME useful for writing
	# KIWI image definitions with reduced diff between SLE, Leap and Tumbleweed:
	#
	# VENDOR:                          openSUSE
	# PRETTY_NAME_DASHED:              openSUSE-Leap-15.3
	# PRETTY_NAME_BEFORE_PAREN:        openSUSE Leap 15.3
	# PRETTY_NAME_BEFORE_PAREN_DASHED: openSUSE-Leap-15.3
	#
	# VENDOR:                          SUSE
	# PRETTY_NAME_DASHED:              SUSE-Linux-Enterprise-Server-15-SP3-Snapshot-16
	# PRETTY_NAME_BEFORE_PAREN:        SUSE Linux Enterprise Server 15 SP3
	# PRETTY_NAME_BEFORE_PAREN_DASHED: SUSE-Linux-Enterprise-Server-15-SP3

	# First word of PRETTY_NAME, typically used as vendor name e.g. SUSE or openSUSE.
	VENDOR="${PRETTY_NAME%% *}"

	# KIWI image name attribute must not contain spaces, replace with dash
	PRETTY_NAME_DASHED="${PRETTY_NAME//[^[:alnum:].]/-}"
	# Remove repeated dashes from parentheses surrounding SLE release labels
	PRETTY_NAME_DASHED="${PRETTY_NAME_DASHED//--/-}"
	# Remove dash at end from parentheses surrounding SLE release labels
	PRETTY_NAME_DASHED="${PRETTY_NAME_DASHED%%-}"

	# Special case for SLE release labels e.g. RC1. Keep only up to (space) open paren.
	# Provides a stable project name for third-party integrations e.g. app store submissions
	PRETTY_NAME_BEFORE_PAREN="${PRETTY_NAME// (*/}"

	# KIWI image name attribute must not contain spaces, replace with dash
	PRETTY_NAME_BEFORE_PAREN_DASHED="${PRETTY_NAME_BEFORE_PAREN//[^[:alnum:].]/-}"

	sed -i"" \
	    -e "s/%OS_VERSION_ID%/${VERSION_ID}/g" \
	    -e "s/%OS_PRETTY_NAME%/${PRETTY_NAME}/g" \
	    -e "s/%OS_VENDOR%/${VENDOR}/g" \
	    -e "s/%OS_PRETTY_NAME_DASHED%/${PRETTY_NAME_DASHED}/g" \
	    -e "s/%OS_PRETTY_NAME_BEFORE_PAREN%/${PRETTY_NAME_BEFORE_PAREN}/g" \
	    -e "s/%OS_PRETTY_NAME_BEFORE_PAREN_DASHED%/${PRETTY_NAME_BEFORE_PAREN_DASHED}/g" \
	    -e "s/%OS_VERSION%/${VERSION}/g" \
	    -e "s/%OS_VERSION_NO_DASH%/${VERSION_NO_DASH}/g" \
	    -e "s/%OS_VERSION_ID_SP%/${VERSION_ID_SP}/g" "${files[@]}"
fi

# Export base container REFNAME and DIGEST
# This XML file was generated by OBS and filled with exactly the data we need.
if [ -f containers/annotation ]; then
    REGISTRY_REFNAME="$(sed -n 's/.*<registry_refname>\([^<]*\)<.*/\1/p' < containers/annotation)"
    REGISTRY_DIGEST="$(sed -n 's/.*<registry_digest>\(.*\)<.*/\1/p' < containers/annotation)"

    if [ -n "$REGISTRY_DIGEST" ]; then
        sed -i"" \
            -e "s#%BASE_REFNAME%#${REGISTRY_REFNAME}#g" \
            -e "s#%BASE_DIGEST%#${REGISTRY_DIGEST}#g" \
            "${files[@]}"
    fi
fi

sed -i"" \
    -e "s#%DISTURL%#${DISTURL}#g" \
    -e "s#%SOURCEURL%#${SOURCEURL}#g" \
    -e "s#%SOURCEURL_WITH(\([^)]\+\))%#${SOURCEURL_PLACEHOLDER}#g" \
    -e "s/%RELEASE%/${RELEASE}/g" \
    -e "s/%ARCH%/${BUILD_ARCH}/g" \
    -e "s/%BUILDTIME%/$(date --utc +%FT%T.%NZ)/g" "${files[@]}"
openSUSE Build Service is sponsored by