grim/convey

merging

2018-02-19, Gary Kramlich
8c7af5e9a4cc
merging
// 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 kubectl
import (
"path/filepath"
"github.com/aphistic/gomol"
"bitbucket.org/rw_grim/convey/command"
"bitbucket.org/rw_grim/convey/docker"
"bitbucket.org/rw_grim/convey/environment"
"bitbucket.org/rw_grim/convey/state"
"bitbucket.org/rw_grim/convey/yaml"
)
// CRUDCommand represents a call to a `kubectl` command to manage deployments.
type CRUDCommand struct {
Context string `yaml:"context"`
Namespace string `yaml:"namespace"`
Files yaml.StringOrSlice `yaml:"files"`
Selector string `yaml:"selector"`
}
// Execute runs the given `kubectl` command with the given arguments.
func (c *CRUDCommand) Execute(name, action string, logger *gomol.LogAdapter, env []string, st *state.State) error {
fullEnv := environment.Merge(env, st.GetEnv())
// if we have a context use it
if c.Context != "" {
err := useContext(name, c.Context, logger, env, st.PlanTimeout)
if err != nil {
return err
}
}
// now build the apply command line
cmd := command.NewGenerator("kubectl", action)
if c.Namespace != "" {
namespace, err := environment.Mapper(c.Namespace, fullEnv)
if err != nil {
return err
}
cmd.Append("-n", namespace)
}
if c.Selector != "" {
selector, err := environment.Mapper(c.Selector, fullEnv)
if err != nil {
return err
}
cmd.Append("-l", selector)
}
// create our scratch directory
path, err := st.TaskDirectory(name)
if err != nil {
return err
}
// update the files we're going to run by joining them with our scratch directory
for _, file := range c.Files {
realFile, err := environment.Mapper(file, fullEnv)
if err != nil {
return err
}
cmd.Append("-f", filepath.Join(path, realFile))
}
// now create an export task to get our files out of the workspace
export := &docker.Export{
Files: c.Files,
Path: path,
}
// make sure the export task is valid
err = export.Valid()
if err != nil {
return err
}
// run the export
err = export.Execute(name, logger, env, st)
if err != nil {
return err
}
// finally run the command
return command.Run(name, cmd.Command(), st.PlanTimeout)
}