2019-02-21 14:54:50 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
package client
|
2019-02-21 14:54:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
rsapitypes "agola.io/agola/services/runservice/api/types"
|
|
|
|
rstypes "agola.io/agola/services/runservice/types"
|
2019-02-21 14:54:50 +00:00
|
|
|
)
|
|
|
|
|
2019-04-08 10:28:15 +00:00
|
|
|
var jsonContent = http.Header{"Content-Type": []string{"application/json"}}
|
2019-02-21 14:54:50 +00:00
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
url string
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient initializes and returns a API client.
|
|
|
|
func NewClient(url string) *Client {
|
|
|
|
return &Client{
|
|
|
|
url: strings.TrimSuffix(url, "/"),
|
|
|
|
client: &http.Client{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHTTPClient replaces default http.Client with user given one.
|
|
|
|
func (c *Client) SetHTTPClient(client *http.Client) {
|
|
|
|
c.client = client
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:49:55 +00:00
|
|
|
func (c *Client) doRequest(ctx context.Context, method, path string, query url.Values, contentLength int64, header http.Header, ibody io.Reader) (*http.Response, error) {
|
2019-02-21 14:54:50 +00:00
|
|
|
u, err := url.Parse(c.url + "/api/v1alpha" + path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, u.String(), ibody)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range header {
|
|
|
|
req.Header[k] = v
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:49:55 +00:00
|
|
|
if contentLength >= 0 {
|
|
|
|
req.ContentLength = contentLength
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
return c.client.Do(req)
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:49:55 +00:00
|
|
|
func (c *Client) getResponse(ctx context.Context, method, path string, query url.Values, contentLength int64, header http.Header, ibody io.Reader) (*http.Response, error) {
|
|
|
|
resp, err := c.doRequest(ctx, method, path, query, contentLength, header, ibody)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.ErrFromRemote(resp); err != nil {
|
|
|
|
return resp, err
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getParsedResponse(ctx context.Context, method, path string, query url.Values, header http.Header, ibody io.Reader, obj interface{}) (*http.Response, error) {
|
2019-05-02 07:49:55 +00:00
|
|
|
resp, err := c.getResponse(ctx, method, path, query, -1, header, ibody)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
d := json.NewDecoder(resp.Body)
|
|
|
|
|
|
|
|
return resp, d.Decode(obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SendExecutorStatus(ctx context.Context, executor *rstypes.Executor) (*http.Response, error) {
|
|
|
|
executorj, err := json.Marshal(executor)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-02 07:49:55 +00:00
|
|
|
return c.getResponse(ctx, "POST", fmt.Sprintf("/executor/%s", executor.ID), nil, -1, jsonContent, bytes.NewReader(executorj))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SendExecutorTaskStatus(ctx context.Context, executorID string, et *rstypes.ExecutorTask) (*http.Response, error) {
|
|
|
|
etj, err := json.Marshal(et)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-02 07:49:55 +00:00
|
|
|
return c.getResponse(ctx, "POST", fmt.Sprintf("/executor/%s/tasks/%s", executorID, et.ID), nil, -1, jsonContent, bytes.NewReader(etj))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetExecutorTask(ctx context.Context, executorID, etID string) (*rstypes.ExecutorTask, *http.Response, error) {
|
|
|
|
et := new(rstypes.ExecutorTask)
|
|
|
|
resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/executor/%s/tasks/%s", executorID, etID), nil, jsonContent, nil, et)
|
|
|
|
return et, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetExecutorTasks(ctx context.Context, executorID string) ([]*rstypes.ExecutorTask, *http.Response, error) {
|
|
|
|
ets := []*rstypes.ExecutorTask{}
|
|
|
|
resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/executor/%s/tasks", executorID), nil, jsonContent, nil, &ets)
|
|
|
|
return ets, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetArchive(ctx context.Context, taskID string, step int) (*http.Response, error) {
|
|
|
|
q := url.Values{}
|
|
|
|
q.Add("taskid", taskID)
|
|
|
|
q.Add("step", strconv.Itoa(step))
|
|
|
|
|
2019-05-02 07:49:55 +00:00
|
|
|
return c.getResponse(ctx, "GET", "/executor/archives", q, -1, nil, nil)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 12:58:56 +00:00
|
|
|
func (c *Client) CheckCache(ctx context.Context, key string, prefix bool) (*http.Response, error) {
|
|
|
|
q := url.Values{}
|
|
|
|
if prefix {
|
|
|
|
q.Add("prefix", "")
|
|
|
|
}
|
2019-05-02 07:49:55 +00:00
|
|
|
return c.getResponse(ctx, "HEAD", fmt.Sprintf("/executor/caches/%s", url.PathEscape(key)), q, -1, nil, nil)
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetCache(ctx context.Context, key string, prefix bool) (*http.Response, error) {
|
|
|
|
q := url.Values{}
|
|
|
|
if prefix {
|
|
|
|
q.Add("prefix", "")
|
|
|
|
}
|
2019-05-02 07:49:55 +00:00
|
|
|
return c.getResponse(ctx, "GET", fmt.Sprintf("/executor/caches/%s", url.PathEscape(key)), q, -1, nil, nil)
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 07:49:55 +00:00
|
|
|
func (c *Client) PutCache(ctx context.Context, key string, size int64, r io.Reader) (*http.Response, error) {
|
|
|
|
return c.getResponse(ctx, "POST", fmt.Sprintf("/executor/caches/%s", url.PathEscape(key)), nil, size, nil, r)
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) GetRuns(ctx context.Context, phaseFilter, resultFilter, groups []string, lastRun bool, changeGroups []string, start string, limit int, asc bool) (*rsapitypes.GetRunsResponse, *http.Response, error) {
|
2019-02-21 14:54:50 +00:00
|
|
|
q := url.Values{}
|
|
|
|
for _, phase := range phaseFilter {
|
|
|
|
q.Add("phase", phase)
|
|
|
|
}
|
2019-07-05 08:32:51 +00:00
|
|
|
for _, result := range resultFilter {
|
|
|
|
q.Add("result", result)
|
|
|
|
}
|
2019-02-21 14:54:50 +00:00
|
|
|
for _, group := range groups {
|
|
|
|
q.Add("group", group)
|
|
|
|
}
|
2019-03-29 11:00:18 +00:00
|
|
|
if lastRun {
|
|
|
|
q.Add("lastrun", "")
|
|
|
|
}
|
2019-02-21 14:54:50 +00:00
|
|
|
for _, changeGroup := range changeGroups {
|
|
|
|
q.Add("changegroup", changeGroup)
|
|
|
|
}
|
|
|
|
if start != "" {
|
|
|
|
q.Add("start", start)
|
|
|
|
}
|
|
|
|
if limit > 0 {
|
|
|
|
q.Add("limit", strconv.Itoa(limit))
|
|
|
|
}
|
|
|
|
if asc {
|
|
|
|
q.Add("asc", "")
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
getRunsResponse := new(rsapitypes.GetRunsResponse)
|
2019-02-21 14:54:50 +00:00
|
|
|
resp, err := c.getParsedResponse(ctx, "GET", "/runs", q, jsonContent, nil, getRunsResponse)
|
|
|
|
return getRunsResponse, resp, err
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) GetQueuedRuns(ctx context.Context, start string, limit int, changeGroups []string) (*rsapitypes.GetRunsResponse, *http.Response, error) {
|
2019-07-05 08:32:51 +00:00
|
|
|
return c.GetRuns(ctx, []string{"queued"}, nil, []string{}, false, changeGroups, start, limit, true)
|
2019-05-06 13:19:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) GetRunningRuns(ctx context.Context, start string, limit int, changeGroups []string) (*rsapitypes.GetRunsResponse, *http.Response, error) {
|
2019-07-05 08:32:51 +00:00
|
|
|
return c.GetRuns(ctx, []string{"running"}, nil, []string{}, false, changeGroups, start, limit, true)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) GetGroupQueuedRuns(ctx context.Context, group string, limit int, changeGroups []string) (*rsapitypes.GetRunsResponse, *http.Response, error) {
|
2019-07-05 08:32:51 +00:00
|
|
|
return c.GetRuns(ctx, []string{"queued"}, nil, []string{group}, false, changeGroups, "", limit, false)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) GetGroupRunningRuns(ctx context.Context, group string, limit int, changeGroups []string) (*rsapitypes.GetRunsResponse, *http.Response, error) {
|
2019-07-05 08:32:51 +00:00
|
|
|
return c.GetRuns(ctx, []string{"running"}, nil, []string{group}, false, changeGroups, "", limit, false)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) GetGroupFirstQueuedRuns(ctx context.Context, group string, changeGroups []string) (*rsapitypes.GetRunsResponse, *http.Response, error) {
|
2019-07-05 08:32:51 +00:00
|
|
|
return c.GetRuns(ctx, []string{"queued"}, nil, []string{group}, false, changeGroups, "", 1, true)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) GetGroupLastRun(ctx context.Context, group string, changeGroups []string) (*rsapitypes.GetRunsResponse, *http.Response, error) {
|
2019-07-05 08:32:51 +00:00
|
|
|
return c.GetRuns(ctx, nil, nil, []string{group}, false, changeGroups, "", 1, false)
|
2019-05-10 09:08:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) CreateRun(ctx context.Context, req *rsapitypes.RunCreateRequest) (*rsapitypes.RunResponse, *http.Response, error) {
|
2019-02-21 14:54:50 +00:00
|
|
|
reqj, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
2019-05-07 21:23:58 +00:00
|
|
|
return nil, nil, err
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
res := new(rsapitypes.RunResponse)
|
2019-05-07 21:23:58 +00:00
|
|
|
resp, err := c.getParsedResponse(ctx, "POST", "/runs", nil, jsonContent, bytes.NewReader(reqj), res)
|
|
|
|
return res, resp, err
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) RunActions(ctx context.Context, runID string, req *rsapitypes.RunActionsRequest) (*http.Response, error) {
|
2019-02-21 14:54:50 +00:00
|
|
|
reqj, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-02 07:49:55 +00:00
|
|
|
return c.getResponse(ctx, "PUT", fmt.Sprintf("/runs/%s/actions", runID), nil, -1, jsonContent, bytes.NewReader(reqj))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) StartRun(ctx context.Context, runID string, changeGroupsUpdateToken string) (*http.Response, error) {
|
2019-07-31 13:39:07 +00:00
|
|
|
req := &rsapitypes.RunActionsRequest{
|
|
|
|
ActionType: rsapitypes.RunActionTypeChangePhase,
|
2019-02-21 14:54:50 +00:00
|
|
|
Phase: rstypes.RunPhaseRunning,
|
|
|
|
ChangeGroupsUpdateToken: changeGroupsUpdateToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.RunActions(ctx, runID, req)
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) RunTaskActions(ctx context.Context, runID, taskID string, req *rsapitypes.RunTaskActionsRequest) (*http.Response, error) {
|
2019-02-21 14:54:50 +00:00
|
|
|
reqj, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-02 07:49:55 +00:00
|
|
|
return c.getResponse(ctx, "PUT", fmt.Sprintf("/runs/%s/tasks/%s/actions", runID, taskID), nil, -1, jsonContent, bytes.NewReader(reqj))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 13:19:29 +00:00
|
|
|
func (c *Client) RunTaskSetAnnotations(ctx context.Context, runID, taskID string, annotations map[string]string, changeGroupsUpdateToken string) (*http.Response, error) {
|
2019-07-31 13:39:07 +00:00
|
|
|
req := &rsapitypes.RunTaskActionsRequest{
|
|
|
|
ActionType: rsapitypes.RunTaskActionTypeSetAnnotations,
|
2019-05-06 13:19:29 +00:00
|
|
|
Annotations: annotations,
|
|
|
|
ChangeGroupsUpdateToken: changeGroupsUpdateToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.RunTaskActions(ctx, runID, taskID, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ApproveRunTask(ctx context.Context, runID, taskID string, changeGroupsUpdateToken string) (*http.Response, error) {
|
2019-07-31 13:39:07 +00:00
|
|
|
req := &rsapitypes.RunTaskActionsRequest{
|
|
|
|
ActionType: rsapitypes.RunTaskActionTypeApprove,
|
2019-02-21 14:54:50 +00:00
|
|
|
ChangeGroupsUpdateToken: changeGroupsUpdateToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.RunTaskActions(ctx, runID, taskID, req)
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (c *Client) GetRun(ctx context.Context, runID string, changeGroups []string) (*rsapitypes.RunResponse, *http.Response, error) {
|
2019-05-06 13:19:29 +00:00
|
|
|
q := url.Values{}
|
|
|
|
for _, changeGroup := range changeGroups {
|
|
|
|
q.Add("changegroup", changeGroup)
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
runResponse := new(rsapitypes.RunResponse)
|
2019-05-06 13:19:29 +00:00
|
|
|
resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/runs/%s", runID), q, jsonContent, nil, runResponse)
|
2019-02-21 14:54:50 +00:00
|
|
|
return runResponse, resp, err
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:35:04 +00:00
|
|
|
func (c *Client) GetLogs(ctx context.Context, runID, taskID string, setup bool, step int, follow bool) (*http.Response, error) {
|
2019-02-21 14:54:50 +00:00
|
|
|
q := url.Values{}
|
|
|
|
q.Add("runid", runID)
|
|
|
|
q.Add("taskid", taskID)
|
2019-03-13 14:48:35 +00:00
|
|
|
if setup {
|
|
|
|
q.Add("setup", "")
|
|
|
|
} else {
|
|
|
|
q.Add("step", strconv.Itoa(step))
|
|
|
|
}
|
2019-02-21 14:54:50 +00:00
|
|
|
if follow {
|
|
|
|
q.Add("follow", "")
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:49:55 +00:00
|
|
|
return c.getResponse(ctx, "GET", "/logs", q, -1, nil, nil)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
2019-05-15 07:46:21 +00:00
|
|
|
|
2019-11-09 10:18:00 +00:00
|
|
|
func (c *Client) DeleteLogs(ctx context.Context, runID, taskID string, setup bool, step int) (*http.Response, error) {
|
|
|
|
q := url.Values{}
|
|
|
|
q.Add("runid", runID)
|
|
|
|
q.Add("taskid", taskID)
|
|
|
|
if setup {
|
|
|
|
q.Add("setup", "")
|
|
|
|
} else {
|
|
|
|
q.Add("step", strconv.Itoa(step))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.getResponse(ctx, "DELETE", "/logs", q, -1, nil, nil)
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:46:21 +00:00
|
|
|
func (c *Client) GetRunEvents(ctx context.Context, startRunEventID string) (*http.Response, error) {
|
|
|
|
q := url.Values{}
|
|
|
|
q.Add("startruneventid", startRunEventID)
|
|
|
|
|
|
|
|
return c.getResponse(ctx, "GET", "/runs/events", q, -1, nil, nil)
|
|
|
|
}
|