Merge pull request #401 from marineam/import

oem/ami: make import zone and bucket conferable
This commit is contained in:
Michael Marineau 2015-04-21 16:32:41 -07:00
commit c81228b60f
2 changed files with 75 additions and 9 deletions

View File

@ -19,6 +19,9 @@ IMAGE="coreos_production_ami_image.bin.bz2"
GS_URL="gs://builds.release.core-os.net" GS_URL="gs://builds.release.core-os.net"
IMG_URL="" IMG_URL=""
IMG_PATH="" IMG_PATH=""
# accepted via the environment
: ${EC2_IMPORT_BUCKET:=}
: ${EC2_IMPORT_ZONE:=}
USAGE="Usage: $0 [-V 1.2.3] [-p path/image.bz2 | -u http://foo/image.bz2] USAGE="Usage: $0 [-V 1.2.3] [-p path/image.bz2 | -u http://foo/image.bz2]
Options: Options:
@ -28,13 +31,15 @@ Options:
-p PATH Path to compressed disk image, overrides -u -p PATH Path to compressed disk image, overrides -u
-u URL URL to compressed disk image, derived from -V if unset. -u URL URL to compressed disk image, derived from -V if unset.
-s STORAGE GS URL for Google storage (used to generate URL) -s STORAGE GS URL for Google storage (used to generate URL)
-B S3 bucket to use for temporary storage.
-Z EC2 availability zone to use.
-h this ;-) -h this ;-)
-v Verbose, see all the things! -v Verbose, see all the things!
This script must be run from an ec2 host with the ec2 tools installed. This script must be run from an ec2 host with the ec2 tools installed.
" "
while getopts "V:b:g:p:u:s:hv" OPTION while getopts "V:b:g:p:u:s:t:B:Z:hv" OPTION
do do
case $OPTION in case $OPTION in
V) VERSION="$OPTARG";; V) VERSION="$OPTARG";;
@ -43,6 +48,8 @@ do
p) IMG_PATH="$OPTARG";; p) IMG_PATH="$OPTARG";;
u) IMG_URL="$OPTARG";; u) IMG_URL="$OPTARG";;
s) GS_URL="$OPTARG";; s) GS_URL="$OPTARG";;
B) EC2_IMPORT_BUCKET="${OPTARG}";;
Z) EC2_IMPORT_ZONE="${OPTARG}";;
t) export TMPDIR="$OPTARG";; t) export TMPDIR="$OPTARG";;
h) echo "$USAGE"; exit;; h) echo "$USAGE"; exit;;
v) set -x;; v) set -x;;
@ -55,6 +62,11 @@ if [[ $(id -u) -eq 0 ]]; then
exit 1 exit 1
fi fi
if [[ -z "${EC2_IMPORT_BUCKET}" ]]; then
echo "$0: -B or \$EC2_IMPORT_BUCKET must be set!" >&2
exit 1
fi
# Quick sanity check that the image exists # Quick sanity check that the image exists
if [[ -n "$IMG_PATH" ]]; then if [[ -n "$IMG_PATH" ]]; then
if [[ ! -f "$IMG_PATH" ]]; then if [[ ! -f "$IMG_PATH" ]]; then
@ -94,9 +106,11 @@ arch=x86_64
name=$(sed -e "s%[^A-Za-z0-9()\\./_-]%_%g" <<< "CoreOS-$GROUP-$VERSION") name=$(sed -e "s%[^A-Za-z0-9()\\./_-]%_%g" <<< "CoreOS-$GROUP-$VERSION")
description="CoreOS $GROUP $VERSION" description="CoreOS $GROUP $VERSION"
zoneurl=http://instance-data/latest/meta-data/placement/availability-zone if [[ -z "${EC2_IMPORT_ZONE}" ]]; then
zone=$(curl --fail -s $zoneurl) zoneurl=http://instance-data/latest/meta-data/placement/availability-zone
region=$(echo $zone | sed 's/.$//') EC2_IMPORT_ZONE=$(curl --fail -s $zoneurl)
fi
region=$(echo "${EC2_IMPORT_ZONE}" | sed 's/.$//')
akiid=${ALL_AKIS[$region]} akiid=${ALL_AKIS[$region]}
if [ -z "$akiid" ]; then if [ -z "$akiid" ]; then
@ -105,7 +119,7 @@ if [ -z "$akiid" ]; then
fi fi
export EC2_URL="http://ec2.${region}.amazonaws.com" export EC2_URL="http://ec2.${region}.amazonaws.com"
echo "Building AMI in zone $zone, region id $akiid" echo "Building AMI in zone ${EC2_IMPORT_ZONE}"
tmpimg=$(mktemp) tmpimg=$(mktemp)
trap "rm -f '${dldir}'" EXIT trap "rm -f '${dldir}'" EXIT
@ -125,10 +139,10 @@ else
curl --fail "$IMG_URL" | bunzip2 >"${tmpimg}" curl --fail "$IMG_URL" | bunzip2 >"${tmpimg}"
fi fi
#aws s3 mb s3://marineam-import-testing --region $region
importid=$(ec2-import-volume "${tmpimg}" \ importid=$(ec2-import-volume "${tmpimg}" \
-f raw -s $size -x 2 -z $zone \ -f raw -s $size -x 2 \
-b marineam-import-testing \ -z "${EC2_IMPORT_ZONE}" \
-b "${EC2_IMPORT_BUCKET}" \
-o "${AWS_ACCESS_KEY}" \ -o "${AWS_ACCESS_KEY}" \
-w "${AWS_SECRET_KEY}" \ -w "${AWS_SECRET_KEY}" \
--no-upload | awk '/IMPORTVOLUME/{print $4}') --no-upload | awk '/IMPORTVOLUME/{print $4}')
@ -192,7 +206,7 @@ amiid=$(ec2-register \
cat <<EOF cat <<EOF
$description $description
architecture: $arch architecture: $arch
region: $region ($zone) region: $region (${EC2_IMPORT_ZONE})
aki id: $akiid aki id: $akiid
name: $name name: $name
description: $description description: $description

52
oem/ami/make_bucket.sh Executable file
View File

@ -0,0 +1,52 @@
#!/bin/bash
#
# This expects to run on an EC2 instance.
# Set pipefail along with -e in hopes that we catch more errors
set -e -o pipefail
# accepted via the environment
: ${EC2_IMPORT_BUCKET:=}
: ${EC2_IMPORT_ZONE:=}
USAGE="Usage: $0 [-B bucket] [-Z zone]
Options:
-B S3 bucket to use for temporary storage.
-Z EC2 availability zone to use.
-h this ;-)
-v Verbose, see all the things!
This script must be run from an ec2 host with the ec2 tools installed.
"
while getopts "B:Z:hv" OPTION
do
case $OPTION in
B) EC2_IMPORT_BUCKET="${OPTARG}";;
Z) EC2_IMPORT_ZONE="${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 [[ -z "${EC2_IMPORT_BUCKET}" ]]; then
echo "$0: -B or \$EC2_IMPORT_BUCKET must be set!" >&2
exit 1
fi
if [[ -z "${EC2_IMPORT_ZONE}" ]]; then
zoneurl=http://instance-data/latest/meta-data/placement/availability-zone
EC2_IMPORT_ZONE=$(curl --fail -s $zoneurl)
fi
region=$(echo "${EC2_IMPORT_ZONE}" | sed 's/.$//')
# The AWS cli uses slightly different vars than the EC2 cli...
export AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY}"
export AWS_SECRET_ACCESS_KEY="${AWS_SECRET_KEY}"
aws s3 mb "s3://${EC2_IMPORT_BUCKET}" --region "$region"