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 (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-02-21 16:58:25 +00:00
|
|
|
rsapi "github.com/sorintlab/agola/internal/services/runservice/scheduler/api"
|
|
|
|
rstypes "github.com/sorintlab/agola/internal/services/runservice/types"
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/sorintlab/agola/internal/util"
|
2019-02-21 16:58:25 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RunsResponse struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Counter uint64 `json:"counter"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Annotations map[string]string `json:"annotations"`
|
|
|
|
Phase rstypes.RunPhase `json:"phase"`
|
|
|
|
Result rstypes.RunResult `json:"result"`
|
|
|
|
|
|
|
|
TasksWaitingApproval []string `json:"tasks_waiting_approval"`
|
|
|
|
|
|
|
|
EnqueueTime *time.Time `json:"enqueue_time"`
|
|
|
|
StartTime *time.Time `json:"start_time"`
|
|
|
|
EndTime *time.Time `json:"end_time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunResponse struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Counter uint64 `json:"counter"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Annotations map[string]string `json:"annotations"`
|
|
|
|
Phase rstypes.RunPhase `json:"phase"`
|
|
|
|
Result rstypes.RunResult `json:"result"`
|
2019-04-09 14:51:37 +00:00
|
|
|
SetupErrors []string `json:"setup_errors"`
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
Tasks map[string]*RunResponseTask `json:"tasks"`
|
|
|
|
TasksWaitingApproval []string `json:"tasks_waiting_approval"`
|
|
|
|
|
|
|
|
EnqueueTime *time.Time `json:"enqueue_time"`
|
|
|
|
StartTime *time.Time `json:"start_time"`
|
|
|
|
EndTime *time.Time `json:"end_time"`
|
2019-03-29 08:22:34 +00:00
|
|
|
|
|
|
|
CanRestartFromScratch bool `json:"can_restart_from_scratch"`
|
|
|
|
CanRestartFromFailedTasks bool `json:"can_restart_from_failed_tasks"`
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RunResponseTask struct {
|
2019-04-12 15:04:07 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Status rstypes.RunTaskStatus `json:"status"`
|
|
|
|
Level int `json:"level"`
|
|
|
|
Depends map[string]*rstypes.RunConfigTaskDepend `json:"depends"`
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-04-08 15:29:57 +00:00
|
|
|
WaitingApproval bool `json:"waiting_approval"`
|
|
|
|
Approved bool `json:"approved"`
|
|
|
|
ApprovalAnnotations map[string]string `json:"approval_annotations"`
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
StartTime *time.Time `json:"start_time"`
|
|
|
|
EndTime *time.Time `json:"end_time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunTaskResponse struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Status rstypes.RunTaskStatus `json:"status"`
|
|
|
|
|
2019-04-08 15:29:57 +00:00
|
|
|
WaitingApproval bool `json:"waiting_approval"`
|
|
|
|
Approved bool `json:"approved"`
|
|
|
|
ApprovalAnnotations map[string]string `json:"approval_annotations"`
|
|
|
|
|
2019-03-13 14:48:35 +00:00
|
|
|
SetupStep *RunTaskResponseSetupStep `json:"setup_step"`
|
|
|
|
Steps []*RunTaskResponseStep `json:"steps"`
|
|
|
|
|
|
|
|
StartTime *time.Time `json:"start_time"`
|
|
|
|
EndTime *time.Time `json:"end_time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunTaskResponseSetupStep struct {
|
|
|
|
Phase rstypes.ExecutorTaskPhase `json:"phase"`
|
|
|
|
Name string `json:"name"`
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
StartTime *time.Time `json:"start_time"`
|
|
|
|
EndTime *time.Time `json:"end_time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunTaskResponseStep struct {
|
|
|
|
Phase rstypes.ExecutorTaskPhase `json:"phase"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Command string `json:"command"`
|
|
|
|
|
|
|
|
StartTime *time.Time `json:"start_time"`
|
|
|
|
EndTime *time.Time `json:"end_time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRunResponse(r *rstypes.Run, rc *rstypes.RunConfig) *RunResponse {
|
|
|
|
run := &RunResponse{
|
|
|
|
ID: r.ID,
|
|
|
|
Counter: r.Counter,
|
|
|
|
Name: r.Name,
|
|
|
|
Annotations: r.Annotations,
|
|
|
|
Phase: r.Phase,
|
|
|
|
Result: r.Result,
|
2019-04-09 14:51:37 +00:00
|
|
|
SetupErrors: rc.SetupErrors,
|
2019-02-21 16:58:25 +00:00
|
|
|
Tasks: make(map[string]*RunResponseTask),
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRunResponseTask(r *rstypes.Run, rt *rstypes.RunTask, rct *rstypes.RunConfigTask) *RunResponseTask {
|
|
|
|
t := &RunResponseTask{
|
|
|
|
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,
|
|
|
|
ApprovalAnnotations: rt.ApprovalAnnotations,
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
Level: rct.Level,
|
|
|
|
Depends: rct.Depends,
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRunTaskResponse(rt *rstypes.RunTask, rct *rstypes.RunConfigTask) *RunTaskResponse {
|
|
|
|
t := &RunTaskResponse{
|
|
|
|
ID: rt.ID,
|
|
|
|
Name: rct.Name,
|
|
|
|
Status: rt.Status,
|
2019-04-08 15:29:57 +00:00
|
|
|
|
|
|
|
WaitingApproval: rt.WaitingApproval,
|
|
|
|
Approved: rt.Approved,
|
|
|
|
ApprovalAnnotations: rt.ApprovalAnnotations,
|
|
|
|
|
|
|
|
Steps: make([]*RunTaskResponseStep, len(rt.Steps)),
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
StartTime: rt.StartTime,
|
|
|
|
EndTime: rt.EndTime,
|
|
|
|
}
|
|
|
|
|
2019-03-13 14:48:35 +00:00
|
|
|
t.SetupStep = &RunTaskResponseSetupStep{
|
|
|
|
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++ {
|
|
|
|
s := &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]
|
|
|
|
switch rcts := rcts.(type) {
|
|
|
|
case *rstypes.RunStep:
|
|
|
|
s.Name = rcts.Name
|
|
|
|
s.Command = rcts.Command
|
|
|
|
case *rstypes.SaveToWorkspaceStep:
|
|
|
|
s.Name = "save to workspace"
|
|
|
|
case *rstypes.RestoreWorkspaceStep:
|
|
|
|
s.Name = "restore workspace"
|
2019-04-13 12:58:56 +00:00
|
|
|
case *rstypes.SaveCacheStep:
|
|
|
|
s.Name = "save cache"
|
|
|
|
case *rstypes.RestoreCacheStep:
|
|
|
|
s.Name = "restore cache"
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Steps[i] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
runserviceClient *rsapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRunHandler(logger *zap.Logger, runserviceClient *rsapi.Client) *RunHandler {
|
|
|
|
return &RunHandler{log: logger.Sugar(), runserviceClient: runserviceClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
runID := vars["runid"]
|
|
|
|
|
|
|
|
runResp, resp, err := h.runserviceClient.GetRun(ctx, runID)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createRunResponse(runResp.Run, runResp.RunConfig)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RuntaskHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
runserviceClient *rsapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRuntaskHandler(logger *zap.Logger, runserviceClient *rsapi.Client) *RuntaskHandler {
|
|
|
|
return &RuntaskHandler{log: logger.Sugar(), runserviceClient: runserviceClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RuntaskHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
runID := vars["runid"]
|
|
|
|
taskID := vars["taskid"]
|
|
|
|
|
|
|
|
runResp, resp, err := h.runserviceClient.GetRun(ctx, runID)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
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 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrNotFound(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)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultRunsLimit = 25
|
|
|
|
MaxRunsLimit = 40
|
|
|
|
)
|
|
|
|
|
|
|
|
func createRunsResponse(r *rstypes.Run) *RunsResponse {
|
|
|
|
run := &RunsResponse{
|
|
|
|
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 {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
runserviceClient *rsapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRunsHandler(logger *zap.Logger, runserviceClient *rsapi.Client) *RunsHandler {
|
|
|
|
return &RunsHandler{log: logger.Sugar(), runserviceClient: runserviceClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-13 14:48:35 +00:00
|
|
|
groups := q["group"]
|
2019-04-03 06:52:29 +00:00
|
|
|
// we require that groups are specified to not return all runs
|
|
|
|
if len(groups) == 0 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("no groups specified")))
|
2019-04-03 06:52:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
phaseFilter := q["phase"]
|
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 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Wrapf(err, "cannot parse limit")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit < 0 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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-03-29 11:00:18 +00:00
|
|
|
runsResp, resp, err := h.runserviceClient.GetRuns(ctx, phaseFilter, groups, lastRun, changeGroups, start, limit, asc)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
runs := make([]*RunsResponse, len(runsResp.Runs))
|
|
|
|
for i, r := range runsResp.Runs {
|
|
|
|
runs[i] = createRunsResponse(r)
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, runs); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 15:12:07 +00:00
|
|
|
type RunActionType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
RunActionTypeRestart RunActionType = "restart"
|
2019-03-08 09:02:37 +00:00
|
|
|
RunActionTypeStop RunActionType = "stop"
|
2019-03-04 15:12:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RunActionsRequest struct {
|
|
|
|
ActionType RunActionType `json:"action_type"`
|
|
|
|
|
|
|
|
// Restart
|
|
|
|
FromStart bool `json:"from_start"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunActionsHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
runserviceClient *rsapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRunActionsHandler(logger *zap.Logger, runserviceClient *rsapi.Client) *RunActionsHandler {
|
|
|
|
return &RunActionsHandler{log: logger.Sugar(), runserviceClient: runserviceClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunActionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
runID := vars["runid"]
|
|
|
|
|
|
|
|
var req RunActionsRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-04 15:12:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.ActionType {
|
|
|
|
case RunActionTypeRestart:
|
2019-04-08 15:29:57 +00:00
|
|
|
rsreq := &rsapi.RunCreateRequest{
|
2019-03-04 15:12:07 +00:00
|
|
|
RunID: runID,
|
|
|
|
FromStart: req.FromStart,
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:29:57 +00:00
|
|
|
resp, err := h.runserviceClient.CreateRun(ctx, rsreq)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-04 15:12:07 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-08 09:02:37 +00:00
|
|
|
|
|
|
|
case RunActionTypeStop:
|
2019-04-08 15:29:57 +00:00
|
|
|
rsreq := &rsapi.RunActionsRequest{
|
2019-03-08 09:02:37 +00:00
|
|
|
ActionType: rsapi.RunActionTypeStop,
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:29:57 +00:00
|
|
|
resp, err := h.runserviceClient.RunActions(ctx, runID, rsreq)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-08 09:02:37 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-04 15:12:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:29:57 +00:00
|
|
|
type RunTaskActionType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
RunTaskActionTypeApprove RunTaskActionType = "approve"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RunTaskActionsRequest struct {
|
|
|
|
ActionType RunTaskActionType `json:"action_type"`
|
|
|
|
ApprovalAnnotations map[string]string `json:"approval_annotations,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunTaskActionsHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
runserviceClient *rsapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRunTaskActionsHandler(logger *zap.Logger, runserviceClient *rsapi.Client) *RunTaskActionsHandler {
|
|
|
|
return &RunTaskActionsHandler{log: logger.Sugar(), runserviceClient: runserviceClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunTaskActionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
runID := vars["runid"]
|
|
|
|
taskID := vars["taskid"]
|
|
|
|
|
|
|
|
var req RunTaskActionsRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-04-08 15:29:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.ActionType {
|
|
|
|
case RunTaskActionTypeApprove:
|
|
|
|
rsreq := &rsapi.RunTaskActionsRequest{
|
|
|
|
ActionType: rsapi.RunTaskActionTypeApprove,
|
|
|
|
ApprovalAnnotations: req.ApprovalAnnotations,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := h.runserviceClient.RunTaskActions(ctx, runID, taskID, rsreq)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-04-08 15:29:57 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
|
2019-04-08 15:29:57 +00:00
|
|
|
default:
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("wrong action type %q", req.ActionType)))
|
2019-04-08 15:29:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type LogsHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
runserviceClient *rsapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogsHandler(logger *zap.Logger, runserviceClient *rsapi.Client) *LogsHandler {
|
|
|
|
return &LogsHandler{log: logger.Sugar(), runserviceClient: runserviceClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 == "" {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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 == "" {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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 == "" {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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 != "" {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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
|
|
|
|
}
|
|
|
|
stream := false
|
2019-03-13 14:48:35 +00:00
|
|
|
if _, ok := q["stream"]; ok {
|
2019-02-21 16:58:25 +00:00
|
|
|
stream = true
|
|
|
|
}
|
|
|
|
if follow {
|
|
|
|
stream = true
|
|
|
|
}
|
|
|
|
|
2019-03-13 14:48:35 +00:00
|
|
|
resp, err := h.runserviceClient.GetLogs(ctx, runID, taskID, setup, step, follow, stream)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if stream {
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
}
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if stream {
|
|
|
|
if err := sendLogs(w, resp.Body); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := io.Copy(w, resp.Body); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendLogs is used during streaming to flush logs lines
|
|
|
|
// TODO(sgotti) there's no need to do br.ReadBytes since the response is
|
|
|
|
// already flushed by the runservice.
|
|
|
|
func sendLogs(w io.Writer, r io.Reader) error {
|
|
|
|
br := bufio.NewReader(r)
|
|
|
|
|
|
|
|
var flusher http.Flusher
|
|
|
|
if fl, ok := w.(http.Flusher); ok {
|
|
|
|
flusher = fl
|
|
|
|
}
|
|
|
|
stop := false
|
|
|
|
for {
|
|
|
|
if stop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
data, err := br.ReadBytes('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
stop = true
|
|
|
|
}
|
|
|
|
if _, err := w.Write(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if flusher != nil {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|