2 ##############################################################################
3 # Copyright (c) 2016 Red Hat Inc.
4 # Dan Radez <dradez@redhat.com>
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
12 # Make sure the cache dir exists
14 if [ -f $CACHE_DIR ]; then rm -rf $CACHE_DIR; fi
15 if [ ! -d $CACHE_DIR/ ]; then mkdir $CACHE_DIR/; fi
16 if [ ! -f $CACHE_DIR/$CACHE_HISTORY ]; then touch $CACHE_DIR/$CACHE_HISTORY; fi
17 echo "Cache Dir: $CACHE_DIR"
21 # $2 = filename to write to
23 if [ -f $CACHE_DIR/$2 ]; then
24 echo "Removing stale $2"
28 echo "Cache download location: $CACHE_DIR/$2"
29 until curl -C- -L -o $CACHE_DIR/$2 $1 || (( count++ >= 20 )); do
30 echo -n '' #do nothing, we just want to loop
32 sed -i "/$2/d" $CACHE_DIR/$CACHE_HISTORY
33 echo "$(md5sum $CACHE_DIR/$2) $2" >> $CACHE_DIR/$CACHE_HISTORY
38 function populate_cache {
47 # check if the cache file exists
48 # and if it has an md5 compare that
49 echo "Checking if cache file exists: ${filename}"
50 if [ ! -f $CACHE_DIR/${filename} ]; then
51 echo "Cache file: ${CACHE_DIR}/${filename} missing...will download..."
52 curl_file $1 $filename
54 echo "Cache file exists...comparing MD5 checksum"
55 if [ -z "$remote_md5" ]; then
56 remote_md5="$(curl -sf -L ${1}.md5 | awk {'print $1'})"
58 if [ -z "$remote_md5" ]; then
59 echo "Got empty MD5 from remote for $filename, skipping MD5 check"
60 curl_file $1 $filename
62 my_md5=$(grep ${filename} ${CACHE_DIR}/${CACHE_HISTORY} | awk {'print $1'})
63 if [ -z "$my_md5" ]; then
64 echo "${filename} missing in ${CACHE_HISTORY} file. Caculating md5..."
65 my_md5=$(md5sum ${CACHE_DIR}/${filename} | awk {'print $1'})
67 if [ "$remote_md5" != "$my_md5" ]; then
68 echo "MD5 mismatch, local cache file MD5 is ${my_md5}"
69 echo " remote cache file MD5 is ${remote_md5}"
70 echo "Downloading $filename"
71 curl_file $1 $filename
73 echo "Will use cache for ${filename}"