flatcar-scripts/oem/ami/publish_ami.sh
Alex Crawford f649a32d48 oem/ami: make snapshots public
This is needed so that users can copy the public AMIs into their own
accounts.
2016-11-02 13:50:36 -07:00

113 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
#
# Set pipefail along with -e in hopes that we catch more errors
set -e -o pipefail
DIR=$(dirname $0)
source $DIR/regions.sh
USAGE="Usage: $0 -V 100.0.0
-V VERSION Find AMI by CoreOS version. (required)
-b BOARD Set to the board name, default is amd64-usr
-g GROUP Set the update group, default is alpha
-h this ;-)
-v Verbose, see all the things!
This script must be run from an ec2 host with the ec2 tools installed.
"
IMAGE="coreos_production_ami"
AMI=
VER=
BOARD="amd64-usr"
GROUP="alpha"
clean_version() {
sed -e 's%[^A-Za-z0-9()\\./_-]%_%g' <<< "$1"
}
while getopts "V:b:g:s:hv" OPTION
do
case $OPTION in
V) VER="$OPTARG";;
b) BOARD="$OPTARG";;
g) GROUP="$OPTARG";;
h) echo "$USAGE"; exit;;
v) set -x;;
*) exit 1;;
esac
done
if [[ $(id -u) -eq 0 ]]; then
echo "$0: This command should not be ran run as root!" >&2
exit 1
fi
if [[ ! -n "$VER" ]]; then
echo "$0: AMI version required via -V" >&2
echo "$USAGE" >&2
exit 1
fi
search_name=$(clean_version "CoreOS-$GROUP-$VER")
declare -A AMIS HVM_AMIS
for r in "${ALL_REGIONS[@]}"; do
# Hacky but avoids writing an indirection layer to handle auth...
if [[ "${r}" == "us-gov-west-1" ]]; then
source $DIR/ami-builder-us-gov-auth.sh
else
source $DIR/marineam-auth.sh
fi
AMI=$(ec2-describe-images --region=${r} -F name="${search_name}" \
| grep -m1 ^IMAGE | cut -f2) || true
if [[ -z "$AMI" ]]; then
echo "$0: Cannot find an AMI named ${search_name} in ${r}" >&2
exit 1
fi
AMIS[${r}]=$AMI
HVM=$(ec2-describe-images --region=${r} -F name="${search_name}-hvm" \
| grep -m1 ^IMAGE | cut -f2) || true
if [[ -z "$HVM" ]]; then
echo "$0: Cannot find an AMI named ${search_name}-hvm in ${r}" >&2
exit 1
fi
HVM_AMIS[${r}]=$HVM
done
publish_ami() {
local r="$1"
local virt_type="$2"
local r_amiid="$3"
if [[ "${r}" == "us-gov-west-1" ]]; then
source $DIR/ami-builder-us-gov-auth.sh
else
source $DIR/marineam-auth.sh
fi
local r_snapshotid=$(ec2-describe-images --region="$r" "$r_amiid" \
| grep -E '^BLOCKDEVICEMAPPING.*/dev/(xv|s)da' | cut -f5) || true
if [[ -z "${r_snapshotid}" ]]; then
echo "$0: Cannot find snapshot id for $r_amiid in $r" >&2
return 1
fi
echo "Making $r_snapshotid in $r public"
ec2-modify-snapshot-attribute --region "$r" \
"$r_snapshotid" --create-volume-permission --add all
echo "Making $r_amiid in $r public"
ec2-modify-image-attribute --region "$r" \
"$r_amiid" --launch-permission -a all
}
for r in "${!AMIS[@]}"; do
publish_ami "$r" pv "${AMIS[$r]}"
done
for r in "${!HVM_AMIS[@]}"; do
publish_ami "$r" hvm "${HVM_AMIS[$r]}"
done