File kiwi_metainfo_helper of Package obs-service-kiwi_metainfo_helper
117
1
#!/bin/bash
2
# Not using -o pipefail, as SIGPIPE is annoying to deal with
3
set -eu
4
shopt -s nullglob
5
6
if [ "${BUILD_DIST+x}" != "x" ]; then
7
echo "Not running in an OBS build container"
8
exit 1
9
fi
10
11
BUILD_DATA="${BUILD_DIST/.dist/.data}"
12
if [ -e "${BUILD_DATA}" ]; then
13
. "${BUILD_DATA}"
14
15
# The build script renames the recipe (to strip _service:foo:), but doesn't update .data
16
RECIPEFILE="${RECIPEFILE##*:}"
17
18
if [ "${RECIPEFILE##*.}" != "kiwi" ] && [ "${RECIPEFILE}" != "Dockerfile" ] && [ "${RECIPEFILE}" != "Chart.yaml" ]; then
19
echo "Recipe is neither Dockerfile, kiwi recipe nor helm chart - exiting"
20
exit 0
21
fi
22
23
files=("${RECIPEFILE}")
24
else
25
echo "Warning: No build data found - chroot build?"
26
DISTURL="local"
27
RELEASE=0
28
29
# Guess the build recipe
30
files=(*.kiwi Dockerfile* Chart.yaml*)
31
if [ "${#files}" -eq 0 ]; then
32
echo "No kiwi recipe, Dockerfile or helm chart found - exiting"
33
exit 0
34
fi
35
fi
36
37
# Print all rpm files which contain os-release
38
find_release_rpms() {
39
find ./repos -name \*-release\*.rpm | while read rpm; do
40
if rpm -qlp "${rpm}" | grep -qE '^(/etc/os-release|/usr/lib/os-release)$'; then
41
echo "${rpm}"
42
fi
43
done
44
}
45
46
if grep -q "%OS_" ${files[@]}; then
47
# Needs os-release, search for RPMs
48
relpkgs=($(find_release_rpms))
49
50
if [ ${#relpkgs[@]} -lt 1 ]; then
51
echo "No release package found, but recipe uses %OS_*% placeholders"
52
exit 1
53
fi
54
55
if [ ${#relpkgs[@]} -gt 1 ]; then
56
echo "Multiple release packages found, don't know which os-release to use"
57
exit 1
58
fi
59
60
# Extract the content
61
tempdir=$(mktemp -d)
62
trap "rm -r ${tempdir}" EXIT
63
rpm2cpio "${relpkgs[0]}" | cpio -idD "${tempdir}"
64
65
# And source it
66
[ -f "${tempdir}/usr/lib/os-release" ] && . "${tempdir}/usr/lib/os-release"
67
[ -f "${tempdir}/etc/os-release" ] && . "${tempdir}/etc/os-release"
68
69
# Special case for SLE X "SP 0", make sure it has .0
70
VERSION_ID_SP="${VERSION_ID}"
71
[[ "${VERSION_ID_SP%}" == *"."* ]] || VERSION_ID_SP="${VERSION_ID}.0"
72
73
# Provide various modified spellings of PRETTY_NAME useful for writing
74
# KIWI image definitions with reduced diff between SLE, Leap and Tumbleweed:
75
#
76
# VENDOR: openSUSE
77
# PRETTY_NAME_DASHED: openSUSE-Leap-15.3
78
# PRETTY_NAME_BEFORE_PAREN: openSUSE Leap 15.3
79
# PRETTY_NAME_BEFORE_PAREN_DASHED: openSUSE-Leap-15.3
80
#
81
# VENDOR: SUSE
82
# PRETTY_NAME_DASHED: SUSE-Linux-Enterprise-Server-15-SP3-Snapshot-16
83
# PRETTY_NAME_BEFORE_PAREN: SUSE Linux Enterprise Server 15 SP3
84
# PRETTY_NAME_BEFORE_PAREN_DASHED: SUSE-Linux-Enterprise-Server-15-SP3
85
86
# First word of PRETTY_NAME, typically used as vendor name e.g. SUSE or openSUSE.
87
VENDOR="${PRETTY_NAME%% *}"
88
89
# KIWI image name attribute must not contain spaces, replace with dash
90
PRETTY_NAME_DASHED="${PRETTY_NAME//[^[:alnum:].]/-}"
91
# Remove repeated dashes from parentheses surrounding SLE release labels
92
PRETTY_NAME_DASHED="${PRETTY_NAME_DASHED//--/-}"
93
# Remove dash at end from parentheses surrounding SLE release labels
94
PRETTY_NAME_DASHED="${PRETTY_NAME_DASHED%%-}"
95
96
# Special case for SLE release labels e.g. RC1. Keep only up to (space) open paren.
97
# Provides a stable project name for third-party integrations e.g. app store submissions
98
PRETTY_NAME_BEFORE_PAREN="${PRETTY_NAME// (*/}"
99
100
# KIWI image name attribute must not contain spaces, replace with dash
101
PRETTY_NAME_BEFORE_PAREN_DASHED="${PRETTY_NAME_BEFORE_PAREN//[^[:alnum:].]/-}"
102
103
sed -i"" \
104
-e "s/%OS_VERSION_ID%/${VERSION_ID}/g" \
105
-e "s/%OS_PRETTY_NAME%/${PRETTY_NAME}/g" \
106
-e "s/%OS_VENDOR%/${VENDOR}/g" \
107
-e "s/%OS_PRETTY_NAME_DASHED%/${PRETTY_NAME_DASHED}/g" \
108
-e "s/%OS_PRETTY_NAME_BEFORE_PAREN%/${PRETTY_NAME_BEFORE_PAREN}/g" \
109
-e "s/%OS_PRETTY_NAME_BEFORE_PAREN_DASHED%/${PRETTY_NAME_BEFORE_PAREN_DASHED}/g" \
110
-e "s/%OS_VERSION_ID_SP%/${VERSION_ID_SP}/g" "${files[@]}"
111
fi
112
113
sed -i"" \
114
-e "s#%DISTURL%#${DISTURL}#g" \
115
-e "s/%RELEASE%/${RELEASE}/g" \
116
-e "s/%BUILDTIME%/$(date --utc +%FT%T.%NZ)/g" "${files[@]}"
117