diff --git a/elements/cache-url/README.md b/elements/cache-url/README.md new file mode 100644 index 00000000..70317a26 --- /dev/null +++ b/elements/cache-url/README.md @@ -0,0 +1 @@ +A helper script to download images into a local cache. diff --git a/elements/cache-url/bin/cache-url b/elements/cache-url/bin/cache-url new file mode 100755 index 00000000..a31a8e49 --- /dev/null +++ b/elements/cache-url/bin/cache-url @@ -0,0 +1,48 @@ +#!/bin/sh + +# Copyright 2012 Hewlett-Packard Development Company, L.P. +# All Rights Reserved. +# Copyright 2013 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +set -e + +# Download a URL to a local cache +# e.g. cache-url http://.../foo ~/.cache/image-create/foo + +url=$1 +dest=$2 + +mkdir -p $(dirname $dest) +tmp=$(mktemp $(dirname $dest)/.download.XXXXXXXX) + +if [ -f $dest ] ; then + time_cond="-z $dest" + success="Server copy has changed. Using server version of $url" +else + success="Downloaded and cached $url for the first time" +fi + +rcode=$(curl -o $tmp -w '%{http_code}' $url $time_cond) +if [ "$rcode" == "200" ] ; then + echo $success + 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 + exit 1 +fi diff --git a/elements/fedora/element-deps b/elements/fedora/element-deps index 40937011..7fa0e591 100644 --- a/elements/fedora/element-deps +++ b/elements/fedora/element-deps @@ -1,2 +1,3 @@ dib-run-parts dracut-network +cache-url diff --git a/elements/fedora/root.d/10-fedora-cloud-image b/elements/fedora/root.d/10-fedora-cloud-image index 46a8dfe1..9b6443cb 100755 --- a/elements/fedora/root.d/10-fedora-cloud-image +++ b/elements/fedora/root.d/10-fedora-cloud-image @@ -15,14 +15,11 @@ DIB_RELEASE=${DIB_RELEASE:-Fedora18} BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-$DIB_RELEASE-Cloud-$ARCH-latest.qcow2} BASE_IMAGE_TAR=$DIB_RELEASE-Cloud-$ARCH-latest.tgz -mkdir -p $IMG_PATH -# TODO: don't cache forever. -if [ ! -f $IMG_PATH/$BASE_IMAGE_FILE ] ; then - echo "Fetching Base Image" - wget $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE -O $IMG_PATH/$BASE_IMAGE_FILE.tmp - mv $IMG_PATH/$BASE_IMAGE_FILE.tmp $IMG_PATH/$BASE_IMAGE_FILE -fi -if [ ! -f $IMG_PATH/$BASE_IMAGE_TAR ] ; then +echo "Fetching Base Image" +$TMP_HOOKS_PATH/bin/cache-url $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE $IMG_PATH/$BASE_IMAGE_FILE + +if [ ! -f $IMG_PATH/$BASE_IMAGE_TAR -o \ + $IMG_PATH/$BASE_IMAGE_FILE -nt $IMG_PATH/$BASE_IMAGE_TAR ] ; then echo "Repacking base image as tarball." WORKING=$(mktemp -d) EACTION="rm -r $WORKING" diff --git a/elements/ubuntu/element-deps b/elements/ubuntu/element-deps index 16fd9fc7..d646207e 100644 --- a/elements/ubuntu/element-deps +++ b/elements/ubuntu/element-deps @@ -1,2 +1,3 @@ dpkg dib-run-parts +cache-url diff --git a/elements/ubuntu/root.d/10-cache-ubuntu-tarball b/elements/ubuntu/root.d/10-cache-ubuntu-tarball index e062163e..cb2159dc 100755 --- a/elements/ubuntu/root.d/10-cache-ubuntu-tarball +++ b/elements/ubuntu/root.d/10-cache-ubuntu-tarball @@ -14,39 +14,9 @@ DIB_RELEASE=${DIB_RELEASE:-quantal} 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} -cache_url() -{ - local url=$1 - local dest=$2 - - mkdir -p $(dirname $dest) - local tmp=$(mktemp $(dirname $dest)/.download.XXXXXXXX) - - if [ -f $dest ] ; then - time_cond="-z $dest" - success="Server copy has changed. Using server version of $url" - else - success="Downloaded and cached $url for the first time" - fi - - rcode=$(curl -o $tmp -w '%{http_code}' $url $time_cond) - if [ "$rcode" == "200" ] ; then - echo $success - 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 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 +$TMP_HOOKS_PATH/bin/cache-url $SHA256SUMS $IMG_PATH/SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH +$TMP_HOOKS_PATH/bin/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