Skip to content

Instantly share code, notes, and snippets.

@smoser
Created September 21, 2011 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save smoser/1231973 to your computer and use it in GitHub Desktop.
Save smoser/1231973 to your computer and use it in GitHub Desktop.
user-data script for easy usage of cloud-publish-ubuntu.
#!/bin/sh
#
# This is a user-data script that launches an instance, and then sets
# it up to be able to publish a daily or released build as easy as
# possible. use:
# euca-run-instances <some-ami> --user-data-file <this file> --key mykey
#
# Then, copy credentials to the instance and read 'README'
#
# gist: https://gist.github.com/1231973
#
#
# Note, further setup by you is required to set the correct
# AKI and ARI ids below pointing to loader kernels that you've uploaded
# see http://people.canonical.com/~smoser/lucid-loaders/ for more info
{
apt-get update
apt-get -y install pastebinit bzr qemu-kvm </dev/null
apt-get -y install distro-info </dev/null # 12.04 has no distro-info
mkdir /mnt/tmp
chown ubuntu:ubuntu /mnt /mnt/tmp
sudo -Hu ubuntu sh <<"END_USER_SETUP"
echo "home=$HOME"
cd
mkdir ~/bin
#bzr branch lp:~smoser/cloud-utils/hook-img cloud-utils
bzr branch lp:cloud-utils
byobu-ctrl-a screen >/dev/null 2>&1
cat >> .profile <<"END"
export PATH=$HOME/cloud-utils/bin:$PATH
export TMPDIR=/mnt/tmp
export EDITOR=vi
END
echo "set -o vi" >> ~/.bashrc
ln -s creds/eucarc ~/.eucarc
ln -sf creds/uploadrc ~/.uploadrc
[ -e creds/novarc ] && ln -sf creds/novarc ~/.eucarc
byobu-launcher-install
cat > README <<END
You should be all setup now, just run 'upload' (from ~/bin).
See its '--help' for usage.
The only other thing you have to do is setup euca2ools.
cd /mnt
upload YOURNAME-cloud-images-testing precise daily
upload YOURNAME-cloud-images precise released
END
cat > ~/bin/upload <<"END_UPLOAD"
#!/bin/bash
# you must edit AKI, ARI, or set them in uploadrc
# suggest getting loader kernels from
# http://people.canonical.com/~smoser/lucid-loaders/
AKI= # you must edit this, or set in uploadrc
ARI= # you must edit this, or set in uploadrc
export CLOUD=${CLOUD:-nova} # set to 'nova' if publishing to nova
[ -e ~/.uploadrc ] && . ~/.uploadrc
Usage() {
cat <<USAGE_END
Usage: bucket-prefix release stream [arch]
Example:
* ${0##*/} smoser-cloud-images quantal released
* ${0##*/} smoser-cloud-images-testing oneiric daily i386
USAGE_END
}
error() { echo "$@" 1>&2; }
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
[ "$1" = "--help" -o "$1" = "-h" ] && { Usage; exit 0; }
[ $# -lt 3 -o $# -gt 4 ] && { Usage 1>&2; exit 1; }
bucket=$1
suite=$2
stream=$3
[ "$stream" = "daily" -o "$stream" = "released" ] ||
{ echo "stream must be 'daily' or 'released'"; exit 1; }
shift 3
[ -z "$1" ] && set -- i386 amd64
# use --disk for oneiric or later on nova. sd_args = "suite dependent args"
if [ "${CLOUD:-nova}" = "nova" ] && [[ "$suite" > "natty" ]]; then
sd_args=( "--disk" )
else
[ -n "$AKI" ] && sd_args[${#sd_args[@]}]="--kernel=$AKI"
[ -n "$ARI" ] && sd_args[${#sd_args[@]}]="--ramdisk=$ARI"
fi
TEMP_D=$(mktemp -d /mnt/tmp/${0##*/}.XXXXXX)
cleanup() {
[ -n "$TEMP_D" ] && rm -Rf "$TEMP_D";
}
euca_has() {
local name="$1" ifile="${2:-${TEMP_D}/images.out}" found=""
if [ ! -f "$ifile" ]; then
euca-describe-images -o self > "$ifile" ||
fail "failed to describe images"
fi
found=$(awk '-F\t' \
'{sub(".manifest.xml","",$3);} $3 == pub { print $2 }' \
pub="$bucket/$pubname" "$ifile")
[ $? -eq 0 ] || return 1
echo "$found"
}
trap cleanup EXIT
rm -Rf ~/.cache/ubuntu-cloudimg-query
if [ "$suite" != "hardy" ]; then
for arch in "$@"; do \
pubname=$(ubuntu-cloudimg-query $suite $stream instance-store $arch \
--format "%{pubname}\n")
found=$(euca_has "$pubname") || fail "failed to check euca images"
[ -n "$found" ] && { echo "$bucket/$pubname already exists: $found"; continue; }
time TMPDIR=/mnt/tmp cloud-publish-ubuntu --save-downloaded \
"${sd_args[@]}" \
"${CLOUD_PUBLISH_ARGS[@]}" \
--stream ${stream} $arch $suite $bucket \
-vv 2>&1 | tee ~/out.$suite.$arch.log; done
else
PATH="$HOME/abuilds:$PATH"
cd "$TEMP_D"
for arch in "$@"; do
pubname=$(ubuntu-cloudimg-query hardy $stream instance-store $arch \
--format "%{pubname}\n") &&
found=$(euca_has "$pubname") || fail "failed to check euca images"
[ -n "$found" ] && { echo "$bucket/$pubname already exists: $found"; continue; }
abuilds_loc="lp:~ubuntu-on-ec2/vmbuilder/automated-ec2-builds"
( cd && { [ -d abuilds ] || bzr branch $abuilds_loc abuilds; } ) ||
fail "failed to get $abuilds_loc"
url=$(ubuntu-cloudimg-query hardy $stream instance-store $arch \
--format '%{url}\n') ||
fail "failed to get url for hardy in $stream/$arch"
tgz="hardy-$arch.tar.gz"
wget --progress=dot:mega "$url" -O "hardy-${arch}.tar.gz" ||
fail "failed to download ${url} for $pubname"
echo "extracting $tgz"
tar -Sxzf "$tgz"
img=""
[ "$arch" = "x86_64" ] && arch="amd64"
for x in *-$arch*.img; do
[ -f "$x" ] || break;
img="$x"; break
done
[ -n "$img" ] || fail "did not find *-$arch-*.img in tarball"
echo "uecifying $img"
uecify=$(which uecify-hardy)
sudo CLOUD=${CLOUD} "$uecify" "$img" || fail "uecifiy failed"
mv "$img" "$img.in"
echo "converting to compressed qcow"
qemu-img convert -c -O qcow2 "$img.in" "$img"
if [ "${HARDY_NO_LOADER_KERNEL:-1}" != "0" ]; then
kernel="${img%.img}-vmlinuz-server"
ramdisk="${img%.img}-initrd-server"
cloud-publish-image --type=kernel "$arch" "$kernel" "$bucket" \
--rename "$pubname-kernel" > "$TEMP_D/out" &&
read aki imgname < "$TEMP_D/out" &&
[ -n "$aki" ] ||
fail "failed publish kernel"
cloud-publish-image --type=ramdisk "$arch" "$ramdisk" "$bucket" \
--rename "$pubname-ramdisk" > "$TEMP_D/out" &&
read ari imgname < "$TEMP_D/out" &&
[ -n "$ari" ] ||
fail "failed publish ramdisk"
# now replace kernel and ramdisk in sd_args if they're there
kfound=""
rfound=""
for((i=0;i<${#sd_args[@]};i++)); do
case "${sd_args[$i]}" in
--kernel=*) kfound=$i;;
--ramdisk=*) rfound=$i;;
esac
done
[ -n "$kfound" ] || kfound=${#sd_args[@]};
sd_args[$kfound]=--kernel=${aki};
[ -n "$rfound" ] || rfound=${#sd_args[@]};
sd_args[$rfound]=--ramdisk=${ari};
fi
echo "sd_args:" "${sd_args[@]}"
cloud-publish-image "$arch" "$img" "$bucket" \
--rename "$pubname" "${sd_args[@]}" -vv
done
fi
END_UPLOAD
chmod 755 ~/bin/upload
END_USER_SETUP
echo "============ $(date) $(cat /proc/uptime) USER SETUP DONE ======="
} 2>&1 | tee /home/ubuntu/setup.log
#ari-0000009f smoser-lucid-loader/lucid-amd64-linux-image-2.6.32-34-virtual-v-2.6.32-34.77~smloader0-build0-loader.manifest.xml
#aki-0000009e smoser-lucid-loader/lucid-amd64-linux-image-2.6.32-34-virtual-v-2.6.32-34.77~smloader0-kernel.manifest.xml
AKI=aki-0000009e # you must edit this
ARI=ari-0000009f # you must edit this
CLOUD=nova
# enable or disable usage of loader kernel for hardy explicitly
# loader kernel and hardy don't seem to get along
HARDY_NO_LOADER_KERNEL=1
CLOUD_PUBLISH_ARGS=( "--resize=5G" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment