Sync from SUSE:ALP:Source:Standard:1.0 obs-service-docker_label_helper revision aaa909addea4b68d3143ac9c83073f33

This commit is contained in:
Adrian Schröter 2024-08-06 15:29:03 +02:00
commit 24054a63e2
7 changed files with 330 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

61
README Normal file
View File

@ -0,0 +1,61 @@
obs-service-docker_label_helper
===============================
This service can be enabled to run during buildtime, when it will edit the
Dockerfile to expand the LABEL instruction.
A new syntax using entry/exit flags has been introduced to provide
compatibility with stock docker build command while allowing to add prefixed
labels based on existing LABEL instructions.
Both new and old syntax are supported, but you should not mix both in the
same Dockerfile.
1 - New syntax using entry/exit flags
Only LABEL between # labelprefix and # endlabelprefix will be considered
Example:
```
FROM opensuse/tumbleweed
# labelprefix=org.opensuse.nano
LABEL org.opencontainers.image.title="Example container"
LABEL org.opencontainers.image.description="This contains nano"
# endlabelprefix
LABEL not.expanded.label="example"
```
expands to
```
FROM opensuse/tumbleweed
LABEL org.opensuse.nano.title="Example container"
LABEL org.opencontainers.image.title="Example container"
LABEL org.opensuse.nano.description="This contains nano"
LABEL org.opencontainers.image.description="This contains nano"
LABEL not.expanded.label="example"
```
2 - Old syntax
Expand the PREFIXEDLABEL instruction.
Example:
```
FROM opensuse/tumbleweed
# labelprefix=org.opensuse.nano
PREFIXEDLABEL org.opencontainers.image.title="Example container"
PREFIXEDLABEL org.opencontainers.image.description="This contains nano"
```
expands to
```
FROM opensuse/tumbleweed
LABEL org.opensuse.nano.title="Example container"
LABEL org.opencontainers.image.title="Example container"
LABEL org.opensuse.nano.description="This contains nano"
LABEL org.opencontainers.image.description="This contains nano"
```

29
docker_label_helper Normal file
View File

