From 8fea9439f862da2833fe0625802649528c1fab9c Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 7 Mar 2019 11:35:56 +0100 Subject: [PATCH] windows-shell provisioner: allow to pass ValidExitCodes --- provisioner/windows-shell/provisioner.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/provisioner/windows-shell/provisioner.go b/provisioner/windows-shell/provisioner.go index 71fcc71d6..dc7b07fc3 100644 --- a/provisioner/windows-shell/provisioner.go +++ b/provisioner/windows-shell/provisioner.go @@ -63,6 +63,11 @@ type Config struct { // inside the `ExecuteCommand` template. EnvVarFormat string + // Valid Exit Codes - 0 is not always the only valid error code! See + // http://www.symantec.com/connect/articles/windows-system-error-codes-exit-codes-description + // for examples such as 3010 - "The requested operation is successful. + ValidExitCodes []int `mapstructure:"valid_exit_codes"` + ctx interpolate.Context } @@ -117,6 +122,10 @@ 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, @@ -243,8 +252,17 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { // Close the original file since we copied it f.Close() - if cmd.ExitStatus != 0 { - return fmt.Errorf("Script exited with non-zero exit status: %d", cmd.ExitStatus) + // 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) } }