grim/convey

Add a new subtask task

2020-03-02, Gary Kramlich
469b76e1bd24
Add a new subtask task
// 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"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"hg.sr.ht/~grim/convey/environment"
"hg.sr.ht/~grim/convey/exec"
"hg.sr.ht/~grim/convey/runtime"
"hg.sr.ht/~grim/convey/tasks"
"hg.sr.ht/~grim/convey/yaml"
)
// CRUDCommand represents a call to a `kubectl` command to manage deployments.
type CRUDCommand struct {
Context string `yaml:"context"`
Files yaml.StringOrSlice `yaml:"files"`
Namespace string `yaml:"namespace"`
Selector yaml.StringOrSlice `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 *exec.Generator, logger *log.Entry, env *environment.Environment, rt *runtime.Runtime) error {
// update the files we're going to run by joining them with our scratch directory
for _, file := range c.Files {
realFile := env.Map(file)
cmd.Append("-f", filepath.Join(scratchDir, realFile))
}
// now create an export task to get our files out of the workspace
export := &tasks.Export{
Files: c.Files,
Path: scratchDir,
}
// make sure the export task is valid
err := export.Valid()
if err != nil {
return err
}
// run the export
return export.Execute(name, logger, env, rt)
}
func templateEnvironmentFile(filename string, env *environment.Environment) error {
// read the file
raw, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
// replace the environment variables in it
data := env.Map(string(raw))
// write the templated data back out
return ioutil.WriteFile(filename, []byte(data), 0700)
}
func templateEnvironmentDirectory(directory string, env *environment.Environment) error {
files, err := ioutil.ReadDir(directory)
if err != nil {
return err
}
for _, file := range files {
absFile := filepath.Join(directory, file.Name())
err = templateEnvironmentFile(absFile, env)
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 *exec.Generator, logger *log.Entry, env *environment.Environment, rt *runtime.Runtime) error {
// run the normal export which we will update in place.
err := c.templateNone(name, scratchDir, cmd, logger, env, rt)
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 := env.Map(file)
// figure out the full path to it
absFile := filepath.Join(scratchDir, resolvedFile)
// check if we're looking at a file or a directory
entry, err := os.Stat(absFile)
if err != nil {
return err
}
if entry.IsDir() {
err = templateEnvironmentDirectory(absFile, env)
} else {
err = templateEnvironmentFile(absFile, env)
}
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 *log.Entry, env *environment.Environment, rt *runtime.Runtime) error {
fullEnv := env.Copy().Merge(rt.Environment)
// if we have a context use it
if c.Context != "" {
err := useContext(name, c.Context, logger, env, rt.State.PlanTimeout)
if err != nil {
return err
}
}
// now build the apply command line
cmd := exec.NewGenerator("kubectl", action)
namespace := fullEnv.Map(c.Namespace)
if namespace != "" {
cmd.Append("-n", namespace)
}
selectors, err := fullEnv.MapSlice(c.Selector)
if err != nil {
return err
}
for _, selector := range selectors {
if selector != "" {
cmd.Append("-l", selector)
}
}
// create our scratch directory
path, err := rt.State.Workspace.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, rt)
case "env":
fallthrough
case "environment":
err = c.templateEnvironment(name, path, cmd, logger, fullEnv, rt)
}
// if there was an error with templating bail
if err != nil {
return err
}
// finally run the command
return exec.Run(name, cmd.Command(), rt.State.PlanTimeout)
}