grim/convey

Apparently these quotes aren't needed anymore.. not sure why, need to check out compatibility
// 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"
"github.com/aphistic/gomol"
"bitbucket.org/rw_grim/convey/docker"
"bitbucket.org/rw_grim/convey/environment"
"bitbucket.org/rw_grim/convey/logging"
"bitbucket.org/rw_grim/convey/stages"
"bitbucket.org/rw_grim/convey/state"
"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"`
}
func (p *Plan) setup(logger *gomol.LogAdapter, st *state.State) error {
// create the network
if lErr := logger.Info("creating network..."); lErr != nil {
fmt.Printf("error reporting info: %s\n", lErr)
}
network, err := docker.NewNetwork(st)
if err != nil {
if lErr := logger.Fatal("failed to create network"); lErr != nil {
fmt.Printf("error reporting fatal: %s\n", lErr)
}
return err
}
st.Network = network
if lErr := logger.Infof("created network %s", st.Network.Name()); lErr != nil {
fmt.Printf("error reporting info: %s\n", lErr)
}
st.Cleanup(func(logger *gomol.LogAdapter) {
if lErr := logger.Infof("removing network %s", st.Network.Name()); lErr != nil {
fmt.Printf("error reporting info: %s\n", lErr)
}
if netErr := st.Network.Destroy(); netErr != nil {
if lErr := logger.Fatalf("failed to remove network %s: %s", st.Network.Name(), netErr); lErr != nil {
fmt.Printf("error reporting fatal: %s\n", lErr)
}
} else {
if lErr := logger.Infof("removed network %s", st.Network.Name()); lErr != nil {
fmt.Printf("error reporting info: %s\n", lErr)
}
}
})
// create the workspace
if lErr := logger.Info("creating workspace..."); lErr != nil {
fmt.Printf("error reporting info: %s\n", lErr)
}
ws, err := docker.NewWorkspace(st)
if err != nil {
if lErr := logger.Fatal("failed to create workspace"); lErr != nil {
fmt.Printf("error reporting fatal: %s\n", lErr)
}
return err
}
st.Workspace = ws
if lErr := logger.Infof("created workspace %s", st.Workspace.Name()); lErr != nil {
fmt.Printf("error reporting info: %s\n", lErr)
}
return nil
}
// Execute runs the plan.
func (p *Plan) Execute(path string, tasks map[string]tasks.Task, env []string, st *state.State) error {
logger := logging.NewAdapter(path)
planEnv := environment.Merge(env, p.Environment)
if err := p.setup(logger, st); err != nil {
return err
}
// 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) {
if lErr := stageLogger.Info("stage starting"); lErr != nil {
fmt.Printf("error reporting info: %s\n", lErr)
}
err := stage.Execute(absStageName, stageLogger, tasks, planEnv, st)
if err != nil {
if lErr := stageLogger.Fatal("stage failed"); lErr != nil {
fmt.Printf("error reporting fatal: %s\n", lErr)
}
planErr = err
continue
}
elapsed := time.Since(start)
st.PlanTimeout -= elapsed
if lErr := logger.Debugf("remaining plantime: %s", st.PlanTimeout); lErr != nil {
fmt.Printf("error reporting debug: %s\n", lErr)
}
if lErr := stageLogger.Infof("stage finished [%s]", elapsed.Round(10*time.Millisecond)); lErr != nil {
fmt.Printf("error reporting info: %s\n", lErr)
}
}
}
return planErr
}
// Valid validates the plan.
func (p *Plan) Valid() error {
if len(p.Stages) == 0 {
return ErrNoStages
}
return nil
}