Merge pull request #46 from vbatts/subcommand_unpack

BuildSourceImage: expose "unpack" as subcommand
This commit is contained in:
Valentin Rothberg 2019-10-16 09:58:50 +02:00 committed by GitHub
commit 05804b31c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 5 deletions

View file

@ -28,6 +28,10 @@ _usage() {
echo -e " -D\t\tdebuging output. Can be set via DEBUG env variable" echo -e " -D\t\tdebuging output. Can be set via DEBUG env variable"
echo -e " -h\t\tthis usage information" echo -e " -h\t\tthis usage information"
echo -e " -v\t\tversion" echo -e " -v\t\tversion"
echo -e ""
echo -e " Subcommands:"
echo -e " unpack\tUnpack an OCI layout to a rootfs directory"
echo -e ""
} }
# sanity checks on startup # sanity checks on startup
@ -44,6 +48,23 @@ _init() {
done done
} }
# enable access to some of functions as subcommands!
_subcommand() {
local command="${1}"
local ret
shift
case "${command}" in
unpack)
# (vb) i'd prefer this subcommand directly match the function name, but it isn't as pretty.
unpack_img "${@}"
ret=$?
exit "${ret}"
;;
esac
}
# _is_sourced tests whether this script is being source, or executed directly # _is_sourced tests whether this script is being source, or executed directly
_is_sourced() { _is_sourced() {
# https://unix.stackexchange.com/a/215279 # https://unix.stackexchange.com/a/215279
@ -292,6 +313,20 @@ unpack_img() {
local unpack_dir="${2}" local unpack_dir="${2}"
local ret local ret
while getopts ":h" opts; do
case "${opts}" in
*)
echo "$0 unpack <oci layout path> <unpack path>"
return 1
;;
esac
done
shift $((OPTIND-1))
if [ -z "${image_dir}" ] || [ -z "${unpack_dir}" ] ; then
_error "[unpack_img] blank arguments provided"
fi
if [ -d "${unpack_dir}" ] ; then if [ -d "${unpack_dir}" ] ; then
_rm_rf "${unpack_dir}" _rm_rf "${unpack_dir}"
fi fi
@ -336,9 +371,10 @@ unpack_img_bash() {
return ${ret} return ${ret}
fi fi
# Since we're landing the reference as an OCI layout, this mediaType is fairly predictable # TODO this will need to be refactored when we start seeing +zstd layers.
# TODO don't always assume +gzip # Then it will be better to no just get a list of digests, but maybe to
layer_dgsts="$(jq '.layers[] | select(.mediaType == "application/vnd.oci.image.layer.v1.tar+gzip") | .digest' "${image_dir}"/blobs/"${mnfst_dgst/:/\/}" | tr -d \")" # iterate on each descriptor independently?
layer_dgsts="$(jq '.layers | map(select(.mediaType == "application/vnd.oci.image.layer.v1.tar+gzip"),select(.mediaType == "application/vnd.oci.image.layer.v1.tar"),select(.mediaType == "application/vnd.docker.image.rootfs.diff.tar.gzip")) | .[] | .digest' "${image_dir}"/blobs/"${mnfst_dgst/:/\/}" | tr -d \")"
ret=$? ret=$?
if [ ${ret} -ne 0 ] ; then if [ ${ret} -ne 0 ] ; then
return ${ret} return ${ret}
@ -1026,8 +1062,9 @@ main() {
local work_dir local work_dir
_init "${@}" _init "${@}"
_subcommand "${@}"
base_dir="$(pwd)/${ABV_NAME}" base_dir="${BASE_DIR:-$(pwd)/${ABV_NAME}}"
# using the bash builtin to parse # using the bash builtin to parse
while getopts ":hlvDi:c:s:e:o:b:d:p:" opts; do while getopts ":hlvDi:c:s:e:o:b:d:p:" opts; do
case "${opts}" in case "${opts}" in

View file

@ -4,4 +4,6 @@ RUN dnf install -y jq skopeo findutils file 'dnf-command(download)'
COPY ./BuildSourceImage.sh /usr/local/bin/BuildSourceImage.sh COPY ./BuildSourceImage.sh /usr/local/bin/BuildSourceImage.sh
ENTRYPOINT ["/usr/local/bin/BuildSourceImage.sh", "-b", "/tmp/"] ENV BASE_DIR=/tmp
ENTRYPOINT ["/usr/local/bin/BuildSourceImage.sh"]

View file

@ -3,6 +3,7 @@
load helpers load helpers
@test "Build from image reference" { @test "Build from image reference" {
#skip "this takes like 20min ..."
local d local d
d=$(mktemp -d) d=$(mktemp -d)
echo "temporary directory: ${d}" echo "temporary directory: ${d}"

32
test/03-unpack.bats Normal file
View file

@ -0,0 +1,32 @@
#!/usr/bin/env bats -t
load helpers
@test "unpack - no args" {
run_ctr $CTR_IMAGE unpack
[ "$status" -eq 1 ]
[[ ${lines[0]} =~ "[SrcImg][ERROR] [unpack_img] blank arguments provided" ]]
}
@test "unpack - Help" {
run_ctr $CTR_IMAGE unpack -h
[ "$status" -eq 1 ]
[[ ${lines[0]} =~ "BuildSourceImage.sh unpack <oci layout path> <unpack path>" ]]
}
@test "unpack - from a SRPM build" {
local d
local r
d=$(mktemp -d)
echo "temporary directories: output - ${d}"
run_ctr -v $(pwd)/.testprep/srpms/:/src:ro --mount type=bind,source=${d},destination=/output $CTR_IMAGE -s /src -o /output
[ "$status" -eq 0 ]
[ -f "${d}/index.json" ]
r=$(mktemp -d)
echo "temporary directories: unpacked - ${r}"
run_ctr --mount type=bind,source=${d},destination=/output -v ${r}:/unpacked/ $CTR_IMAGE unpack /output/ /unpacked/
[ "$(find ${r} -type f | wc -l)" -eq 3 ] # regular files
[ "$(find ${r} -type l | wc -l)" -eq 3 ] # and symlinks
}

View file

@ -1,3 +1,8 @@
#!/bin/bash
export CTR_IMAGE="${CTR_IMAGE:-localhost/containers/buildsourceimage}"
export CTR_ENGINE="${CTR_ENGINE:-podman}"
function run_ctr() { function run_ctr() {
run $CTR_ENGINE run --security-opt label=disable --rm "$@" run $CTR_ENGINE run --security-opt label=disable --rm "$@"
} }