diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index ae5b27a1c..57b3c4849 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -146,10 +146,6 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { p.config.Vars = make([]string, 0) } - if p.config.ValidExitCodes == nil { - p.config.ValidExitCodes = []int{0} - } - var errs error if p.config.Script != "" && len(p.config.Scripts) > 0 { errs = packer.MultiErrorAppend(errs, @@ -274,17 +270,8 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { // Close the original file since we copied it f.Close() - // Check exit code against allowed codes (likely just 0) - validExitCode := false - for _, v := range p.config.ValidExitCodes { - if cmd.ExitStatus == v { - validExitCode = true - } - } - if !validExitCode { - return fmt.Errorf( - "Script exited with non-zero exit status: %d. Allowed exit codes are: %v", - cmd.ExitStatus, p.config.ValidExitCodes) + if err := p.config.ValidExitCode(cmd.ExitStatus); err != nil { + return err } } diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 4618ef043..1fa14d9d5 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -335,10 +335,11 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { return fmt.Errorf("Script disconnected unexpectedly. " + "If you expected your script to disconnect, i.e. from a " + "restart, you can try adding `\"expect_disconnect\": true` " + - "to the shell provisioner parameters.") + "or `\"valid_exit_codes\": [0, 2300218]` to the shell " + + "provisioner parameters.") } - } else if cmd.ExitStatus != 0 { - return fmt.Errorf("Script exited with non-zero exit status: %d", cmd.ExitStatus) + } else if err := p.config.ValidExitCode(cmd.ExitStatus); err != nil { + return err } if !p.config.SkipClean { diff --git a/provisioner/windows-shell/provisioner.go b/provisioner/windows-shell/provisioner.go index 1c052a7e5..6d37e2514 100644 --- a/provisioner/windows-shell/provisioner.go +++ b/provisioner/windows-shell/provisioner.go @@ -96,10 +96,6 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { p.config.Vars = make([]string, 0) } - if len(p.config.ValidExitCodes) == 0 { - p.config.ValidExitCodes = []int{0} - } - var errs error if p.config.Script != "" && len(p.config.Scripts) > 0 { errs = packer.MultiErrorAppend(errs, @@ -226,17 +222,8 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { // Close the original file since we copied it f.Close() - // Check exit code against allowed codes (likely just 0) - validExitCode := false - for _, v := range p.config.ValidExitCodes { - if cmd.ExitStatus == v { - validExitCode = true - } - } - if !validExitCode { - return fmt.Errorf( - "Script exited with non-zero exit status: %d. Allowed exit codes are: %v", - cmd.ExitStatus, p.config.ValidExitCodes) + if err := p.config.ValidExitCode(cmd.ExitStatus); err != nil { + return err } }