From 5a23f8ba313f2e5eed2e1664528c6ea772020bb4 Mon Sep 17 00:00:00 2001 From: infogulch Date: Tue, 11 May 2021 21:16:54 -0500 Subject: [PATCH] Initial impl download; adjust example.sh to show multiple files --- docker-artifact.sh | 25 ++++++++++++++++++++++++- example/example.sh | 23 +++++++++++++---------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/docker-artifact.sh b/docker-artifact.sh index 0bb06bf..01da528 100755 --- a/docker-artifact.sh +++ b/docker-artifact.sh @@ -52,6 +52,30 @@ function main { esac } +function download { + local image="$1" files=("${@:2}") registry repo tag token labels + { read -r registry; read -r repo; read -r tag; } <<< "$(image_parts "$1")" + token="$(get_token "$registry" "$repo")" + labels="$(list_json "$registry" "$repo" "$tag" "$token")" + # safely convert bash array of args into json array + files="$(for i in "${files[@]}"; do jq -n --arg f "$i" '$f'; done | jq -s '.')" + # test if all of the requested files are present in manifest labels + if ! jq -e --argjson f "$files" 'keys|contains($f)' <<< "$labels" > /dev/null ; then + echo " ** These files are not avilable to download from $image:" + echo "$(jq -r --argjson f "$files" '$f - keys | .[]' <<< "$labels" | sed 's_^_ _')" + exit 1 + fi + # filter labels to just the ones that match files + labels="$(jq --argjson f "$files" 'with_entries(select(. as $e | $f | index($e.key)))' <<< "$labels")" + local shas="$(jq -r 'to_entries | map({(.value): {(.key): null}}) | reduce .[] as $i {{}; . * $i) | to_entries | map({key:.key, value:(.value|keys)}' <<< "$labels")" + echo "$shas" + #local a="$(xargs -L1 -I {} bash -c \ + #'curl -s -L -H "Authorization: Bearer $1" "$2/$3" | tar -xz -O "$3"' \ + #_ "$token" "https://$registry/v2/$repo/blobs/" {} \ + #<<< "$shas")" + echo "Done!" +} + function list { local registry repo tag token list { read -r registry; read -r repo; read -r tag; } <<< "$(image_parts "$1")" @@ -112,7 +136,6 @@ function get_token { *.dkr.ecr.*.amazonaws.com) aws ecr get-authorization-token | jq -r '.authorizationData[0].authorizationToken' ;; - esac } diff --git a/example/example.sh b/example/example.sh index 2255d2e..c153a7a 100755 --- a/example/example.sh +++ b/example/example.sh @@ -1,23 +1,26 @@ #!/usr/bin/env bash +set -e pushd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null # Build example image -echo "Rebuilding docker image infogulch/artifact-test" -printf 'FROM busybox \n RUN mkdir app && echo "Hello World!" > /app/testfile.txt' | docker build -t infogulch/artifact-test - +>&2 printf "Rebuilding docker image infogulch/artifact-test\n" +printf 'FROM busybox \n RUN mkdir app && echo "Test data" > /app/testfile.txt && echo "Hello world!" > /hello.txt' | docker build -t infogulch/artifact-test - -# Execute docker artifact to add a label that identifies /app/testfile.txt -echo "" -echo "Labeling /app/testfile.txt in infogulch/artifact-test" -../docker-artifact.sh artifact label infogulch/artifact-test /app/testfile.txt +>&2 printf "\nLabeling /app/testfile.txt and /hello.txt in infogulch/artifact-test\n" +../docker-artifact.sh artifact label infogulch/artifact-test /app/testfile.txt /hello.txt -echo "" -echo "Pushing image infogulch/artifact-test" +>&2 printf "\nPushing image infogulch/artifact-test\n" docker push infogulch/artifact-test -echo "" -echo "Listing files in infogulch/artifact-test" +>&2 printf "\nListing files in infogulch/artifact-test\n" ../docker-artifact.sh artifact ls infogulch/artifact-test +>&2 printf "\nAttempt to download missing file /oops.txt\n" +! ../docker-artifact.sh artifact download infogulch/artifact-test /oops.txt + +>&2 printf "\nDownloading /hello.txt and /app/testfile.txt\n" +../docker-artifact.sh artifact download infogulch/artifact-test /hello.txt /app/testfile.txt + popd &> /dev/null