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 // Check for properly formatted go os types
supported_syslist := []string{"darwin", "freebsd", "linux", "openbsd", "solaris", "windows"} supported_syslist := []string{"darwin", "freebsd", "linux", "openbsd", "solaris", "windows"}
supported_os := false
if len(config.OnlyOn) > 0 { if len(config.OnlyOn) > 0 {
for _, provided_os := range config.OnlyOn { for _, provided_os := range config.OnlyOn {
supported_os := false
for _, go_os := range supported_syslist { for _, go_os := range supported_syslist {
if provided_os == go_os { if provided_os == go_os {
supported_os = true supported_os = true
break break
} }
if supported_os != true { }
errs = packer.MultiErrorAppend(errs, if supported_os != true {
fmt.Errorf("Invalid OS specified in only_on: '%s'", provided_os)) 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) { 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)) scripts := make([]string, len(config.Scripts))
if len(config.Scripts) > 0 { if len(config.Scripts) > 0 {
copy(scripts, config.Scripts) copy(scripts, config.Scripts)
@ -59,50 +77,31 @@ func Run(ui packer.Ui, config *Config) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
ui.Say(fmt.Sprintf("Running local shell script: %s", script))
// Check if command can execute against runtime. comm := &Communicator{
runCommand := false ExecuteCommand: interpolatedCmds,
if len(config.OnlyOn) > 0 {
for _, os := range config.OnlyOn {
if os == runtime.GOOS {
runCommand = true
}
}
} else {
runCommand = true
} }
if runCommand { // The remoteCmd generated here isn't actually run, but it allows us to
ui.Say(fmt.Sprintf("Running local shell script: %s", script)) // use the same interafce for the shell-local communicator as we use for
// the other communicators; ultimately, this command is just used for
comm := &Communicator{ // buffers and for reading the final exit status.
ExecuteCommand: interpolatedCmds, flattenedCmd := strings.Join(interpolatedCmds, " ")
} cmd := &packer.RemoteCmd{Command: flattenedCmd}
log.Printf("[INFO] (shell-local): starting local command: %s", flattenedCmd)
// The remoteCmd generated here isn't actually run, but it allows us to if err := cmd.StartWithUi(comm, ui); err != nil {
// use the same interafce for the shell-local communicator as we use for return false, fmt.Errorf(
// the other communicators; ultimately, this command is just used for "Error executing script: %s\n\n"+
// buffers and for reading the final exit status. "Please see output above for more information.",
flattenedCmd := strings.Join(interpolatedCmds, " ") script)
}
cmd := &packer.RemoteCmd{Command: flattenedCmd} if cmd.ExitStatus != 0 {
log.Printf("[INFO] (shell-local): starting local command: %s", flattenedCmd) return false, fmt.Errorf(
if err := cmd.StartWithUi(comm, ui); err != nil { "Erroneous exit code %d while executing script: %s\n\n"+
return false, fmt.Errorf( "Please see output above for more information.",
"Error executing script: %s\n\n"+ cmd.ExitStatus,
"Please see output above for more information.", script)
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.")
} }
} }