github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/amazon/chroot/run_local_commands.go (about)

     1  package chroot
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/packer/packer"
     7  	"github.com/mitchellh/packer/post-processor/shell-local"
     8  	"github.com/mitchellh/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 := &shell_local.Communicator{}
    25  		cmd := &packer.RemoteCmd{Command: command}
    26  		if err := cmd.StartWithUi(comm, ui); err != nil {
    27  			return fmt.Errorf("Error executing command: %s", err)
    28  		}
    29  		if cmd.ExitStatus != 0 {
    30  			return fmt.Errorf(
    31  				"Received non-zero exit code %d from command: %s",
    32  				cmd.ExitStatus,
    33  				command)
    34  		}
    35  	}
    36  	return nil
    37  }