2018-02-27 15:50:42 -05:00
|
|
|
package shell_local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-03-08 19:42:17 -05:00
|
|
|
"path/filepath"
|
2018-02-27 15:50:42 -05:00
|
|
|
"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"`
|
|
|
|
|
2018-10-18 16:10:04 -04:00
|
|
|
// An array of multiple Runtime OSs to run on.
|
2018-10-19 01:46:50 -04:00
|
|
|
OnlyOn []string `mapstructure:"only_on"`
|
2018-10-18 16:10:04 -04:00
|
|
|
|
2018-04-04 14:07:10 -04:00
|
|
|
// The file extension to use for the file generated from the inline commands
|
|
|
|
TempfileExtension string `mapstructure:"tempfile_extension"`
|
|
|
|
|
2018-02-27 15:50:42 -05:00
|
|
|
// 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"`
|
2018-03-12 14:25:39 -04:00
|
|
|
|
2018-04-04 14:07:10 -04:00
|
|
|
EnvVarFormat string `mapstructure:"env_var_format"`
|
2018-02-27 15:50:42 -05:00
|
|
|
// 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"`
|
|
|
|
|
2018-03-08 19:42:17 -05:00
|
|
|
UseLinuxPathing bool `mapstructure:"use_linux_pathing"`
|
|
|
|
|
2018-02-27 15:50:42 -05:00
|
|
|
Ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func Decode(config *Config, raws ...interface{}) error {
|
2018-05-08 13:35:09 -04:00
|
|
|
//Create passthrough for winrm password so we can fill it in once we know it
|
|
|
|
config.Ctx.Data = &EnvVarsTemplate{
|
|
|
|
WinRMPassword: `{{.WinRMPassword}}`,
|
|
|
|
}
|
|
|
|
|
2018-02-27 15:50:42 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if len(config.ExecuteCommand) == 0 {
|
2018-02-28 17:43:58 -05:00
|
|
|
config.ExecuteCommand = []string{
|
|
|
|
"cmd",
|
2018-04-04 14:07:10 -04:00
|
|
|
"/V",
|
2018-02-28 17:43:58 -05:00
|
|
|
"/C",
|
|
|
|
"{{.Vars}}",
|
2018-04-04 14:07:10 -04:00
|
|
|
"call",
|
2018-02-28 17:43:58 -05:00
|
|
|
"{{.Script}}",
|
|
|
|
}
|
2018-02-28 18:19:28 -05:00
|
|
|
}
|
2018-08-24 12:44:50 -04:00
|
|
|
if len(config.TempfileExtension) == 0 {
|
|
|
|
config.TempfileExtension = ".cmd"
|
|
|
|
}
|
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-04-04 14:07:10 -04: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))
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 01:46:50 -04:00
|
|
|
|
|
|
|
// Check for properly formatted go os types
|
2018-10-22 17:34:50 -04:00
|
|
|
supportedSyslist := []string{"darwin", "freebsd", "linux", "openbsd", "solaris", "windows"}
|
2018-10-19 01:46:50 -04:00
|
|
|
if len(config.OnlyOn) > 0 {
|
|
|
|
for _, provided_os := range config.OnlyOn {
|
2018-10-19 01:52:20 -04:00
|
|
|
supported_os := false
|
2018-10-22 17:34:50 -04:00
|
|
|
for _, go_os := range supportedSyslist {
|
2018-10-19 01:46:50 -04:00
|
|
|
if provided_os == go_os {
|
|
|
|
supported_os = true
|
|
|
|
break
|
|
|
|
}
|
2018-10-19 01:52:20 -04:00
|
|
|
}
|
|
|
|
if supported_os != true {
|
2018-10-19 14:48:30 -04:00
|
|
|
return fmt.Errorf("Invalid OS specified in only_on: '%s'\n"+
|
2018-10-22 17:34:50 -04:00
|
|
|
"Supported OS names: %s", provided_os, strings.Join(supportedSyslist, ", "))
|
2018-10-19 01:46:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 19:42:17 -05:00
|
|
|
if config.UseLinuxPathing {
|
|
|
|
for index, script := range config.Scripts {
|
2018-05-21 17:56:44 -04:00
|
|
|
scriptAbsPath, err := filepath.Abs(script)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error converting %s to absolute path: %s", script, err.Error())
|
|
|
|
}
|
|
|
|
converted, err := ConvertToLinuxPath(scriptAbsPath)
|
2018-03-08 19:42:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.Scripts[index] = converted
|
|
|
|
}
|
2018-03-09 18:14:52 -05:00
|
|
|
// Interoperability issues with WSL makes creating and running tempfiles
|
|
|
|
// via golang's os package basically impossible.
|
|
|
|
if len(config.Inline) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Packer is unable to use the Command and Inline "+
|
|
|
|
"features with the Windows Linux Subsystem. Please use "+
|
|
|
|
"the Script or Scripts options instead"))
|
|
|
|
}
|
2018-03-08 19:42:17 -05:00
|
|
|
}
|
2019-01-09 14:16:48 -05:00
|
|
|
|
2018-03-12 14:25:39 -04:00
|
|
|
if config.EnvVarFormat == "" {
|
|
|
|
if (runtime.GOOS == "windows") && !config.UseLinuxPathing {
|
2018-04-04 14:07:10 -04:00
|
|
|
config.EnvVarFormat = "set %s=%s && "
|
2018-03-12 14:25:39 -04:00
|
|
|
} else {
|
|
|
|
config.EnvVarFormat = "%s='%s' "
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 15:50:42 -05:00
|
|
|
|
2018-04-04 14:07:10 -04:00
|
|
|
// drop unnecessary "." in extension; we add this later.
|
|
|
|
if config.TempfileExtension != "" {
|
|
|
|
if strings.HasPrefix(config.TempfileExtension, ".") {
|
|
|
|
config.TempfileExtension = config.TempfileExtension[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 15:50:42 -05:00
|
|
|
// 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
|
|
|
|
}
|
2018-03-08 19:42:17 -05:00
|
|
|
|
|
|
|
// C:/path/to/your/file becomes /mnt/c/path/to/your/file
|
2018-05-21 17:56:44 -04:00
|
|
|
func ConvertToLinuxPath(winAbsPath string) (string, error) {
|
2018-03-08 19:42:17 -05:00
|
|
|
// get absolute path of script, and morph it into the bash path
|
|
|
|
winAbsPath = strings.Replace(winAbsPath, "\\", "/", -1)
|
|
|
|
splitPath := strings.SplitN(winAbsPath, ":/", 2)
|
2018-06-22 16:49:39 -04:00
|
|
|
if len(splitPath) == 2 {
|
|
|
|
winBashPath := fmt.Sprintf("/mnt/%s/%s", strings.ToLower(splitPath[0]), splitPath[1])
|
|
|
|
return winBashPath, nil
|
|
|
|
} else {
|
|
|
|
err := fmt.Errorf("There was an error splitting your absolute path; expected "+
|
|
|
|
"to find a drive following the format ':/' but did not: absolute "+
|
|
|
|
"path: %s", winAbsPath)
|
|
|
|
return "", err
|
|
|
|
}
|
2018-03-08 19:42:17 -05:00
|
|
|
}
|