From 8fea9439f862da2833fe0625802649528c1fab9c Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 7 Mar 2019 11:35:56 +0100 Subject: [PATCH 01/10] 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) } } From f369ae217868b8ccfe7c00c31865f050a16ba77c Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 7 Mar 2019 11:37:11 +0100 Subject: [PATCH 02/10] Update windows-shell.html.md copy paste valid_exit_codes from powershell.html.md --- website/source/docs/provisioners/windows-shell.html.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/website/source/docs/provisioners/windows-shell.html.md b/website/source/docs/provisioners/windows-shell.html.md index 50768a320..4b13ab243 100644 --- a/website/source/docs/provisioners/windows-shell.html.md +++ b/website/source/docs/provisioners/windows-shell.html.md @@ -75,6 +75,9 @@ Optional parameters: exists in order to deal with times when SSH may restart, such as a system reboot. Set this to a higher value if reboots take a longer amount of time. +- `valid_exit_codes` (list of ints) - Valid exit codes for the script. By + default this is just 0. + ## Default Environmental Variables In addition to being able to specify custom environmental variables using the From 30a65c858ae1b89424fb3703dd9ae4c2efcfc134 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 14 Mar 2019 11:57:54 +0100 Subject: [PATCH 03/10] regroup shell provisioner params into a common struct --- common/shell/shell.go | 42 ++++++++++++++++++++++++ provisioner/powershell/provisioner.go | 37 ++------------------- provisioner/shell/provisioner.go | 31 ++--------------- provisioner/windows-shell/provisioner.go | 30 ++--------------- 4 files changed, 48 insertions(+), 92 deletions(-) create mode 100644 common/shell/shell.go diff --git a/common/shell/shell.go b/common/shell/shell.go new file mode 100644 index 000000000..5650440d6 --- /dev/null +++ b/common/shell/shell.go @@ -0,0 +1,42 @@ +// Package shell defines conde that is common in shells +package shell + +import "github.com/hashicorp/packer/common" + +// Provisioner contains common fields to all provisioners +type Provisioner struct { + common.PackerConfig `mapstructure:",squash"` + + // If true, the script contains binary and line endings will not be + // converted from Windows to Unix-style. + Binary bool + + // The command used to execute the script. The '{{ .Path }}' variable + // should be used to specify where the script goes, {{ .Vars }} + // can be used to inject the environment_vars into the environment. + ExecuteCommand string `mapstructure:"execute_command"` + + // An inline script to execute. Multiple strings are all executed + // in the context of a single shell. + Inline []string + + // The remote path where the local shell script will be uploaded to. + // This should be set to a writable file that is in a pre-existing directory. + // This defaults to remote_folder/remote_file + RemotePath string `mapstructure:"remote_path"` + + // The local path of the shell script to upload and execute. + Script string + + // An array of multiple scripts to run. + Scripts []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"` + + // An array of environment variables that will be injected before + // your command(s) are executed. + Vars []string `mapstructure:"environment_vars"` +} diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index 74256355e..ae5b27a1c 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -13,6 +13,7 @@ import ( "time" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/shell" "github.com/hashicorp/packer/common/uuid" commonhelper "github.com/hashicorp/packer/helper/common" "github.com/hashicorp/packer/helper/config" @@ -32,41 +33,13 @@ var psEscape = strings.NewReplacer( ) type Config struct { - common.PackerConfig `mapstructure:",squash"` - - // If true, the script contains binary and line endings will not be - // converted from Windows to Unix-style. - Binary bool - - // An inline script to execute. Multiple strings are all executed in the - // context of a single shell. - Inline []string - - // The local path of the powershell script to upload and execute. - Script string - - // An array of multiple scripts to run. - Scripts []string - - // An array of environment variables that will be injected before your - // command(s) are executed. - Vars []string `mapstructure:"environment_vars"` - - // The remote path where the local powershell script will be uploaded to. - // This should be set to a writable file that is in a pre-existing - // directory. - RemotePath string `mapstructure:"remote_path"` + shell.Provisioner `mapstructure:",squash"` // The remote path where the file containing the environment variables // will be uploaded to. This should be set to a writable file that is in a // pre-existing directory. RemoteEnvVarPath string `mapstructure:"remote_env_var_path"` - // The command used to execute the script. The '{{ .Path }}' variable - // should be used to specify where the script goes, {{ .Vars }} can be - // used to inject the environment_vars into the environment. - ExecuteCommand string `mapstructure:"execute_command"` - // The command used to execute the elevated script. The '{{ .Path }}' // variable should be used to specify where the script goes, {{ .Vars }} // can be used to inject the environment_vars into the environment. @@ -91,12 +64,6 @@ type Config struct { ElevatedUser string `mapstructure:"elevated_user"` ElevatedPassword string `mapstructure:"elevated_password"` - // 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. - // Changes will not be effective until the system is rebooted." - ValidExitCodes []int `mapstructure:"valid_exit_codes"` - ctx interpolate.Context } diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index f0eb325a4..4618ef043 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -15,6 +15,7 @@ import ( "time" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/shell" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer/tmp" @@ -22,29 +23,11 @@ import ( ) type Config struct { - common.PackerConfig `mapstructure:",squash"` - - // If true, the script contains binary and line endings will not be - // converted from Windows to Unix-style. - Binary bool - - // An inline script to execute. Multiple strings are all executed - // in the context of a single shell. - Inline []string + shell.Provisioner `mapstructure:",squash"` // The shebang value used when running inline scripts. InlineShebang string `mapstructure:"inline_shebang"` - // The local path of the shell script to upload and execute. - Script string - - // An array of multiple scripts to run. - Scripts []string - - // An array of environment variables that will be injected before - // your command(s) are executed. - Vars []string `mapstructure:"environment_vars"` - // A duration of how long to pause after the provisioner RawPauseAfter string `mapstructure:"pause_after"` @@ -62,16 +45,6 @@ type Config struct { // This defaults to script_nnn.sh RemoteFile string `mapstructure:"remote_file"` - // The remote path where the local shell script will be uploaded to. - // This should be set to a writable file that is in a pre-existing directory. - // This defaults to remote_folder/remote_file - RemotePath string `mapstructure:"remote_path"` - - // The command used to execute the script. The '{{ .Path }}' variable - // should be used to specify where the script goes, {{ .Vars }} - // can be used to inject the environment_vars into the environment. - ExecuteCommand string `mapstructure:"execute_command"` - // The timeout for retrying to start the process. Until this timeout // is reached, if the provisioner can't start a process, it retries. // This can be set high to allow for reboots. diff --git a/provisioner/windows-shell/provisioner.go b/provisioner/windows-shell/provisioner.go index dc7b07fc3..1c052a7e5 100644 --- a/provisioner/windows-shell/provisioner.go +++ b/provisioner/windows-shell/provisioner.go @@ -13,6 +13,7 @@ import ( "time" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/shell" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer/tmp" @@ -25,29 +26,7 @@ const DefaultRemotePath = "c:/Windows/Temp/script.bat" var retryableSleep = 2 * time.Second type Config struct { - common.PackerConfig `mapstructure:",squash"` - - // If true, the script contains binary and line endings will not be - // converted from Windows to Unix-style. - Binary bool - - // An inline script to execute. Multiple strings are all executed - // in the context of a single shell. - Inline []string - - // The local path of the shell script to upload and execute. - Script string - - // An array of multiple scripts to run. - Scripts []string - - // An array of environment variables that will be injected before - // your command(s) are executed. - Vars []string `mapstructure:"environment_vars"` - - // The remote path where the local shell script will be uploaded to. - // This should be set to a writable file that is in a pre-existing directory. - RemotePath string `mapstructure:"remote_path"` + shell.Provisioner `mapstructure:",squash"` // The command used to execute the script. The '{{ .Path }}' variable // should be used to specify where the script goes, {{ .Vars }} @@ -63,11 +42,6 @@ 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 } From f0a23bb81df3122f3ef48a5fd427d90e2dbad35a Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 14 Mar 2019 12:46:32 +0100 Subject: [PATCH 04/10] common shell provisioner: define a ValidExitCode func --- common/shell/exit_code.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 common/shell/exit_code.go diff --git a/common/shell/exit_code.go b/common/shell/exit_code.go new file mode 100644 index 000000000..815e92f88 --- /dev/null +++ b/common/shell/exit_code.go @@ -0,0 +1,39 @@ +package shell + +import "fmt" + +func (p *Provisioner) ValidExitCode(code int) error { + // Check exit code against allowed codes (likely just 0) + validCodes := p.ValidExitCodes + if len(validCodes) == 0 { + validCodes = []int{0} + } + validExitCode := false + for _, v := range validCodes { + if code == v { + validExitCode = true + break + } + } + if !validExitCode { + return &ErrorInvalidExitCode{ + Code: code, + Allowed: validCodes, + } + } + return nil +} + +type ErrorInvalidExitCode struct { + Code int + Allowed []int +} + +func (e *ErrorInvalidExitCode) Error() string { + if e == nil { + return "" + } + return fmt.Sprintf("Script exited with non-zero exit status: %d."+ + "Allowed exit codes are: %v", + e.Code, e.Allowed) +} From 72e5ae9ddc69af4408cfa4fa39a7b45610fed541 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 14 Mar 2019 12:47:22 +0100 Subject: [PATCH 05/10] use ValidExitCode in provisioners windows-shell, powershell and shell --- provisioner/powershell/provisioner.go | 17 ++--------------- provisioner/shell/provisioner.go | 7 ++++--- provisioner/windows-shell/provisioner.go | 17 ++--------------- 3 files changed, 8 insertions(+), 33 deletions(-) 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 } } From a77ce59e36f5ab16e2b87156206ffd9536ba2485 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 14 Mar 2019 12:48:21 +0100 Subject: [PATCH 06/10] Update provisioner_test.go remove unecessary test --- provisioner/powershell/provisioner_test.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/provisioner/powershell/provisioner_test.go b/provisioner/powershell/provisioner_test.go index eef61bf8d..7e7ab2dcd 100644 --- a/provisioner/powershell/provisioner_test.go +++ b/provisioner/powershell/provisioner_test.go @@ -87,18 +87,6 @@ func TestProvisionerPrepare_Defaults(t *testing.T) { t.Fatalf(`Default command should be 'powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }"', but got '%s'`, p.config.ElevatedExecuteCommand) } - if p.config.ValidExitCodes == nil { - t.Fatalf("ValidExitCodes should not be nil") - } - if p.config.ValidExitCodes != nil { - expCodes := []int{0} - for i, v := range p.config.ValidExitCodes { - if v != expCodes[i] { - t.Fatalf("Expected ValidExitCodes don't match actual") - } - } - } - if p.config.ElevatedEnvVarFormat != `$env:%s="%s"; ` { t.Fatalf(`Default command should be powershell '$env:%%s="%%s"; ', but got %s`, p.config.ElevatedEnvVarFormat) } From eedf5e43872890b698d4a52e576d0a69f30d38d9 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 14 Mar 2019 12:50:19 +0100 Subject: [PATCH 07/10] Update shell.go remove typo --- common/shell/shell.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/shell/shell.go b/common/shell/shell.go index 5650440d6..2bd67f848 100644 --- a/common/shell/shell.go +++ b/common/shell/shell.go @@ -1,4 +1,4 @@ -// Package shell defines conde that is common in shells +// Package shell defines code that is common in shells package shell import "github.com/hashicorp/packer/common" From a9e9fff3ad10f7860bd9d53f43a5b0df7a4bce2c Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 14 Mar 2019 13:12:03 +0100 Subject: [PATCH 08/10] docs: refactor comon shell configurations into a partial --- ...ershell.html.md => powershell.html.md.erb} | 31 +------------------ .../{shell.html.md => shell.html.md.erb} | 28 +---------------- ...hell.html.md => windows-shell.html.md.erb} | 31 +------------------ .../provisioners/_shell-config.html.md | 30 ++++++++++++++++++ 4 files changed, 33 insertions(+), 87 deletions(-) rename website/source/docs/provisioners/{powershell.html.md => powershell.html.md.erb} (89%) rename website/source/docs/provisioners/{shell.html.md => shell.html.md.erb} (89%) rename website/source/docs/provisioners/{windows-shell.html.md => windows-shell.html.md.erb} (67%) create mode 100644 website/source/partials/provisioners/_shell-config.html.md diff --git a/website/source/docs/provisioners/powershell.html.md b/website/source/docs/provisioners/powershell.html.md.erb similarity index 89% rename from website/source/docs/provisioners/powershell.html.md rename to website/source/docs/provisioners/powershell.html.md.erb index d12a39f14..50b4ba312 100644 --- a/website/source/docs/provisioners/powershell.html.md +++ b/website/source/docs/provisioners/powershell.html.md.erb @@ -31,33 +31,7 @@ The example below is fully functional. ## Configuration Reference -The reference of available configuration options is listed below. The only -required element is either "inline" or "script". Every other option is -optional. - -Exactly *one* of the following is required: - -- `inline` (array of strings) - This is an array of commands to execute. The - commands are concatenated by newlines and turned into a single file, so - they are all executed within the same context. This allows you to change - directories in one command and use something in the directory in the next - and so on. Inline scripts are the easiest way to pull off simple tasks - within the machine. - -- `script` (string) - The path to a script to upload and execute in the - machine. This path can be absolute or relative. If it is relative, it is - relative to the working directory when Packer is executed. - -- `scripts` (array of strings) - An array of scripts to execute. The scripts - will be uploaded and executed in the order specified. Each script is - executed in isolation, so state such as variables from one script won't - carry on to the next. - -Optional parameters: - -- `binary` (boolean) - If true, specifies that the script(s) are binary - files, and Packer should therefore not convert Windows line endings to Unix - line endings (if there are any). By default this is false. +<%= partial "partials/provisioners/shell-config" %> - `elevated_execute_command` (string) - The command to use to execute the elevated script. By default this is as follows: @@ -155,9 +129,6 @@ Optional parameters: exists in order to deal with times when SSH may restart, such as a system reboot. Set this to a higher value if reboots take a longer amount of time. -- `valid_exit_codes` (list of ints) - Valid exit codes for the script. By - default this is just 0. - ## Default Environmental Variables In addition to being able to specify custom environmental variables using the diff --git a/website/source/docs/provisioners/shell.html.md b/website/source/docs/provisioners/shell.html.md.erb similarity index 89% rename from website/source/docs/provisioners/shell.html.md rename to website/source/docs/provisioners/shell.html.md.erb index 65ad496fd..d4b40c96d 100644 --- a/website/source/docs/provisioners/shell.html.md +++ b/website/source/docs/provisioners/shell.html.md.erb @@ -33,33 +33,7 @@ The example below is fully functional. ## Configuration Reference -The reference of available configuration options is listed below. The only -required element is either "inline" or "script". Every other option is -optional. - -Exactly *one* of the following is required: - -- `inline` (array of strings) - This is an array of commands to execute. The - commands are concatenated by newlines and turned into a single file, so - they are all executed within the same context. This allows you to change - directories in one command and use something in the directory in the next - and so on. Inline scripts are the easiest way to pull off simple tasks - within the machine. - -- `script` (string) - The path to a script to upload and execute in the - machine. This path can be absolute or relative. If it is relative, it is - relative to the working directory when Packer is executed. - -- `scripts` (array of strings) - An array of scripts to execute. The scripts - will be uploaded and executed in the order specified. Each script is - executed in isolation, so state such as variables from one script won't - carry on to the next. - -Optional parameters: - -- `binary` (boolean) - If true, specifies that the script(s) are binary - files, and Packer should therefore not convert Windows line endings to Unix - line endings (if there are any). By default this is false. +<%= partial "partials/provisioners/shell-config" %> - `environment_vars` (array of strings) - An array of key/value pairs to inject prior to the execute\_command. The format should be `key=value`. diff --git a/website/source/docs/provisioners/windows-shell.html.md b/website/source/docs/provisioners/windows-shell.html.md.erb similarity index 67% rename from website/source/docs/provisioners/windows-shell.html.md rename to website/source/docs/provisioners/windows-shell.html.md.erb index 4b13ab243..cbec0c3cc 100644 --- a/website/source/docs/provisioners/windows-shell.html.md +++ b/website/source/docs/provisioners/windows-shell.html.md.erb @@ -27,33 +27,7 @@ The example below is fully functional. ## Configuration Reference -The reference of available configuration options is listed below. The only -required element is either "inline" or "script". Every other option is -optional. - -Exactly *one* of the following is required: - -- `inline` (array of strings) - This is an array of commands to execute. The - commands are concatenated by newlines and turned into a single file, so - they are all executed within the same context. This allows you to change - directories in one command and use something in the directory in the next - and so on. Inline scripts are the easiest way to pull off simple tasks - within the machine. - -- `script` (string) - The path to a script to upload and execute in the - machine. This path can be absolute or relative. If it is relative, it is - relative to the working directory when Packer is executed. - -- `scripts` (array of strings) - An array of scripts to execute. The scripts - will be uploaded and executed in the order specified. Each script is - executed in isolation, so state such as variables from one script won't - carry on to the next. - -Optional parameters: - -- `binary` (boolean) - If true, specifies that the script(s) are binary - files, and Packer should therefore not convert Windows line endings to Unix - line endings (if there are any). By default this is false. +<%= partial "partials/provisioners/shell-config" %> - `environment_vars` (array of strings) - An array of key/value pairs to inject prior to the execute\_command. The format should be `key=value`. @@ -75,9 +49,6 @@ Optional parameters: exists in order to deal with times when SSH may restart, such as a system reboot. Set this to a higher value if reboots take a longer amount of time. -- `valid_exit_codes` (list of ints) - Valid exit codes for the script. By - default this is just 0. - ## Default Environmental Variables In addition to being able to specify custom environmental variables using the diff --git a/website/source/partials/provisioners/_shell-config.html.md b/website/source/partials/provisioners/_shell-config.html.md new file mode 100644 index 000000000..7e2ae0311 --- /dev/null +++ b/website/source/partials/provisioners/_shell-config.html.md @@ -0,0 +1,30 @@ +The reference of available configuration options is listed below. The only +required element is either "inline" or "script". Every other option is +optional. + +Exactly *one* of the following is required: + +- `inline` (array of strings) - This is an array of commands to execute. The + commands are concatenated by newlines and turned into a single file, so + they are all executed within the same context. This allows you to change + directories in one command and use something in the directory in the next + and so on. Inline scripts are the easiest way to pull off simple tasks + within the machine. + +- `script` (string) - The path to a script to upload and execute in the + machine. This path can be absolute or relative. If it is relative, it is + relative to the working directory when Packer is executed. + +- `scripts` (array of strings) - An array of scripts to execute. The scripts + will be uploaded and executed in the order specified. Each script is + executed in isolation, so state such as variables from one script won't + carry on to the next. + +Optional parameters: + +- `binary` (boolean) - If true, specifies that the script(s) are binary + files, and Packer should therefore not convert Windows line endings to Unix + line endings (if there are any). By default this is false. + +- `valid_exit_codes` (list of ints) - Valid exit codes for the script. By + default this is just 0. From b4ec6e2ed2d751d5a1c3b4f023f3256747b8fbe8 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 14 Mar 2019 13:32:46 +0100 Subject: [PATCH 09/10] Create exit_code_test.go --- common/shell/exit_code_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 common/shell/exit_code_test.go diff --git a/common/shell/exit_code_test.go b/common/shell/exit_code_test.go new file mode 100644 index 000000000..c0ce58e99 --- /dev/null +++ b/common/shell/exit_code_test.go @@ -0,0 +1,31 @@ +package shell + +import ( + "fmt" + "testing" +) + +func TestProvisioner_ValidExitCode(t *testing.T) { + + tests := []struct { + exitCodes []int + code int + wantErr bool + }{ + {nil, 0, false}, + {nil, 1, true}, + {[]int{2}, 2, false}, + {[]int{2}, 3, true}, + } + for n := range tests { + tt := tests[n] + t.Run(fmt.Sprintf("%v - %v - %v", tt.exitCodes, tt.code, tt.wantErr), func(t *testing.T) { + p := Provisioner{ + ValidExitCodes: tt.exitCodes, + } + if err := p.ValidExitCode(tt.code); (err != nil) != tt.wantErr { + t.Errorf("Provisioner.ValidExitCode() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From 6ab28ba89ca3c099eb74cbbcc4d94e9c25f90329 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 15 Mar 2019 12:21:27 +0100 Subject: [PATCH 10/10] Update common/shell/shell.go be more descriptive --- common/shell/shell.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/shell/shell.go b/common/shell/shell.go index 2bd67f848..39a89fa5e 100644 --- a/common/shell/shell.go +++ b/common/shell/shell.go @@ -3,7 +3,7 @@ package shell import "github.com/hashicorp/packer/common" -// Provisioner contains common fields to all provisioners +// Provisioner contains common fields to all shell provisioners type Provisioner struct { common.PackerConfig `mapstructure:",squash"`