Sync from SUSE:SLFO:Main obs-service-kiwi_metainfo_helper revision 4fd64bd558f8a22d18d8b8c06b98050f

This commit is contained in:
Adrian Schröter 2024-05-03 17:14:20 +02:00
commit b7f52975d9
8 changed files with 468 additions and 0 deletions

23
.gitattributes vendored Normal file
View File

@ -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

23
README Normal file
View File

@ -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 (<cicnt\>.<bldcnt\>) | 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 |

132
kiwi_metainfo_helper Normal file
View File

@ -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[@]}"

View File

@ -0,0 +1,4 @@
<service name="kiwi_metainfo_helper">
<summary>Substitute various variables in build recipes</summary>
<description>Substitutes variables in build recipes. See README for variable listing.</description>
</service>

View File

@ -0,0 +1,121 @@
-------------------------------------------------------------------
Tue Dec 6 17:10:20 UTC 2022 - Dominique Leuenberger <dimstar@opensuse.org>
- 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 <fcrozat@suse.com>
- 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 <fcrozat@suse.com>
- Improve examples in README.
-------------------------------------------------------------------
Mon Jan 24 09:36:17 UTC 2022 - Frederic Crozat <fcrozat@suse.com>
- 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 <fvogt@suse.com>
- 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 <fcrozat@suse.com>
- Generate SOURCEURL based on DISTURL.
- Bump version to 0.4
-------------------------------------------------------------------
Wed Jun 2 14:23:53 UTC 2021 - Jeff Kowalczyk <jkowalczyk@suse.com>
- 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 <fvogt@suse.com>
- 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 <fvogt@suse.com>
- 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 <fvogt@suse.com>
- 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 <fvogt@suse.com>
- 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 <snica@suse.com>
- Also allow working on Chart.yaml (jsc#CAPS-5)
-------------------------------------------------------------------
Tue Jul 7 13:53:33 UTC 2020 - Fabian Vogt <fvogt@suse.com>
- Add explicit fallback for chroot builds
- Refactor into a single sed call
-------------------------------------------------------------------
Tue May 5 09:19:56 UTC 2020 - Fabian Vogt <fvogt@suse.com>
- Also allow working on Dockerfile (jsc#CAPS-10)
-------------------------------------------------------------------
Mon May 4 13:40:20 UTC 2020 - Fabian Vogt <fvogt@suse.com>
- Strip service prefix from the recipe name to work with enabled
services in OBS
-------------------------------------------------------------------
Thu Jan 10 15:42:38 UTC 2019 - Fabian Vogt <fvogt@suse.com>
- Add %OS_VERSION_ID(_SP)% and %PRETTY_NAME% (boo#1119378)
-------------------------------------------------------------------
Thu Jan 10 08:06:05 UTC 2019 - Fabian Vogt <fvogt@suse.com>
- 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 <fvogt@suse.com>
- Initial commit (fate#326856, bsc#1118103)

View File

@ -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

BIN
sles-release-15.4-150400.32.2.x86_64.rpm (Stored with Git LFS) Normal file

Binary file not shown.

101
test.sh Normal file
View File

@ -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 <<EOF
DISTURL="obs://build.opensuse.org/openSUSE:Factory/images/0f40c57dd619e1dff9e512949b6bca09-opensuse-tumbleweed-image:docker"
RELEASE=4.2
RECIPEFILE=_service:foobar:Dockerfile
EOF
export BUILD_DIST=.dist
cat >Dockerfile <<EOF
DISTURL=%DISTURL%
SOURCEURL=%SOURCEURL%
RELEASE=%RELEASE%
BUILDTIME=%BUILDTIME%
OS_VERSION=%OS_VERSION%
OS_VERSION_NO_DASH=%OS_VERSION_NO_DASH%
OS_VERSION_ID=%OS_VERSION_ID%
OS_VERSION_ID_SP=%OS_VERSION_ID_SP%
OS_PRETTY_NAME=%OS_PRETTY_NAME%
OS_VENDOR=%OS_VENDOR%
OS_PRETTY_NAME_DASHED=%OS_PRETTY_NAME_DASHED%
OS_PRETTY_NAME_BEFORE_PAREN=%OS_PRETTY_NAME_BEFORE_PAREN%
OS_PRETTY_NAME_BEFORE_PAREN_DASHED=%OS_PRETTY_NAME_BEFORE_PAREN_DASHED%
EOF
bash "${script}"
diff -u Dockerfile - <<EOF
DISTURL=obs://build.opensuse.org/openSUSE:Factory/images/0f40c57dd619e1dff9e512949b6bca09-opensuse-tumbleweed-image:docker
SOURCEURL=https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=0f40c57dd619e1dff9e512949b6bca09
RELEASE=4.2
BUILDTIME=2018-10-30T09:19:02.074934628Z
OS_VERSION=15-SP4
OS_VERSION_NO_DASH=15 SP4
OS_VERSION_ID=15.4
OS_VERSION_ID_SP=15.4
OS_PRETTY_NAME=SUSE Linux Enterprise Server 15 SP4
OS_VENDOR=SUSE
OS_PRETTY_NAME_DASHED=SUSE-Linux-Enterprise-Server-15-SP4
OS_PRETTY_NAME_BEFORE_PAREN=SUSE Linux Enterprise Server 15 SP4
OS_PRETTY_NAME_BEFORE_PAREN_DASHED=SUSE-Linux-Enterprise-Server-15-SP4
EOF
# Now with a build.suse.de DISTURL
cat >.data <<EOF
DISTURL="obs://build.suse.de/SUSE:SLE-15-SP3:Update:CR/images/5f0a221b7877396cbf977205e64690d2-sles15-image"
RELEASE=4.2
RECIPEFILE=_service:foobar:Dockerfile
EOF
cat >Dockerfile <<EOF
DISTURL=%DISTURL%
SOURCEURL=%SOURCEURL%
EOF
bash "${script}"
diff -u Dockerfile - <<EOF
DISTURL=obs://build.suse.de/SUSE:SLE-15-SP3:Update:CR/images/5f0a221b7877396cbf977205e64690d2-sles15-image
SOURCEURL=https://sources.suse.com/SUSE:SLE-15-SP3:Update:CR/sles15-image/5f0a221b7877396cbf977205e64690d2/
EOF
# Now test without build data (osc chroot build) and that without %OS_*% it doesn't need a release RPM
rm -r ./.data ./repos/
cat >Dockerfile <<EOF
DISTURL=%DISTURL%
SOURCEURL=%SOURCEURL%
RELEASE=%RELEASE%
BUILDTIME=%BUILDTIME%
EOF
bash "${script}"
diff -u Dockerfile - <<EOF
DISTURL=local
SOURCEURL=https://local/package/show/local/local?rev=local
RELEASE=0
BUILDTIME=2018-10-30T09:19:02.074934628Z
EOF