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