diff options
author | Jonas Bjurel <jonas.bjurel@ericsson.com> | 2015-11-25 11:32:57 +0100 |
---|---|---|
committer | Jonas Bjurel <jonas.bjurel@ericsson.com> | 2015-11-27 10:24:23 +0100 |
commit | 24a95306d2564b272b5320e9149d9aea70b4061c (patch) | |
tree | 8c1a2c9f7acbfed41f8ebc56a8ae1f7d316f7cff /build/cache.sh | |
parent | 7077376d6e0ff0dec77080fa21b75911b811475d (diff) |
Restructcture of the directory layout
Restructure of the directory layout due to move of Fuel into it's own repo
JIRA: FUEL-85
Change-Id: I3647e1992a508f29dce06a5d6c790725c527f6f5
Signed-off-by: Jonas Bjurel <jonas.bjurel@ericsson.com>
Diffstat (limited to 'build/cache.sh')
-rwxr-xr-x | build/cache.sh | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/build/cache.sh b/build/cache.sh new file mode 100755 index 000000000..c8cd1b03d --- /dev/null +++ b/build/cache.sh @@ -0,0 +1,112 @@ +#!/bin/bash +############################################################################## +# Copyright (c) 2015 Ericsson AB and others. +# stefan.k.berg@ericsson.com +# jonas.bjurel@ericsson.com +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +CACHETRANSPORT=${CACHETRANSPORT:-"curl --silent"} +CACHEBASE=${CACHEBASE:-"file://${HOME}/cache"} +CACHEMAXAGE=${CACHEMAXAGE:-$[14*24*3600]} +CACHEDEBUG=${CACHEDEBUG:-1} + +debugmsg () { + if [ "$CACHEDEBUG" -eq 1 ]; then + echo "$@" >&2 + fi +} + +errormsg () { + echo "$@" >&2 +} + +# Get a SHA1 based on what's piped into the cache command +getid() { + debugmsg "Generating sha1sum" + sha1sum | sed 's/ .*//' +} + + +# Put in cache +put() { + if check $1; then + debugmsg "SHA1 $1 already in cache, skipping storage" + else + debugmsg "Storing SHA1 $1 in cache" + ${CACHETRANSPORT} -T - ${CACHEBASE}/$1.blob + echo "Expires: $[`date +"%s"` + $CACHEMAXAGE]" | ${CACHETRANSPORT} -T - ${CACHEBASE}/$1.meta + fi + exit 0 +} + +# Get from cache +get() { + local rc + + ${CACHETRANSPORT} -o - ${CACHEBASE}/$1.blob 2>/dev/null + rc=$? + + if [ $rc -eq 0 ]; then + echo "Got SHA1 $1 from cache" 2>/dev/null + else + echo "Tried to get SHA1 $1 from cache but failed" 2>/dev/null + fi + + return $? +} + +# Check if in cache +check() { + local rc + + ${CACHETRANSPORT} ${CACHEBASE}/$1.meta &>/dev/null + rc=$? + + if [ $rc -eq 0 ]; then + debugmsg "Checking for SHA1 $1 in cache and found it, rc = $rc" + else + debugmsg "Checking for SHA1 $1 in cache and failed, rc = $rc" + fi + + return $rc +} + +# Verify that SHA1 seems to be a SHA1... +validSHA1() { + if [ $(echo $1 | wc -c) -ne 41 ]; then + return 1 + else + return 0 + fi +} + +case $1 in + getid) + if [ $# -ne 1 ]; then + errormsg "No arguments can be given to getid!" + exit 1 + fi + getid + ;; + get|check|put) + if [ $# -ne 2 ]; then + errormsg "Only one argument, the SHA1 sum, can be given to getid!" + exit 1 + else + if ! validSHA1 $2; then + errormsg "Invalid SHA1 format!" + exit 1 + fi + fi + + $1 $2 + exit $rc + ;; + *) + errormsg "I only know about getid, check, get and put!" + exit 1 +esac |