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.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/datamanager"
|
|
|
|
"agola.io/agola/internal/db"
|
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/etcd"
|
|
|
|
"agola.io/agola/internal/objectstorage"
|
|
|
|
"agola.io/agola/internal/services/runservice/action"
|
|
|
|
"agola.io/agola/internal/services/runservice/common"
|
|
|
|
"agola.io/agola/internal/services/runservice/readdb"
|
|
|
|
"agola.io/agola/internal/services/runservice/store"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
rsapitypes "agola.io/agola/services/runservice/api/types"
|
|
|
|
"agola.io/agola/services/runservice/types"
|
2019-02-21 14:54:50 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-05-15 07:46:21 +00:00
|
|
|
etcdclientv3 "go.etcd.io/etcd/clientv3"
|
|
|
|
etcdclientv3rpc "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
|
|
|
|
"go.etcd.io/etcd/mvcc/mvccpb"
|
2019-02-21 14:54:50 +00:00
|
|
|
)
|
|
|
|
|
2019-04-08 16:04:42 +00:00
|
|
|
type ErrorResponse struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
type LogsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-02-21 14:54:50 +00:00
|
|
|
e *etcd.Store
|
2019-04-27 13:16:48 +00:00
|
|
|
ost *objectstorage.ObjStorage
|
2019-04-26 14:00:03 +00:00
|
|
|
dm *datamanager.DataManager
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewLogsHandler(log zerolog.Logger, e *etcd.Store, ost *objectstorage.ObjStorage, dm *datamanager.DataManager) *LogsHandler {
|
2019-02-21 14:54:50 +00:00
|
|
|
return &LogsHandler{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-02-21 14:54:50 +00:00
|
|
|
e: e,
|
2019-04-27 13:16:48 +00:00
|
|
|
ost: ost,
|
2019-04-26 14:00:03 +00:00
|
|
|
dm: dm,
|
2019-02-21 14:54:50 +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 14:54:50 +00:00
|
|
|
|
2019-03-13 14:48:35 +00:00
|
|
|
runID := q.Get("runid")
|
2019-02-21 14:54:50 +00:00
|
|
|
if runID == "" {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2019-03-13 14:48:35 +00:00
|
|
|
taskID := q.Get("taskid")
|
2019-02-21 14:54:50 +00:00
|
|
|
if taskID == "" {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2019-03-13 14:48:35 +00:00
|
|
|
|
|
|
|
_, setup := q["setup"]
|
|
|
|
stepStr := q.Get("step")
|
|
|
|
if !setup && stepStr == "" {
|
2019-02-21 14:54:50 +00:00
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2019-03-13 14:48:35 +00:00
|
|
|
if setup && stepStr != "" {
|
2019-02-21 14:54:50 +00:00
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
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 {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
follow := false
|
2019-03-13 14:48:35 +00:00
|
|
|
if _, ok := q["follow"]; ok {
|
2019-02-21 14:54:50 +00:00
|
|
|
follow = true
|
|
|
|
}
|
|
|
|
|
2022-02-22 14:01:29 +00:00
|
|
|
if sendError, err := h.readTaskLogs(ctx, runID, taskID, setup, step, w, follow); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 14:54:50 +00:00
|
|
|
if sendError {
|
2019-11-06 12:29:42 +00:00
|
|
|
switch {
|
2022-02-21 11:19:55 +00:00
|
|
|
case util.APIErrorIs(err, util.ErrNotExist):
|
2022-02-22 14:01:29 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrNotExist, errors.Wrapf(err, "log doesn't exist")))
|
2019-02-21 14:54:50 +00:00
|
|
|
default:
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 14:01:29 +00:00
|
|
|
func (h *LogsHandler) readTaskLogs(ctx context.Context, runID, taskID string, setup bool, step int, w http.ResponseWriter, follow bool) (bool, error) {
|
2019-04-26 14:00:03 +00:00
|
|
|
r, err := store.GetRunEtcdOrOST(ctx, h.e, h.dm, runID)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, errors.WithStack(err)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
if r == nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, util.NewAPIError(util.ErrNotExist, errors.Errorf("no such run with id: %s", runID))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 15:45:38 +00:00
|
|
|
task, ok := r.Tasks[taskID]
|
2019-02-21 14:54:50 +00:00
|
|
|
if !ok {
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, util.NewAPIError(util.ErrNotExist, errors.Errorf("no such task with ID %s in run %s", taskID, runID))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
if len(task.Steps) <= step {
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, util.NewAPIError(util.ErrNotExist, errors.Errorf("no such step for task %s in run %s", taskID, runID))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the log has been already fetched use it, otherwise fetch it from the executor
|
|
|
|
if task.Steps[step].LogPhase == types.RunTaskFetchPhaseFinished {
|
2019-03-13 14:48:35 +00:00
|
|
|
var logPath string
|
|
|
|
if setup {
|
2019-04-27 13:16:48 +00:00
|
|
|
logPath = store.OSTRunTaskSetupLogPath(task.ID)
|
2019-03-13 14:48:35 +00:00
|
|
|
} else {
|
2019-04-27 13:16:48 +00:00
|
|
|
logPath = store.OSTRunTaskStepLogPath(task.ID, step)
|
2019-03-13 14:48:35 +00:00
|
|
|
}
|
2019-04-27 13:16:48 +00:00
|
|
|
f, err := h.ost.ReadObject(logPath)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
2019-11-06 12:29:42 +00:00
|
|
|
if objectstorage.IsNotExist(err) {
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, util.NewAPIError(util.ErrNotExist, err)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, errors.WithStack(err)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
2022-02-22 14:01:29 +00:00
|
|
|
return false, sendLogs(w, f)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
et, err := store.GetExecutorTask(ctx, h.e, task.ID)
|
|
|
|
if err != nil {
|
2022-02-24 11:56:52 +00:00
|
|
|
if errors.Is(err, etcd.ErrKeyNotFound) {
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, util.NewAPIError(util.ErrNotExist, errors.Errorf("executor task with id %q doesn't exist", task.ID))
|
2019-11-13 14:46:26 +00:00
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, errors.WithStack(err)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
2019-09-12 08:36:45 +00:00
|
|
|
executor, err := store.GetExecutor(ctx, h.e, et.Spec.ExecutorID)
|
2019-11-13 14:46:26 +00:00
|
|
|
if err != nil {
|
2022-02-24 11:56:52 +00:00
|
|
|
if errors.Is(err, etcd.ErrKeyNotFound) {
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, util.NewAPIError(util.ErrNotExist, errors.Errorf("executor with id %q doesn't exist", et.Spec.ExecutorID))
|
2019-11-13 14:46:26 +00:00
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, errors.WithStack(err)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 14:48:35 +00:00
|
|
|
var url string
|
|
|
|
if setup {
|
|
|
|
url = fmt.Sprintf("%s/api/v1alpha/executor/logs?taskid=%s&setup", executor.ListenURL, taskID)
|
|
|
|
} else {
|
|
|
|
url = fmt.Sprintf("%s/api/v1alpha/executor/logs?taskid=%s&step=%d", executor.ListenURL, taskID, step)
|
|
|
|
}
|
2019-02-21 14:54:50 +00:00
|
|
|
if follow {
|
|
|
|
url += "&follow"
|
|
|
|
}
|
|
|
|
req, err := http.Get(url)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, errors.WithStack(err)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
if req.StatusCode != http.StatusOK {
|
|
|
|
if req.StatusCode == http.StatusNotFound {
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, util.NewAPIError(util.ErrNotExist, errors.New("no log on executor"))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return true, errors.Errorf("received http status: %d", req.StatusCode)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
var flusher http.Flusher
|
|
|
|
if fl, ok := w.(http.Flusher); ok {
|
|
|
|
flusher = fl
|
|
|
|
}
|
|
|
|
if flusher != nil {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
|
2022-02-22 14:01:29 +00:00
|
|
|
return false, sendLogs(w, req.Body)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 12:35:04 +00:00
|
|
|
func sendLogs(w http.ResponseWriter, r io.Reader) error {
|
|
|
|
buf := make([]byte, 406)
|
2019-02-21 14:54:50 +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)
|
|
|
|
//data, err := br.ReadBytes('\n')
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
2019-05-19 12:35:04 +00:00
|
|
|
if n == 0 {
|
2019-02-21 14:54:50 +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 14:54:50 +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
|
|
|
e *etcd.Store
|
|
|
|
ost *objectstorage.ObjStorage
|
|
|
|
dm *datamanager.DataManager
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewLogsDeleteHandler(log zerolog.Logger, e *etcd.Store, ost *objectstorage.ObjStorage, dm *datamanager.DataManager) *LogsDeleteHandler {
|
2019-11-09 10:18:00 +00:00
|
|
|
return &LogsDeleteHandler{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-11-09 10:18:00 +00:00
|
|
|
e: e,
|
|
|
|
ost: ost,
|
|
|
|
dm: dm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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("runid is empty")))
|
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("taskid is empty")))
|
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("setup is false and step is empty")))
|
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("setup is true and step is %s", stepStr)))
|
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-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("step %s is not a valid number", stepStr)))
|
2019-11-09 10:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.deleteTaskLogs(ctx, runID, taskID, setup, step, w); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-11-09 10:18:00 +00:00
|
|
|
switch {
|
2022-02-21 11:19:55 +00:00
|
|
|
case util.APIErrorIs(err, util.ErrNotExist):
|
2022-02-22 14:01:29 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrNotExist, errors.Wrapf(err, "log doesn't exist")))
|
2019-11-09 10:18:00 +00:00
|
|
|
default:
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-11-09 10:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *LogsDeleteHandler) deleteTaskLogs(ctx context.Context, runID, taskID string, setup bool, step int, w http.ResponseWriter) error {
|
|
|
|
r, err := store.GetRunEtcdOrOST(ctx, h.e, h.dm, runID)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-11-09 10:18:00 +00:00
|
|
|
}
|
|
|
|
if r == nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrNotExist, errors.Errorf("no such run with id: %s", runID))
|
2019-11-09 10:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
task, ok := r.Tasks[taskID]
|
|
|
|
if !ok {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrNotExist, errors.Errorf("no such task with ID %s in run %s", taskID, runID))
|
2019-11-09 10:18:00 +00:00
|
|
|
}
|
|
|
|
if len(task.Steps) <= step {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrNotExist, errors.Errorf("no such step for task %s in run %s", taskID, runID))
|
2019-11-09 10:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if task.Steps[step].LogPhase == types.RunTaskFetchPhaseFinished {
|
|
|
|
var logPath string
|
|
|
|
if setup {
|
|
|
|
logPath = store.OSTRunTaskSetupLogPath(task.ID)
|
|
|
|
} else {
|
|
|
|
logPath = store.OSTRunTaskStepLogPath(task.ID, step)
|
|
|
|
}
|
|
|
|
err := h.ost.DeleteObject(logPath)
|
|
|
|
if err != nil {
|
|
|
|
if objectstorage.IsNotExist(err) {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrNotExist, err)
|
2019-11-09 10:18:00 +00:00
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-11-09 10:18:00 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("Log for task %s in run %s is not yet archived", taskID, runID))
|
2019-11-09 10:18:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
type ChangeGroupsUpdateTokensHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-02-21 14:54:50 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewChangeGroupsUpdateTokensHandler(log zerolog.Logger, readDB *readdb.ReadDB) *ChangeGroupsUpdateTokensHandler {
|
2019-02-21 14:54:50 +00:00
|
|
|
return &ChangeGroupsUpdateTokensHandler{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-02-21 14:54:50 +00:00
|
|
|
readDB: readDB,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ChangeGroupsUpdateTokensHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-07-25 08:46:02 +00:00
|
|
|
ctx := r.Context()
|
2019-02-21 14:54:50 +00:00
|
|
|
query := r.URL.Query()
|
|
|
|
groups := query["changegroup"]
|
|
|
|
|
|
|
|
var cgt *types.ChangeGroupsUpdateToken
|
|
|
|
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-02-21 14:54:50 +00:00
|
|
|
var err error
|
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, groups)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 14:54:50 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cgts, err := types.MarshalChangeGroupsUpdateToken(cgt)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, cgts); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-02-21 14:54:50 +00:00
|
|
|
e *etcd.Store
|
2019-04-26 14:00:03 +00:00
|
|
|
dm *datamanager.DataManager
|
2019-02-21 14:54:50 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRunHandler(log zerolog.Logger, e *etcd.Store, dm *datamanager.DataManager, readDB *readdb.ReadDB) *RunHandler {
|
2019-02-21 14:54:50 +00:00
|
|
|
return &RunHandler{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-02-21 14:54:50 +00:00
|
|
|
e: e,
|
2019-04-26 14:00:03 +00:00
|
|
|
dm: dm,
|
2019-02-21 14:54:50 +00:00
|
|
|
readDB: readDB,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-07-25 08:46:02 +00:00
|
|
|
ctx := r.Context()
|
2019-02-21 14:54:50 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
runID := vars["runid"]
|
|
|
|
|
2019-05-06 13:18:49 +00:00
|
|
|
query := r.URL.Query()
|
|
|
|
changeGroups := query["changegroup"]
|
|
|
|
|
|
|
|
var run *types.Run
|
|
|
|
var cgt *types.ChangeGroupsUpdateToken
|
|
|
|
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-06 13:18:49 +00:00
|
|
|
var err error
|
|
|
|
run, err = h.readDB.GetRun(tx, runID)
|
|
|
|
if err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-06 13:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, changeGroups)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-06 13:18:49 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-02-21 14:54:50 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2019-07-08 07:30:15 +00:00
|
|
|
if run == nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrNotExist, errors.Errorf("run %q doesn't exist", runID)))
|
2019-07-08 07:30:15 +00:00
|
|
|
return
|
|
|
|
}
|
2019-05-06 13:18:49 +00:00
|
|
|
|
|
|
|
cgts, err := types.MarshalChangeGroupsUpdateToken(cgt)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2019-02-21 14:54:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-26 14:00:03 +00:00
|
|
|
rc, err := store.OSTGetRunConfig(h.dm, run.ID)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
res := &rsapitypes.RunResponse{
|
2019-05-06 13:18:49 +00:00
|
|
|
Run: run,
|
|
|
|
RunConfig: rc,
|
|
|
|
ChangeGroupsUpdateToken: cgts,
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
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 14:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultRunsLimit = 25
|
|
|
|
MaxRunsLimit = 40
|
|
|
|
)
|
|
|
|
|
|
|
|
type RunsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-02-21 14:54:50 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRunsHandler(log zerolog.Logger, readDB *readdb.ReadDB) *RunsHandler {
|
2019-02-21 14:54:50 +00:00
|
|
|
return &RunsHandler{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-02-21 14:54:50 +00:00
|
|
|
readDB: readDB,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-07-25 08:46:02 +00:00
|
|
|
ctx := r.Context()
|
2019-02-21 14:54:50 +00:00
|
|
|
query := r.URL.Query()
|
|
|
|
phaseFilter := types.RunPhaseFromStringSlice(query["phase"])
|
2019-07-05 08:32:51 +00:00
|
|
|
resultFilter := types.RunResultFromStringSlice(query["result"])
|
2019-02-21 14:54:50 +00:00
|
|
|
|
|
|
|
changeGroups := query["changegroup"]
|
|
|
|
groups := query["group"]
|
2019-03-29 11:00:18 +00:00
|
|
|
_, lastRun := query["lastrun"]
|
2019-02-21 14:54:50 +00:00
|
|
|
|
|
|
|
limitS := query.Get("limit")
|
|
|
|
limit := DefaultRunsLimit
|
|
|
|
if limitS != "" {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(limitS)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit < 0 {
|
|
|
|
http.Error(w, "limit must be greater or equal than 0", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if limit > MaxRunsLimit {
|
|
|
|
limit = MaxRunsLimit
|
|
|
|
}
|
|
|
|
sortOrder := types.SortOrderDesc
|
|
|
|
if _, ok := query["asc"]; ok {
|
|
|
|
sortOrder = types.SortOrderAsc
|
|
|
|
}
|
|
|
|
|
|
|
|
start := query.Get("start")
|
|
|
|
|
|
|
|
var runs []*types.Run
|
|
|
|
var cgt *types.ChangeGroupsUpdateToken
|
|
|
|
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-02-21 14:54:50 +00:00
|
|
|
var err error
|
2019-07-05 08:32:51 +00:00
|
|
|
runs, err = h.readDB.GetRuns(tx, groups, lastRun, phaseFilter, resultFilter, start, limit, sortOrder)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, changeGroups)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 14:54:50 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cgts, err := types.MarshalChangeGroupsUpdateToken(cgt)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
res := &rsapitypes.GetRunsResponse{
|
2019-02-21 14:54:50 +00:00
|
|
|
Runs: runs,
|
|
|
|
ChangeGroupsUpdateToken: cgts,
|
|
|
|
}
|
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 14:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunCreateHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-03 21:59:21 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRunCreateHandler(log zerolog.Logger, ah *action.ActionHandler) *RunCreateHandler {
|
2019-02-21 14:54:50 +00:00
|
|
|
return &RunCreateHandler{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-05-03 21:59:21 +00:00
|
|
|
ah: ah,
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req rsapitypes.RunCreateRequest
|
2019-02-21 14:54:50 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
creq := &action.RunCreateRequest{
|
2019-04-09 16:11:00 +00:00
|
|
|
RunConfigTasks: req.RunConfigTasks,
|
|
|
|
Name: req.Name,
|
|
|
|
Group: req.Group,
|
2019-04-09 14:51:37 +00:00
|
|
|
SetupErrors: req.SetupErrors,
|
2019-04-09 16:11:00 +00:00
|
|
|
StaticEnvironment: req.StaticEnvironment,
|
2019-07-03 13:16:37 +00:00
|
|
|
CacheGroup: req.CacheGroup,
|
2019-04-09 16:11:00 +00:00
|
|
|
|
|
|
|
RunID: req.RunID,
|
|
|
|
FromStart: req.FromStart,
|
|
|
|
ResetTasks: req.ResetTasks,
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
Environment: req.Environment,
|
|
|
|
Annotations: req.Annotations,
|
|
|
|
ChangeGroupsUpdateToken: req.ChangeGroupsUpdateToken,
|
|
|
|
}
|
2019-05-03 21:59:21 +00:00
|
|
|
rb, err := h.ah.CreateRun(ctx, creq)
|
2019-03-04 15:11:18 +00:00
|
|
|
if err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-21 14:54:50 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-04 15:11:18 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
res := &rsapitypes.RunResponse{
|
2019-03-04 15:11:18 +00:00
|
|
|
Run: rb.Run,
|
|
|
|
RunConfig: rb.Rc,
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RunActionsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-07-02 12:53:01 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRunActionsHandler(log zerolog.Logger, ah *action.ActionHandler) *RunActionsHandler {
|
2019-02-21 14:54:50 +00:00
|
|
|
return &RunActionsHandler{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-05-03 21:59:21 +00:00
|
|
|
ah: ah,
|
2019-02-21 14:54:50 +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 rsapitypes.RunActionsRequest
|
2019-02-21 14:54:50 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.ActionType {
|
2019-07-31 13:39:07 +00:00
|
|
|
case rsapitypes.RunActionTypeChangePhase:
|
2019-05-03 21:59:21 +00:00
|
|
|
creq := &action.RunChangePhaseRequest{
|
2019-02-21 14:54:50 +00:00
|
|
|
RunID: runID,
|
|
|
|
Phase: req.Phase,
|
|
|
|
ChangeGroupsUpdateToken: req.ChangeGroupsUpdateToken,
|
|
|
|
}
|
2019-05-03 21:59:21 +00:00
|
|
|
if err := h.ah.ChangeRunPhase(ctx, creq); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-21 14:54:50 +00:00
|
|
|
return
|
|
|
|
}
|
2019-07-31 13:39:07 +00:00
|
|
|
case rsapitypes.RunActionTypeStop:
|
2019-05-03 21:59:21 +00:00
|
|
|
creq := &action.RunStopRequest{
|
2019-03-08 09:02:37 +00:00
|
|
|
RunID: runID,
|
|
|
|
ChangeGroupsUpdateToken: req.ChangeGroupsUpdateToken,
|
|
|
|
}
|
2019-05-03 21:59:21 +00:00
|
|
|
if err := h.ah.StopRun(ctx, creq); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-03-08 09:02:37 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-21 14:54:50 +00:00
|
|
|
default:
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunTaskActionsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-07-02 12:53:01 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRunTaskActionsHandler(log zerolog.Logger, ah *action.ActionHandler) *RunTaskActionsHandler {
|
2019-02-21 14:54:50 +00:00
|
|
|
return &RunTaskActionsHandler{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-05-03 21:59:21 +00:00
|
|
|
ah: ah,
|
2019-02-21 14:54:50 +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 rsapitypes.RunTaskActionsRequest
|
2019-02-21 14:54:50 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.ActionType {
|
2019-07-31 13:39:07 +00:00
|
|
|
case rsapitypes.RunTaskActionTypeSetAnnotations:
|
2019-05-06 13:19:29 +00:00
|
|
|
creq := &action.RunTaskSetAnnotationsRequest{
|
|
|
|
RunID: runID,
|
|
|
|
TaskID: taskID,
|
|
|
|
Annotations: req.Annotations,
|
|
|
|
ChangeGroupsUpdateToken: req.ChangeGroupsUpdateToken,
|
|
|
|
}
|
|
|
|
if err := h.ah.RunTaskSetAnnotations(ctx, creq); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-05-06 13:19:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
case rsapitypes.RunTaskActionTypeApprove:
|
2019-05-03 21:59:21 +00:00
|
|
|
creq := &action.RunTaskApproveRequest{
|
2019-02-21 14:54:50 +00:00
|
|
|
RunID: runID,
|
|
|
|
TaskID: taskID,
|
|
|
|
ChangeGroupsUpdateToken: req.ChangeGroupsUpdateToken,
|
|
|
|
}
|
2019-05-03 21:59:21 +00:00
|
|
|
if err := h.ah.ApproveRunTask(ctx, creq); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-21 14:54:50 +00:00
|
|
|
return
|
|
|
|
}
|
2019-05-06 13:19:29 +00:00
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
default:
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 07:46:21 +00:00
|
|
|
|
|
|
|
type RunEventsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-15 07:46:21 +00:00
|
|
|
e *etcd.Store
|
|
|
|
ost *objectstorage.ObjStorage
|
|
|
|
dm *datamanager.DataManager
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRunEventsHandler(log zerolog.Logger, e *etcd.Store, ost *objectstorage.ObjStorage, dm *datamanager.DataManager) *RunEventsHandler {
|
2019-05-15 07:46:21 +00:00
|
|
|
return &RunEventsHandler{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-05-15 07:46:21 +00:00
|
|
|
e: e,
|
|
|
|
ost: ost,
|
|
|
|
dm: dm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
func (h *RunEventsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
|
|
|
|
// TODO(sgotti) handle additional events filtering (by type, etc...)
|
|
|
|
startRunEventID := q.Get("startruneventid")
|
|
|
|
|
|
|
|
if err := h.sendRunEvents(ctx, startRunEventID, w); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-15 07:46:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RunEventsHandler) sendRunEvents(ctx context.Context, startRunEventID string, w http.ResponseWriter) error {
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
|
|
|
|
var flusher http.Flusher
|
|
|
|
if fl, ok := w.(http.Flusher); ok {
|
|
|
|
flusher = fl
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(sgotti) fetch from previous events (handle startRunEventID).
|
|
|
|
// Use the readdb instead of etcd
|
|
|
|
|
|
|
|
wctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
wctx = etcdclientv3.WithRequireLeader(wctx)
|
|
|
|
wch := h.e.WatchKey(wctx, common.EtcdRunEventKey, 0)
|
|
|
|
for wresp := range wch {
|
|
|
|
if wresp.Canceled {
|
|
|
|
err := wresp.Err()
|
2022-02-24 11:56:52 +00:00
|
|
|
if errors.Is(err, etcdclientv3rpc.ErrCompacted) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Msgf("required events already compacted")
|
2019-05-15 07:46:21 +00:00
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "watch error")
|
2019-05-15 07:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ev := range wresp.Events {
|
|
|
|
switch ev.Type {
|
|
|
|
case mvccpb.PUT:
|
|
|
|
var runEvent *types.RunEvent
|
|
|
|
if err := json.Unmarshal(ev.Kv.Value, &runEvent); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "failed to unmarshal run")
|
2019-05-15 07:46:21 +00:00
|
|
|
}
|
|
|
|
if _, err := w.Write([]byte(fmt.Sprintf("data: %s\n\n", ev.Kv.Value))); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-15 07:46:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if flusher != nil {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|