grim/convey

expose the Run* variables so they can be used in testing and stuff
// Convey
// Copyright 2016-2018 Gary Kramlich <grim@reaperworld.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package plans contains the plans structure.
package plans
import (
"errors"
"fmt"
"time"
log "github.com/sirupsen/logrus"
"bitbucket.org/rw_grim/convey/docker"
"bitbucket.org/rw_grim/convey/environment"
"bitbucket.org/rw_grim/convey/logging"
"bitbucket.org/rw_grim/convey/runtime"
"bitbucket.org/rw_grim/convey/stages"
"bitbucket.org/rw_grim/convey/tasks"
"bitbucket.org/rw_grim/convey/yaml"
)
var (
// ErrNoStages is an error that can be returned from Plan.Valid.
ErrNoStages = errors.New("no stages found")
)
// Plan is the representation of a Plan.
type Plan struct {
Environment yaml.StringOrSlice `yaml:"environment"`
Stages []stages.Stage `yaml:"stages"`
Merge bool `yaml:"merge"`
logger *log.Entry
}
func (p *Plan) haveRunTask(taskMap map[string]tasks.Task) bool {
for _, stage := range p.Stages {
for _, taskName := range stage.Tasks {
task := taskMap[taskName]
if _, isRun := task.(*docker.Run); isRun {
return true
}
}
}
return false
}
// Execute runs the plan.
func (p *Plan) Execute(path string, tasks map[string]tasks.Task, env *environment.Environment, rt *runtime.Runtime) error {
planEnv := env.Copy().Merge(rt.Environment).MergeSlice(p.Environment)
p.logger = logging.NewAdapter(path)
// set a flag on whether or not we've failed
var planErr error
for _, stage := range p.Stages {
start := time.Now()
absStageName := fmt.Sprintf("%s/%s", path, stage.Name)
stageLogger := logging.NewAdapter(absStageName)
if stage.ShouldRun(planErr) {
stageLogger.Info("stage starting")
err := stage.Execute(absStageName, stageLogger, tasks, planEnv, rt)
if err != nil {
stageLogger.Fatal("stage failed")
planErr = err
continue
}
elapsed := time.Since(start)
rt.State.PlanTimeout -= elapsed
p.logger.Debugf("remaining plantime: %s", rt.State.PlanTimeout)
stageLogger.Infof("stage finished [%s]", elapsed.Round(10*time.Millisecond))
}
}
return planErr
}
// Valid validates the plan.
func (p *Plan) Valid() error {
if len(p.Stages) == 0 {
return ErrNoStages
}
return nil
}