-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudbuild.go
182 lines (155 loc) · 4.21 KB
/
cloudbuild.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
package cloudbuild
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"time"
"github.com/urvil38/kubepaas/config"
"google.golang.org/api/cloudbuild/v1"
)
const (
maxRetries = 60 * 60
retryInterval = 1 * time.Second
)
type builder struct {
ProjectName string
Build cloudbuild.Build
CloudbuildService cloudbuild.Service
}
var errTimeout = errors.New("Operation %s timed out. This operation may still be underway")
func newBuilder(projectName, buildType string) (builder, error) {
var build cloudbuild.Build
if buildType == "docker" {
cb, err := ioutil.ReadFile(filepath.Join(config.KubeConfig.KubepaasRoot, "docker-cloudbuild.json"))
if err != nil {
return builder{}, err
}
err = json.Unmarshal(cb, &build)
if err != nil {
return builder{}, err
}
}
if buildType == "kubernetes" {
cb, err := ioutil.ReadFile(filepath.Join(config.KubeConfig.KubepaasRoot, "kubernetes-cloudbuild.json"))
if err != nil {
return builder{}, err
}
err = json.Unmarshal(cb, &build)
if err != nil {
return builder{}, err
}
}
cloudbuildService, err := getCloudBuildClient()
if err != nil {
return builder{}, err
}
return builder{
ProjectName: projectName,
Build: build,
CloudbuildService: *cloudbuildService,
}, nil
}
func CreateNewBuild(projectName string, buildType string) error {
builder, err := newBuilder(projectName, buildType)
if err != nil {
return err
}
ProjectBuildService := cloudbuild.NewProjectsBuildsService(&builder.CloudbuildService)
ProjectBuildCreateCall := ProjectBuildService.Create(builder.ProjectName, &builder.Build)
buildOp, err := ProjectBuildCreateCall.Do()
if err != nil {
return err
}
err = waitAndStreamLogs(buildOp)
if err != nil {
return err
}
return nil
}
type logConfig struct {
buildID string
logBucket string
}
func waitAndStreamLogs(buildOp *cloudbuild.Operation) error {
m, _ := buildOp.Metadata.MarshalJSON()
var cb customBuild
err := json.Unmarshal(m, &cb)
if err != nil {
fmt.Print(err)
}
var gcsPrefix = "gs://"
if strings.Contains(cb.Build.LogsBucket, gcsPrefix) {
cb.Build.LogsBucket = strings.TrimPrefix(cb.Build.LogsBucket, gcsPrefix)
}
lc := logConfig{
buildID: cb.Build.ID,
logBucket: cb.Build.LogsBucket,
}
//fmt.Printf("Started cloud build [%v]\n", lc.buildID)
cloudbuildLogfileFmtString := `log-%s.txt`
if lc.logBucket != "" {
logObject := fmt.Sprintf(cloudbuildLogfileFmtString, lc.buildID)
logTailer := NewLogTailer(lc.logBucket, logObject)
var callback func(bool)
if logTailer != nil {
callback = logTailer.Poll
}
op, err := waitForOperation(buildOp, callback)
if err == errTimeout {
return errTimeout
}
if logTailer != nil {
logTailer.Poll(true)
}
finalStatus := getStatusFromOperation(op)
if finalStatus != "SUCCESS" {
return fmt.Errorf("cloud build failed. Faliure status : %v", finalStatus)
}
}
return nil
}
func waitForOperation(buildOp *cloudbuild.Operation, callback func(bool)) (cloudbuild.Operation, error) {
completedOperation, err := pollUntilDone(buildOp, callback)
if err != nil {
return cloudbuild.Operation{}, err
}
return completedOperation, nil
}
func getStatusFromOperation(op cloudbuild.Operation) string {
m, _ := op.Metadata.MarshalJSON()
var cb customBuild
err := json.Unmarshal(m, &cb)
if err != nil {
fmt.Printf("Unable to unmarshal Operation struct : %v\n", err)
}
if cb.Build.Status != "" {
return cb.Build.Status
}
return "UNKNOWN"
}
func pollUntilDone(op *cloudbuild.Operation, callback func(bool)) (cloudbuild.Operation, error) {
if op.Done {
return *op, nil
}
cloudService, _ := getCloudBuildClient()
operationGetCall := cloudService.Operations.Get(op.Name)
for i := 0; i < maxRetries; i++ {
operation, err := operationGetCall.Do()
if err != nil {
return cloudbuild.Operation{}, err
}
if operation.Done {
//fmt.Printf("Operation %v complete\n", operation.Name)
return *operation, nil
}
//fmt.Printf("Operation %v not complete. Waiting %v\n",operation.Name,RETRY_INTERVAL.String())
time.Sleep(retryInterval)
if callback != nil {
callback(false)
}
}
return cloudbuild.Operation{}, errors.New("Unexpected errors occured in pollUntilDone() function")
}