-
Notifications
You must be signed in to change notification settings - Fork 695
Expand file tree
/
Copy pathclient.go
More file actions
189 lines (163 loc) · 4.02 KB
/
Copy pathclient.go
File metadata and controls
189 lines (163 loc) · 4.02 KB
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
package coglog
import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/replicate/cog/pkg/env"
"github.com/replicate/cog/pkg/util/console"
)
type Client struct {
client *http.Client
}
type buildLog struct {
DurationMs float32 `json:"length_ms"`
BuildError *string `json:"error"`
Fast bool `json:"fast"`
LocalImage bool `json:"local_image"`
}
type pushLog struct {
DurationMs float32 `json:"length_ms"`
BuildError *string `json:"error"`
Fast bool `json:"fast"`
LocalImage bool `json:"local_image"`
}
type migrateLog struct {
DurationMs float32 `json:"length_ms"`
BuildError *string `json:"error"`
Accept bool `json:"accept"`
}
func NewClient(client *http.Client) *Client {
return &Client{
client: client,
}
}
func (c *Client) StartBuild(fast bool, localImage bool) BuildLogContext {
logContext := BuildLogContext{
started: time.Now(),
fast: fast,
localImage: localImage,
}
return logContext
}
func (c *Client) EndBuild(ctx context.Context, err error, logContext BuildLogContext) bool {
var errorStr *string = nil
if err != nil {
errStr := err.Error()
errorStr = &errStr
}
buildLog := buildLog{
DurationMs: float32(time.Now().Sub(logContext.started).Milliseconds()),
BuildError: errorStr,
Fast: logContext.fast,
LocalImage: logContext.localImage,
}
jsonData, err := json.Marshal(buildLog)
if err != nil {
console.Warn("Failed to marshal JSON for build log: " + err.Error())
return false
}
err = c.postLog(ctx, jsonData, "build")
if err != nil {
console.Warn(err.Error())
return false
}
return true
}
func (c *Client) StartPush(fast bool, localImage bool) PushLogContext {
logContext := PushLogContext{
started: time.Now(),
fast: fast,
localImage: localImage,
}
return logContext
}
func (c *Client) EndPush(ctx context.Context, err error, logContext PushLogContext) bool {
var errorStr *string = nil
if err != nil {
errStr := err.Error()
errorStr = &errStr
}
pushLog := pushLog{
DurationMs: float32(time.Now().Sub(logContext.started).Milliseconds()),
BuildError: errorStr,
Fast: logContext.fast,
LocalImage: logContext.localImage,
}
jsonData, err := json.Marshal(pushLog)
if err != nil {
console.Warn("Failed to marshal JSON for build log: " + err.Error())
return false
}
err = c.postLog(ctx, jsonData, "push")
if err != nil {
console.Warn(err.Error())
return false
}
return true
}
func (c *Client) StartMigrate(accept bool) *MigrateLogContext {
logContext := NewMigrateLogContext(accept)
return logContext
}
func (c *Client) EndMigrate(ctx context.Context, err error, logContext *MigrateLogContext) bool {
var errorStr *string = nil
if err != nil {
errStr := err.Error()
errorStr = &errStr
}
migrateLog := migrateLog{
DurationMs: float32(time.Now().Sub(logContext.started).Milliseconds()),
BuildError: errorStr,
Accept: logContext.accept,
}
jsonData, err := json.Marshal(migrateLog)
if err != nil {
console.Warn("Failed to marshal JSON for build log: " + err.Error())
return false
}
err = c.postLog(ctx, jsonData, "migrate")
if err != nil {
console.Warn(err.Error())
return false
}
return true
}
func (c *Client) postLog(ctx context.Context, jsonData []byte, action string) error {
disabled, err := DisableFromEnvironment()
if err != nil {
return err
}
if disabled {
return errors.New("Cog logging disabled")
}
url := actionURL(action)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url.String(), bytes.NewReader(jsonData))
if err != nil {
return err
}
resp, err := c.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New("Bad response from build log: " + strconv.Itoa(resp.StatusCode))
}
return nil
}
func baseURL() url.URL {
return url.URL{
Scheme: env.SchemeFromEnvironment(),
Host: HostFromEnvironment(),
}
}
func actionURL(action string) url.URL {
url := baseURL()
url.Path = strings.Join([]string{"", "v1", action}, "/")
return url
}