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}
cache_url()
{
local url=$1
local dest=$2
local tmp=$(mktemp $(dirname $dest)/.download.XXXXXXXX)
rcode=$(curl -o $tmp -z $dest -w '%{http_code}' $url)
if [ "$rcode" == "200" ] ; then
echo "Server copy has changed. Using server version of $url"
mv $tmp $dest
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
}
mkdir -p $IMG_PATH mkdir -p $IMG_PATH
# TODO: don't cache -current forever. echo "Fetching Base Image"
if [ ! -f $IMG_PATH/$BASE_IMAGE_FILE ] ; then cache_url $SHA256SUMS $IMG_PATH/SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH
echo "Fetching Base Image" cache_url $DIB_CLOUD_IMAGES/$DIB_RELEASE/current/$BASE_IMAGE_FILE $IMG_PATH/$BASE_IMAGE_FILE
wget $SHA256SUMS -O $IMG_PATH/SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH pushd $IMG_PATH
wget $DIB_CLOUD_IMAGES/$DIB_RELEASE/current/$BASE_IMAGE_FILE -O $IMG_PATH/$BASE_IMAGE_FILE.tmp grep "$BASE_IMAGE_FILE" SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH | sha256sum --check -
pushd $IMG_PATH popd
awk "/$BASE_IMAGE_FILE/ { print \$0 \".tmp\" }" SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH | sha256sum --check -
popd
mv $IMG_PATH/$BASE_IMAGE_FILE.tmp $IMG_PATH/$BASE_IMAGE_FILE
fi
# 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