Use conditional GET to fetch latest ubuntu image.

This also switches to using curl which some people may not have
installed. However, curl is far superior for this type of download.

Change-Id: I7ac5a84b30eb8daad320c082f976931c41a24669
This commit is contained in:
Clint Byrum 2013-04-30 16:36:56 -07:00
parent f725be5dd1
commit 60a1405eef

View File

@ -14,17 +14,32 @@ DIB_RELEASE=${DIB_RELEASE:-quantal}
BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-$DIB_RELEASE-server-cloudimg-$ARCH-root.tar.gz} BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-$DIB_RELEASE-server-cloudimg-$ARCH-root.tar.gz}
SHA256SUMS=${SHA256SUMS:-https://${DIB_CLOUD_IMAGES##http?(s)://}/$DIB_RELEASE/current/SHA256SUMS} SHA256SUMS=${SHA256SUMS:-https://${DIB_CLOUD_IMAGES##http?(s)://}/$DIB_RELEASE/current/SHA256SUMS}
mkdir -p $IMG_PATH cache_url()
# TODO: don't cache -current forever. {
if [ ! -f $IMG_PATH/$BASE_IMAGE_FILE ] ; then local url=$1
echo "Fetching Base Image" local dest=$2
wget $SHA256SUMS -O $IMG_PATH/SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH local tmp=$(mktemp $(dirname $dest)/.download.XXXXXXXX)
wget $DIB_CLOUD_IMAGES/$DIB_RELEASE/current/$BASE_IMAGE_FILE -O $IMG_PATH/$BASE_IMAGE_FILE.tmp rcode=$(curl -o $tmp -z $dest -w '%{http_code}' $url)
pushd $IMG_PATH if [ "$rcode" == "200" ] ; then
awk "/$BASE_IMAGE_FILE/ { print \$0 \".tmp\" }" SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH | sha256sum --check - echo "Server copy has changed. Using server version of $url"
popd mv $tmp $dest
mv $IMG_PATH/$BASE_IMAGE_FILE.tmp $IMG_PATH/$BASE_IMAGE_FILE elif [ "$rcode" == "304" ] ; then
echo "Server copy has not changed. Using locally cached $url"
rm -f $tmp
else
echo "Server returned an unexpected response code. [$rcode]"
rm -f $tmp
return 1
fi fi
}
mkdir -p $IMG_PATH
echo "Fetching Base Image"
cache_url $SHA256SUMS $IMG_PATH/SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH
cache_url $DIB_CLOUD_IMAGES/$DIB_RELEASE/current/$BASE_IMAGE_FILE $IMG_PATH/$BASE_IMAGE_FILE
pushd $IMG_PATH
grep "$BASE_IMAGE_FILE" SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH | sha256sum --check -
popd
# Extract the base image # Extract the base image
sudo tar -C $TARGET_ROOT -xzf $IMG_PATH/$BASE_IMAGE_FILE sudo tar -C $TARGET_ROOT -xzf $IMG_PATH/$BASE_IMAGE_FILE
sudo rmdir $TARGET_ROOT/lost+found sudo rmdir $TARGET_ROOT/lost+found