From 88ec874c323fd4b4120c30dcc166f26a9ba4e4dd Mon Sep 17 00:00:00 2001 From: Nathan Hartwell Date: Fri, 9 May 2014 09:08:41 -0500 Subject: [PATCH] Adding disable_sudo support to salt-masterless provisioner This is already present in some other provisioners and is helpful when using a builder that gives you root access. --- provisioner/salt-masterless/provisioner.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/provisioner/salt-masterless/provisioner.go b/provisioner/salt-masterless/provisioner.go index d13ca1f84..1f65b7ac5 100644 --- a/provisioner/salt-masterless/provisioner.go +++ b/provisioner/salt-masterless/provisioner.go @@ -19,6 +19,8 @@ type Config struct { SkipBootstrap bool `mapstructure:"skip_bootstrap"` BootstrapArgs string `mapstructure:"bootstrap_args"` + DisableSudo bool `mapstructure:"disable_sudo"` + // Local path to the minion config MinionConfig string `mapstructure:"minion_config"` @@ -108,7 +110,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { ui.Say("Provisioning with Salt...") if !p.config.SkipBootstrap { cmd := &packer.RemoteCmd{ - Command: fmt.Sprintf("wget -O - http://bootstrap.saltstack.org | sudo sh -s %s", p.config.BootstrapArgs), + Command: fmt.Sprintf("wget -O - http://bootstrap.saltstack.org | %s %s", p.sudo("sh -s"), p.config.BootstrapArgs), } ui.Message(fmt.Sprintf("Installing Salt with command %s", cmd)) if err = cmd.StartWithUi(comm, ui); err != nil { @@ -133,7 +135,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } ui.Message(fmt.Sprintf("Moving %s/minion to /etc/salt/minion", p.config.TempConfigDir)) - cmd = &packer.RemoteCmd{Command: fmt.Sprintf("sudo mv %s/minion /etc/salt/minion", p.config.TempConfigDir)} + cmd = &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("mv %s/minion /etc/salt/minion", p.config.TempConfigDir))} if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 { if err == nil { err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus) @@ -150,7 +152,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } ui.Message(fmt.Sprintf("Moving %s/states to /srv/salt", p.config.TempConfigDir)) - cmd = &packer.RemoteCmd{Command: fmt.Sprintf("sudo mv %s/states /srv/salt", p.config.TempConfigDir)} + cmd = &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("mv %s/states /srv/salt", p.config.TempConfigDir))} if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 { if err == nil { err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus) @@ -167,7 +169,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } ui.Message(fmt.Sprintf("Moving %s/pillar to /srv/pillar", p.config.TempConfigDir)) - cmd = &packer.RemoteCmd{Command: fmt.Sprintf("sudo mv %s/pillar /srv/pillar", p.config.TempConfigDir)} + cmd = &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("mv %s/pillar /srv/pillar", p.config.TempConfigDir))} if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 { if err == nil { err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus) @@ -178,7 +180,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } ui.Message("Running highstate") - cmd = &packer.RemoteCmd{Command: "sudo salt-call --local state.highstate -l info"} + cmd = &packer.RemoteCmd{Command: p.sudo("salt-call --local state.highstate -l info")} if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 { if err == nil { err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus) @@ -196,6 +198,15 @@ func (p *Provisioner) Cancel() { os.Exit(0) } +// Prepends sudo to supplied command if config says to +func (p *Provisioner) sudo(cmd string) string { + if p.config.DisableSudo { + return cmd + } + + return "sudo " + cmd +} + func uploadMinionConfig(comm packer.Communicator, dst string, src string) error { f, err := os.Open(src) if err != nil {