Cache as we go instead of in the end
[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
13 exit_trap() {
14     if [ -d "$TMPDIR" ]; then
15         rm -rf $TMPDIR
16     fi
17 }
18
19 trap exit_trap EXIT
20
21 CACHETRANSPORT=${CACHETRANSPORT:-"curl --silent"}
22 CACHEMAXAGE=${CACHEMAXAGE:-$[14*24*3600]}
23 CACHEDEBUG=${CACHEDEBUG:-1}
24
25 debugmsg () {
26     if [ "$CACHEDEBUG" -eq 1 ]; then
27         echo "$@" >&2
28     fi
29 }
30
31 errorexit () {
32     echo "$@" >&2
33     exit 1
34 }
35
36 # Get a SHA1 based on what's piped into the cache command
37 getid() {
38     debugmsg "Generating sha1sum"
39     sha1sum | sed 's/ .*//'
40 }
41
42
43 # Put in cache
44 put() {
45     if check $1; then
46        debugmsg "SHA1 $1 already in cache, skipping storage"
47     else
48         debugmsg "Storing SHA1 $1 in cache"
49         ${CACHETRANSPORT} -T - ${CACHEBASE}/$1.blob
50         echo "Expires: $[`date +"%s"` + $CACHEMAXAGE]" | ${CACHETRANSPORT} -T - ${CACHEBASE}/$1.meta
51     fi
52     exit 0
53 }
54
55 # Get from cache
56 get() {
57     local rc
58
59     ${CACHETRANSPORT} -o - ${CACHEBASE}/$1.blob 2>/dev/null
60     rc=$?
61
62     if [ $rc -eq 0 ]; then
63         echo "Got SHA1 $1 from cache" 2>/dev/null
64     else
65         echo "Tried to get SHA1 $1 from cache but failed" 2>/dev/null
66     fi
67
68     return $?
69 }
70
71 # Check if in cache
72 check() {
73     local rc
74
75     ${CACHETRANSPORT} ${CACHEBASE}/$1.meta &>/dev/null
76     rc=$?
77
78     if [ $rc -eq 0 ]; then
79         debugmsg "Checking for SHA1 $1 in cache and found it, rc = $rc"
80     else
81         debugmsg "Checking for SHA1 $1 in cache and failed, rc = $rc"
82     fi
83
84     return $rc
85 }
86
87 # Verify that SHA1 seems to be a SHA1...
88 validSHA1() {
89     if [ $(echo $1 | wc -c) -ne 41 ]; then
90         return 1
91     else
92         return 0
93     fi
94 }
95
96 # Figure out commit ID from URI and tag/branch/commit ID
97 getcommitid() {
98     if echo $2 | grep -q '^refs/changes/'; then
99         REF=`echo $2 | sed "s,refs\/changes\/\(.*\),\1,"`
100     else
101         REF=$2
102     fi
103
104     echo "Repo is $1, ref is ${REF}" >&2
105
106     HEADMATCH=`git ls-remote $1 | grep "refs/heads/${REF}$" | awk '{ print $1 }'`
107     TAGMATCH=`git ls-remote $1 | grep "refs/tags/${REF}$" | awk '{ print $1 }'`
108     CHANGEMATCH=`git ls-remote $1 | grep "refs/changes/${REF}$" | awk '{ print $1 }'`
109
110     if [ -n "$HEADMATCH" ]; then
111         echo "$HEADMATCH"
112     elif [ -n "$TAGMATCH" ]; then
113         echo "$TAGMATCH"
114     elif [ -n "$CHANGEMATCH" ]; then
115         echo "Warning: ${REF} is a change!" >&2
116         TMPDIR=`mktemp -d /tmp/cacheXXXXX`
117         cd $TMPDIR
118         git clone $1 &>/dev/null || errorexit "Could not clone $1"
119         cd * || errorexit "Could not enter clone of $1"
120         git fetch $1 refs/changes/$REF &>/dev/null || errorexit "Could not fetch change"
121         git checkout FETCH_HEAD &>/dev/null || errorexit "Could not checkout FETCH_HEAD"
122         git show HEAD &>/dev/null || errorexit "Could not find commit $2"
123         git show HEAD | head -1 | awk '{ print $2 }'
124     else
125         TMPDIR=`mktemp -d /tmp/cacheXXXXX`
126         cd $TMPDIR
127         git clone $1 &>/dev/null || errorexit "Could not clone $1"
128         cd * || errorexit "Could not enter clone of $1"
129         git show $2 &>/dev/null || errorexit "Could not find commit $2"
130         git show $2 | head -1 | awk '{ print $2 }'
131     fi
132 }
133
134
135
136 if [ -z "$CACHEBASE" ]; then
137   errorexit "CACHEBASE not set - exiting..."
138 fi
139
140 case $1 in
141     getcommitid)
142         if [ $# -ne 3 ]; then
143             errorexit "Arg 1 needs to be URI and arg 2 tag/branch/commit"
144         fi
145         shift
146         getcommitid $@
147         ;;
148     getid)
149         if [ $# -ne 1 ]; then
150             errorexit "No arguments can be given to getid!"
151         fi
152         getid
153         ;;
154     get|check|put)
155         if [ $# -ne 2 ]; then
156             errorexit "Only one argument, the SHA1 sum, can be given to getid!"
157         else
158             if ! validSHA1 $2; then
159                 errorexit "Invalid SHA1 format!"
160             fi
161         fi
162
163         $1 $2
164         exit $rc
165         ;;
166     *)
167         errorexit "I only know about getcommitid, getid, check, get and put!"
168 esac