Merge "Re-use cache_url() in fedora element."

This commit is contained in:
Jenkins 2013-06-26 13:04:24 +00:00 committed by Gerrit Code Review
commit 860090bd1a
6 changed files with 58 additions and 40 deletions

View File

@ -0,0 +1 @@
A helper script to download images into a local cache.

View File

@ -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

View File

@ -1,2 +1,3 @@
dib-run-parts
dracut-network
cache-url

View File

@ -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"

View File

@ -1,2 +1,3 @@
dpkg
dib-run-parts
cache-url

View File

@ -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