From 6c997fda973486254aa7b0b4f01d622d596a0f78 Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Mon, 17 Jun 2013 16:26:02 +0100 Subject: [PATCH] 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 --- elements/cache-url/README.md | 1 + elements/cache-url/bin/cache-url | 48 +++++++++++++++++++ elements/fedora/element-deps | 1 + elements/fedora/root.d/10-fedora-cloud-image | 13 ++--- elements/ubuntu/element-deps | 1 + .../ubuntu/root.d/10-cache-ubuntu-tarball | 34 +------------ 6 files changed, 58 insertions(+), 40 deletions(-) create mode 100644 elements/cache-url/README.md create mode 100755 elements/cache-url/bin/cache-url 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