Skip to content

Instantly share code, notes, and snippets.

@mainyaa
Last active August 17, 2016 04:15
Show Gist options
  • Save mainyaa/8936794 to your computer and use it in GitHub Desktop.
Save mainyaa/8936794 to your computer and use it in GitHub Desktop.
boot instance from boot2docker image on Google Compute Engine (WIP)
#!/usr/bin/env bash
VERSION="0.0.1"
set -e
[ -n "$DEBUG" ] && set -x
usage() {
printf "
Usage: $(basename $0) PROJECT_ID
requirements: boot2docker, Google Cloud SDK, qemu, GNU tar
"
}
PROJECT_ID=$1
IMAGE=boot2docker.iso
BUCKET=${BUCKET:-${PROJECT_ID}-boot2docker}
NAME=boot2docker-$(date +%s)
TMPDIR=$(mktemp -d tmp.XXXX)
TAR=tar
defer() {
popd
#delete_bucket
#rm -r $TMPDIR
}
create_tmp() {
pushd $TMPDIR
trap defer EXIT
}
download_iso() {
boot2docker download
}
create_image() {
if [ -f ../$IMAGE ] ; then
IMAGE=../$IMAGE
fi
#qemu-img create disk.raw 10g
qemu-img convert -O raw $IMAGE disk.raw
#qemu-system-x86_64 -smp 1 -m 3750m -net nic,model=virtio -net user -device virtio-scsi-pci,id=scsi -device scsi-hd,drive=hd,physical_block_size=4096 -drive if=none,id=hd,file=disk.raw,cache=none -cdrom $IMAGE
$TAR -Szcf $NAME.tar.gz disk.raw
}
create_bucket() {
if `gsutil mb gs://$BUCKET/ > /dev/null 2>&1`; then
echo "gs://$BUCKET/ already exists"
fi
}
delete_bucket() {
if `gsutil rb gs://$BUCKET/ > /dev/null 2>&1`; then
echo "gs://$BUCKET/ delete failure"
fi
}
upload() {
gsutil cp $NAME.tar.gz gs://$BUCKET/
gcutil addimage $NAME gs://$BUCKET/$NAME.tar.gz
gcutil addinstance vm-$NAME --image=$NAME --zone=us-central1-a --machine_type=machineTypes/f1-micro
gcutil getserialportoutput vm-$NAME
}
banner() {
echo "-----> $*"
}
fail() {
echo ">>>>>> $*" >&2
exit 1
}
resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}
abs_dirname() {
local cwd="$(pwd)"
local path="$1"
while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done
pwd
cd "$cwd"
}
check() {
if command -v boot2docker >/dev/null ; then
banner "boot2docker install detected"
else
fail "'boot2docker' was not found in PATH, please install boot2docker"
fi
if command -v gsutil >/dev/null ; then
banner "gsutil install detected"
else
fail "'gsutil' was not found in PATH, please install Google Cloud SDK"
fi
if command -v gcutil >/dev/null ; then
banner "gcutil install detected"
else
fail "'gcutil' was not found in PATH, please install Google Cloud SDK"
fi
if command -v qemu-img >/dev/null ; then
banner "qemu-img install detected"
else
fail "'qemu-img' was not found in PATH, please install qemu"
fi
if $(tar --version 2>/dev/null | grep -q 'GNU tar') ; then
banner "GNU tar install detected"
elif $(gtar --version 2>/dev/null | grep -q 'GNU tar') ; then
TAR=gtar
banner "GNU tar install detected"
else
fail "'GNU tar' was not found in PATH, please install GNU tar"
fi
}
main() {
if [ -z $PROJECT_ID ] ; then
usage;exit 0
fi
check
banner 'Ready to go!'
local base_path="$(dirname $(abs_dirname $0))"
if ! [ -f $IMAGE ] ; then
download_iso
fi
create_tmp
create_image
create_bucket
upload
banner 'Instance created.'
exit 0
}
case "$1" in
--version|-v) echo "$(basename $0): $VERSION";;
--help|-h|help) usage;;
*) main;;
esac
@mattes
Copy link

mattes commented Apr 23, 2014

still not working, isn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment