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