@ -0,0 +1,29 @@
#!/bin/bash
set -eu
file="Dockerfile"
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
file="${RECIPEFILE##*:}"
if [[ ! "${file}" =~ ^Dockerfile.* ]]; then
echo "Recipe ${file} is not a Dockerfile - exiting"
exit 0
fi
fi
# Note: Avoid assigning to $1 etc. as that converts whitespace (field separators) to single spaces.
gawk -i inplace '
match($0, /^# labelprefix=(.*)$/, m) { labelprefix=m[1]; next }
labelprefix != "" && match($0, /^(PREFIXED)?LABEL[[:space:]]+[^=]*\.([^.=]*)=(.*)$/, m) { printf "LABEL %s.%s=%s\n", labelprefix, m[2], m[3]; gsub(/^PREFIXEDLABEL/, "LABEL") }
match($0, /^# endlabelprefix/) { labelprefix=""; next }
1' "${file}"

View File

@ -0,0 +1,4 @@
<service name="docker_label_helper">
<summary>Add prefixed labels in Dockerfile builds</summary>
<description>An OBS service which expands LABEL instructions in Dockerfile with prefixes. Useful for implementing https://en.opensuse.org/Building_derived_containers#Labels.</description>
</service>

View File

@ -0,0 +1,57 @@
-------------------------------------------------------------------
Fri Jun 7 08:31:35 UTC 2024 - Andrea Mazzotti <andrea.mazzotti@suse.com>
- Support Docker.FLAVOR in _multibuild (boo#1225985)
-------------------------------------------------------------------
Wed Mar 1 07:54:19 UTC 2023 - Fabian Vogt <fvogt@suse.com>
- Handle LABEL statements with any whitespace
- Handle LABEL values containing "=" properly
-------------------------------------------------------------------
Wed Dec 7 09:48:52 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.
-------------------------------------------------------------------
Thu Jul 21 12:50:28 UTC 2022 - Fabian Vogt <fvogt@suse.com>
- Avoid mangling whitespace by using gsub instead of assigning to
fields
-------------------------------------------------------------------
Wed Dec 15 14:52:19 UTC 2021 - Fabian Vogt <fvogt@suse.com>
- Improve summary, description and README.md a bit
- Mention bsc#1193429 in this changelog
-------------------------------------------------------------------
Fri Jul 23 08:50:08 UTC 2021 - Bruno Leon <bruno.leon@suse.com>
- Improve regexp instead of having two as proposed by Fabian Vogt
-------------------------------------------------------------------
Wed Jul 21 10:38:53 UTC 2021 - Bruno Leon <bruno.leon@suse.com>
- Retain compatibility with old syntax. Update documentation.
-------------------------------------------------------------------
Tue Jul 20 14:28:28 UTC 2021 - Bruno Leon <bruno.leon@suse.com>
- Use entry/exit point to determine whether labels should be expanded.
Doing so makes the Dockerfile compatible with standard
docker build command.
-------------------------------------------------------------------
Fri Sep 11 10:07:44 UTC 2020 - Fabian Vogt <fvogt@suse.com>
- Add documentation and a test
-------------------------------------------------------------------
Tue May 5 09:03:39 UTC 2020 - Fabian Vogt <fvogt@suse.com>
- Initial commit

View File

@ -0,0 +1,58 @@
#
# spec file for package obs-service-docker_label_helper
#
# Copyright (c) 2023 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-docker_label_helper
Version: 0.0
Release: 0
Summary: OBS Service to add prefixed labels in Dockerfile builds
License: GPL-2.0-or-later
Group: Development/Tools/Building
URL: https://build.opensuse.org
Source0: docker_label_helper.service
Source1: docker_label_helper
Source2: README
Source3: test.sh
Requires: gawk
BuildArch: noarch
# For %check
BuildRequires: diffutils
%description
An OBS service which expands LABEL instructions in Dockerfile with prefixes.
Useful for implementing https://en.opensuse.org/Building_derived_containers#Labels.
%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

98
test.sh Normal file
View File

@ -0,0 +1,98 @@
#!/bin/sh
set -eu
tmpdir=$(mktemp -d)
trap 'rm -rf ${tmpdir}' EXIT
script="$(realpath "$(dirname $0)")/docker_label_helper"
cd $tmpdir
# Test old syntax
cat >Dockerfile <<EOF
# labelprefix=org.opensuse.nano
PREFIXEDLABEL org.opencontainers.image.title="Example container."
PREFIXEDLABEL org.opencontainers.image.description="This contains nano"
PREFIXEDLABEL org.opencontainers.image.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
PREFIXEDLABEL test.whitespace="Two spaces. One tab."
EOF
export BUILD_DIST=
sh "${script}"
diff -u Dockerfile - <<EOF
LABEL org.opensuse.nano.title="Example container."
LABEL org.opencontainers.image.title="Example container."
LABEL org.opensuse.nano.description="This contains nano"
LABEL org.opencontainers.image.description="This contains nano"
LABEL org.opensuse.nano.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
LABEL org.opencontainers.image.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
LABEL org.opensuse.nano.whitespace="Two spaces. One tab."
LABEL test.whitespace="Two spaces. One tab."
EOF
rm -f Dockerfile
# Test new syntax
cat >Dockerfile <<EOF
# labelprefix=org.opensuse.nano
LABEL org.opencontainers.image.title="Example container."
LABEL org.opencontainers.image.description="This contains nano"
LABEL org.opencontainers.image.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
LABEL test.whitespace="Two spaces. One tab."
# endlabelprefix
LABEL not.expanded.label="example"
EOF
export BUILD_DIST=
sh "${script}"
diff -u Dockerfile - <<EOF
LABEL org.opensuse.nano.title="Example container."
LABEL org.opencontainers.image.title="Example container."
LABEL org.opensuse.nano.description="This contains nano"
LABEL org.opencontainers.image.description="This contains nano"
LABEL org.opensuse.nano.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
LABEL org.opencontainers.image.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
LABEL org.opensuse.nano.whitespace="Two spaces. One tab."
LABEL test.whitespace="Two spaces. One tab."
LABEL not.expanded.label="example"
EOF
# Test _multibuild
cat >Dockerfile.FLAVOR <<EOF
# labelprefix=org.opensuse.nano
LABEL org.opencontainers.image.title="Example container."
# endlabelprefix
EOF
export BUILD_DIST="$tmpdir/test_multibuild/.build/build.dist"
mkdir -p "$tmpdir/test_multibuild/.build"
cat >"$tmpdir/test_multibuild/.build/build.data" <<EOF
RECIPEFILE=_service:obs_scm:Dockerfile.FLAVOR
EOF
sh "${script}"
diff -u Dockerfile.FLAVOR - <<EOF
LABEL org.opensuse.nano.title="Example container."
LABEL org.opencontainers.image.title="Example container."
EOF
# Test _multibuild when not a Dockerfile.*
cat >NotADockerfile <<EOF
# labelprefix=org.opensuse.nano
LABEL org.opencontainers.image.title="Example container."
# endlabelprefix
EOF
export BUILD_DIST="$tmpdir/test_multibuild/.build/build.dist"
mkdir -p "$tmpdir/test_multibuild/.build"
cat >"$tmpdir/test_multibuild/.build/build.data" <<EOF
RECIPEFILE=_service:obs_scm:NotADockerfile
EOF
sh "${script}"
diff -u NotADockerfile - <<EOF
# labelprefix=org.opensuse.nano
LABEL org.opencontainers.image.title="Example container."
# endlabelprefix
EOF