feat: ability to specify runtime OSs where shell-local should run
This commit is contained in:
parent
29d041ea8a
commit
25c0b98363
|
@ -29,6 +29,9 @@ type Config struct {
|
||||||
// The shebang value used when running inline scripts.
|
// The shebang value used when running inline scripts.
|
||||||
InlineShebang string `mapstructure:"inline_shebang"`
|
InlineShebang string `mapstructure:"inline_shebang"`
|
||||||
|
|
||||||
|
// An array of multiple Runtime OSs to run on.
|
||||||
|
OnlyOn []string
|
||||||
|
|
||||||
// The file extension to use for the file generated from the inline commands
|
// The file extension to use for the file generated from the inline commands
|
||||||
TempfileExtension string `mapstructure:"tempfile_extension"`
|
TempfileExtension string `mapstructure:"tempfile_extension"`
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -58,31 +59,50 @@ func Run(ui packer.Ui, config *Config) (bool, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
ui.Say(fmt.Sprintf("Running local shell script: %s", script))
|
|
||||||
|
|
||||||
comm := &Communicator{
|
// Check if command can execute against runtime.
|
||||||
ExecuteCommand: interpolatedCmds,
|
runCommand := false
|
||||||
|
if len(config.OnlyOn) > 0 {
|
||||||
|
for _, os := range config.OnlyOn {
|
||||||
|
if os == runtime.GOOS {
|
||||||
|
runCommand = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
runCommand = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// The remoteCmd generated here isn't actually run, but it allows us to
|
if runCommand {
|
||||||
// use the same interafce for the shell-local communicator as we use for
|
ui.Say(fmt.Sprintf("Running local shell script: %s", script))
|
||||||
// the other communicators; ultimately, this command is just used for
|
|
||||||
// buffers and for reading the final exit status.
|
comm := &Communicator{
|
||||||
flattenedCmd := strings.Join(interpolatedCmds, " ")
|
ExecuteCommand: interpolatedCmds,
|
||||||
cmd := &packer.RemoteCmd{Command: flattenedCmd}
|
}
|
||||||
log.Printf("[INFO] (shell-local): starting local command: %s", flattenedCmd)
|
|
||||||
if err := cmd.StartWithUi(comm, ui); err != nil {
|
// The remoteCmd generated here isn't actually run, but it allows us to
|
||||||
return false, fmt.Errorf(
|
// use the same interafce for the shell-local communicator as we use for
|
||||||
"Error executing script: %s\n\n"+
|
// the other communicators; ultimately, this command is just used for
|
||||||
"Please see output above for more information.",
|
// buffers and for reading the final exit status.
|
||||||
script)
|
flattenedCmd := strings.Join(interpolatedCmds, " ")
|
||||||
}
|
|
||||||
if cmd.ExitStatus != 0 {
|
cmd := &packer.RemoteCmd{Command: flattenedCmd}
|
||||||
return false, fmt.Errorf(
|
log.Printf("[INFO] (shell-local): starting local command: %s", flattenedCmd)
|
||||||
"Erroneous exit code %d while executing script: %s\n\n"+
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
||||||
"Please see output above for more information.",
|
return false, fmt.Errorf(
|
||||||
cmd.ExitStatus,
|
"Error executing script: %s\n\n"+
|
||||||
script)
|
"Please see output above for more information.",
|
||||||
|
script)
|
||||||
|
}
|
||||||
|
if cmd.ExitStatus != 0 {
|
||||||
|
return false, fmt.Errorf(
|
||||||
|
"Erroneous exit code %d while executing script: %s\n\n"+
|
||||||
|
"Please see output above for more information.",
|
||||||
|
cmd.ExitStatus,
|
||||||
|
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.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -108,6 +108,14 @@ Optional parameters:
|
||||||
**Important:** If you customize this, be sure to include something like the
|
**Important:** If you customize this, be sure to include something like the
|
||||||
`-e` flag, otherwise individual steps failing won't fail the provisioner.
|
`-e` flag, otherwise individual steps failing won't fail the provisioner.
|
||||||
|
|
||||||
|
- `onlyon` (array of strings) - This is an array of
|
||||||
|
[runtime operating systems](https://golang.org/doc/install/source#environment)
|
||||||
|
where `shell-local` will only run on. This allows you to run `shell-local` *only*
|
||||||
|
on specific compatible operating systems. If specified, shell-local will only
|
||||||
|
execute if runtime operating system is in the list; otherwise it will skip the
|
||||||
|
`shell-local` command. The default behavior is for `shell-local` to run if
|
||||||
|
`onlyon` is not specified.
|
||||||
|
|
||||||
- `use_linux_pathing` (bool) - This is only relevant to windows hosts. If you
|
- `use_linux_pathing` (bool) - This is only relevant to windows hosts. If you
|
||||||
are running Packer in a Windows environment with the Windows Subsystem for
|
are running Packer in a Windows environment with the Windows Subsystem for
|
||||||
Linux feature enabled, and would like to invoke a bash script rather than
|
Linux feature enabled, and would like to invoke a bash script rather than
|
||||||
|
|
Loading…
Reference in New Issue