2019-02-21 14:54:50 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
package action
|
2019-02-21 14:54:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-04-12 14:46:04 +00:00
|
|
|
"fmt"
|
2019-03-29 11:09:32 +00:00
|
|
|
"path"
|
2019-02-21 14:54:50 +00:00
|
|
|
"time"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/datamanager"
|
|
|
|
"agola.io/agola/internal/db"
|
|
|
|
"agola.io/agola/internal/etcd"
|
|
|
|
"agola.io/agola/internal/objectstorage"
|
|
|
|
"agola.io/agola/internal/runconfig"
|
|
|
|
"agola.io/agola/internal/sequence"
|
|
|
|
"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/services/runservice/types"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-02-21 14:54:50 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 14:54:50 +00:00
|
|
|
)
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
type ActionHandler struct {
|
2019-04-01 10:54:43 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
e *etcd.Store
|
|
|
|
readDB *readdb.ReadDB
|
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
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func NewActionHandler(logger *zap.Logger, e *etcd.Store, readDB *readdb.ReadDB, ost *objectstorage.ObjStorage, dm *datamanager.DataManager) *ActionHandler {
|
|
|
|
return &ActionHandler{
|
2019-04-01 10:54:43 +00:00
|
|
|
log: logger.Sugar(),
|
|
|
|
e: e,
|
|
|
|
readDB: readDB,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunChangePhaseRequest struct {
|
|
|
|
RunID string
|
|
|
|
Phase types.RunPhase
|
|
|
|
ChangeGroupsUpdateToken string
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func (h *ActionHandler) ChangeRunPhase(ctx context.Context, req *RunChangePhaseRequest) error {
|
2019-02-21 14:54:50 +00:00
|
|
|
cgt, err := types.UnmarshalChangeGroupsUpdateToken(req.ChangeGroupsUpdateToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
r, _, err := store.GetRun(ctx, h.e, req.RunID)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:41:56 +00:00
|
|
|
var runEvent *types.RunEvent
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
switch req.Phase {
|
|
|
|
case types.RunPhaseRunning:
|
|
|
|
if r.Phase != types.RunPhaseQueued {
|
2019-05-15 12:42:50 +00:00
|
|
|
return errors.Errorf("run %q is not queued but in %q phase", r.ID, r.Phase)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
r.ChangePhase(types.RunPhaseRunning)
|
2019-05-15 07:41:56 +00:00
|
|
|
runEvent, err = common.NewRunEvent(ctx, h.e, r.ID, r.Phase, r.Result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-15 12:42:50 +00:00
|
|
|
case types.RunPhaseCancelled:
|
|
|
|
if r.Phase != types.RunPhaseQueued {
|
|
|
|
return errors.Errorf("run %q is not queued but in %q phase", r.ID, r.Phase)
|
|
|
|
}
|
|
|
|
r.ChangePhase(types.RunPhaseCancelled)
|
|
|
|
runEvent, err = common.NewRunEvent(ctx, h.e, r.ID, r.Phase, r.Result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-08 09:02:37 +00:00
|
|
|
}
|
2019-05-15 12:42:50 +00:00
|
|
|
default:
|
|
|
|
return errors.Errorf("unsupport change phase %q", req.Phase)
|
|
|
|
|
2019-03-08 09:02:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 07:41:56 +00:00
|
|
|
_, err = store.AtomicPutRun(ctx, h.e, r, runEvent, cgt)
|
2019-03-08 09:02:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunStopRequest struct {
|
|
|
|
RunID string
|
|
|
|
ChangeGroupsUpdateToken string
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func (h *ActionHandler) StopRun(ctx context.Context, req *RunStopRequest) error {
|
2019-03-08 09:02:37 +00:00
|
|
|
cgt, err := types.UnmarshalChangeGroupsUpdateToken(req.ChangeGroupsUpdateToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
r, _, err := store.GetRun(ctx, h.e, req.RunID)
|
2019-03-08 09:02:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Phase != types.RunPhaseRunning {
|
|
|
|
return errors.Errorf("run %s is not running but in %q phase", r.ID, r.Phase)
|
|
|
|
}
|
2019-07-23 10:11:01 +00:00
|
|
|
r.Stop = true
|
2019-02-21 14:54:50 +00:00
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
_, err = store.AtomicPutRun(ctx, h.e, r, nil, cgt)
|
2019-02-21 14:54:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunCreateRequest struct {
|
2019-04-09 16:11:00 +00:00
|
|
|
RunConfigTasks map[string]*types.RunConfigTask
|
|
|
|
Name string
|
|
|
|
Group string
|
2019-04-09 14:51:37 +00:00
|
|
|
SetupErrors []string
|
2019-04-09 16:11:00 +00:00
|
|
|
StaticEnvironment map[string]string
|
2019-07-03 13:16:37 +00:00
|
|
|
CacheGroup string
|
2019-04-09 16:11:00 +00:00
|
|
|
|
|
|
|
// existing run fields
|
|
|
|
RunID string
|
|
|
|
FromStart bool
|
|
|
|
ResetTasks []string
|
|
|
|
|
|
|
|
// common fields
|
|
|
|
Environment map[string]string
|
|
|
|
Annotations map[string]string
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
ChangeGroupsUpdateToken string
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func (h *ActionHandler) CreateRun(ctx context.Context, req *RunCreateRequest) (*types.RunBundle, error) {
|
2019-02-21 14:54:50 +00:00
|
|
|
runcgt, err := types.UnmarshalChangeGroupsUpdateToken(req.ChangeGroupsUpdateToken)
|
|
|
|
if err != nil {
|
2019-03-04 15:11:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var rb *types.RunBundle
|
|
|
|
if req.RunID == "" {
|
2019-05-03 21:59:21 +00:00
|
|
|
rb, err = h.newRun(ctx, req)
|
2019-03-04 15:11:18 +00:00
|
|
|
} else {
|
2019-05-03 21:59:21 +00:00
|
|
|
rb, err = h.recreateRun(ctx, req)
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
return rb, h.saveRun(ctx, rb, runcgt)
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func (h *ActionHandler) newRun(ctx context.Context, req *RunCreateRequest) (*types.RunBundle, error) {
|
2019-04-09 16:11:00 +00:00
|
|
|
rcts := req.RunConfigTasks
|
2019-04-09 14:51:37 +00:00
|
|
|
setupErrors := req.SetupErrors
|
2019-02-21 14:54:50 +00:00
|
|
|
|
2019-03-29 11:09:32 +00:00
|
|
|
if req.Group == "" {
|
2019-04-08 16:04:42 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("run group is empty"))
|
2019-03-29 11:09:32 +00:00
|
|
|
}
|
|
|
|
if !path.IsAbs(req.Group) {
|
2019-04-08 16:04:42 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("run group %q must be an absolute path", req.Group))
|
2019-03-29 11:09:32 +00:00
|
|
|
}
|
2019-04-09 14:51:37 +00:00
|
|
|
if req.RunConfigTasks == nil && len(setupErrors) == 0 {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("empty run config tasks and setup errors"))
|
|
|
|
}
|
2019-03-29 11:09:32 +00:00
|
|
|
|
2019-04-09 16:11:00 +00:00
|
|
|
// generate a new run sequence that will be the same for the run and runconfig
|
2019-05-03 21:59:21 +00:00
|
|
|
seq, err := sequence.IncSequence(ctx, h.e, common.EtcdRunSequenceKey)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
2019-03-04 15:11:18 +00:00
|
|
|
return nil, err
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
id := seq.String()
|
|
|
|
|
2019-04-09 16:11:00 +00:00
|
|
|
if err := runconfig.CheckRunConfigTasks(rcts); err != nil {
|
2019-05-03 21:59:21 +00:00
|
|
|
h.log.Errorf("check run config tasks failed: %+v", err)
|
2019-04-09 14:51:37 +00:00
|
|
|
setupErrors = append(setupErrors, err.Error())
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate tasks levels
|
2019-04-09 14:51:37 +00:00
|
|
|
if len(setupErrors) == 0 {
|
|
|
|
if err := runconfig.GenTasksLevels(rcts); err != nil {
|
2019-05-03 21:59:21 +00:00
|
|
|
h.log.Errorf("gen tasks leveles failed: %+v", err)
|
2019-04-09 14:51:37 +00:00
|
|
|
setupErrors = append(setupErrors, err.Error())
|
|
|
|
}
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 16:11:00 +00:00
|
|
|
rc := &types.RunConfig{
|
|
|
|
ID: id,
|
|
|
|
Name: req.Name,
|
|
|
|
Group: req.Group,
|
2019-04-09 14:51:37 +00:00
|
|
|
SetupErrors: setupErrors,
|
2019-04-09 16:11:00 +00:00
|
|
|
Tasks: rcts,
|
|
|
|
StaticEnvironment: req.StaticEnvironment,
|
|
|
|
Environment: req.Environment,
|
|
|
|
Annotations: req.Annotations,
|
2019-07-03 13:16:37 +00:00
|
|
|
CacheGroup: req.CacheGroup,
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 14:46:04 +00:00
|
|
|
run := genRun(rc)
|
2019-05-03 21:59:21 +00:00
|
|
|
h.log.Debugf("created run: %s", util.Dump(run))
|
2019-02-21 14:54:50 +00:00
|
|
|
|
2019-03-04 15:11:18 +00:00
|
|
|
return &types.RunBundle{
|
|
|
|
Run: run,
|
|
|
|
Rc: rc,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func (h *ActionHandler) recreateRun(ctx context.Context, req *RunCreateRequest) (*types.RunBundle, error) {
|
2019-04-09 16:11:00 +00:00
|
|
|
// generate a new run sequence that will be the same for the run and runconfig
|
2019-05-03 21:59:21 +00:00
|
|
|
seq, err := sequence.IncSequence(ctx, h.e, common.EtcdRunSequenceKey)
|
2019-03-04 15:11:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id := seq.String()
|
|
|
|
|
2019-04-09 16:11:00 +00:00
|
|
|
// fetch the existing runconfig and run
|
2019-05-03 21:59:21 +00:00
|
|
|
h.log.Infof("creating run from existing run")
|
|
|
|
rc, err := store.OSTGetRunConfig(h.dm, req.RunID)
|
2019-03-04 15:11:18 +00:00
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("runconfig %q doesn't exist: %w", req.RunID, err))
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
run, err := store.GetRunEtcdOrOST(ctx, h.e, h.dm, req.RunID)
|
2019-03-04 15:11:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if run == nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("run %q doesn't exist: %w", req.RunID, err))
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 09:28:00 +00:00
|
|
|
h.log.Debugf("rc: %s", util.Dump(rc))
|
|
|
|
h.log.Debugf("run: %s", util.Dump(run))
|
2019-04-12 14:46:04 +00:00
|
|
|
|
2019-03-29 08:09:57 +00:00
|
|
|
if req.FromStart {
|
|
|
|
if canRestart, reason := run.CanRestartFromScratch(); !canRestart {
|
2019-04-08 16:04:42 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("run cannot be restarted: %s", reason))
|
2019-03-29 08:09:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if canRestart, reason := run.CanRestartFromFailedTasks(); !canRestart {
|
2019-04-08 16:04:42 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("run cannot be restarted: %s", reason))
|
2019-03-29 08:09:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 14:46:04 +00:00
|
|
|
rb := recreateRun(util.DefaultUUIDGenerator{}, run, rc, id, req)
|
|
|
|
|
2019-06-14 09:28:00 +00:00
|
|
|
h.log.Debugf("created rc from existing rc: %s", util.Dump(rb.Rc))
|
|
|
|
h.log.Debugf("created run from existing run: %s", util.Dump(rb.Run))
|
2019-04-12 14:46:04 +00:00
|
|
|
|
|
|
|
return rb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func recreateRun(uuid util.UUIDGenerator, run *types.Run, rc *types.RunConfig, newID string, req *RunCreateRequest) *types.RunBundle {
|
|
|
|
// update the run config ID
|
|
|
|
rc.ID = newID
|
|
|
|
// update the run config Environment
|
|
|
|
rc.Environment = req.Environment
|
|
|
|
|
2019-03-04 15:11:18 +00:00
|
|
|
// update the run ID
|
2019-04-12 14:46:04 +00:00
|
|
|
run.ID = newID
|
2019-03-04 15:11:18 +00:00
|
|
|
// reset run revision
|
|
|
|
run.Revision = 0
|
|
|
|
// reset phase/result/archived/stop
|
|
|
|
run.Phase = types.RunPhaseQueued
|
|
|
|
run.Result = types.RunResultUnknown
|
|
|
|
run.Archived = false
|
2019-03-08 09:02:37 +00:00
|
|
|
run.Stop = false
|
2019-04-12 14:46:04 +00:00
|
|
|
run.EnqueueTime = nil
|
|
|
|
run.StartTime = nil
|
|
|
|
run.EndTime = nil
|
2019-03-04 15:11:18 +00:00
|
|
|
|
|
|
|
// TODO(sgotti) handle reset tasks
|
|
|
|
// currently we only restart a run resetting al failed tasks
|
2019-04-12 14:46:04 +00:00
|
|
|
recreatedRCTasks := map[string]struct{}{}
|
2019-03-04 15:11:18 +00:00
|
|
|
|
2019-04-12 15:45:38 +00:00
|
|
|
for _, rt := range run.Tasks {
|
2019-03-04 15:11:18 +00:00
|
|
|
if req.FromStart || rt.Status != types.RunTaskStatusSuccess {
|
2019-04-12 14:46:04 +00:00
|
|
|
rct, ok := rc.Tasks[rt.ID]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("no runconfig task %q", rt.ID))
|
|
|
|
}
|
2019-03-04 15:11:18 +00:00
|
|
|
// change rct id
|
2019-04-12 14:46:04 +00:00
|
|
|
rct.ID = uuid.New(rct.Name).String()
|
2019-03-04 15:11:18 +00:00
|
|
|
|
2019-04-12 14:46:04 +00:00
|
|
|
// update runconfig with new tasks
|
2019-03-04 15:11:18 +00:00
|
|
|
delete(rc.Tasks, rt.ID)
|
|
|
|
rc.Tasks[rct.ID] = rct
|
2019-04-12 14:46:04 +00:00
|
|
|
|
|
|
|
// update other runconfig tasks depends to new task id
|
2019-03-04 15:11:18 +00:00
|
|
|
for _, t := range rc.Tasks {
|
2019-04-12 14:46:04 +00:00
|
|
|
if d, ok := t.Depends[rt.ID]; ok {
|
|
|
|
delete(t.Depends, rt.ID)
|
|
|
|
nd := &types.RunConfigTaskDepend{
|
|
|
|
TaskID: rct.ID,
|
|
|
|
Conditions: d.Conditions,
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
2019-04-12 14:46:04 +00:00
|
|
|
t.Depends[rct.ID] = nd
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 14:46:04 +00:00
|
|
|
recreatedRCTasks[rct.ID] = struct{}{}
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-12 14:46:04 +00:00
|
|
|
|
|
|
|
// also recreate all runconfig tasks that are childs of a previously recreated
|
|
|
|
// runconfig task
|
|
|
|
rcTasksToRecreate := map[string]struct{}{}
|
|
|
|
for _, rct := range rc.Tasks {
|
|
|
|
parents := runconfig.GetAllParents(rc.Tasks, rct)
|
|
|
|
for _, parent := range parents {
|
|
|
|
if _, ok := recreatedRCTasks[parent.ID]; ok {
|
|
|
|
rcTasksToRecreate[rct.ID] = struct{}{}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for rcTaskToRecreate := range rcTasksToRecreate {
|
|
|
|
rct := rc.Tasks[rcTaskToRecreate]
|
|
|
|
// change rct id
|
|
|
|
rct.ID = uuid.New(rct.Name).String()
|
|
|
|
|
|
|
|
// update runconfig with new tasks
|
|
|
|
delete(rc.Tasks, rcTaskToRecreate)
|
|
|
|
rc.Tasks[rct.ID] = rct
|
|
|
|
|
|
|
|
// update other runconfig tasks depends to new task id
|
|
|
|
for _, t := range rc.Tasks {
|
|
|
|
if d, ok := t.Depends[rcTaskToRecreate]; ok {
|
|
|
|
delete(t.Depends, rcTaskToRecreate)
|
|
|
|
nd := &types.RunConfigTaskDepend{
|
|
|
|
TaskID: rct.ID,
|
|
|
|
Conditions: d.Conditions,
|
|
|
|
}
|
|
|
|
t.Depends[rct.ID] = nd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update run
|
|
|
|
|
|
|
|
// remove deleted tasks from run config
|
|
|
|
tasksToDelete := []string{}
|
2019-04-12 15:45:38 +00:00
|
|
|
for _, rt := range run.Tasks {
|
2019-04-12 14:46:04 +00:00
|
|
|
if _, ok := rc.Tasks[rt.ID]; !ok {
|
|
|
|
tasksToDelete = append(tasksToDelete, rt.ID)
|
|
|
|
}
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
|
|
|
for _, rtID := range tasksToDelete {
|
2019-04-12 15:45:38 +00:00
|
|
|
delete(run.Tasks, rtID)
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
2019-04-12 14:46:04 +00:00
|
|
|
// create new tasks from runconfig
|
|
|
|
for _, rct := range rc.Tasks {
|
2019-04-12 15:45:38 +00:00
|
|
|
if _, ok := run.Tasks[rct.ID]; !ok {
|
2019-04-12 14:46:04 +00:00
|
|
|
nrt := genRunTask(rct)
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks[nrt.ID] = nrt
|
2019-04-12 14:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-04 15:11:18 +00:00
|
|
|
|
|
|
|
return &types.RunBundle{
|
|
|
|
Run: run,
|
|
|
|
Rc: rc,
|
2019-04-12 14:46:04 +00:00
|
|
|
}
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func (h *ActionHandler) saveRun(ctx context.Context, rb *types.RunBundle, runcgt *types.ChangeGroupsUpdateToken) error {
|
2019-03-04 15:11:18 +00:00
|
|
|
run := rb.Run
|
|
|
|
rc := rb.Rc
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
c, cgt, err := h.getRunCounter(run.Group)
|
2019-06-14 09:28:00 +00:00
|
|
|
h.log.Debugf("c: %d, cgt: %s", c, util.Dump(cgt))
|
2019-04-01 10:54:43 +00:00
|
|
|
if err != nil {
|
2019-02-21 14:54:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
c++
|
|
|
|
run.Counter = c
|
|
|
|
|
2019-04-12 14:46:04 +00:00
|
|
|
run.EnqueueTime = util.TimePtr(time.Now())
|
|
|
|
|
2019-04-26 14:00:03 +00:00
|
|
|
actions := []*datamanager.Action{}
|
2019-02-21 14:54:50 +00:00
|
|
|
|
|
|
|
// persist group counter
|
2019-04-27 13:16:48 +00:00
|
|
|
rca, err := store.OSTUpdateRunCounterAction(ctx, c, run.Group)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actions = append(actions, rca)
|
|
|
|
|
|
|
|
// persist run config
|
2019-04-27 13:16:48 +00:00
|
|
|
rca, err = store.OSTSaveRunConfigAction(rc)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actions = append(actions, rca)
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
if _, err = h.dm.WriteWal(ctx, actions, cgt); err != nil {
|
2019-02-21 14:54:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
runEvent, err := common.NewRunEvent(ctx, h.e, run.ID, run.Phase, run.Result)
|
2019-04-10 12:48:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-03 21:59:21 +00:00
|
|
|
if _, err := store.AtomicPutRun(ctx, h.e, run, runEvent, runcgt); err != nil {
|
2019-02-21 14:54:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-12 14:46:04 +00:00
|
|
|
func genRunTask(rct *types.RunConfigTask) *types.RunTask {
|
2019-02-21 14:54:50 +00:00
|
|
|
rt := &types.RunTask{
|
|
|
|
ID: rct.ID,
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
2019-03-07 17:01:34 +00:00
|
|
|
Skip: rct.Skip,
|
2019-02-21 14:54:50 +00:00
|
|
|
Steps: make([]*types.RunTaskStep, len(rct.Steps)),
|
|
|
|
WorkspaceArchives: []int{},
|
|
|
|
}
|
2019-03-07 17:01:34 +00:00
|
|
|
if rt.Skip {
|
|
|
|
rt.Status = types.RunTaskStatusSkipped
|
|
|
|
}
|
2019-03-13 14:48:35 +00:00
|
|
|
rt.SetupStep = types.RunTaskStep{
|
|
|
|
Phase: types.ExecutorTaskPhaseNotStarted,
|
|
|
|
LogPhase: types.RunTaskFetchPhaseNotStarted,
|
|
|
|
}
|
2019-02-21 14:54:50 +00:00
|
|
|
for i := range rt.Steps {
|
|
|
|
s := &types.RunTaskStep{
|
|
|
|
Phase: types.ExecutorTaskPhaseNotStarted,
|
|
|
|
LogPhase: types.RunTaskFetchPhaseNotStarted,
|
|
|
|
}
|
|
|
|
rt.Steps[i] = s
|
|
|
|
}
|
|
|
|
for i, ps := range rct.Steps {
|
|
|
|
switch ps.(type) {
|
|
|
|
case *types.SaveToWorkspaceStep:
|
|
|
|
rt.WorkspaceArchives = append(rt.WorkspaceArchives, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rt.WorkspaceArchivesPhase = make([]types.RunTaskFetchPhase, len(rt.WorkspaceArchives))
|
|
|
|
for i := range rt.WorkspaceArchivesPhase {
|
|
|
|
rt.WorkspaceArchivesPhase[i] = types.RunTaskFetchPhaseNotStarted
|
|
|
|
}
|
|
|
|
|
|
|
|
return rt
|
|
|
|
}
|
|
|
|
|
2019-04-12 14:46:04 +00:00
|
|
|
func genRun(rc *types.RunConfig) *types.Run {
|
2019-02-21 14:54:50 +00:00
|
|
|
r := &types.Run{
|
|
|
|
ID: rc.ID,
|
|
|
|
Name: rc.Name,
|
2019-04-09 16:11:00 +00:00
|
|
|
Group: rc.Group,
|
|
|
|
Annotations: rc.Annotations,
|
2019-02-21 14:54:50 +00:00
|
|
|
Phase: types.RunPhaseQueued,
|
|
|
|
Result: types.RunResultUnknown,
|
2019-04-12 15:45:38 +00:00
|
|
|
Tasks: make(map[string]*types.RunTask),
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 14:51:37 +00:00
|
|
|
if len(rc.SetupErrors) > 0 {
|
|
|
|
r.Phase = types.RunPhaseSetupError
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
for _, rct := range rc.Tasks {
|
2019-04-12 14:46:04 +00:00
|
|
|
rt := genRunTask(rct)
|
2019-04-12 15:45:38 +00:00
|
|
|
r.Tasks[rt.ID] = rt
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 16:11:00 +00:00
|
|
|
return r
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 13:19:29 +00:00
|
|
|
type RunTaskSetAnnotationsRequest struct {
|
|
|
|
RunID string
|
|
|
|
TaskID string
|
|
|
|
Annotations map[string]string
|
|
|
|
ChangeGroupsUpdateToken string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ActionHandler) RunTaskSetAnnotations(ctx context.Context, req *RunTaskSetAnnotationsRequest) error {
|
|
|
|
cgt, err := types.UnmarshalChangeGroupsUpdateToken(req.ChangeGroupsUpdateToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r, _, err := store.GetRun(ctx, h.e, req.RunID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
task, ok := r.Tasks[req.TaskID]
|
|
|
|
if !ok {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("run %q doesn't have task %q", r.ID, req.TaskID))
|
|
|
|
}
|
|
|
|
|
|
|
|
task.Annotations = req.Annotations
|
|
|
|
|
|
|
|
_, err = store.AtomicPutRun(ctx, h.e, r, nil, cgt)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:54:50 +00:00
|
|
|
type RunTaskApproveRequest struct {
|
|
|
|
RunID string
|
|
|
|
TaskID string
|
|
|
|
ChangeGroupsUpdateToken string
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func (h *ActionHandler) ApproveRunTask(ctx context.Context, req *RunTaskApproveRequest) error {
|
2019-02-21 14:54:50 +00:00
|
|
|
cgt, err := types.UnmarshalChangeGroupsUpdateToken(req.ChangeGroupsUpdateToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
r, _, err := store.GetRun(ctx, h.e, req.RunID)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:45:38 +00:00
|
|
|
task, ok := r.Tasks[req.TaskID]
|
2019-02-21 14:54:50 +00:00
|
|
|
if !ok {
|
2019-04-08 16:04:42 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("run %q doesn't have task %q", r.ID, req.TaskID))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !task.WaitingApproval {
|
2019-04-08 16:04:42 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("run %q, task %q is not in waiting approval state", r.ID, req.TaskID))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:29:57 +00:00
|
|
|
if task.Approved {
|
2019-04-08 16:04:42 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("run %q, task %q is already approved", r.ID, req.TaskID))
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:29:57 +00:00
|
|
|
task.WaitingApproval = false
|
2019-02-21 14:54:50 +00:00
|
|
|
task.Approved = true
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
_, err = store.AtomicPutRun(ctx, h.e, r, nil, cgt)
|
2019-02-21 14:54:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func (h *ActionHandler) DeleteExecutor(ctx context.Context, executorID string) error {
|
2019-02-21 14:54:50 +00:00
|
|
|
// mark all executor tasks as failed
|
2019-05-03 21:59:21 +00:00
|
|
|
ets, err := store.GetExecutorTasks(ctx, h.e, executorID)
|
2019-02-21 14:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, et := range ets {
|
|
|
|
et.Status.Phase = types.ExecutorTaskPhaseFailed
|
|
|
|
et.FailError = "executor deleted"
|
2019-05-03 21:59:21 +00:00
|
|
|
if _, err := store.AtomicPutExecutorTask(ctx, h.e, et); err != nil {
|
2019-02-21 14:54:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the executor
|
2019-05-03 21:59:21 +00:00
|
|
|
if err := store.DeleteExecutor(ctx, h.e, executorID); err != nil {
|
2019-02-21 14:54:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-01 10:54:43 +00:00
|
|
|
|
2019-05-03 21:59:21 +00:00
|
|
|
func (h *ActionHandler) getRunCounter(group string) (uint64, *datamanager.ChangeGroupsUpdateToken, error) {
|
2019-04-01 10:54:43 +00:00
|
|
|
// use the first group dir after the root
|
|
|
|
pl := util.PathList(group)
|
|
|
|
if len(pl) < 2 {
|
|
|
|
return 0, nil, errors.Errorf("cannot determine group counter name, wrong group path %q", group)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c uint64
|
2019-04-26 14:00:03 +00:00
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
2019-05-03 21:59:21 +00:00
|
|
|
err := h.readDB.Do(func(tx *db.Tx) error {
|
2019-04-01 10:54:43 +00:00
|
|
|
var err error
|
2019-05-03 21:59:21 +00:00
|
|
|
c, err = h.readDB.GetRunCounterOST(tx, pl[1])
|
2019-04-01 10:54:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-03 21:59:21 +00:00
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokensOST(tx, []string{"counter-" + pl[1]})
|
2019-04-01 10:54:43 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, cgt, nil
|
|
|
|
}
|