-
Notifications
You must be signed in to change notification settings - Fork 290
/
update_uploader.go
207 lines (170 loc) · 5.12 KB
/
update_uploader.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
package cloud
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/windmilleng/tilt/internal/cloud/cloudurl"
"github.com/windmilleng/tilt/internal/feature"
"github.com/windmilleng/tilt/internal/store"
"github.com/windmilleng/tilt/internal/token"
"github.com/windmilleng/tilt/pkg/logger"
"github.com/windmilleng/tilt/pkg/model"
)
type UpdateUploader struct {
client HttpClient
addr cloudurl.Address
lastCompletedBuildCount int
lastFinishTime time.Time
}
func NewUpdateUploader(client HttpClient, addr cloudurl.Address) *UpdateUploader {
return &UpdateUploader{
client: client,
addr: addr,
}
}
// TODO(nick): Generate these with protobufs.
type updateServiceSpec struct {
Name string `json:"name"`
}
type snapshotID struct {
ID string `json:"id"`
}
type update struct {
Service updateServiceSpec `json:"service"`
StartTime string `json:"start_time"`
Duration string `json:"duration"`
IsLiveUpdate bool `json:"is_live_update"`
// 0 = SUCCESS, 1 = FAILURE
Result int `json:"result"`
ResultDescription string `json:"result_description"`
SnapshotID snapshotID `json:"snapshot_id"`
}
type teamID struct {
ID string `json:"id"`
}
type updatePayload struct {
TeamID teamID `json:"team_id"`
Updates []update `json:"updates"`
}
func (p updatePayload) empty() bool {
return len(p.Updates) == 0
}
type updateTask struct {
token token.Token
updatePayload updatePayload
}
func (t updateTask) empty() bool {
return t.updatePayload.empty()
}
func (t updateTask) updates() []update {
return t.updatePayload.Updates
}
func (u *UpdateUploader) putUpdatesURL() string {
url := cloudurl.URL(string(u.addr))
url.Path = "/api/usage/team/put_updates"
return url.String()
}
// Check the engine state to see if we have any updates.
func (u *UpdateUploader) makeUpdates(ctx context.Context, st store.RStore) updateTask {
state := st.RLockState()
defer st.RUnlockState()
// Check if this feature is disabled
if !state.Features[feature.UpdateHistory] {
return updateTask{}
}
// If we don't have an authenticated token or team-name,
// we won't be able to upload anything anyway.
if state.Token == "" || state.TeamName == "" || state.TiltCloudUsername == "" {
return updateTask{}
}
// Do a quick check to see if any builds have completed since we last checked.
if state.CompletedBuildCount == 0 || state.CompletedBuildCount <= u.lastCompletedBuildCount {
return updateTask{}
}
// OK, we know we have work to do!
highWaterMark := u.lastFinishTime
updates := []update{}
processManifestState := func(name model.ManifestName, status store.ManifestState) {
for _, record := range status.BuildHistory {
// The BuildHistory is stored most-recent first, so we can stop iterating
// as soon as we see one newer than the high-water mark.
if !record.FinishTime.After(u.lastFinishTime) {
break
}
if record.FinishTime.After(highWaterMark) {
highWaterMark = record.FinishTime
}
resultCode := 0
resultDescription := ""
if record.Error != nil {
resultCode = 1
resultDescription = record.Error.Error()
}
updates = append(updates, update{
Service: updateServiceSpec{
Name: name.String(),
},
StartTime: record.StartTime.Format(time.RFC3339),
Duration: record.Duration().String(),
IsLiveUpdate: record.HasBuildType(model.BuildTypeLiveUpdate),
Result: resultCode,
ResultDescription: resultDescription,
})
}
}
processManifestState(store.TiltfileManifestName, state.TiltfileState)
for _, target := range state.ManifestTargets {
processManifestState(target.Manifest.Name, *(target.State))
}
u.lastFinishTime = highWaterMark
u.lastCompletedBuildCount = state.CompletedBuildCount
return updateTask{
token: state.Token,
updatePayload: updatePayload{
TeamID: teamID{ID: state.TeamName},
Updates: updates,
},
}
}
func (u *UpdateUploader) sendUpdates(ctx context.Context, task updateTask) {
buf := bytes.NewBuffer(nil)
encoder := json.NewEncoder(buf)
err := encoder.Encode(task.updatePayload)
if err != nil {
logger.Get(ctx).Infof("Error encoding updates: %v", err)
return
}
request, err := http.NewRequest(http.MethodPost, u.putUpdatesURL(), buf)
if err != nil {
logger.Get(ctx).Infof("Error sending updates: %v", err)
return
}
request.Header.Set(TiltTokenHeaderName, task.token.String())
response, err := u.client.Do(request)
if err != nil {
logger.Get(ctx).Infof("Error sending updates: %v", err)
return
}
if response.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(response.Body)
if err != nil {
logger.Get(ctx).Infof("Error reading update-send response body: %v", err)
return
}
logger.Get(ctx).Infof("Error sending updates. status: %s. response: %s", response.Status, b)
}
defer func() {
_ = response.Body.Close()
}()
logger.Get(ctx).Infof("Update records reported to %s", u.addr)
}
func (u *UpdateUploader) OnChange(ctx context.Context, st store.RStore) {
task := u.makeUpdates(ctx, st)
if !task.empty() {
u.sendUpdates(ctx, task)
}
}
var _ store.Subscriber = &UpdateUploader{}