Clean up example; split new function image_parts

This commit is contained in:
Joe 2021-05-06 15:28:22 -05:00
parent 2c05bfe4f8
commit 9f953b7ab3
2 changed files with 47 additions and 33 deletions

View File

@ -1,5 +1,4 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
function docker_cli_plugin_metadata { function docker_cli_plugin_metadata {
@ -40,22 +39,14 @@ Examples:
function main { function main {
case "$1" in case "$1" in
docker-cli-plugin-metadata) docker-cli-plugin-metadata)
docker_cli_plugin_metadata docker_cli_plugin_metadata
;; ;;
artifact) artifact)
case "$2" in case "$2" in
ls|list) ls|list) list "${@:3}" ;;
list "${@:3}" label) label "${@:3}" ;;
;; download) download "${@:3}" ;;
label) *) echo "$__usage" ;;
label "${@:3}"
;;
download)
download "${@:3}"
;;
*)
echo "$__usage"
;;
esac esac
;; ;;
esac esac
@ -68,28 +59,38 @@ function list {
} }
function list_json { function list_json {
{ read -r registry; read -r repo; read -r tag; } <<< "$(image_parts "$1")"
local token="$(get_token "$registry" "$repo")"
LOG "Querying manifest to extract labels for '$registry/$repo:$tag"
local manifest="$(curl --silent \
-H "Accept:application/vnd.docker.container.image.v1+json" \
-H "Authorization: Bearer $token" \
"https://$registry/v2/$repo/manifests/$tag")"
# >&2 jq <<< "$manifest"
local labels="$(jq -r '.history[].v1Compatibility' <<< "$manifest" | jq --slurp '[.[].config | select(.Labels != null) | .Labels] | add')"
jq <<< "$labels"
}
function image_parts {
local image="$1" local image="$1"
local regex='^([-_a-z\.]+)/([-_a-z]+)(:([-_a-z]+))?$' local regex='^([-_a-z\.]+)/([-_a-z]+)(:([-_a-z]+))?$'
if [[ $image =~ $regex ]] ; then if [[ $image =~ $regex ]] ; then
registry="${BASH_REMATCH[1]}" local registry="${BASH_REMATCH[1]}"
image="${BASH_REMATCH[2]}" local repo="${BASH_REMATCH[2]}"
tag="${BASH_REMATCH[4]:-latest}" local tag="${BASH_REMATCH[4]:-latest}"
# if no . in registry part, then it must be an image from docker hub; translate appropriately
if ! [[ $registry =~ \. ]] ; then if ! [[ $registry =~ \. ]] ; then
image="$registry/$image" repo="$registry/$repo"
registry="registry-1.docker.io" registry="registry-1.docker.io"
fi fi
else else
echo "Failed to parse image '$image'" echo "Failed to parse image '$image'"
exit 1 exit 1
fi fi
local token="$(get_token "$registry" "$image")" # >&2 echo "$image => $registry - $repo - $tag" #debug
LOG "Querying manifest to extract labels for '$registry/$image:$tag" echo "$registry"
local manifest="$(curl --silent \ echo "$repo"
-H "Accept:application/vnd.docker.container.image.v1+json" \ echo "$tag"
-H "Authorization: Bearer $token" \
"https://$registry/v2/$image/manifests/$tag")"
local labels="$(jq -r '.history[].v1Compatibility' <<< "$manifest" | jq --slurp -r '[.[].config | select(.Labels != null) | .Labels] | add')"
jq <<< "$labels"
} }
function get_token { function get_token {
@ -184,14 +185,17 @@ function delete_files {
rm "$@" rm "$@"
} }
export verbose=1
function LOG { function LOG {
[ $verbose ] && >&2 echo -e "$(tput setaf 4) => $@$(tput sgr0)" [ $verbose ] && >&2 echo -e "$(tput setaf 4) => $@$(tput sgr0)"
} }
# Export functions used in subshells # run in subshell to allow sourcing this file while not stomping on parents' namespace
export -f LOG (
export -f _search_layer # Exports used in subshells
export verbose=1
export -f LOG
export -f _search_layer
main "$@" main "$@"
)

View File

@ -3,11 +3,21 @@
pushd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null pushd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null
# Build example image # Build example image
echo "Rebuilding docker image for example" 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 - &> /dev/null printf 'FROM busybox \n RUN mkdir app && echo "Hello World!" > /app/testfile.txt' | docker build -t infogulch/artifact-test -
# Execute docker artifact to add a label that identifies /app/testfile.txt # 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 ../docker-artifact.sh artifact label infogulch/artifact-test /app/testfile.txt
echo ""
echo "Pushing image infogulch/artifact-test"
docker push infogulch/artifact-test
echo ""
echo "Listing files in infogulch/artifact-test"
../docker-artifact.sh artifact ls infogulch/artifact-test
popd &> /dev/null popd &> /dev/null