fix: moved os check to entry of func

This commit is contained in:
Joel Vasallo 2018-10-19 00:52:20 -05:00
parent 0912adea34
commit d408c9e69c
2 changed files with 44 additions and 46 deletions

View File

@ -165,18 +165,17 @@ func Validate(config *Config) error {
// Check for properly formatted go os types
supported_syslist := []string{"darwin", "freebsd", "linux", "openbsd", "solaris", "windows"}
supported_os := false
if len(config.OnlyOn) > 0 {
for _, provided_os := range config.OnlyOn {
supported_os := false
for _, go_os := range supported_syslist {
if provided_os == go_os {
supported_os = true
break
}
if supported_os != true {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Invalid OS specified in only_on: '%s'", provided_os))
}
}
if supported_os != true {
return fmt.Errorf("Invalid OS specified in only_on: '%s'", provided_os)
}
}
}

View File

@ -28,6 +28,24 @@ type EnvVarsTemplate struct {
}
func Run(ui packer.Ui, config *Config) (bool, error) {
// Check if shell-local can even execute against this runtime OS
runCommand := false
if len(config.OnlyOn) > 0 {
for _, os := range config.OnlyOn {
if os == runtime.GOOS {
runCommand = true
break
}
}
} else {
runCommand = true
}
if !runCommand {
ui.Say(fmt.Sprintf("Skipping shell-local due to runtime OS"))
log.Printf("[INFO] (shell-local): skipping shell-local due to missing runtime OS")
return true, nil
}
scripts := make([]string, len(config.Scripts))
if len(config.Scripts) > 0 {
copy(scripts, config.Scripts)
@ -59,50 +77,31 @@ func Run(ui packer.Ui, config *Config) (bool, error) {
if err != nil {
return false, err
}
ui.Say(fmt.Sprintf("Running local shell script: %s", script))
// Check if command can execute against runtime.
runCommand := false
if len(config.OnlyOn) > 0 {
for _, os := range config.OnlyOn {
if os == runtime.GOOS {
runCommand = true
}
}
} else {
runCommand = true
comm := &Communicator{
ExecuteCommand: interpolatedCmds,
}
if runCommand {
ui.Say(fmt.Sprintf("Running local shell script: %s", script))
comm := &Communicator{
ExecuteCommand: interpolatedCmds,
}
// The remoteCmd generated here isn't actually run, but it allows us to
// use the same interafce for the shell-local communicator as we use for
// the other communicators; ultimately, this command is just used for
// buffers and for reading the final exit status.
flattenedCmd := strings.Join(interpolatedCmds, " ")
cmd := &packer.RemoteCmd{Command: flattenedCmd}
log.Printf("[INFO] (shell-local): starting local command: %s", flattenedCmd)
if err := cmd.StartWithUi(comm, ui); err != nil {
return false, fmt.Errorf(
"Error executing script: %s\n\n"+
"Please see output above for more information.",
script)
}
if cmd.ExitStatus != 0 {
return false, fmt.Errorf(
"Erroneous exit code %d while executing script: %s\n\n"+
"Please see output above for more information.",
cmd.ExitStatus,
script)
}
} else {
ui.Say(fmt.Sprintf("Skipping local shell script due to runtime OS: %s", script))
log.Printf("[INFO] (shell-local): skipping command due to runtime OS not specified.")
// The remoteCmd generated here isn't actually run, but it allows us to
// use the same interafce for the shell-local communicator as we use for
// the other communicators; ultimately, this command is just used for
// buffers and for reading the final exit status.
flattenedCmd := strings.Join(interpolatedCmds, " ")
cmd := &packer.RemoteCmd{Command: flattenedCmd}
log.Printf("[INFO] (shell-local): starting local command: %s", flattenedCmd)
if err := cmd.StartWithUi(comm, ui); err != nil {
return false, fmt.Errorf(
"Error executing script: %s\n\n"+
"Please see output above for more information.",
script)
}
if cmd.ExitStatus != 0 {
return false, fmt.Errorf(
"Erroneous exit code %d while executing script: %s\n\n"+
"Please see output above for more information.",
cmd.ExitStatus,
script)
}
}