2022-11-18 04:29:02 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Copyright (c) Edgeless Systems GmbH
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
shopt -s inherit_errexit
|
|
|
|
|
2022-12-09 05:51:38 -05:00
|
|
|
ref="-"
|
|
|
|
stream="stable"
|
2022-12-13 04:08:42 -05:00
|
|
|
json=false
|
|
|
|
cdn_url="https://cdn.confidential.cloud"
|
|
|
|
|
|
|
|
function usage() {
|
2022-12-13 05:08:36 -05:00
|
|
|
cat << 'EOF'
|
2022-12-13 04:08:42 -05:00
|
|
|
Usage: find-image.sh [options] [command]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-r, --ref <ref> Ref to search for (default: "-")
|
|
|
|
-s, --stream <stream> Stream to search for (default: "stable")
|
|
|
|
--json Output JSON instead of shortname(s)
|
|
|
|
--help Show this help
|
|
|
|
|
|
|
|
Commands:
|
|
|
|
latest Find latest image for ref and stream
|
|
|
|
list List all images for ref and stream
|
|
|
|
EOF
|
|
|
|
}
|
2022-12-09 05:51:38 -05:00
|
|
|
|
|
|
|
POSITIONAL_ARGS=()
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case $1 in
|
|
|
|
-r | --ref)
|
2022-12-13 04:08:42 -05:00
|
|
|
# Canonicalize ref format (e.g. "feat/foo/bar" -> "feat-foo-bar")
|
|
|
|
ref=$(echo -n "$2" | tr -c '[:alnum:]' '-')
|
2022-12-09 05:51:38 -05:00
|
|
|
shift # past argument
|
|
|
|
shift # past value
|
|
|
|
;;
|
|
|
|
-s | --stream)
|
|
|
|
stream="$2"
|
|
|
|
shift # past argument
|
|
|
|
shift # past value
|
|
|
|
;;
|
2022-12-13 04:08:42 -05:00
|
|
|
--json)
|
|
|
|
json=true
|
|
|
|
shift # past argument
|
|
|
|
;;
|
|
|
|
--help)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
2022-12-09 05:51:38 -05:00
|
|
|
-*)
|
|
|
|
echo "Unknown option $1"
|
2022-12-13 04:08:42 -05:00
|
|
|
echo
|
|
|
|
usage
|
2022-12-09 05:51:38 -05:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
POSITIONAL_ARGS+=("$1") # save positional arg
|
|
|
|
shift # past argument
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
|
|
|
|
|
2022-12-13 04:08:42 -05:00
|
|
|
# shortname <ref> <stream> <version>
|
|
|
|
function shortname() {
|
|
|
|
shortname=""
|
|
|
|
|
|
|
|
if [[ ${ref} != "-" ]]; then
|
|
|
|
shortname+="ref/${1}/"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ${stream} != "stable" ]]; then
|
|
|
|
shortname+="stream/${2}/"
|
|
|
|
fi
|
|
|
|
|
|
|
|
shortname+="${3}"
|
|
|
|
|
|
|
|
echo "${shortname}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function latest() {
|
|
|
|
latest_path="constellation/v1/ref/${ref}/stream/${stream}/versions/latest/image.json"
|
|
|
|
latest_url="${cdn_url}/${latest_path}"
|
|
|
|
|
|
|
|
latest_status=$(curl -s -o /dev/null -w "%{http_code}" "${latest_url}")
|
|
|
|
if [[ ${latest_status} != "200" ]]; then
|
|
|
|
echo "[Error] No image found for ref ${ref} and stream ${stream} (${latest_status})"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
latest_json=$(curl -sL "${latest_url}")
|
|
|
|
|
|
|
|
if [[ ${json} == true ]]; then
|
2022-12-13 05:08:36 -05:00
|
|
|
jq <<< "${latest_json}"
|
2022-12-13 04:08:42 -05:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
latest_version=$(echo "${latest_json}" | jq -r '.version')
|
|
|
|
|
|
|
|
shortname "${ref}" "${stream}" "${latest_version}"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function list() {
|
|
|
|
major="v2"
|
|
|
|
list_path="constellation/v1/ref/${ref}/stream/${stream}/versions/major/${major}/image.json"
|
|
|
|
list_url="${cdn_url}/${list_path}"
|
|
|
|
|
|
|
|
list_status=$(curl -s -o /dev/null -w "%{http_code}" "${list_url}")
|
|
|
|
if [[ ${list_status} != "200" ]]; then
|
|
|
|
echo "[Error] No minor image list found for ref ${ref} and stream ${stream} and version ${major} (${list_status})"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
minor_list=$(curl -sL "${list_url}" | jq -r '.versions[]')
|
|
|
|
|
|
|
|
for minor in ${minor_list}; do
|
|
|
|
list_path="constellation/v1/ref/${ref}/stream/${stream}/versions/minor/${minor}/image.json"
|
|
|
|
list_url="${cdn_url}/${list_path}"
|
|
|
|
|
|
|
|
list_status=$(curl -s -o /dev/null -w "%{http_code}" "${list_url}")
|
|
|
|
if [[ ${list_status} != "200" ]]; then
|
|
|
|
echo "[Error] No patch image list found for ref ${ref} and stream ${stream} and version ${minor} (${list_status})"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
patch_list="${patch_list-""} $(curl -sL "${list_url}" | jq -r '.versions[]')"
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${json} == true ]]; then
|
|
|
|
out="{}"
|
2022-12-13 05:08:36 -05:00
|
|
|
out=$(jq <<< "${out}" --arg ref "${ref}" '.ref = $ref')
|
|
|
|
out=$(jq <<< "${out}" --arg stream "${stream}" '.stream = $stream')
|
2022-12-13 04:08:42 -05:00
|
|
|
for patch in ${patch_list}; do
|
2022-12-13 05:08:36 -05:00
|
|
|
out=$(jq <<< "${out}" --arg patch "${patch}" '.versions += [$patch]')
|
2022-12-13 04:08:42 -05:00
|
|
|
done
|
2022-12-13 05:08:36 -05:00
|
|
|
jq <<< "${out}"
|
2022-12-13 04:08:42 -05:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
for version in ${patch_list}; do
|
|
|
|
shortname "${ref}" "${stream}" "${version}"
|
|
|
|
done
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
case ${1-"latest"} in
|
|
|
|
"list")
|
|
|
|
list
|
|
|
|
;;
|
|
|
|
"latest")
|
|
|
|
latest
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown command $1"
|
|
|
|
;;
|
|
|
|
esac
|