github.phpd.cn/hashicorp/packer@v1.3.2/builder/amazon/chroot/step_pre_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 preMountCommandsData struct {
    11  	Device string
    12  }
    13  
    14  // StepPreMountCommands sets up the a new block device when building from scratch
    15  type StepPreMountCommands struct {
    16  	Commands []string
    17  }
    18  
    19  func (s *StepPreMountCommands) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    20  	config := state.Get("config").(*Config)
    21  	device := state.Get("device").(string)
    22  	ui := state.Get("ui").(packer.Ui)
    23  	wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
    24  
    25  	if len(s.Commands) == 0 {
    26  		return multistep.ActionContinue
    27  	}
    28  
    29  	ctx := config.ctx
    30  	ctx.Data = &preMountCommandsData{Device: device}
    31  
    32  	ui.Say("Running device setup commands...")
    33  	if err := RunLocalCommands(s.Commands, wrappedCommand, ctx, ui); err != nil {
    34  		state.Put("error", err)
    35  		ui.Error(err.Error())
    36  		return multistep.ActionHalt
    37  	}
    38  	return multistep.ActionContinue
    39  }
    40  
    41  func (s *StepPreMountCommands) Cleanup(state multistep.StateBag) {}