2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
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"
|
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/shell"
|
2020-11-18 13:34:59 -05:00
|
|
|
configHelper "github.com/hashicorp/packer/packer-plugin-sdk/template/config"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2018-02-27 15:50:42 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2019-11-21 19:43:13 -05:00
|
|
|
shell.Provisioner `mapstructure:",squash"`
|
|
|
|
|
2018-02-27 15:50:42 -05:00
|
|
|
// ** DEPRECATED: USE INLINE INSTEAD **
|
2019-07-25 00:49:51 -04:00
|
|
|
// ** Only Present for backwards compatibility **
|
2018-02-27 15:50:42 -05:00
|
|
|
// Command is the command to execute
|
|
|
|
Command string
|
|
|
|
|
2019-11-21 19:43:13 -05:00
|
|
|
// 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-02-27 15:50:42 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
// End dedupe with postprocessor
|
2018-03-08 19:42:17 -05:00
|
|
|
UseLinuxPathing bool `mapstructure:"use_linux_pathing"`
|
|
|
|
|
2019-10-04 14:36:57 -04:00
|
|
|
// used to track the data sent to shell-local from the builder
|
|
|
|
// GeneratedData
|
|
|
|
|
2019-12-12 13:59:44 -05:00
|
|
|
ctx interpolate.Context
|
|
|
|
generatedData map[string]interface{}
|
2018-02-27 15:50:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func Decode(config *Config, raws ...interface{}) error {
|
2019-12-17 05:25:56 -05:00
|
|
|
err := configHelper.Decode(config, &configHelper.DecodeOpts{
|
2018-02-27 15:50:42 -05:00
|
|
|
Interpolate: true,
|
2019-10-14 10:15:47 -04:00
|
|
|
InterpolateContext: &config.ctx,
|
2018-02-27 15:50:42 -05:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"execute_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return fmt.Errorf("Error decoding config: %s", err)
|
|
|
|
// 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 {
|
2020-11-19 15:07:02 -05:00
|
|
|
var errs *packersdk.MultiError
|
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",
|
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 == "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs,
|
2018-02-27 15:50:42 -05:00
|
|
|
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 != "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, tooManyOptionsErr)
|
2018-02-28 18:19:28 -05:00
|
|
|
} 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 {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, tooManyOptionsErr)
|
2018-02-28 18:19:28 -05:00
|
|
|
} else {
|
|
|
|
config.Scripts = []string{config.Script}
|
|
|
|
}
|
2018-02-27 15:50:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.Scripts) > 0 && config.Inline != nil {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.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 {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs,
|
2018-02-27 15:50:42 -05:00
|
|
|
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 {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs,
|
2018-03-09 18:14:52 -05:00
|
|
|
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] == "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs,
|
2018-02-27 15:50:42 -05:00
|
|
|
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
|
|
|
}
|