grim/convey

Bump the version for release
v0.14.0-alpha3
2018-02-20, Gary Kramlich
166a6d1979fa
Bump the version for release
// 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 (
"io/ioutil"
"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"`
Engine string `yaml:"engine"`
}
// templateNone just copies files from the workspace into the scratch directory.
func (c *CRUDCommand) templateNone(name, scratchDir string, cmd *command.Generator, logger *gomol.LogAdapter, env []string, st *state.State) error {
// 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, env)
if err != nil {
return err
}
cmd.Append("-f", filepath.Join(scratchDir, realFile))
}
// now create an export task to get our files out of the workspace
export := &docker.Export{
Files: c.Files,
Path: scratchDir,
}
// 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
}
return nil
}
// templateEnvironment will copy files from the workspace into the scratch
// directory and replace environment variables with those that convey knows
// about.
func (c *CRUDCommand) templateEnvironment(name, scratchDir string, cmd *command.Generator, logger *gomol.LogAdapter, env []string, st *state.State) error {
// run the normal export which we will update in place.
err := c.templateNone(name, scratchDir, cmd, logger, env, st)
if err != nil {
return err
}
// now iterate the files and replace all their environment variables with those that we have
for _, file := range c.Files {
// resolve the filename if it's templated
resolvedFile, err := environment.Mapper(file, env)
if err != nil {
return err
}
// figure out the full path to it
absFile := filepath.Join(scratchDir, resolvedFile)
// read the file
raw, err := ioutil.ReadFile(absFile)
if err != nil {
return err
}
// replace the environment variables in it
data, err := environment.Mapper(string(raw), env)
if err != nil {
return err
}
// write the templated data back out
err = ioutil.WriteFile(absFile, []byte(data), 0700)
if err != nil {
return err
}
}
return nil
}
// 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
}
// run our files through the template engine
switch c.Engine {
case "":
fallthrough
case "none":
err = c.templateNone(name, path, cmd, logger, fullEnv, st)
case "env":
fallthrough
case "environment":
err = c.templateEnvironment(name, path, cmd, logger, fullEnv, st)
}
// if there was an error with templating bail
if err != nil {
return err
}
// finally run the command
return command.Run(name, cmd.Command(), st.PlanTimeout)
}