2018-02-27 15:50:42 -05:00
|
|
|
package shell_local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
configHelper "github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
|
|
|
// ** DEPRECATED: USE INLINE INSTEAD **
|
|
|
|
// ** Only Present for backwards compatibiltiy **
|
|
|
|
// Command is the command to execute
|
|
|
|
Command string
|
|
|
|
|
|
|
|
// An inline script to execute. Multiple strings are all executed
|
|
|
|
// in the context of a single shell.
|
|
|
|
Inline []string
|
|
|
|
|
|
|
|
// The shebang value used when running inline scripts.
|
|
|
|
InlineShebang string `mapstructure:"inline_shebang"`
|
|
|
|
|
|
|
|
// The local path of the shell script to upload and execute.
|
|
|
|
Script string
|
|
|
|
|
|
|
|
// An array of multiple scripts to run.
|
|
|
|
Scripts []string
|
|
|
|
|
|
|
|
// An array of environment variables that will be injected before
|
|
|
|
// your command(s) are executed.
|
|
|
|
Vars []string `mapstructure:"environment_vars"`
|
|
|
|
// End dedupe with postprocessor
|
|
|
|
|
|
|
|
// The command used to execute the script. The '{{ .Path }}' variable
|
|
|
|
// should be used to specify where the script goes, {{ .Vars }}
|
|
|
|
// can be used to inject the environment_vars into the environment.
|
|
|
|
ExecuteCommand []string `mapstructure:"execute_command"`
|
|
|
|
|
|
|
|
Ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func Decode(config *Config, raws ...interface{}) error {
|
|
|
|
err := configHelper.Decode(&config, &configHelper.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &config.Ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"execute_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2018-02-28 17:35:42 -05:00
|
|
|
return fmt.Errorf("Error decoding config: %s, config is %#v, and raws is %#v", err, config, raws)
|
2018-02-27 15:50:42 -05:00
|
|
|
}
|
|
|
|
|
2018-02-28 17:35:42 -05:00
|
|
|
return nil
|
2018-02-27 15:50:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func Validate(config *Config) error {
|
|
|
|
var errs *packer.MultiError
|
|
|
|
|
2018-03-02 15:32:34 -05:00
|
|
|
// Do not treat these defaults as a source of truth; the shell-local
|
|
|
|
// provisioner sets these defaults before Validate is called. Eventually
|
|
|
|
// we will have to bring the provisioner and post-processor defaults in
|
|
|
|
// line with one another, but for now the following may or may not be
|
|
|
|
// applied depending on where Validate is being called from.
|
2018-02-27 15:50:42 -05:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if len(config.ExecuteCommand) == 0 {
|
2018-02-28 17:43:58 -05:00
|
|
|
config.ExecuteCommand = []string{
|
|
|
|
"cmd",
|
|
|
|
"/C",
|
|
|
|
"{{.Vars}}",
|
|
|
|
"{{.Script}}",
|
|
|
|
}
|
2018-02-28 18:19:28 -05:00
|
|
|
}
|
2018-02-27 15:50:42 -05:00
|
|
|
} else {
|
|
|
|
if config.InlineShebang == "" {
|
|
|
|
config.InlineShebang = "/bin/sh -e"
|
|
|
|
}
|
|
|
|
if len(config.ExecuteCommand) == 0 {
|
2018-02-28 17:43:58 -05:00
|
|
|
config.ExecuteCommand = []string{
|
|
|
|
"/bin/sh",
|
|
|
|
"-c",
|
2018-03-02 15:32:34 -05:00
|
|
|
"{{.Vars}} {{.Script}}",
|
2018-02-28 17:43:58 -05:00
|
|
|
}
|
2018-02-27 15:50:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up input
|
|
|
|
if config.Inline != nil && len(config.Inline) == 0 {
|
2018-02-28 12:45:29 -05:00
|
|
|
config.Inline = make([]string, 0)
|
2018-02-27 15:50:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Scripts == nil {
|
|
|
|
config.Scripts = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Vars == nil {
|
|
|
|
config.Vars = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the user has given us a command to run
|
2018-02-28 17:35:42 -05:00
|
|
|
if config.Command == "" && len(config.Inline) == 0 &&
|
2018-02-27 15:50:42 -05:00
|
|
|
len(config.Scripts) == 0 && config.Script == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("Command, Inline, Script and Scripts options cannot all be empty."))
|
|
|
|
}
|
|
|
|
|
2018-02-28 18:19:28 -05:00
|
|
|
// Check that user hasn't given us too many commands to run
|
|
|
|
tooManyOptionsErr := errors.New("You may only specify one of the " +
|
|
|
|
"following options: Command, Inline, Script or Scripts. Please" +
|
|
|
|
" consolidate these options in your config.")
|
2018-02-27 15:50:42 -05:00
|
|
|
|
2018-02-28 18:19:28 -05:00
|
|
|
if config.Command != "" {
|
|
|
|
if len(config.Inline) != 0 || len(config.Scripts) != 0 || config.Script != "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, tooManyOptionsErr)
|
|
|
|
} else {
|
|
|
|
config.Inline = []string{config.Command}
|
|
|
|
}
|
2018-02-27 15:50:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Script != "" {
|
2018-02-28 18:19:28 -05:00
|
|
|
if len(config.Scripts) > 0 || len(config.Inline) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, tooManyOptionsErr)
|
|
|
|
} else {
|
|
|
|
config.Scripts = []string{config.Script}
|
|
|
|
}
|
2018-02-27 15:50:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.Scripts) > 0 && config.Inline != nil {
|
2018-02-28 18:19:28 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, tooManyOptionsErr)
|
2018-02-27 15:50:42 -05:00
|
|
|
}
|
|
|
|
|
2018-02-28 18:19:28 -05:00
|
|
|
// Check that all scripts we need to run exist locally
|
2018-02-27 15:50:42 -05:00
|
|
|
for _, path := range config.Scripts {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Bad script '%s': %s", path, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a check for bad environment variables, such as '=foo', 'foobar'
|
|
|
|
for _, kv := range config.Vars {
|
|
|
|
vs := strings.SplitN(kv, "=", 2)
|
|
|
|
if len(vs) != 2 || vs[0] == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Environment variable not in format 'key=value': %s", kv))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|