Skip to content

Instantly share code, notes, and snippets.

@codyshepherd
Last active September 11, 2019 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyshepherd/aef9f62e7b8fd813bc39462bfee3676b to your computer and use it in GitHub Desktop.
Save codyshepherd/aef9f62e7b8fd813bc39462bfee3676b to your computer and use it in GitHub Desktop.
Find the latest Ubuntu image serials for AWS, GCP, and Azure
#!/bin/bash
# Find the latest Ubuntu image serial for any/all of AWS EC2, GCE, or Azure
# clouds.
#
# 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 image-status snap developed and maintained by the
# Ubuntu CPC Team:
# - https://snapcraft.io/image-status
# - https://github.com/smoser/talk-simplestreams/
snap_binary=$(command -v snap)
snap_path=$(command -v image-status | 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 'image-status' snap, which is not present on"
echo "your machine."
echo "Please install by running 'snap install image-status'"
exit 1
fi
CLOUD="all"
MINIMAL=""
RELEASE="bionic"
TYPE="daily"
function usage() {
echo "$0: [-c cloud] 'ec2', 'gce', and 'azure' are supported"
echo " [-m] switch to indicate minimal image"
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 'release' is supported"
echo ""
echo "Defaults:"
echo " cloud: ${CLOUD}"
echo " minimal: false"
echo " release: ${RELEASE}"
echo " type: ${TYPE}"
}
POSITIONAL=()
for var in "$@"; do
if [ "$var" = "-m" ]; then
MINIMAL="-minimal"
else
POSITIONAL+=("$var")
fi
done
set -- "${POSITIONAL[@]}"
while getopts "c:m:r:t:h" opt; do
case "${opt}" in
c) CLOUD="${OPTARG}";;
m) MINIMAL="-minimal";;
r) RELEASE="${OPTARG}";;
t) TYPE="${OPTARG}";;
h)
usage
exit 0
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
usage
exit 1
;;
esac
done
TYPE="-${TYPE}${MINIMAL}"
if [ "$CLOUD" = "all" ] || [ "$CLOUD" = 'ec2' ]; then
echo "Getting latest ${RELEASE} ec2${TYPE} image..."
AWS=$(image-status --max 1 ec2"${TYPE}" release="${RELEASE}" region=us-west-2 root_store=ssd virt=hvm 2> /dev/null)
AWS_SERIAL=$(echo "$AWS" | tr -s ' ' | cut -d ' ' -f2)
echo "AWS: $AWS_SERIAL"
fi
if [ "$CLOUD" = "all" ] || [ "$CLOUD" = 'gce' ]; then
echo "Getting latest ${RELEASE} gce${TYPE} image..."
GCE=$(image-status --max 1 gce"${TYPE}" release="${RELEASE}" region=us-west2 2> /dev/null)
GCE_SERIAL=$(echo "$GCE" | tr -s ' ' | cut -d ' ' -f2)
echo "GCE: $GCE_SERIAL"
fi
if [ "$CLOUD" = "all" ] || [ "$CLOUD" = 'azure' ]; then
echo "Getting latest ${RELEASE} azure${TYPE} image..."
AZURE=$(image-status --max 1 azure"${TYPE}" release="${RELEASE}" region='West US 2' 2> /dev/null)
AZURE_SERIAL=$(echo "$AZURE" | tr -s ' ' | cut -d ' ' -f2)
echo "Azure: $AZURE_SERIAL"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment