This repository has been archived by the owner on Aug 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
/
cache.go
121 lines (102 loc) · 2.5 KB
/
cache.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package shells
import (
"bytes"
"encoding/xml"
"io/ioutil"
"net/http"
"net/url"
"path"
"strconv"
"time"
"github.com/minio/minio-go"
"github.com/Sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
)
type bucketLocationTripper struct {
bucketLocation string
}
func (b *bucketLocationTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {
var buffer bytes.Buffer
xml.NewEncoder(&buffer).Encode(b.bucketLocation)
res = &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(&buffer),
}
return
}
func (b *bucketLocationTripper) CancelRequest(req *http.Request) {
// Do nothing
}
func getCacheObjectName(build *common.Build, cache *common.CacheConfig, key string) string {
if key == "" {
return ""
}
return path.Join("runner", build.Runner.ShortDescription(), "project", strconv.Itoa(build.ProjectID), key)
}
func getCacheStorageClient(cache *common.CacheConfig) (scl *minio.Client, err error) {
scl, err = minio.New(cache.ServerAddress, cache.AccessKey, cache.SecretKey, cache.Insecure)
if err != nil {
logrus.Warningln(err)
return
}
scl.SetCustomTransport(&bucketLocationTripper{cache.BucketLocation})
return
}
func getS3DownloadURL(build *common.Build, key string) (url *url.URL) {
cache := build.Runner.Cache
objectName := getCacheObjectName(build, cache, key)
if objectName == "" {
return
}
scl, err := getCacheStorageClient(cache)
if err != nil {
logrus.Warningln(err)
return
}
url, err = scl.PresignedGetObject(cache.BucketName, objectName, time.Second*time.Duration(build.Timeout), nil)
if err != nil {
logrus.Warningln(err)
return
}
return
}
func getCacheDownloadURL(build *common.Build, key string) (url *url.URL) {
cache := build.Runner.Cache
if cache == nil {
return
}
switch cache.Type {
case "s3":
return getS3DownloadURL(build, key)
}
return
}
func getS3UploadURL(build *common.Build, key string) (url *url.URL) {
cache := build.Runner.Cache
objectName := getCacheObjectName(build, cache, key)
if objectName == "" {
return
}
scl, err := getCacheStorageClient(cache)
if err != nil {
logrus.Warningln(err)
return
}
url, err = scl.PresignedPutObject(cache.BucketName, objectName, time.Second*time.Duration(build.Timeout))
if err != nil {
logrus.Warningln(err)
return
}
return
}
func getCacheUploadURL(build *common.Build, key string) (url *url.URL) {
cache := build.Runner.Cache
if cache == nil {
return
}
switch cache.Type {
case "s3":
return getS3UploadURL(build, key)
}
return
}