Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Glance load - a utility to monitor an ETag and load the corresponding file into glance.
#!/usr/bin/env bash
# glance_load.sh
# Brian "Redbeard" Harrington <brian@dead-city.org>
#
# This code is in the public domain.
#
# A tool for monitoring remote files and loading them into a glance image
# store. This tool has been optimized for use on CoreOS but should work
# for most files where an ETag is exposed.
# by default we retrieve the alpha image, set to a different value to retrieve
# that release
release=${1:-"alpha"}
# use the following location as our local work space
tmplocation="/tmp/glancesync"
mkdir -p ${tmplocation}
pushd ${tmplocation} > /dev/null 2>&1
#retrieve the remote "ETag" header and set it to a variable
remote_etag=$(curl -I http://storage.core-os.net/coreos/amd64-usr/beta/version.txt -k -s | gawk '/ETag/ {print gensub("\"", "", "g", $2)}')
# source in the previously saved etag
source ${release}_version > /dev/null 2>&1
if [ "$remote_etag" == "$local_etag" ]; then
echo "Everything is current"
exit 0
else
curl -s -L -O http://storage.core-os.net/coreos/amd64-usr/$release/version.txt
. version.txt
#load openstack credentials
. $HOME/.openstack || echo "No glance credentials available" && exit 1
# if we already have the image don't waste time
glance image-show CoreOS-${release}-v${COREOS_VERSION} || skip="false"
if [ "$skip" == "false" ]; then
# change the following line to reflect the image to be chosen, openstack
# is used by default
curl -s -L -O http://storage.core-os.net/coreos/amd64-usr/$release/coreos_production_openstack_image.img.bz2
bunzip2 -d coreos_production_openstack_image.img.bz2
# things can get confusing if you're constantly working with the same filename
# then again, this leaves you needing to clean up old images
mv coreos_production_openstack_image.img coreos_${COREOS_VERSION}_openstack_image.img
coreosimg=coreos_${COREOS_VERSION}_openstack_image.img
# perform actual image creation
# here we set the os_release, os_verison, os_family, and os_distro variables
# for intelligent consumption of images by scripts
glance image-create --name CoreOS-${release}-v${COREOS_VERSION} --progress --is-public true \
--property os_distro=coreos --property os_family=coreos --property os_version=${COREOS_VERSION} --property os_release=${release} \
--disk-format qcow2 --container-format bare --min-disk 6 --file $coreosimg
# grab UUID of newly created image
new_glance_uuid=$(glance image-show CoreOS-${release}-v${COREOS_VERSION} | awk '/ id / {print $4}')
# purge os_release tag from all previous versions of said release
glance image-list --property "os_release=$release" \
| gawk '$4 ~ /CoreOS/ && !/'"$new_glance_uuid"'/ {printf \
"glance image-update --property os_distro=coreos --property \
os_family=coreos --property os_version=%s --purge-props %s \n",\
gensub(/CoreOS_v/, "", "g", $4), $2}' | sh
fi
echo "local_etag=$remote_etag" > ${release}_version
fi
popd > /dev/null 2>&1
# vim: set ts=2 sw=2 expandtab:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment