-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcached_curl
executable file
·59 lines (44 loc) · 948 Bytes
/
cached_curl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
url=$1
if [ -z "$BUILD_CACHE_BUCKET" ]; then
exit 1
fi
if [ -z $url ]; then
exit 0
fi
get_s3() {
s3path=$1
filename=$2
aws s3 cp $s3path $filename 1>&2
}
put_s3() {
s3path=$1
filename=$2
aws s3 cp --dryrun $filename $s3path 1>&2
aws s3 cp $filename $s3path 1>&2
}
urlid=$(echo -n "$url" | md5sum)
if [ -n "$WORKDIR" ]; then
mkdir -p $WORKDIR/tmp
TMPDIR=$WORKDIR/tmp
export TMPDIR
fi
tempfile=$(mktemp)
cached_url="s3://${BUILD_CACHE_BUCKET}/cache/$urlid"
get_s3 $cached_url $tempfile
if [[ $? -gt 0 || -s "$tempfile" ]]; then
>&2 echo "Retrieving $@ into build cache (via $tempfile)"
curl --progress-bar $@ -o $tempfile
if [ $? -gt 0 ]; then
>&2 echo "Failure to retrieve file"
exit 1
fi
>&2 echo "Copying $tempfile to $cached_url"
put_s3 $cached_url $tempfile
if [ $? -gt 0 ]; then
>&2 echo "Could not cache file"
exit 1
fi
>&2 echo "Copied file for caching"
fi
cat $tempfile && rm $tempfile