Re-use cache_url() in fedora element.

The fedora element downloads images too, so we should re-use the caching
code from the ubuntu element.

There doesn't seem to be other examples of code shared between root.d
scripts. In the fedora and dpkg elements we copy install-packages into
the chroot, but that model doesn't apply when we're running scripts
outside of the chroot. Seems sane to just run it directly from the bin/
dir in the temporary hooks directory.

Change-Id: Iaa6aca660042fea323cab4271633a4bdbbc271b8
This commit is contained in:
Mark McLoughlin 2013-06-17 16:26:02 +01:00
parent efb1f435d4
commit 6c997fda97
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 dib-run-parts
dracut-network 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_FILE=${BASE_IMAGE_FILE:-$DIB_RELEASE-Cloud-$ARCH-latest.qcow2}
BASE_IMAGE_TAR=$DIB_RELEASE-Cloud-$ARCH-latest.tgz 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" echo "Fetching Base Image"
wget $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE -O $IMG_PATH/$BASE_IMAGE_FILE.tmp $TMP_HOOKS_PATH/bin/cache-url $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE $IMG_PATH/$BASE_IMAGE_FILE
mv $IMG_PATH/$BASE_IMAGE_FILE.tmp $IMG_PATH/$BASE_IMAGE_FILE
fi if [ ! -f $IMG_PATH/$BASE_IMAGE_TAR -o \
if [ ! -f $IMG_PATH/$BASE_IMAGE_TAR ] ; then $IMG_PATH/$BASE_IMAGE_FILE -nt $IMG_PATH/$BASE_IMAGE_TAR ] ; then
echo "Repacking base image as tarball." echo "Repacking base image as tarball."
WORKING=$(mktemp -d) WORKING=$(mktemp -d)
EACTION="rm -r $WORKING" EACTION="rm -r $WORKING"

View File

@ -1,2 +1,3 @@
dpkg dpkg
dib-run-parts 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} 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
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" echo "Fetching Base Image"
cache_url $SHA256SUMS $IMG_PATH/SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH $TMP_HOOKS_PATH/bin/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 $DIB_CLOUD_IMAGES/$DIB_RELEASE/current/$BASE_IMAGE_FILE $IMG_PATH/$BASE_IMAGE_FILE
pushd $IMG_PATH pushd $IMG_PATH
grep "$BASE_IMAGE_FILE" SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH | sha256sum --check - grep "$BASE_IMAGE_FILE" SHA256SUMS.ubuntu.$DIB_RELEASE.$ARCH | sha256sum --check -
popd popd