commit b7f52975d95e81619f847c07c621f832ffbbd37d98d79765cac4ec0c1af8260e Author: Adrian Schröter Date: Fri May 3 17:14:20 2024 +0200 Sync from SUSE:SLFO:Main obs-service-kiwi_metainfo_helper revision 4fd64bd558f8a22d18d8b8c06b98050f diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9b03811 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,23 @@ +## Default LFS +*.7z filter=lfs diff=lfs merge=lfs -text +*.bsp filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.gem filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.jar filter=lfs diff=lfs merge=lfs -text +*.lz filter=lfs diff=lfs merge=lfs -text +*.lzma filter=lfs diff=lfs merge=lfs -text +*.obscpio filter=lfs diff=lfs merge=lfs -text +*.oxt filter=lfs diff=lfs merge=lfs -text +*.pdf filter=lfs diff=lfs merge=lfs -text +*.png filter=lfs diff=lfs merge=lfs -text +*.rpm filter=lfs diff=lfs merge=lfs -text +*.tbz filter=lfs diff=lfs merge=lfs -text +*.tbz2 filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.ttf filter=lfs diff=lfs merge=lfs -text +*.txz filter=lfs diff=lfs merge=lfs -text +*.whl filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text diff --git a/README b/README new file mode 100644 index 0000000..58b3a23 --- /dev/null +++ b/README @@ -0,0 +1,23 @@ +obs-service-kiwi_metainfo_helper +================================ + +This service can be enabled to run during buildtime, when it will edit the +build recipe (.kiwi, Dockerfile, Chart.yaml) to replace placeholders with +build-specific metainfo. + +| Placeholder | Value | Example | +|--------------------------------------|-------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------| +| %DISTURL% | The OBS dist url | obs://build.opensuse.org/openSUSE:Factory/images/0f40c57dd619e1dff9e512949b6bca09-opensuse-tumbleweed-image | +| %SOURCEURL% | Source url for container recipe (OBS) | https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=0f40c57dd619e1dff9e512949b6bca09 | +| %SOURCEURL% | Source url for container recipe (IBS) | https://sources.suse.com/SUSE:SLE-15:Update:CR/sles15-image/2951b67133dd6384cacb28203174e030/ | +| %RELEASE% | The OBS release number (.) | 4.2 | +| %BUILDTIME% | $(date --utc +%FT%T.%NZ) | 2018-10-30T09:19:02.074934628Z | +| %OS_VERSION% | VERSION in the os-release file | 15-SP3 | +| %OS_VERSION_NO_DASH% | VERSION in the os-release file, with space (SLE only) | 15 SP3 | +| %OS_VERSION_ID% | VERSION_ID in the os-release file | 15 | +| %OS_VERSION_ID_SP% | Like VERSION_ID, but with SP (SLE only) | 15.3 | +| %OS_PRETTY_NAME% | PRETTY_NAME in the os-release file | SUSE Linux Enterprise Server 15 SP3 (Snapshot16) | +| %OS_VENDOR% | PRETTY_NAME up to first space character | SUSE | +| %OS_PRETTY_NAME_DASHED% | PRETTY_NAME with dashes in place of spaces | SUSE-Linux-Enterprise-Server-15-SP3-Snapshot-16 | +| %OS_PRETTY_NAME_BEFORE_PAREN% | PRETTY_NAME up to the first open parentheses | SUSE Linux Enterprise Server 15 SP3 | +| %OS_PRETTY_NAME_BEFORE_PAREN_DASHED% | PRETTY_NAME up to first open paren with dashes | SUSE-Linux-Enterprise-Server-15-SP3 | diff --git a/kiwi_metainfo_helper b/kiwi_metainfo_helper new file mode 100644 index 0000000..95aee5e --- /dev/null +++ b/kiwi_metainfo_helper @@ -0,0 +1,132 @@ +#!/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}" + + # 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 is neither Dockerfile, kiwi recipe nor helm chart - exiting" + exit 0 + fi + + files=("${RECIPEFILE}") +else + echo "Warning: No build data found - chroot build?" + DISTURL="local" + RELEASE=0 + + # 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 +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}/" +else + SOURCEURL="https://$(echo ${DISTURL} | cut -d/ -f3)/package/show/${prj}/${packagename}?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]}" | cpio -idD "${tempdir}" + + # 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 + +sed -i"" \ + -e "s#%DISTURL%#${DISTURL}#g" \ + -e "s#%SOURCEURL%#${SOURCEURL}#g" \ + -e "s/%RELEASE%/${RELEASE}/g" \ + -e "s/%BUILDTIME%/$(date --utc +%FT%T.%NZ)/g" "${files[@]}" diff --git a/kiwi_metainfo_helper.service b/kiwi_metainfo_helper.service new file mode 100644 index 0000000..c158d03 --- /dev/null +++ b/kiwi_metainfo_helper.service @@ -0,0 +1,4 @@ + + Substitute various variables in build recipes + Substitutes variables in build recipes. See README for variable listing. + diff --git a/obs-service-kiwi_metainfo_helper.changes b/obs-service-kiwi_metainfo_helper.changes new file mode 100644 index 0000000..24578f8 --- /dev/null +++ b/obs-service-kiwi_metainfo_helper.changes @@ -0,0 +1,121 @@ +------------------------------------------------------------------- +Tue Dec 6 17:10:20 UTC 2022 - Dominique Leuenberger + +- Do not ever use "%setup -n .": rpm 4.18 tries to be cleaner and + remove stuff it extraced, which would lead to 'rm -rf .', which + rm does not like. Use "%setup -c" instead, which creates the + appropriate %{name}-%{version} directory expected. + +------------------------------------------------------------------- +Tue Feb 15 11:03:50 UTC 2022 - Frederic Crozat + +- Generate OS_VERSION_NO_DASH based on os-release VERSION, + as workaround to replace dash with space in OS name + (bsc#1195061). +- Bump version to 0.6 + +------------------------------------------------------------------- +Tue Feb 8 16:10:01 UTC 2022 - Frederic Crozat + +- Improve examples in README. + +------------------------------------------------------------------- +Mon Jan 24 09:36:17 UTC 2022 - Frederic Crozat + +- Generate OS_VERSION based on os-release VERSION (bsc#1195061). +- Bump version to 0.5 + +------------------------------------------------------------------- +Mon Jan 17 09:57:02 UTC 2022 - Fabian Vogt + +- Add test suite (test.sh, sles-release-15.4-150400.32.2.x86_64.rpm) +- Fix SOURCEURL for multibuild DISTURLs +- Set SOURCEURL also in chroot builds + +------------------------------------------------------------------- +Thu Jan 13 09:32:09 UTC 2022 - Frederic Crozat + +- Generate SOURCEURL based on DISTURL. +- Bump version to 0.4 + +------------------------------------------------------------------- +Wed Jun 2 14:23:53 UTC 2021 - Jeff Kowalczyk + +- Provide various modified spellings of PRETTY_NAME useful for + writing KIWI image definitions with reduced diff between SLE, + Leap and Tumbleweed. DASHED satisfies the requirement that kiwi + image names must not have spaces. The BEFORE_PAREN variations + drop the release label in parentheses (SLE only) and can be used + as a stable name for image registries and WSL app store + submissions. + * VENDOR + * PRETTY_NAME_DASHED + * PRETTY_NAME_BEFORE_PAREN + * PRETTY_NAME_BEFORE_PAREN_DASHED + (Needed for jsc#SLE-12986) +- Update README and service definition XML to reflect all variables +- Bump version to 0.3 + +------------------------------------------------------------------- +Mon Feb 1 10:29:08 UTC 2021 - Fabian Vogt + +- Don't enable pipefail: rpm got SIGPIPE because grep -q exits early + and closes the other end, which made the whole expression fail + +------------------------------------------------------------------- +Wed Jan 27 11:25:26 UTC 2021 - Fabian Vogt + +- Read os-release from the to be installed RPMs. This avoids pulling + in distribution-release when not necessary (boo#1180263) +- Bump version to 0.2 + +------------------------------------------------------------------- +Tue Sep 1 08:42:45 UTC 2020 - Fabian Vogt + +- Use a boolean dep to avoid use of %{is_opensuse}. The prjconf + selects which one to use this way. +- Bump version to 0.1 + +------------------------------------------------------------------- +Thu Aug 27 07:12:48 UTC 2020 - Fabian Vogt + +- Fix grammar, update README and .service. Technically the name + should be changed as well, but that's just cosmetic. + +------------------------------------------------------------------- +Tue Aug 25 11:10:50 UTC 2020 - Stefan Nica + +- Also allow working on Chart.yaml (jsc#CAPS-5) + +------------------------------------------------------------------- +Tue Jul 7 13:53:33 UTC 2020 - Fabian Vogt + +- Add explicit fallback for chroot builds +- Refactor into a single sed call + +------------------------------------------------------------------- +Tue May 5 09:19:56 UTC 2020 - Fabian Vogt + +- Also allow working on Dockerfile (jsc#CAPS-10) + +------------------------------------------------------------------- +Mon May 4 13:40:20 UTC 2020 - Fabian Vogt + +- Strip service prefix from the recipe name to work with enabled + services in OBS + +------------------------------------------------------------------- +Thu Jan 10 15:42:38 UTC 2019 - Fabian Vogt + +- Add %OS_VERSION_ID(_SP)% and %PRETTY_NAME% (boo#1119378) + +------------------------------------------------------------------- +Thu Jan 10 08:06:05 UTC 2019 - Fabian Vogt + +- Remove spurious parameter to %setup -T +- Mention that it's a service in the summary + +------------------------------------------------------------------- +Wed Oct 17 14:56:38 UTC 2018 - Fabian Vogt + +- Initial commit (fate#326856, bsc#1118103) diff --git a/obs-service-kiwi_metainfo_helper.spec b/obs-service-kiwi_metainfo_helper.spec new file mode 100644 index 0000000..6f97a7b --- /dev/null +++ b/obs-service-kiwi_metainfo_helper.spec @@ -0,0 +1,61 @@ +# +# spec file for package obs-service-kiwi_metainfo_helper +# +# Copyright (c) 2022 SUSE LLC +# +# All modifications and additions to the file contributed by third parties +# remain the property of their copyright owners, unless otherwise agreed +# upon. The license for this file, and modifications and additions to the +# file, is the same license as for the pristine package itself (unless the +# license for the pristine package is not an Open Source License, in which +# case the license is the MIT License). An "Open Source License" is a +# license that conforms to the Open Source Definition (Version 1.9) +# published by the Open Source Initiative. + +# Please submit bugfixes or comments via https://bugs.opensuse.org/ +# + + +Name: obs-service-kiwi_metainfo_helper +Version: 0.6 +Release: 0 +Summary: Service for substituting various variables in build recipes +License: GPL-2.0-or-later +Group: Development/Tools/Building +URL: https://build.opensuse.org +Source0: kiwi_metainfo_helper.service +Source1: kiwi_metainfo_helper +Source2: README +# For %%check +Source3: test.sh +Source4: sles-release-15.4-150400.32.2.x86_64.rpm +BuildRequires: diffutils +Requires: /usr/bin/find +Requires: /usr/bin/grep +Requires: /usr/bin/sed +BuildArch: noarch + +%description +This service can be used during buildtime to gain access to various variables +in build recipes. + +%prep +%setup -q -D -T -c +cp %{SOURCE2} . + +%build + +%install +mkdir -p %{buildroot}%{_prefix}/lib/obs/service/ +install -m 0644 %{SOURCE0} %{buildroot}%{_prefix}/lib/obs/service/ +install -m 0755 %{SOURCE1} %{buildroot}%{_prefix}/lib/obs/service/ + +%check +sh %{SOURCE3} + +%files +%doc README +%dir %{_prefix}/lib/obs +%{_prefix}/lib/obs/service + +%changelog diff --git a/sles-release-15.4-150400.32.2.x86_64.rpm b/sles-release-15.4-150400.32.2.x86_64.rpm new file mode 100644 index 0000000..c610cd1 --- /dev/null +++ b/sles-release-15.4-150400.32.2.x86_64.rpm @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:05a4bd43da6d55fa1f03bff7f3cea8c634d1dcbdc9bbb51e40472bc936c4c497 +size 427932 diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..9249713 --- /dev/null +++ b/test.sh @@ -0,0 +1,101 @@ +#!/bin/sh +set -eu +tmpdir=$(mktemp -d) +trap 'rm -rf ${tmpdir}' EXIT + +sourcedir="$(realpath "$(dirname $0)")" +script="${sourcedir}/kiwi_metainfo_helper" + +cd $tmpdir + +# Setup environment + +mkdir -p ${tmpdir}/repos/ +cp "${sourcedir}/sles-release-15.4-150400.32.2.x86_64.rpm" ${tmpdir}/repos/ + +# Mock "date" +export PATH=${tmpdir}:$PATH +cat >${tmpdir}/date <<'EOF' +#!/bin/sh +exec /usr/bin/date -d "2018-10-30T09:19:02.074934628Z" "$@" +EOF +chmod a+x ${tmpdir}/date + +cat >.data <Dockerfile <.data <Dockerfile <Dockerfile <