2016-08-10 20:24:30 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-02-23 16:26:31 -05:00
|
|
|
sl "github.com/hashicorp/packer/common/shell-local"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2016-08-10 20:24:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func RunLocalCommands(commands []string, wrappedCommand CommandWrapper, ctx interpolate.Context, ui packer.Ui) error {
|
|
|
|
for _, rawCmd := range commands {
|
|
|
|
intCmd, err := interpolate.Render(rawCmd, &ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error interpolating: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
command, err := wrappedCommand(intCmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error wrapping command: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Executing command: %s", command))
|
2018-02-23 16:26:31 -05:00
|
|
|
comm := &sl.Communicator{
|
2018-06-08 16:14:55 -04:00
|
|
|
ExecuteCommand: []string{"sh", "-c", command},
|
2018-02-23 16:26:31 -05:00
|
|
|
}
|
2016-08-10 20:24:30 -04:00
|
|
|
cmd := &packer.RemoteCmd{Command: command}
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return fmt.Errorf("Error executing command: %s", err)
|
|
|
|
}
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Received non-zero exit code %d from command: %s",
|
|
|
|
cmd.ExitStatus,
|
|
|
|
command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|