Salt provisioner: option to set logging level on Salt highstate run

This commit is contained in:
Matt Black 2015-08-23 13:47:49 +01:00
parent 9ccf298be9
commit 33071150c1
3 changed files with 37 additions and 1 deletions

View File

@ -49,6 +49,9 @@ type Config struct {
// Don't exit packer if salt-call returns an error code
NoExitOnFailure bool `mapstructure:"no_exit_on_failure"`
// Set the logging level for the salt highstate run
LogLevel string `mapstructure:"log_level"`
// Command line args passed onto salt-call
CmdArgs string ""
@ -123,6 +126,13 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
cmd_args.WriteString(" --retcode-passthrough")
}
if p.config.LogLevel == "" {
cmd_args.WriteString(" -l info")
} else {
cmd_args.WriteString(" -l ")
cmd_args.WriteString(p.config.LogLevel)
}
p.config.CmdArgs = cmd_args.String()
if errs != nil && len(errs.Errors) > 0 {
@ -224,7 +234,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
}
ui.Message("Running highstate")
cmd := &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("salt-call --local state.highstate -l info %s", p.config.CmdArgs))}
cmd := &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("salt-call --local state.highstate %s", p.config.CmdArgs))}
if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 {
if err == nil {
err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus)

View File

@ -237,3 +237,27 @@ func TestProvisionerPrepare_NoExitOnFailure(t *testing.T) {
t.Fatal("--retcode-passthrough should not be set in CmdArgs")
}
}
func TestProvisionerPrepare_LogLevel(t *testing.T) {
var p Provisioner
config := testConfig()
err := p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
if !strings.Contains(p.config.CmdArgs, "-l info") {
t.Fatal("-l info should be set in CmdArgs")
}
config["log_level"] = "debug"
err = p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
if !strings.Contains(p.config.CmdArgs, "-l debug") {
t.Fatal("-l debug should be set in CmdArgs")
}
}

View File

@ -71,3 +71,5 @@ Optional:
- `no_exit_on_failure` (boolean) - Packer will exit if the Salt highstate command
fails. Set this option to true to ignore Salt failures.
- `log_level` (string) - Set the logging level for the Salt highstate run.