-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
169 lines (149 loc) · 4.17 KB
/
client.go
File metadata and controls
169 lines (149 loc) · 4.17 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
package agent
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type Client struct {
serverURL string
token string
uuid string
httpClient *http.Client
}
func NewClient(serverURL, token string) *Client {
return &Client{
serverURL: serverURL,
token: token,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}
func (c *Client) SetUUID(uuid string) {
c.uuid = uuid
}
func (c *Client) doRequest(method, path string, body interface{}) (*http.Response, error) {
var bodyReader io.Reader
if body != nil {
data, err := json.Marshal(body)
if err != nil {
return nil, err
}
bodyReader = bytes.NewReader(data)
}
req, err := http.NewRequest(method, c.serverURL+path, bodyReader)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.token)
if c.uuid != "" {
req.Header.Set("X-Agent-UUID", c.uuid)
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
return c.httpClient.Do(req)
}
func (c *Client) ServerURL() string {
return c.serverURL
}
func (c *Client) Token() string {
return c.token
}
func (c *Client) Claim() (*ClaimResponse, error) {
resp, err := c.doRequest("POST", "/api/v2/ci/runner/claim", nil)
if err != nil {
return nil, fmt.Errorf("claim request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNoContent {
return nil, nil
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("claim failed: %s %s", resp.Status, string(body))
}
var claim ClaimResponse
if err := json.NewDecoder(resp.Body).Decode(&claim); err != nil {
return nil, fmt.Errorf("decode claim response: %w", err)
}
return &claim, nil
}
// drainAndCheck reads the response body to allow connection reuse and returns
// an error if the status code is not in the 2xx range.
func drainAndCheck(resp *http.Response) error {
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("unexpected status: %s", resp.Status)
}
return nil
}
// SendLogs sends a batch of log lines and returns the current job status from the server.
func (c *Client) SendLogs(jobID string, batch LogBatch) (string, error) {
resp, err := c.doRequest("POST", fmt.Sprintf("/api/v2/ci/runner/logs/%s", jobID), batch)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
_, _ = io.Copy(io.Discard, resp.Body)
return "", fmt.Errorf("unexpected status: %s", resp.Status)
}
var result struct {
Status string `json:"status"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", nil // non-fatal: old server without status in response
}
return result.Status, nil
}
func (c *Client) ReportStatus(jobID string, status RunStatus, exitCode *int) error {
report := StatusReport{
Status: status,
ExitCode: exitCode,
}
resp, err := c.doRequest("PATCH", fmt.Sprintf("/api/v2/ci/runner/jobs/%s", jobID), report)
if err != nil {
return err
}
return drainAndCheck(resp)
}
func (c *Client) SubmitGraph(jobID string, graph string) error {
resp, err := c.doRequest("POST", fmt.Sprintf("/api/v2/ci/runner/jobs/%s/graph", jobID), map[string]string{"graph": graph})
if err != nil {
return err
}
return drainAndCheck(resp)
}
func (c *Client) ReportRef(jobID, commitSHA string) error {
resp, err := c.doRequest("POST", fmt.Sprintf("/api/v2/ci/runner/jobs/%s/ref", jobID), map[string]string{"commit_sha": commitSHA})
if err != nil {
return err
}
return drainAndCheck(resp)
}
func (c *Client) SubmitActiveNodes(jobID string, nodes []ActiveNode) error {
resp, err := c.doRequest("POST", fmt.Sprintf("/api/v2/ci/runner/jobs/%s/nodes", jobID), map[string]interface{}{"active_nodes": nodes})
if err != nil {
return err
}
return drainAndCheck(resp)
}
func (c *Client) Heartbeat(req HeartbeatRequest) error {
resp, err := c.doRequest("POST", "/api/v2/ci/runner/heartbeat", req)
if err != nil {
return err
}
return drainAndCheck(resp)
}
func (c *Client) Disconnect() error {
resp, err := c.doRequest("POST", "/api/v2/ci/runner/disconnect", nil)
if err != nil {
return err
}
return drainAndCheck(resp)
}