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 (
|
|
|
|
"bufio"
|
2019-04-24 11:25:41 +00:00
|
|
|
"context"
|
2019-02-21 14:54:50 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2019-06-12 16:12:37 +00:00
|
|
|
"time"
|
2019-02-21 14:54:50 +00:00
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/etcd"
|
|
|
|
"agola.io/agola/internal/objectstorage"
|
|
|
|
ostypes "agola.io/agola/internal/objectstorage/types"
|
|
|
|
"agola.io/agola/internal/services/runservice/action"
|
|
|
|
"agola.io/agola/internal/services/runservice/common"
|
|
|
|
"agola.io/agola/internal/services/runservice/store"
|
|
|
|
"agola.io/agola/internal/services/runservice/types"
|
2019-05-21 13:17:53 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2019-02-21 14:54:50 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ExecutorStatusHandler struct {
|
2019-04-24 11:25:41 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
e *etcd.Store
|
2019-05-03 21:59:21 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func NewExecutorStatusHandler(logger *zap.Logger, e *etcd.Store, ah *action.ActionHandler) *ExecutorStatusHandler {
|
|
|
|
return &ExecutorStatusHandler{log: logger.Sugar(), e: e, ah: ah}
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ExecutorStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
// TODO(sgotti) Check authorized call from executors
|
|
|
|
var executor *types.Executor
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if err := d.Decode(&executor); err != nil {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:12:37 +00:00
|
|
|
// set last status update time
|
|
|
|
executor.LastStatusUpdateTime = time.Now()
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
if _, err := store.PutExecutor(ctx, h.e, executor); err != nil {
|
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2019-04-24 11:25:41 +00:00
|
|
|
|
|
|
|
if err := h.deleteStaleExecutors(ctx, executor); err != nil {
|
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ExecutorStatusHandler) deleteStaleExecutors(ctx context.Context, curExecutor *types.Executor) error {
|
|
|
|
executors, err := store.GetExecutors(ctx, h.e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, executor := range executors {
|
|
|
|
if executor.ID == curExecutor.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !executor.Dynamic {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if executor.ExecutorGroup != curExecutor.ExecutorGroup {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// executor is dynamic and in the same executor group
|
|
|
|
active := false
|
|
|
|
for _, seID := range curExecutor.SiblingsExecutors {
|
|
|
|
if executor.ID == seID {
|
|
|
|
active = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !active {
|
2019-05-03 21:59:21 +00:00
|
|
|
if err := h.ah.DeleteExecutor(ctx, executor.ID); err != nil {
|
2019-04-24 11:25:41 +00:00
|
|
|
h.log.Errorf("failed to delete executor %q: %v", executor.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExecutorTaskStatusHandler struct {
|
|
|
|
e *etcd.Store
|
|
|
|
c chan<- *types.ExecutorTask
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExecutorTaskStatusHandler(e *etcd.Store, c chan<- *types.ExecutorTask) *ExecutorTaskStatusHandler {
|
|
|
|
return &ExecutorTaskStatusHandler{e: e, c: c}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ExecutorTaskStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
// TODO(sgotti) Check authorized call from executors
|
|
|
|
var et *types.ExecutorTask
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if err := d.Decode(&et); err != nil {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := store.UpdateExecutorTaskStatus(ctx, h.e, et); err != nil {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() { h.c <- et }()
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecutorTaskHandler struct {
|
|
|
|
e *etcd.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExecutorTaskHandler(e *etcd.Store) *ExecutorTaskHandler {
|
|
|
|
return &ExecutorTaskHandler{e: e}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ExecutorTaskHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
// TODO(sgotti) Check authorized call from executors
|
|
|
|
etID := vars["taskid"]
|
|
|
|
if etID == "" {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
et, err := store.GetExecutorTask(ctx, h.e, etID)
|
|
|
|
if err != nil && err != etcd.ErrKeyNotFound {
|
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if et == nil {
|
|
|
|
http.Error(w, "", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(et); err != nil {
|
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecutorTasksHandler struct {
|
|
|
|
e *etcd.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExecutorTasksHandler(e *etcd.Store) *ExecutorTasksHandler {
|
|
|
|
return &ExecutorTasksHandler{e: e}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ExecutorTasksHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
// TODO(sgotti) Check authorized call from executors
|
|
|
|
executorID := vars["executorid"]
|
|
|
|
if executorID == "" {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ets, err := store.GetExecutorTasks(ctx, h.e, executorID)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(ets); err != nil {
|
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ArchivesHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-04-27 13:16:48 +00:00
|
|
|
ost *objectstorage.ObjStorage
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 13:16:48 +00:00
|
|
|
func NewArchivesHandler(logger *zap.Logger, ost *objectstorage.ObjStorage) *ArchivesHandler {
|
2019-02-21 14:54:50 +00:00
|
|
|
return &ArchivesHandler{
|
|
|
|
log: logger.Sugar(),
|
2019-04-27 13:16:48 +00:00
|
|
|
ost: ost,
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ArchivesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-04-13 12:58:56 +00:00
|
|
|
// TODO(sgotti) Check authorized call from executors
|
2019-02-21 14:54:50 +00:00
|
|
|
|
|
|
|
taskID := r.URL.Query().Get("taskid")
|
|
|
|
if taskID == "" {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s := r.URL.Query().Get("step")
|
|
|
|
if s == "" {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
step, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
|
|
|
|
if err := h.readArchive(taskID, step, w); err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case common.ErrNotExist:
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
default:
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ArchivesHandler) readArchive(rtID string, step int, w io.Writer) error {
|
2019-05-08 10:11:46 +00:00
|
|
|
archivePath := store.OSTRunTaskArchivePath(rtID, step)
|
2019-04-27 13:16:48 +00:00
|
|
|
f, err := h.ost.ReadObject(archivePath)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
2019-05-21 13:17:53 +00:00
|
|
|
if err == ostypes.ErrNotExist {
|
2019-02-21 14:54:50 +00:00
|
|
|
return common.NewErrNotExist(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
br := bufio.NewReader(f)
|
|
|
|
|
|
|
|
_, err = io.Copy(w, br)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:58:56 +00:00
|
|
|
type CacheHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-04-27 13:16:48 +00:00
|
|
|
ost *objectstorage.ObjStorage
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 13:16:48 +00:00
|
|
|
func NewCacheHandler(logger *zap.Logger, ost *objectstorage.ObjStorage) *CacheHandler {
|
2019-04-13 12:58:56 +00:00
|
|
|
return &CacheHandler{
|
|
|
|
log: logger.Sugar(),
|
2019-04-27 13:16:48 +00:00
|
|
|
ost: ost,
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CacheHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
// TODO(sgotti) Check authorized call from executors
|
2019-05-08 10:15:17 +00:00
|
|
|
|
|
|
|
// keep and use the escaped path
|
|
|
|
key := vars["key"]
|
2019-04-13 12:58:56 +00:00
|
|
|
if key == "" {
|
|
|
|
http.Error(w, "empty cache key", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(key) > common.MaxCacheKeyLength {
|
|
|
|
http.Error(w, "cache key too long", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
query := r.URL.Query()
|
|
|
|
_, prefix := query["prefix"]
|
|
|
|
|
2019-04-27 13:16:48 +00:00
|
|
|
matchedKey, err := matchCache(h.ost, key, prefix)
|
2019-04-13 12:58:56 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if matchedKey == "" {
|
|
|
|
http.Error(w, "", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
|
|
|
|
if err := h.readCache(matchedKey, w); err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case common.ErrNotExist:
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
default:
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 13:16:48 +00:00
|
|
|
func matchCache(ost *objectstorage.ObjStorage, key string, prefix bool) (string, error) {
|
|
|
|
cachePath := store.OSTCachePath(key)
|
2019-04-13 12:58:56 +00:00
|
|
|
|
|
|
|
if prefix {
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
|
|
|
|
// get the latest modified object
|
2019-05-21 13:17:53 +00:00
|
|
|
var lastObject *ostypes.ObjectInfo
|
2019-04-27 13:16:48 +00:00
|
|
|
for object := range ost.List(store.OSTCacheDir()+"/"+key, "", false, doneCh) {
|
2019-04-13 12:58:56 +00:00
|
|
|
if object.Err != nil {
|
|
|
|
return "", object.Err
|
|
|
|
}
|
|
|
|
|
2019-07-02 12:53:01 +00:00
|
|
|
if (lastObject == nil) || (lastObject.LastModified.Before(object.LastModified)) {
|
2019-04-13 12:58:56 +00:00
|
|
|
lastObject = &object
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if lastObject == nil {
|
|
|
|
return "", nil
|
|
|
|
|
|
|
|
}
|
2019-04-27 13:16:48 +00:00
|
|
|
return store.OSTCacheKey(lastObject.Path), nil
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 13:16:48 +00:00
|
|
|
_, err := ost.Stat(cachePath)
|
2019-05-21 13:17:53 +00:00
|
|
|
if err == ostypes.ErrNotExist {
|
2019-04-13 12:58:56 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CacheHandler) readCache(key string, w io.Writer) error {
|
2019-04-27 13:16:48 +00:00
|
|
|
cachePath := store.OSTCachePath(key)
|
|
|
|
f, err := h.ost.ReadObject(cachePath)
|
2019-04-13 12:58:56 +00:00
|
|
|
if err != nil {
|
2019-05-21 13:17:53 +00:00
|
|
|
if err == ostypes.ErrNotExist {
|
2019-04-13 12:58:56 +00:00
|
|
|
return common.NewErrNotExist(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
br := bufio.NewReader(f)
|
|
|
|
|
|
|
|
_, err = io.Copy(w, br)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type CacheCreateHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-04-27 13:16:48 +00:00
|
|
|
ost *objectstorage.ObjStorage
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 13:16:48 +00:00
|
|
|
func NewCacheCreateHandler(logger *zap.Logger, ost *objectstorage.ObjStorage) *CacheCreateHandler {
|
2019-04-13 12:58:56 +00:00
|
|
|
return &CacheCreateHandler{
|
|
|
|
log: logger.Sugar(),
|
2019-04-27 13:16:48 +00:00
|
|
|
ost: ost,
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CacheCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
// TODO(sgotti) Check authorized call from executors
|
2019-05-08 10:15:17 +00:00
|
|
|
|
|
|
|
// keep and use the escaped path
|
|
|
|
key := vars["key"]
|
2019-04-13 12:58:56 +00:00
|
|
|
if key == "" {
|
|
|
|
http.Error(w, "empty cache key", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(key) > common.MaxCacheKeyLength {
|
|
|
|
http.Error(w, "cache key too long", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
|
2019-04-27 13:16:48 +00:00
|
|
|
matchedKey, err := matchCache(h.ost, key, false)
|
2019-04-13 12:58:56 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if matchedKey != "" {
|
|
|
|
http.Error(w, "", http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:49:55 +00:00
|
|
|
size := int64(-1)
|
|
|
|
sizeStr := r.Header.Get("Content-Length")
|
|
|
|
if sizeStr != "" {
|
|
|
|
size, err = strconv.ParseInt(sizeStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 13:16:48 +00:00
|
|
|
cachePath := store.OSTCachePath(key)
|
2019-05-02 07:49:55 +00:00
|
|
|
if err := h.ost.WriteObject(cachePath, r.Body, size, false); err != nil {
|
2019-04-13 12:58:56 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
type ExecutorDeleteHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:59:21 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func NewExecutorDeleteHandler(logger *zap.Logger, ah *action.ActionHandler) *ExecutorDeleteHandler {
|
2019-02-21 14:54:50 +00:00
|
|
|
return &ExecutorDeleteHandler{
|
|
|
|
log: logger.Sugar(),
|
2019-05-03 21:59:21 +00:00
|
|
|
ah: ah,
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ExecutorDeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
// TODO(sgotti) Check authorized call from executors
|
|
|
|
executorID := vars["executorid"]
|
|
|
|
if executorID == "" {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
if err := h.ah.DeleteExecutor(ctx, executorID); err != nil {
|
2019-02-21 14:54:50 +00:00
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|