github.phpd.cn/hashicorp/packer@v1.3.2/builder/amazon/chroot/run_local_commands.go (about)

     1  package chroot
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sl "github.com/hashicorp/packer/common/shell-local"
     7  	"github.com/hashicorp/packer/packer"
     8  	"github.com/hashicorp/packer/template/interpolate"
     9  )
    10  
    11  func RunLocalCommands(commands []string, wrappedCommand CommandWrapper, ctx interpolate.Context, ui packer.Ui) error {
    12  	for _, rawCmd := range commands {
    13  		intCmd, err := interpolate.Render(rawCmd, &ctx)
    14  		if err != nil {
    15  			return fmt.Errorf("Error interpolating: %s", err)
    16  		}
    17  
    18  		command, err := wrappedCommand(intCmd)
    19  		if err != nil {
    20  			return fmt.Errorf("Error wrapping command: %s", err)
    21  		}
    22  
    23  		ui.Say(fmt.Sprintf("Executing command: %s", command))
    24  		comm := &sl.Communicator{
    25  			ExecuteCommand: []string{"sh", "-c", command},
    26  		}
    27  		cmd := &packer.RemoteCmd{Command: command}
    28  		if err := cmd.StartWithUi(comm, ui); err != nil {
    29  			return fmt.Errorf("Error executing command: %s", err)
    30  		}
    31  		if cmd.ExitStatus != 0 {
    32  			return fmt.Errorf(
    33  				"Received non-zero exit code %d from command: %s",
    34  				cmd.ExitStatus,
    35  				command)
    36  		}
    37  	}
    38  	return nil
    39  }