2016-08-10 20:24:30 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
2019-04-03 11:14:55 -04:00
|
|
|
"context"
|
2016-08-10 20:24:30 -04:00
|
|
|
"fmt"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
|
|
|
sl "github.com/hashicorp/packer/packer-plugin-sdk/shell-local"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2016-08-10 20:24:30 -04:00
|
|
|
)
|
|
|
|
|
2019-10-23 23:57:17 -04:00
|
|
|
func RunLocalCommands(commands []string, wrappedCommand common.CommandWrapper, ictx interpolate.Context, ui packer.Ui) error {
|
2019-04-03 11:14:55 -04:00
|
|
|
ctx := context.TODO()
|
2016-08-10 20:24:30 -04:00
|
|
|
for _, rawCmd := range commands {
|
2019-03-29 11:50:02 -04:00
|
|
|
intCmd, err := interpolate.Render(rawCmd, &ictx)
|
2016-08-10 20:24:30 -04:00
|
|
|
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}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2016-08-10 20:24:30 -04:00
|
|
|
return fmt.Errorf("Error executing command: %s", err)
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 {
|
2016-08-10 20:24:30 -04:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Received non-zero exit code %d from command: %s",
|
2019-04-03 11:14:55 -04:00
|
|
|
cmd.ExitStatus(),
|
2016-08-10 20:24:30 -04:00
|
|
|
command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|