-
Notifications
You must be signed in to change notification settings - Fork 4
/
builder.go
294 lines (241 loc) · 6.68 KB
/
builder.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package google
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
log "github.com/Sirupsen/logrus"
pb "github.com/datacol-io/datacol/api/models"
"github.com/datacol-io/datacol/common"
docker "github.com/fsouza/go-dockerclient"
"google.golang.org/api/cloudbuild/v1"
"google.golang.org/api/googleapi"
"google.golang.org/api/storage/v1"
)
const (
buildKind = "Build"
releaseKind = "Release"
)
func (g *GCPCloud) BuildGet(app, id string) (*pb.Build, error) {
b, err := g.store.BuildGet(app, id)
if err != nil {
return nil, err
}
// Sometime GCP don't assign Status for a newly trigged build. We should also check for empty build status.
if b.Status == "WORKING" || b.Status == "CREATED" || b.Status == "" {
cb := g.cloudbuilder()
rb, err := cb.Projects.Builds.Get(g.Project, b.RemoteId).Do()
if err != nil {
return nil, err
}
if b.Status != rb.Status {
b.Status = rb.Status
return b, g.store.BuildSave(b)
}
}
return b, nil
}
func (g *GCPCloud) BuildDelete(app, id string) error {
ctx, key := g.nestedKey(buildKind, id)
return g.datastore().Delete(ctx, key)
}
func (g *GCPCloud) BuildList(app string, limit int64) (pb.Builds, error) {
return g.store.BuildList(app, limit)
}
func (g *GCPCloud) BuildCreate(app string, req *pb.CreateBuildOptions) (*pb.Build, error) {
id := req.DockerTag
if id == "" {
id = generateId("B", 5)
}
build := &pb.Build{
App: app,
Id: id,
Status: "CREATED",
Version: req.Version,
Procfile: req.Procfile,
CreatedAt: timestampNow(),
}
return build, g.store.BuildSave(build)
}
func (a *GCPCloud) BuildImport(id string, tr io.Reader, w io.WriteCloser) error {
build, err := a.BuildGet("", id)
if err != nil {
return err
}
dkr, err := docker.NewClientFromEnv()
if err != nil {
return fmt.Errorf("docker client: %v", err)
}
app, id := build.App, build.Id
target := fmt.Sprintf("gcr.io/%s/%v", a.Project, app)
err = common.BuildDockerLoad(target, id, dkr, tr, w, nil)
if err == nil {
build.Status = "SUCCESS"
} else {
build.Status = "FAILED"
}
// Ignore the error by build save
if berr := a.store.BuildSave(build); berr != nil {
log.Errorf("Failed to save build: %v", berr)
}
return err
}
func (g *GCPCloud) BuildUpload(id, filename string) error {
build, err := g.BuildGet("", id)
if err != nil {
return err
}
service, bucket := g.storage(), g.BucketName
gskey, app := fmt.Sprintf("%s.tar.gz", id), build.App
log.Infof("Pushing code to gs://%s/%s", bucket, gskey)
object := &storage.Object{
Bucket: bucket,
Name: gskey,
ContentType: "application/gzip",
}
reader, err := os.Open(filename)
if err != nil {
return fmt.Errorf("reading tempfile err: %v", err)
}
defer reader.Close()
if _, err := service.Objects.Insert(bucket, object).Media(reader).Do(); err != nil {
return fmt.Errorf("Uploading to gs://%s/%s err: %s", bucket, gskey, err)
}
cb := g.cloudbuilder()
log.Infof("Building from gs://%s/%s", bucket, gskey)
tag := fmt.Sprintf("gcr.io/$PROJECT_ID/%v:%v", app, id)
latestTag := fmt.Sprintf("gcr.io/$PROJECT_ID/%v:latest", app)
stepBuildArgs := []string{"build", "-t", tag, "-t", latestTag}
envVars, _ := g.EnvironmentGet(app)
for key, val := range envVars {
if val != "" {
stepBuildArgs = append(stepBuildArgs, fmt.Sprintf("--build-arg=%s=%s", key, val))
}
}
op, err := cb.Projects.Builds.Create(g.Project, &cloudbuild.Build{
LogsBucket: bucket,
Source: &cloudbuild.Source{
StorageSource: &cloudbuild.StorageSource{
Bucket: bucket,
Object: gskey,
},
},
Steps: []*cloudbuild.BuildStep{
{
Name: "gcr.io/cloud-builders/docker:17.12.0",
Args: append(stepBuildArgs, "."),
},
},
Images: []string{tag},
}).Do()
if err != nil {
if ae, ok := err.(*googleapi.Error); ok && ae.Code == 403 {
log.Fatal(ae)
}
return fmt.Errorf("failed to initiate build %v", err)
}
remoteId, err := getBuildID(op)
if err != nil {
return fmt.Errorf("failed to get Id for build %v", err)
}
build.RemoteId = remoteId
return g.store.BuildSave(build)
}
func (g *GCPCloud) BuildLogs(app, id string, index int) (int, []string, error) {
storageService := g.storage()
i, logs, err := buildLogs(storageService, g.BucketName, id, index)
if err != nil {
return i, logs, err
}
return i, logs, err
}
func (g *GCPCloud) BuildLogsStream(id string) (io.Reader, error) {
return nil, fmt.Errorf("Not supported on GCP.")
}
func getBuildID(op *cloudbuild.Operation) (string, error) {
if len(op.Metadata) == 0 {
return "", fmt.Errorf("missing Metadata in operation")
}
bm := &cloudbuild.BuildOperationMetadata{}
if err := json.Unmarshal(op.Metadata, &bm); err != nil {
return "", err
}
return bm.Build.Id, nil
}
func buildLogs(service *storage.Service, bucket, bid string, index int) (int, []string, error) {
if bid == "" {
return 0, []string{}, errors.New("GCR build Id (build.RemoteID) is not provided")
}
key := fmt.Sprintf("log-%s.txt", bid)
log.Debugf("fetching logs from gs://%s/%s", bucket, bid)
lines := []string{}
// container builder might take little time to download source from storage bucket.
// We will loop for 5 minutes and check the status after 1 second.
timeout := time.After(5 * time.Minute)
tick := time.Tick(1 * time.Second)
var resp *http.Response
var err error
Loop:
for {
select {
case <-tick:
resp, err = service.Objects.Get(bucket, key).Download()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
fmt.Print(".")
} else {
return index, lines, fmt.Errorf("fetching body err: %v", err)
}
} else {
break Loop
}
case <-timeout:
return index, lines, fmt.Errorf("Timeout for fetching logs. err: %s", err)
}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return index, lines, err
}
parts := strings.Split(string(body), "\n")
for _, line := range parts[index:] {
if len(line) > 0 && line != "\n" {
lines = append(lines, line)
}
}
return len(parts) - 1, lines, nil
}
func waitForOp(svc *cloudbuild.Service, stsvc *storage.Service, projectId, bucket, id string) (string, error) {
log.Infof("Waiting on build %s", id)
status := "PENDING"
index := 0
for {
time.Sleep(2 * time.Second)
b, err := svc.Projects.Builds.Get(projectId, id).Do()
if err != nil {
log.Fatal(err)
}
status = b.Status
logKey := fmt.Sprintf("log-%s.txt", id)
i, logs, err := buildLogs(stsvc, bucket, logKey, index)
index = i
if err != nil {
log.Fatal(err)
}
for _, line := range logs {
fmt.Println(line)
}
if b.Status != "WORKING" && b.Status != "QUEUED" {
fmt.Printf("\n")
log.Infof("Build status: %s", b.Status)
break
}
}
return status, nil
}