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

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