c8cd1b03d69c517d8ad27aea239ddd8e235ea4a6
[fuel.git] / build / cache.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # stefan.k.berg@ericsson.com
5 # jonas.bjurel@ericsson.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 CACHETRANSPORT=${CACHETRANSPORT:-"curl --silent"}
13 CACHEBASE=${CACHEBASE:-"file://${HOME}/cache"}
14 CACHEMAXAGE=${CACHEMAXAGE:-$[14*24*3600]}
15 CACHEDEBUG=${CACHEDEBUG:-1}
16
17 debugmsg () {
18     if [ "$CACHEDEBUG" -eq 1 ]; then
19         echo "$@" >&2
20     fi
21 }
22
23 errormsg () {
24     echo "$@" >&2
25 }
26
27 # Get a SHA1 based on what's piped into the cache command
28 getid() {
29     debugmsg "Generating sha1sum"
30     sha1sum | sed 's/ .*//'
31 }
32
33
34 # Put in cache
35 put() {
36     if check $1; then
37        debugmsg "SHA1 $1 already in cache, skipping storage"
38     else
39         debugmsg "Storing SHA1 $1 in cache"
40         ${CACHETRANSPORT} -T - ${CACHEBASE}/$1.blob
41         echo "Expires: $[`date +"%s"` + $CACHEMAXAGE]" | ${CACHETRANSPORT} -T - ${CACHEBASE}/$1.meta
42     fi
43     exit 0
44 }
45
46 # Get from cache
47 get() {
48     local rc
49
50     ${CACHETRANSPORT} -o - ${CACHEBASE}/$1.blob 2>/dev/null
51     rc=$?
52
53     if [ $rc -eq 0 ]; then
54         echo "Got SHA1 $1 from cache" 2>/dev/null
55     else
56         echo "Tried to get SHA1 $1 from cache but failed" 2>/dev/null
57     fi
58
59     return $?
60 }
61
62 # Check if in cache
63 check() {
64     local rc
65
66     ${CACHETRANSPORT} ${CACHEBASE}/$1.meta &>/dev/null
67     rc=$?
68
69     if [ $rc -eq 0 ]; then
70         debugmsg "Checking for SHA1 $1 in cache and found it, rc = $rc"
71     else
72         debugmsg "Checking for SHA1 $1 in cache and failed, rc = $rc"
73     fi
74
75     return $rc
76 }
77
78 # Verify that SHA1 seems to be a SHA1...
79 validSHA1() {
80     if [ $(echo $1 | wc -c) -ne 41 ]; then
81         return 1
82     else
83         return 0
84     fi
85 }
86
87 case $1 in
88     getid)
89         if [ $# -ne 1 ]; then
90             errormsg "No arguments can be given to getid!"
91             exit 1
92         fi
93         getid
94         ;;
95     get|check|put)
96         if [ $# -ne 2 ]; then
97             errormsg "Only one argument, the SHA1 sum, can be given to getid!"
98             exit 1
99         else
100             if ! validSHA1 $2; then
101                 errormsg "Invalid SHA1 format!"
102                 exit 1
103             fi
104         fi
105
106         $1 $2
107         exit $rc
108         ;;
109     *)
110         errormsg "I only know about getid, check, get and put!"
111         exit 1
112 esac