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

     1  package chroot
     2  
     3  import (
     4  	"github.com/mitchellh/multistep"
     5  	"github.com/mitchellh/packer/packer"
     6  )
     7  
     8  type postMountCommandsData struct {
     9  	Device    string
    10  	MountPath string
    11  }
    12  
    13  // StepPostMountCommands allows running arbitrary commands after mounting the
    14  // device, but prior to the bind mount and copy steps.
    15  type StepPostMountCommands struct {
    16  	Commands []string
    17  }
    18  
    19  func (s *StepPostMountCommands) Run(state multistep.StateBag) multistep.StepAction {
    20  	config := state.Get("config").(*Config)
    21  	device := state.Get("device").(string)
    22  	mountPath := state.Get("mount_path").(string)
    23  	ui := state.Get("ui").(packer.Ui)
    24  	wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
    25  
    26  	if len(s.Commands) == 0 {
    27  		return multistep.ActionContinue
    28  	}
    29  
    30  	ctx := config.ctx
    31  	ctx.Data = &postMountCommandsData{
    32  		Device:    device,
    33  		MountPath: mountPath,
    34  	}
    35  
    36  	ui.Say("Running post-mount commands...")
    37  	if err := RunLocalCommands(s.Commands, wrappedCommand, ctx, ui); err != nil {
    38  		state.Put("error", err)
    39  		ui.Error(err.Error())
    40  		return multistep.ActionHalt
    41  	}
    42  	return multistep.ActionContinue
    43  }
    44  
    45  func (s *StepPostMountCommands) Cleanup(state multistep.StateBag) {}