|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +type Client struct { |
| 13 | + serverURL string |
| 14 | + token string |
| 15 | + httpClient *http.Client |
| 16 | +} |
| 17 | + |
| 18 | +func NewClient(serverURL, token string) *Client { |
| 19 | + return &Client{ |
| 20 | + serverURL: serverURL, |
| 21 | + token: token, |
| 22 | + httpClient: &http.Client{ |
| 23 | + Timeout: 30 * time.Second, |
| 24 | + }, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func (c *Client) doRequest(method, path string, body interface{}) (*http.Response, error) { |
| 29 | + var bodyReader io.Reader |
| 30 | + if body != nil { |
| 31 | + data, err := json.Marshal(body) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + bodyReader = bytes.NewReader(data) |
| 36 | + } |
| 37 | + |
| 38 | + req, err := http.NewRequest(method, c.serverURL+path, bodyReader) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + req.Header.Set("Authorization", "Bearer "+c.token) |
| 43 | + if body != nil { |
| 44 | + req.Header.Set("Content-Type", "application/json") |
| 45 | + } |
| 46 | + return c.httpClient.Do(req) |
| 47 | +} |
| 48 | + |
| 49 | +func (c *Client) ServerURL() string { |
| 50 | + return c.serverURL |
| 51 | +} |
| 52 | + |
| 53 | +func (c *Client) Token() string { |
| 54 | + return c.token |
| 55 | +} |
| 56 | + |
| 57 | +func (c *Client) Claim() (*ClaimResponse, error) { |
| 58 | + resp, err := c.doRequest("POST", "/api/v2/ci/runner/claim", nil) |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("claim request failed: %w", err) |
| 61 | + } |
| 62 | + defer resp.Body.Close() |
| 63 | + |
| 64 | + if resp.StatusCode == http.StatusNoContent { |
| 65 | + return nil, nil |
| 66 | + } |
| 67 | + if resp.StatusCode != http.StatusOK { |
| 68 | + body, _ := io.ReadAll(resp.Body) |
| 69 | + return nil, fmt.Errorf("claim failed: %s %s", resp.Status, string(body)) |
| 70 | + } |
| 71 | + |
| 72 | + var claim ClaimResponse |
| 73 | + if err := json.NewDecoder(resp.Body).Decode(&claim); err != nil { |
| 74 | + return nil, fmt.Errorf("decode claim response: %w", err) |
| 75 | + } |
| 76 | + return &claim, nil |
| 77 | +} |
| 78 | + |
| 79 | +// drainAndCheck reads the response body to allow connection reuse and returns |
| 80 | +// an error if the status code is not in the 2xx range. |
| 81 | +func drainAndCheck(resp *http.Response) error { |
| 82 | + _, _ = io.Copy(io.Discard, resp.Body) |
| 83 | + resp.Body.Close() |
| 84 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 85 | + return fmt.Errorf("unexpected status: %s", resp.Status) |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// SendLogs sends a batch of log lines and returns the current job status from the server. |
| 91 | +func (c *Client) SendLogs(jobID string, batch LogBatch) (string, error) { |
| 92 | + resp, err := c.doRequest("POST", fmt.Sprintf("/api/v2/ci/runner/logs/%s", jobID), batch) |
| 93 | + if err != nil { |
| 94 | + return "", err |
| 95 | + } |
| 96 | + defer resp.Body.Close() |
| 97 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 98 | + _, _ = io.Copy(io.Discard, resp.Body) |
| 99 | + return "", fmt.Errorf("unexpected status: %s", resp.Status) |
| 100 | + } |
| 101 | + var result struct { |
| 102 | + Status string `json:"status"` |
| 103 | + } |
| 104 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 105 | + return "", nil // non-fatal: old server without status in response |
| 106 | + } |
| 107 | + return result.Status, nil |
| 108 | +} |
| 109 | + |
| 110 | +func (c *Client) ReportStatus(jobID string, status RunStatus, exitCode *int) error { |
| 111 | + report := StatusReport{ |
| 112 | + Status: status, |
| 113 | + ExitCode: exitCode, |
| 114 | + } |
| 115 | + resp, err := c.doRequest("PATCH", fmt.Sprintf("/api/v2/ci/runner/jobs/%s", jobID), report) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + return drainAndCheck(resp) |
| 120 | +} |
| 121 | + |
| 122 | +func (c *Client) SubmitGraph(jobID string, graph string) error { |
| 123 | + resp, err := c.doRequest("POST", fmt.Sprintf("/api/v2/ci/runner/jobs/%s/graph", jobID), map[string]string{"graph": graph}) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + return drainAndCheck(resp) |
| 128 | +} |
| 129 | + |
| 130 | +func (c *Client) ReportRef(jobID, commitSHA string) error { |
| 131 | + resp, err := c.doRequest("POST", fmt.Sprintf("/api/v2/ci/runner/jobs/%s/ref", jobID), map[string]string{"commit_sha": commitSHA}) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + return drainAndCheck(resp) |
| 136 | +} |
| 137 | + |
| 138 | +func (c *Client) SubmitActiveNodes(jobID string, nodes []ActiveNode) error { |
| 139 | + resp, err := c.doRequest("POST", fmt.Sprintf("/api/v2/ci/runner/jobs/%s/nodes", jobID), map[string]interface{}{"active_nodes": nodes}) |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + return drainAndCheck(resp) |
| 144 | +} |
| 145 | + |
| 146 | +func (c *Client) Heartbeat(req HeartbeatRequest) error { |
| 147 | + resp, err := c.doRequest("POST", "/api/v2/ci/runner/heartbeat", req) |
| 148 | + if err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + return drainAndCheck(resp) |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | +func (c *Client) Disconnect() error { |
| 156 | + resp, err := c.doRequest("POST", "/api/v2/ci/runner/disconnect", nil) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + return drainAndCheck(resp) |
| 161 | +} |
0 commit comments