Skip to content

Commit e0730a3

Browse files
committed
build: inventory installed packages
This adds logic to `rpm2img` to generate an inventory of the installed rpms to `/usr/share/bottlerocket/application-inventory.json`.
1 parent 434d443 commit e0730a3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tools/rpm2img

+32
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ shopt -qs failglob
88
. "${0%/*}/partyplanner"
99

1010
OUTPUT_FMT="raw"
11+
BUILDER_ARCH="$(uname -m)"
1112

1213
for opt in "$@"; do
1314
optarg="$(expr "${opt}" : '[^=]*=\(.*\)')"
@@ -135,7 +136,38 @@ if [ "${PARTITION_PLAN}" == "split" ] ; then
135136
--sort --print "${DATA_IMAGE}"
136137
fi
137138

139+
INSTALL_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
138140
rpm -iv --root "${ROOT_MOUNT}" "${PACKAGE_DIR}"/*.rpm
141+
142+
# inventory installed packages
143+
INVENTORY_QUERY="\{\"Name\":\"%{NAME}\"\
144+
,\"Publisher\":\"Bottlerocket\"\
145+
,\"Version\":\"${VERSION_ID}\"\
146+
,\"Release\":\"${BUILD_ID}\"\
147+
,\"InstalledTime\":\"${INSTALL_TIME}\"\
148+
,\"ApplicationType\":\"%{GROUP}\"\
149+
,\"Architecture\":\"%{ARCH}\"\
150+
,\"Url\":\"%{URL}\"\
151+
,\"Summary\":\"%{Summary}\"\}\n"
152+
153+
mapfile -t installed_rpms <<< "$(rpm -qa --root "${ROOT_MOUNT}" \
154+
--queryformat "${INVENTORY_QUERY}")"
155+
156+
# wrap installed_rpms mapfile into json
157+
INVENTORY_DATA="$(jq --raw-output . <<< "${installed_rpms[@]}")"
158+
# replace the package architecture with the target architecture (for cross-compiled builds)
159+
if [[ "${BUILDER_ARCH}" != "${ARCH}" ]]; then
160+
INVENTORY_DATA="$(jq --arg BUILDER_ARCH "${BUILDER_ARCH}" --arg TARGET_ARCH "${ARCH}" \
161+
'(.Architecture) |= sub($BUILDER_ARCH; $TARGET_ARCH)' <<< "${INVENTORY_DATA}")"
162+
fi
163+
# remove the 'bottlerocket-<arch>-' prefix from package names
164+
INVENTORY_DATA="$(jq --arg PKG_PREFIX "bottlerocket-${ARCH}-" \
165+
'(.Name) |= sub($PKG_PREFIX; "")' <<< "${INVENTORY_DATA}")"
166+
# sort by package name and add 'Content' as top-level
167+
INVENTORY_DATA="$(jq --slurp 'sort_by(.Name)' <<< "${INVENTORY_DATA}" | jq '{"Content": .}')"
168+
printf "%s\n" "${INVENTORY_DATA}" > "${ROOT_MOUNT}/usr/share/bottlerocket/application-inventory.json"
169+
170+
# install licenses
139171
install -p -m 0644 /host/{COPYRIGHT,LICENSE-APACHE,LICENSE-MIT} "${ROOT_MOUNT}"/usr/share/licenses/
140172
mksquashfs \
141173
"${ROOT_MOUNT}"/usr/share/licenses \

0 commit comments

Comments
 (0)