2019-02-21 16:58:25 +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.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/gateway/action"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
gwapitypes "agola.io/agola/services/gateway/api/types"
|
|
|
|
rstypes "agola.io/agola/services/runservice/types"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-02-21 16:58:25 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createRunResponse(r *rstypes.Run, rc *rstypes.RunConfig) *gwapitypes.RunResponse {
|
|
|
|
run := &gwapitypes.RunResponse{
|
2019-05-15 12:49:46 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Counter: r.Counter,
|
|
|
|
Name: r.Name,
|
|
|
|
Annotations: r.Annotations,
|
|
|
|
Phase: r.Phase,
|
|
|
|
Result: r.Result,
|
|
|
|
Stopping: r.Stop,
|
|
|
|
SetupErrors: rc.SetupErrors,
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
Tasks: make(map[string]*gwapitypes.RunResponseTask),
|
2019-02-21 16:58:25 +00:00
|
|
|
TasksWaitingApproval: r.TasksWaitingApproval(),
|
|
|
|
|
|
|
|
EnqueueTime: r.EnqueueTime,
|
|
|
|
StartTime: r.StartTime,
|
|
|
|
EndTime: r.EndTime,
|
|
|
|
}
|
|
|
|
|
2019-03-29 08:22:34 +00:00
|
|
|
run.CanRestartFromScratch, _ = r.CanRestartFromScratch()
|
|
|
|
run.CanRestartFromFailedTasks, _ = r.CanRestartFromFailedTasks()
|
|
|
|
|
2019-04-12 15:45:38 +00:00
|
|
|
for name, rt := range r.Tasks {
|
2019-02-21 16:58:25 +00:00
|
|
|
rct := rc.Tasks[rt.ID]
|
|
|
|
run.Tasks[name] = createRunResponseTask(r, rt, rct)
|
|
|
|
}
|
|
|
|
|
|
|
|
return run
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createRunResponseTask(r *rstypes.Run, rt *rstypes.RunTask, rct *rstypes.RunConfigTask) *gwapitypes.RunResponseTask {
|
|
|
|
t := &gwapitypes.RunResponseTask{
|
2019-02-21 16:58:25 +00:00
|
|
|
ID: rt.ID,
|
|
|
|
Name: rct.Name,
|
|
|
|
Status: rt.Status,
|
|
|
|
|
|
|
|
StartTime: rt.StartTime,
|
|
|
|
EndTime: rt.EndTime,
|
|
|
|
|
2019-04-08 15:29:57 +00:00
|
|
|
WaitingApproval: rt.WaitingApproval,
|
|
|
|
Approved: rt.Approved,
|
2019-05-06 13:19:29 +00:00
|
|
|
ApprovalAnnotations: rt.Annotations,
|
2019-04-08 15:29:57 +00:00
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
Level: rct.Level,
|
|
|
|
Depends: rct.Depends,
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createRunTaskResponse(rt *rstypes.RunTask, rct *rstypes.RunConfigTask) *gwapitypes.RunTaskResponse {
|
|
|
|
t := &gwapitypes.RunTaskResponse{
|
2019-02-21 16:58:25 +00:00
|
|
|
ID: rt.ID,
|
|
|
|
Name: rct.Name,
|
|
|
|
Status: rt.Status,
|
2019-04-08 15:29:57 +00:00
|
|
|
|
|
|
|
WaitingApproval: rt.WaitingApproval,
|
|
|
|
Approved: rt.Approved,
|
2019-05-06 13:19:29 +00:00
|
|
|
ApprovalAnnotations: rt.Annotations,
|
2019-04-08 15:29:57 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
Steps: make([]*gwapitypes.RunTaskResponseStep, len(rt.Steps)),
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
StartTime: rt.StartTime,
|
|
|
|
EndTime: rt.EndTime,
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
t.SetupStep = &gwapitypes.RunTaskResponseSetupStep{
|
2019-03-13 14:48:35 +00:00
|
|
|
Name: "Task setup",
|
|
|
|
Phase: rt.SetupStep.Phase,
|
|
|
|
StartTime: rt.SetupStep.StartTime,
|
|
|
|
EndTime: rt.SetupStep.EndTime,
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
for i := 0; i < len(t.Steps); i++ {
|
2019-07-31 13:39:07 +00:00
|
|
|
s := &gwapitypes.RunTaskResponseStep{
|
2019-03-13 14:48:35 +00:00
|
|
|
Phase: rt.Steps[i].Phase,
|
2019-02-21 16:58:25 +00:00
|
|
|
StartTime: rt.Steps[i].StartTime,
|
|
|
|
EndTime: rt.Steps[i].EndTime,
|
|
|
|
}
|
|
|
|
rcts := rct.Steps[i]
|
2019-09-11 12:38:50 +00:00
|
|
|
rts := rt.Steps[i]
|
2019-11-12 11:06:55 +00:00
|
|
|
|
|
|
|
if rts.LogPhase == rstypes.RunTaskFetchPhaseFinished {
|
|
|
|
s.LogArchived = true
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
switch rcts := rcts.(type) {
|
|
|
|
case *rstypes.RunStep:
|
2019-09-11 12:38:50 +00:00
|
|
|
s.Type = "run"
|
2019-02-21 16:58:25 +00:00
|
|
|
s.Name = rcts.Name
|
|
|
|
s.Command = rcts.Command
|
2019-09-11 12:38:50 +00:00
|
|
|
|
|
|
|
shell := rcts.Shell
|
|
|
|
if shell == "" {
|
|
|
|
shell = rct.Shell
|
|
|
|
}
|
|
|
|
s.Shell = shell
|
|
|
|
|
|
|
|
s.ExitStatus = rts.ExitStatus
|
2019-02-21 16:58:25 +00:00
|
|
|
case *rstypes.SaveToWorkspaceStep:
|
2019-09-11 12:38:50 +00:00
|
|
|
s.Type = "save_to_workspace"
|
2019-02-21 16:58:25 +00:00
|
|
|
s.Name = "save to workspace"
|
|
|
|
case *rstypes.RestoreWorkspaceStep:
|
2019-09-11 12:38:50 +00:00
|
|
|
s.Type = "restore_workspace"
|
2019-02-21 16:58:25 +00:00
|
|
|
s.Name = "restore workspace"
|
2019-04-13 12:58:56 +00:00
|
|
|
case *rstypes.SaveCacheStep:
|
2019-09-11 12:38:50 +00:00
|
|
|
s.Type = "save_cache"
|
2019-04-13 12:58:56 +00:00
|
|
|
s.Name = "save cache"
|
|
|
|
case *rstypes.RestoreCacheStep:
|
2019-09-11 12:38:50 +00:00
|
|
|
s.Type = "restore_cache"
|
2019-04-13 12:58:56 +00:00
|
|
|
s.Name = "restore cache"
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Steps[i] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 22:00:45 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRunHandler(log zerolog.Logger, ah *action.ActionHandler) *RunHandler {
|
|
|
|
return &RunHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
runID := vars["runid"]
|
|
|
|
|
2019-05-05 22:00:45 +00:00
|
|
|
runResp, err := h.ah.GetRun(ctx, runID)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createRunResponse(runResp.Run, runResp.RunConfig)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RuntaskHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 22:00:45 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRuntaskHandler(log zerolog.Logger, ah *action.ActionHandler) *RuntaskHandler {
|
|
|
|
return &RuntaskHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RuntaskHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
runID := vars["runid"]
|
|
|
|
taskID := vars["taskid"]
|
|
|
|
|
2019-05-05 22:00:45 +00:00
|
|
|
runResp, err := h.ah.GetRun(ctx, runID)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
run := runResp.Run
|
|
|
|
rc := runResp.RunConfig
|
|
|
|
|
2019-04-12 15:45:38 +00:00
|
|
|
rt, ok := run.Tasks[taskID]
|
2019-02-21 16:58:25 +00:00
|
|
|
if !ok {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrNotExist, errors.Errorf("run %q task %q not found", runID, taskID)))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
rct := rc.Tasks[rt.ID]
|
|
|
|
|
|
|
|
res := createRunTaskResponse(rt, rct)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultRunsLimit = 25
|
|
|
|
MaxRunsLimit = 40
|
|
|
|
)
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createRunsResponse(r *rstypes.Run) *gwapitypes.RunsResponse {
|
|
|
|
run := &gwapitypes.RunsResponse{
|
2019-02-21 16:58:25 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Counter: r.Counter,
|
|
|
|
Name: r.Name,
|
|
|
|
Annotations: r.Annotations,
|
|
|
|
Phase: r.Phase,
|
|
|
|
Result: r.Result,
|
|
|
|
|
|
|
|
TasksWaitingApproval: r.TasksWaitingApproval(),
|
|
|
|
|
|
|
|
EnqueueTime: r.EnqueueTime,
|
|
|
|
StartTime: r.StartTime,
|
|
|
|
EndTime: r.EndTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
return run
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 22:00:45 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRunsHandler(log zerolog.Logger, ah *action.ActionHandler) *RunsHandler {
|
|
|
|
return &RunsHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-03-13 14:48:35 +00:00
|
|
|
q := r.URL.Query()
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-05-03 21:19:23 +00:00
|
|
|
// we currently accept only one group
|
|
|
|
group := q.Get("group")
|
2019-04-03 06:52:29 +00:00
|
|
|
// we require that groups are specified to not return all runs
|
2019-05-03 21:19:23 +00:00
|
|
|
if group == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("no groups specified")))
|
2019-04-03 06:52:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
phaseFilter := q["phase"]
|
2019-07-05 08:42:45 +00:00
|
|
|
resultFilter := q["result"]
|
2019-03-13 14:48:35 +00:00
|
|
|
changeGroups := q["changegroup"]
|
2019-03-29 11:00:18 +00:00
|
|
|
_, lastRun := q["lastrun"]
|
2019-03-13 14:48:35 +00:00
|
|
|
|
|
|
|
limitS := q.Get("limit")
|
2019-02-21 16:58:25 +00:00
|
|
|
limit := DefaultRunsLimit
|
|
|
|
if limitS != "" {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(limitS)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Wrapf(err, "cannot parse limit")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit < 0 {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("limit must be greater or equal than 0")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if limit > MaxRunsLimit {
|
|
|
|
limit = MaxRunsLimit
|
|
|
|
}
|
|
|
|
asc := false
|
2019-03-13 14:48:35 +00:00
|
|
|
if _, ok := q["asc"]; ok {
|
2019-02-21 16:58:25 +00:00
|
|
|
asc = true
|
|
|
|
}
|
|
|
|
|
2019-03-13 14:48:35 +00:00
|
|
|
start := q.Get("start")
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-05-05 22:00:45 +00:00
|
|
|
areq := &action.GetRunsRequest{
|
|
|
|
PhaseFilter: phaseFilter,
|
2019-07-05 08:42:45 +00:00
|
|
|
ResultFilter: resultFilter,
|
2019-05-03 21:19:23 +00:00
|
|
|
Group: group,
|
2019-05-05 22:00:45 +00:00
|
|
|
LastRun: lastRun,
|
|
|
|
ChangeGroups: changeGroups,
|
|
|
|
StartRunID: start,
|
|
|
|
Limit: limit,
|
|
|
|
Asc: asc,
|
|
|
|
}
|
|
|
|
runsResp, err := h.ah.GetRuns(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
runs := make([]*gwapitypes.RunsResponse, len(runsResp.Runs))
|
2019-02-21 16:58:25 +00:00
|
|
|
for i, r := range runsResp.Runs {
|
|
|
|
runs[i] = createRunsResponse(r)
|
|
|
|
}
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, runs); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 15:12:07 +00:00
|
|
|
type RunActionsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 22:00:45 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-04 15:12:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRunActionsHandler(log zerolog.Logger, ah *action.ActionHandler) *RunActionsHandler {
|
|
|
|
return &RunActionsHandler{log: log, ah: ah}
|
2019-03-04 15:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunActionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
runID := vars["runid"]
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.RunActionsRequest
|
2019-03-04 15:12:07 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-03-04 15:12:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 22:00:45 +00:00
|
|
|
areq := &action.RunActionsRequest{
|
|
|
|
RunID: runID,
|
2019-07-31 13:39:07 +00:00
|
|
|
ActionType: action.RunActionType(req.ActionType),
|
2019-05-05 22:00:45 +00:00
|
|
|
FromStart: req.FromStart,
|
|
|
|
}
|
2019-03-08 09:02:37 +00:00
|
|
|
|
2019-05-07 21:23:58 +00:00
|
|
|
runResp, err := h.ah.RunAction(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-05 22:00:45 +00:00
|
|
|
return
|
2019-03-04 15:12:07 +00:00
|
|
|
}
|
2019-05-07 21:23:58 +00:00
|
|
|
|
|
|
|
res := createRunResponse(runResp.Run, runResp.RunConfig)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-07 21:23:58 +00:00
|
|
|
}
|
2019-03-04 15:12:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:29:57 +00:00
|
|
|
type RunTaskActionsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 22:00:45 +00:00
|
|
|
ah *action.ActionHandler
|
2019-04-08 15:29:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRunTaskActionsHandler(log zerolog.Logger, ah *action.ActionHandler) *RunTaskActionsHandler {
|
|
|
|
return &RunTaskActionsHandler{log: log, ah: ah}
|
2019-04-08 15:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunTaskActionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
runID := vars["runid"]
|
|
|
|
taskID := vars["taskid"]
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.RunTaskActionsRequest
|
2019-04-08 15:29:57 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-04-08 15:29:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 22:00:45 +00:00
|
|
|
areq := &action.RunTaskActionsRequest{
|
2019-05-06 13:19:29 +00:00
|
|
|
RunID: runID,
|
|
|
|
TaskID: taskID,
|
2019-07-31 13:39:07 +00:00
|
|
|
ActionType: action.RunTaskActionType(req.ActionType),
|
2019-05-05 22:00:45 +00:00
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
|
2019-05-05 22:00:45 +00:00
|
|
|
err := h.ah.RunTaskAction(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-08 15:29:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type LogsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 22:00:45 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewLogsHandler(log zerolog.Logger, ah *action.ActionHandler) *LogsHandler {
|
|
|
|
return &LogsHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *LogsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-03-13 14:48:35 +00:00
|
|
|
q := r.URL.Query()
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-03-13 14:48:35 +00:00
|
|
|
runID := q.Get("runID")
|
2019-02-21 16:58:25 +00:00
|
|
|
if runID == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("empty run id")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-13 14:48:35 +00:00
|
|
|
taskID := q.Get("taskID")
|
2019-02-21 16:58:25 +00:00
|
|
|
if taskID == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("empty task id")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-13 14:48:35 +00:00
|
|
|
|
|
|
|
_, setup := q["setup"]
|
|
|
|
stepStr := q.Get("step")
|
|
|
|
if !setup && stepStr == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("no setup or step number provided")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-13 14:48:35 +00:00
|
|
|
if setup && stepStr != "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("both setup and step number provided")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-13 14:48:35 +00:00
|
|
|
|
|
|
|
var step int
|
|
|
|
if stepStr != "" {
|
|
|
|
var err error
|
|
|
|
step, err = strconv.Atoi(stepStr)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Wrapf(err, "cannot parse step number")))
|
2019-03-13 14:48:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
follow := false
|
2019-03-13 14:48:35 +00:00
|
|
|
if _, ok := q["follow"]; ok {
|
2019-02-21 16:58:25 +00:00
|
|
|
follow = true
|
|
|
|
}
|
|
|
|
|
2019-05-05 22:00:45 +00:00
|
|
|
areq := &action.GetLogsRequest{
|
|
|
|
RunID: runID,
|
|
|
|
TaskID: taskID,
|
|
|
|
Setup: setup,
|
|
|
|
Step: step,
|
|
|
|
Follow: follow,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := h.ah.GetLogs(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:49:21 +00:00
|
|
|
// write and flush the headers so the client will receive the response
|
|
|
|
// header also if there're currently no lines to send
|
2019-02-21 16:58:25 +00:00
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
2019-11-14 09:49:21 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
var flusher http.Flusher
|
|
|
|
if fl, ok := w.(http.Flusher); ok {
|
|
|
|
flusher = fl
|
|
|
|
}
|
|
|
|
if flusher != nil {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
defer resp.Body.Close()
|
2019-05-19 12:35:04 +00:00
|
|
|
if err := sendLogs(w, resp.Body); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-19 12:35:04 +00:00
|
|
|
return
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:35:04 +00:00
|
|
|
// sendLogs streams received logs lines and flushes them
|
2019-02-21 16:58:25 +00:00
|
|
|
func sendLogs(w io.Writer, r io.Reader) error {
|
2019-05-19 12:35:04 +00:00
|
|
|
buf := make([]byte, 4096)
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
var flusher http.Flusher
|
|
|
|
if fl, ok := w.(http.Flusher); ok {
|
|
|
|
flusher = fl
|
|
|
|
}
|
|
|
|
stop := false
|
|
|
|
for {
|
|
|
|
if stop {
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-19 12:35:04 +00:00
|
|
|
n, err := r.Read(buf)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-05-19 12:35:04 +00:00
|
|
|
if n == 0 {
|
2019-02-21 16:58:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
stop = true
|
|
|
|
}
|
2019-05-19 12:35:04 +00:00
|
|
|
if _, err := w.Write(buf[:n]); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
if flusher != nil {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 10:18:00 +00:00
|
|
|
|
|
|
|
type LogsDeleteHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-11-09 10:18:00 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewLogsDeleteHandler(log zerolog.Logger, ah *action.ActionHandler) *LogsDeleteHandler {
|
|
|
|
return &LogsDeleteHandler{log: log, ah: ah}
|
2019-11-09 10:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *LogsDeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
|
|
|
|
runID := q.Get("runID")
|
|
|
|
if runID == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("empty run id")))
|
2019-11-09 10:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
taskID := q.Get("taskID")
|
|
|
|
if taskID == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("empty task id")))
|
2019-11-09 10:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, setup := q["setup"]
|
|
|
|
stepStr := q.Get("step")
|
|
|
|
if !setup && stepStr == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("no setup or step number provided")))
|
2019-11-09 10:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if setup && stepStr != "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("both setup and step number provided")))
|
2019-11-09 10:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var step int
|
|
|
|
if stepStr != "" {
|
|
|
|
var err error
|
|
|
|
step, err = strconv.Atoi(stepStr)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Wrapf(err, "cannot parse step number")))
|
2019-11-09 10:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
areq := &action.DeleteLogsRequest{
|
|
|
|
RunID: runID,
|
|
|
|
TaskID: taskID,
|
|
|
|
Setup: setup,
|
|
|
|
Step: step,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := h.ah.DeleteLogs(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-11-09 10:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|