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,
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) { 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,20 +77,6 @@ func Run(ui packer.Ui, config *Config) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
// 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
}
if runCommand {
ui.Say(fmt.Sprintf("Running local shell script: %s", script)) ui.Say(fmt.Sprintf("Running local shell script: %s", script))
comm := &Communicator{ comm := &Communicator{
@ -84,7 +88,6 @@ func Run(ui packer.Ui, config *Config) (bool, error) {
// the other communicators; ultimately, this command is just used for // the other communicators; ultimately, this command is just used for
// buffers and for reading the final exit status. // buffers and for reading the final exit status.
flattenedCmd := strings.Join(interpolatedCmds, " ") flattenedCmd := strings.Join(interpolatedCmds, " ")
cmd := &packer.RemoteCmd{Command: flattenedCmd} cmd := &packer.RemoteCmd{Command: flattenedCmd}
log.Printf("[INFO] (shell-local): starting local command: %s", flattenedCmd) log.Printf("[INFO] (shell-local): starting local command: %s", flattenedCmd)
if err := cmd.StartWithUi(comm, ui); err != nil { if err := cmd.StartWithUi(comm, ui); err != nil {
@ -100,10 +103,6 @@ func Run(ui packer.Ui, config *Config) (bool, error) {
cmd.ExitStatus, cmd.ExitStatus,
script) 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.")
}
} }
return true, nil return true, nil