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