-
Notifications
You must be signed in to change notification settings - Fork 85
/
build_script_generator.go
166 lines (142 loc) · 4.54 KB
/
build_script_generator.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package worker
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
gocontext "context"
"github.com/travis-ci/worker/config"
"github.com/travis-ci/worker/metrics"
)
// A BuildScriptGeneratorError is sometimes used by the Generate method on a
// BuildScriptGenerator to return more metadata about an error.
type BuildScriptGeneratorError struct {
error
// true when this error can be recovered by retrying later
Recover bool
}
// A BuildScriptGenerator generates a build script for a given job payload.
type BuildScriptGenerator interface {
Generate(gocontext.Context, Job) ([]byte, error)
}
type webBuildScriptGenerator struct {
URL string
aptCacheHost string
npmCacheHost string
paranoid bool
fixResolvConf bool
fixEtcHosts bool
cacheType string
cacheFetchTimeout int
cachePushTimeout int
s3CacheOptions s3BuildCacheOptions
httpClient *http.Client
}
type s3BuildCacheOptions struct {
scheme string
region string
bucket string
accessKeyID string
secretAccessKey string
}
// NewBuildScriptGenerator creates a generator backed by an HTTP API.
func NewBuildScriptGenerator(cfg *config.Config) BuildScriptGenerator {
return &webBuildScriptGenerator{
URL: cfg.BuildAPIURI,
aptCacheHost: cfg.BuildAptCache,
npmCacheHost: cfg.BuildNpmCache,
paranoid: cfg.BuildParanoid,
fixResolvConf: cfg.BuildFixResolvConf,
fixEtcHosts: cfg.BuildFixEtcHosts,
cacheType: cfg.BuildCacheType,
cacheFetchTimeout: int(cfg.BuildCacheFetchTimeout.Seconds()),
cachePushTimeout: int(cfg.BuildCachePushTimeout.Seconds()),
s3CacheOptions: s3BuildCacheOptions{
scheme: cfg.BuildCacheS3Scheme,
region: cfg.BuildCacheS3Region,
bucket: cfg.BuildCacheS3Bucket,
accessKeyID: cfg.BuildCacheS3AccessKeyID,
secretAccessKey: cfg.BuildCacheS3SecretAccessKey,
},
httpClient: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: cfg.BuildAPIInsecureSkipVerify,
},
},
},
}
}
func (g *webBuildScriptGenerator) Generate(ctx gocontext.Context, job Job) ([]byte, error) {
payload := job.RawPayload()
if g.aptCacheHost != "" {
payload.SetPath([]string{"hosts", "apt_cache"}, g.aptCacheHost)
}
if g.npmCacheHost != "" {
payload.SetPath([]string{"hosts", "npm_cache"}, g.npmCacheHost)
}
payload.Set("paranoid", g.paranoid)
payload.Set("fix_resolv_conf", g.fixResolvConf)
payload.Set("fix_etc_hosts", g.fixEtcHosts)
if g.cacheType != "" {
payload.SetPath([]string{"cache_options", "type"}, g.cacheType)
payload.SetPath([]string{"cache_options", "fetch_timeout"}, g.cacheFetchTimeout)
payload.SetPath([]string{"cache_options", "push_timeout"}, g.cachePushTimeout)
payload.SetPath([]string{"cache_options", "s3", "scheme"}, g.s3CacheOptions.scheme)
payload.SetPath([]string{"cache_options", "s3", "region"}, g.s3CacheOptions.region)
payload.SetPath([]string{"cache_options", "s3", "bucket"}, g.s3CacheOptions.bucket)
payload.SetPath([]string{"cache_options", "s3", "access_key_id"}, g.s3CacheOptions.accessKeyID)
payload.SetPath([]string{"cache_options", "s3", "secret_access_key"}, g.s3CacheOptions.secretAccessKey)
}
b, err := payload.Encode()
if err != nil {
return nil, err
}
var token string
u, err := url.Parse(g.URL)
if err != nil {
return nil, err
}
if u.User != nil {
token = u.User.Username()
u.User = nil
}
jp := job.Payload()
if jp != nil {
q := u.Query()
q.Set("job_id", strconv.FormatUint(jp.Job.ID, 10))
q.Set("source", "worker")
u.RawQuery = q.Encode()
}
buf := bytes.NewBuffer(b)
req, err := http.NewRequest("POST", u.String(), buf)
if err != nil {
return nil, err
}
if token != "" {
req.Header.Set("Authorization", "token "+token)
}
req.Header.Set("User-Agent", fmt.Sprintf("worker-go v=%v rev=%v d=%v", VersionString, RevisionString, GeneratedString))
req.Header.Set("Content-Type", "application/json")
startRequest := time.Now()
resp, err := g.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
metrics.TimeSince("worker.job.script.api", startRequest)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 500 {
return nil, BuildScriptGeneratorError{error: fmt.Errorf("server error: %q", string(body)), Recover: true}
} else if resp.StatusCode >= 400 {
return nil, BuildScriptGeneratorError{error: fmt.Errorf("client error: %q", string(body)), Recover: false}
}
return body, nil
}