mirror of https://github.com/hashicorp/packer
Merge pull request #7385 from hashicorp/windows_shell_allow_exit_code
allowed_exit_codes for windows-shell and shell provisionerspull/7416/head
commit
8853fc1946
@ -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 "<nil>"
|
||||
}
|
||||
return fmt.Sprintf("Script exited with non-zero exit status: %d."+
|
||||
"Allowed exit codes are: %v",
|
||||
e.Code, e.Allowed)
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
// Package shell defines code that is common in shells
|
||||
package shell
|
||||
|
||||
import "github.com/hashicorp/packer/common"
|
||||
|
||||
// Provisioner contains common fields to all shell 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"`
|
||||
}
|
||||
@ -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.
|
||||
Loading…
Reference in new issue