|
| 1 | +#!/usr/bin/env bash |
| 2 | +# shellcheck shell=bash |
| 3 | +## |
| 4 | +## USAGE: __PROG__ |
| 5 | +## |
| 6 | +## "__PROG__" loads oci tarballs created with xbuild into docker. |
| 7 | +## |
| 8 | +## Usage example(s): |
| 9 | +## ./__PROG__ |
| 10 | +## PLATFORM=linux/arm64 ./__PROG__ |
| 11 | +## |
| 12 | +## Commands |
| 13 | +## - ./__PROG__ loads the oci tarball into Docker. |
| 14 | + |
| 15 | +function usage { |
| 16 | + grep '^##' "$0" | sed -e 's/^##//' -e "s/__PROG__/$me/" >&2 |
| 17 | +} |
| 18 | + |
| 19 | +function normalize_path { |
| 20 | + # Remove all /./ sequences. |
| 21 | + local path=${1//\/.\//\/} |
| 22 | + local npath |
| 23 | + # Remove first dir/.. sequence. |
| 24 | + npath="${path//[^\/][^\/]*\/\.\.\//}" |
| 25 | + # Remove remaining dir/.. sequence. |
| 26 | + while [[ $npath != "$path" ]] ; do |
| 27 | + path=$npath |
| 28 | + npath="${path//[^\/][^\/]*\/\.\.\//}" |
| 29 | + done |
| 30 | + echo "$path" |
| 31 | +} |
| 32 | + |
| 33 | +me=$(basename "$0") |
| 34 | +BASEDIR=$(dirname "$0") |
| 35 | +ROOTDIR="$(normalize_path "$BASEDIR/../../../")" |
| 36 | + |
| 37 | +command -v regctl >/dev/null 2>&1 || { usage; echo -e "\n * The regctl cli is required to run this script." >&2 ; exit 1; } |
| 38 | +command -v docker >/dev/null 2>&1 || { usage; echo -e "\n * The docker cli is required to run this script." >&2 ; exit 1; } |
| 39 | + |
| 40 | +# Takes the current platform architecture or plaftorm as defined externally in a platform variable. |
| 41 | +# e.g.: |
| 42 | +# linux/amd64 |
| 43 | +# linux/arm64 |
| 44 | +PLATFORM="${PLATFORM:-local}" |
| 45 | +OCI_IMAGES=( |
| 46 | + spiffe-csi-driver |
| 47 | +) |
| 48 | + |
| 49 | +echo "Importing ${OCI_IMAGES[*]} into docker". |
| 50 | +for img in "${OCI_IMAGES[@]}"; do |
| 51 | + oci_dir="ocidir://${ROOTDIR}oci/${img}" |
| 52 | + platform_tar="${img}-${PLATFORM}-image.tar" |
| 53 | + |
| 54 | + # regclient works with directories rather than tars, so import the OCI tar to a directory |
| 55 | + regctl image import "$oci_dir" "${img}-image.tar" |
| 56 | + dig="$(regctl image digest --platform "$PLATFORM" "$oci_dir")" |
| 57 | + # export the single platform image using the digest |
| 58 | + regctl image export "$oci_dir@${dig}" "${platform_tar}" |
| 59 | + |
| 60 | + docker load < "${platform_tar}" |
| 61 | + docker image tag "localhost/oci/${img}:latest" "${img}:devel" |
| 62 | + docker image rm "localhost/oci/${img}:latest" |
| 63 | +done |
| 64 | + |
| 65 | +# shellcheck disable=SC2046 |
| 66 | +docker image rm $(docker images -qf dangling=true) 2>/dev/null || true |
0 commit comments