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 command
|
|
|
|
|
|
|
|
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-04-26 14:00:03 +00:00
|
|
|
"github.com/sorintlab/agola/internal/datamanager"
|
2019-04-01 10:54:43 +00:00
|
|
|
"github.com/sorintlab/agola/internal/db"
|
2019-02-21 14:54:50 +00:00
|
|
|
"github.com/sorintlab/agola/internal/etcd"
|
|
|
|
"github.com/sorintlab/agola/internal/objectstorage"
|
|
|
|
"github.com/sorintlab/agola/internal/runconfig"
|
|
|
|
"github.com/sorintlab/agola/internal/sequence"
|
|
|
|
"github.com/sorintlab/agola/internal/services/runservice/scheduler/common"
|
2019-04-01 10:54:43 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/runservice/scheduler/readdb"
|
2019-02-21 14:54:50 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/runservice/scheduler/store"
|
|
|
|
"github.com/sorintlab/agola/internal/services/runservice/types"
|
|
|
|
"github.com/sorintlab/agola/internal/util"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommandHandler 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-04-26 14:00:03 +00:00
|
|
|
func NewCommandHandler(logger *zap.Logger, e *etcd.Store, readDB *readdb.ReadDB, ost *objectstorage.ObjStorage, dm *datamanager.DataManager) *CommandHandler {
|
2019-02-21 14:54:50 +00:00
|
|
|
return &CommandHandler{
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommandHandler) ChangeRunPhase(ctx context.Context, req *RunChangePhaseRequest) error {
|
|
|
|
cgt, err := types.UnmarshalChangeGroupsUpdateToken(req.ChangeGroupsUpdateToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r, _, err := store.GetRun(ctx, s.e, req.RunID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Phase {
|
|
|
|
case types.RunPhaseRunning:
|
|
|
|
if r.Phase != types.RunPhaseQueued {
|
|
|
|
return errors.Errorf("run %s is not queued but in %q phase", r.ID, r.Phase)
|
|
|
|
}
|
|
|
|
r.ChangePhase(types.RunPhaseRunning)
|
2019-03-08 09:02:37 +00:00
|
|
|
case types.RunPhaseFinished:
|
|
|
|
if r.Phase != types.RunPhaseRunning {
|
|
|
|
return errors.Errorf("run %s is not running but in %q phase", r.ID, r.Phase)
|
|
|
|
}
|
|
|
|
r.Stop = true
|
|
|
|
}
|
|
|
|
|
2019-04-10 12:48:47 +00:00
|
|
|
_, err = store.AtomicPutRun(ctx, s.e, r, nil, cgt)
|
2019-03-08 09:02:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunStopRequest struct {
|
|
|
|
RunID string
|
|
|
|
ChangeGroupsUpdateToken string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommandHandler) StopRun(ctx context.Context, req *RunStopRequest) error {
|
|
|
|
cgt, err := types.UnmarshalChangeGroupsUpdateToken(req.ChangeGroupsUpdateToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r, _, err := store.GetRun(ctx, s.e, req.RunID)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if !r.Result.IsSet() {
|
|
|
|
// stop only if the result is not setted yet
|
|
|
|
r.Stop = true
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 12:48:47 +00:00
|
|
|
_, err = store.AtomicPutRun(ctx, s.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
|
|
|
|
|
|
|
|
// 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-03-04 15:11:18 +00:00
|
|
|
func (s *CommandHandler) 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 == "" {
|
|
|
|
rb, err = s.newRun(ctx, req)
|
|
|
|
} else {
|
|
|
|
rb, err = s.recreateRun(ctx, req)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 15:11:18 +00:00
|
|
|
return rb, s.saveRun(ctx, rb, runcgt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommandHandler) 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-02-21 14:54:50 +00:00
|
|
|
seq, err := sequence.IncSequence(ctx, s.e, common.EtcdRunSequenceKey)
|
|
|
|
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-04-09 14:51:37 +00:00
|
|
|
s.log.Errorf("check run config tasks failed: %+v", err)
|
|
|
|
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 {
|
|
|
|
s.log.Errorf("gen tasks leveles failed: %+v", err)
|
|
|
|
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-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 14:46:04 +00:00
|
|
|
run := genRun(rc)
|
2019-02-21 14:54:50 +00:00
|
|
|
s.log.Debugf("created run: %s", util.Dump(run))
|
|
|
|
|
2019-03-04 15:11:18 +00:00
|
|
|
return &types.RunBundle{
|
|
|
|
Run: run,
|
|
|
|
Rc: rc,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommandHandler) 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-03-04 15:11:18 +00:00
|
|
|
seq, err := sequence.IncSequence(ctx, s.e, common.EtcdRunSequenceKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id := seq.String()
|
|
|
|
|
2019-04-09 16:11:00 +00:00
|
|
|
// fetch the existing runconfig and run
|
2019-03-04 15:11:18 +00:00
|
|
|
s.log.Infof("creating run from existing run")
|
2019-04-26 14:00:03 +00:00
|
|
|
rc, err := store.OSTGetRunConfig(s.dm, req.RunID)
|
2019-03-04 15:11:18 +00:00
|
|
|
if err != nil {
|
2019-04-08 16:04:42 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Wrapf(err, "runconfig %q doens't exist", req.RunID))
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 14:00:03 +00:00
|
|
|
run, err := store.GetRunEtcdOrOST(ctx, s.e, s.dm, req.RunID)
|
2019-03-04 15:11:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if run == nil {
|
2019-04-08 16:04:42 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Wrapf(err, "run %q doens't exist", req.RunID))
|
2019-03-04 15:11:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 14:46:04 +00:00
|
|
|
s.log.Infof("rc: %s", util.Dump(rc))
|
|
|
|
s.log.Infof("run: %s", util.Dump(run))
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
s.log.Infof("created rc from existing rc: %s", util.Dump(rb.Rc))
|
|
|
|
s.log.Infof("created run from existing run: %s", util.Dump(rb.Run))
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommandHandler) saveRun(ctx context.Context, rb *types.RunBundle, runcgt *types.ChangeGroupsUpdateToken) error {
|
|
|
|
run := rb.Run
|
|
|
|
rc := rb.Rc
|
|
|
|
|
2019-04-01 10:54:43 +00:00
|
|
|
c, cgt, err := s.getRunCounter(run.Group)
|
2019-02-21 14:54:50 +00:00
|
|
|
s.log.Infof("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-04-26 14:00:03 +00:00
|
|
|
if _, err = s.dm.WriteWal(ctx, actions, cgt); err != nil {
|
2019-02-21 14:54:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-10 12:48:47 +00:00
|
|
|
runEvent, err := common.NewRunEvent(ctx, s.e, run.ID, run.Phase, run.Result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := store.AtomicPutRun(ctx, s.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
|
|
|
}
|
|
|
|
|
|
|
|
type RunTaskApproveRequest struct {
|
|
|
|
RunID string
|
|
|
|
TaskID string
|
|
|
|
ApprovalAnnotations map[string]string
|
|
|
|
ChangeGroupsUpdateToken string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommandHandler) ApproveRunTask(ctx context.Context, req *RunTaskApproveRequest) error {
|
|
|
|
cgt, err := types.UnmarshalChangeGroupsUpdateToken(req.ChangeGroupsUpdateToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r, _, err := store.GetRun(ctx, s.e, req.RunID)
|
|
|
|
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
|
|
|
|
task.ApprovalAnnotations = req.ApprovalAnnotations
|
|
|
|
|
2019-04-10 12:48:47 +00:00
|
|
|
_, err = store.AtomicPutRun(ctx, s.e, r, nil, cgt)
|
2019-02-21 14:54:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommandHandler) DeleteExecutor(ctx context.Context, executorID string) error {
|
|
|
|
// mark all executor tasks as failed
|
|
|
|
ets, err := store.GetExecutorTasks(ctx, s.e, executorID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, et := range ets {
|
|
|
|
et.Status.Phase = types.ExecutorTaskPhaseFailed
|
|
|
|
et.FailError = "executor deleted"
|
|
|
|
if _, err := store.AtomicPutExecutorTask(ctx, s.e, et); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the executor
|
|
|
|
if err := store.DeleteExecutor(ctx, s.e, executorID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-01 10:54:43 +00:00
|
|
|
|
2019-04-26 14:00:03 +00:00
|
|
|
func (s *CommandHandler) 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-04-01 10:54:43 +00:00
|
|
|
err := s.readDB.Do(func(tx *db.Tx) error {
|
|
|
|
var err error
|
2019-04-27 13:16:48 +00:00
|
|
|
c, err = s.readDB.GetRunCounterOST(tx, pl[1])
|
2019-04-01 10:54:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-27 13:16:48 +00:00
|
|
|
cgt, err = s.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
|
|
|
|
}
|