Last active
September 19, 2022 22:27
-
-
Save codyshepherd/ec4d1798ae123e91c9e25ba67d30e6b0 to your computer and use it in GitHub Desktop.
Query Ubuntu cloud image serials by platform, date, release, and type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Get serials for Ubuntu images in public cloud platforms | |
# | |
# Some assumptions are made here about image particulars, e.g. region, | |
# root storage type, and virtualization type (the latter two are only | |
# applicable for AWS). | |
# | |
# This script uses the sstream-query-cpc snap developed and maintained by the | |
# Ubuntu CPC Team: | |
# - https://snapcraft.io/sstream-query-cpc | |
# - https://launchpad.net/simplestreams | |
snap_binary=$(command -v snap) | |
snap_path=$(command -v sstream-query | grep '/snap/bin/') | |
if [ -z "$snap_binary" ]; then | |
echo "This script uses snap packages, which require snapd to be present." | |
echo "Find instructions for installing snapd at the following url:" | |
echo "https://snapcraft.io/docs/installing-snapd" | |
exit 1 | |
fi | |
if [ -z "$snap_path" ]; then | |
echo "This script requires the 'sstream-query-cpc' snap, aliased to" | |
echo "'sstream-query'; this snap or alias is not present on" | |
echo "your machine." | |
echo "Please install by running:" | |
echo " sudo snap install sstream-query-cpc --edge" | |
echo " sudo snap alias sstream-query-cpc sstream-query" | |
exit 1 | |
fi | |
ARCH=amd64 | |
CLOUD=all | |
DATE=$(date +%Y%m) | |
EMIT_IMAGE_ID=false | |
MINIMAL="" | |
NUMBER=1 | |
RELEASE=bionic | |
TYPE=daily | |
function usage() { | |
echo "$0:" | |
echo " [-c cloud] 'ec2', 'gce', and 'azure' are supported" | |
echo " [-d YYYYMM] specify a month to query for images; setting this" | |
echo " option overrides the -p switch" | |
echo " [-i] switch to emit image ID along with serial" | |
echo " [-m] switch to indicate minimal image" | |
echo " [-n] specify a number of serials to retrieve" | |
echo " [-p] switch to fetch serials from the previous month" | |
echo " [-r release] supported shorthand release names, e.g. 'bionic'" | |
echo " for 18.04 images or 'xenial' for 16.04" | |
echo " [-t type] 'daily' or 'releases' is supported" | |
echo "" | |
echo "Defaults:" | |
echo " cloud: ${CLOUD}" | |
echo " date: today's date ${DATE}" | |
echo " minimal: false" | |
echo " number: ${NUMBER}" | |
echo " previous-month: false" | |
echo " release: ${RELEASE}" | |
echo " type: ${TYPE}" | |
} | |
OPTIONS=() | |
for var in "$@"; do | |
if [ "$var" = "-m" ]; then | |
MINIMAL="minimal/" | |
elif [ "$var" = "-p" ]; then | |
DATE=$(date --date='-1 month' +%Y%m) | |
elif [ "$var" = "-i" ]; then | |
EMIT_IMAGE_ID=true | |
else | |
OPTIONS+=("$var") | |
fi | |
done | |
set -- "${OPTIONS[@]}" | |
while getopts "c:d:n:r:t:h" opt; do | |
case "${opt}" in | |
c) CLOUD="${OPTARG}";; | |
d) DATE="${OPTARG}";; | |
n) NUMBER="${OPTARG}";; | |
r) RELEASE="${OPTARG}";; | |
t) TYPE="${OPTARG}";; | |
h) | |
usage | |
exit 0 | |
;; | |
\?) | |
echo "Invalid option: -${OPTARG}" >&2 | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
if [ "$TYPE" = "release" ]; then | |
TYPE=releases | |
fi | |
TYPE="${MINIMAL}${TYPE}" | |
echo "Getting latest ${RELEASE} ${TYPE} serials from date ${DATE}..." | |
RESULTS=$(sstream-query --max "${NUMBER}" \ | |
"http://cloud-images.ubuntu.com/${TYPE}" \ | |
arch="${ARCH}" \ | |
release="${RELEASE}" \ | |
version_name~"${DATE}" | tr \' \") | |
if [ "$CLOUD" = "all" ] || [ "$CLOUD" = 'ec2' ]; then | |
AWS_SERIALS=$(echo "${RESULTS}" \ | |
| jq -r "select((.region==\"us-west-2\") and (.virt==\"hvm\") and (.root_store==\"ssd\") and (.version_name|test(\"${DATE}.*\"))) | .version_name") | |
AWS_SERIALS_ARR=($AWS_SERIALS) | |
AWS_IMAGE_IDS=$(echo "${RESULTS}" \ | |
| jq -r "select((.region==\"us-west-2\") and (.virt==\"hvm\") and (.root_store==\"ssd\") and (.version_name|test(\"${DATE}.*\"))) | .id") | |
AWS_IMAGE_IDS_ARR=($AWS_IMAGE_IDS) | |
echo "AWS:" | |
if [ "$EMIT_IMAGE_ID" = true ]; then | |
for (( i=0; i<${#AWS_SERIALS_ARR[@]}; ++i)); do | |
echo ${AWS_SERIALS_ARR[$i]}: ${AWS_IMAGE_IDS_ARR[$i]} | |
done | |
else | |
echo "$AWS_SERIALS" | |
fi | |
fi | |
if [ "$CLOUD" = "all" ] || [ "$CLOUD" = 'gce' ]; then | |
GCE_SERIALS=$(echo "${RESULTS}" \ | |
| jq -r "select((.region==\"us-west2\") and (.version_name|test(\"${DATE}.*\"))) | .version_name") | |
GCE_SERIALS_ARR=($GCE_SERIALS) | |
GCE_IMAGE_IDS=$(echo "${RESULTS}" \ | |
| jq -r "select((.region==\"us-west2\") and (.version_name|test(\"${DATE}.*\"))) | .id") | |
GCE_IMAGE_IDS_ARR=($GCE_IMAGE_IDS) | |
echo "GCE:" | |
if [ "$EMIT_IMAGE_ID" = true ]; then | |
for (( i=0; i<${#AWS_SERIALS_ARR[@]}; ++i)); do | |
echo ${GCE_SERIALS_ARR[$i]}: ${GCE_IMAGE_IDS_ARR[$i]} | |
done | |
else | |
echo "$GCE_SERIALS" | |
fi | |
fi | |
if [ "$CLOUD" = "all" ] || [ "$CLOUD" = 'azure' ]; then | |
AZURE_SERIALS=$(echo "${RESULTS}" \ | |
| jq -r "select((.region==\"West US 2\") and (.version_name|test(\"${DATE}.*\"))) | .version_name") | |
AZURE_SERIALS_ARR=($AZURE_SERIALS) | |
AZURE_IMAGE_IDS=$(echo "${RESULTS}" \ | |
| jq -r "select((.region==\"West US 2\") and (.version_name|test(\"${DATE}.*\"))) | .id") | |
AZURE_IMAGE_IDS_ARR=($AZURE_IMAGE_IDS) | |
echo "Azure:" | |
if [ "$EMIT_IMAGE_ID" = true ]; then | |
for (( i=0; i<${#AWS_SERIALS_ARR[@]}; ++i)); do | |
echo ${AZURE_SERIALS_ARR[$i]}: ${AZURE_IMAGE_IDS_ARR[$i]} | |
done | |
else | |
echo "$AZURE_SERIALS" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment