github.com/sneal/packer@v0.5.2/builder/virtualbox/common/step_vboxmanage.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"strings"
     8  )
     9  
    10  type commandTemplate struct {
    11  	Name string
    12  }
    13  
    14  // This step executes additional VBoxManage commands as specified by the
    15  // template.
    16  //
    17  // Uses:
    18  //   driver Driver
    19  //   ui packer.Ui
    20  //   vmName string
    21  //
    22  // Produces:
    23  type StepVBoxManage struct {
    24  	Commands [][]string
    25  	Tpl      *packer.ConfigTemplate
    26  }
    27  
    28  func (s *StepVBoxManage) Run(state multistep.StateBag) multistep.StepAction {
    29  	driver := state.Get("driver").(Driver)
    30  	ui := state.Get("ui").(packer.Ui)
    31  	vmName := state.Get("vmName").(string)
    32  
    33  	if len(s.Commands) > 0 {
    34  		ui.Say("Executing custom VBoxManage commands...")
    35  	}
    36  
    37  	tplData := &commandTemplate{
    38  		Name: vmName,
    39  	}
    40  
    41  	for _, originalCommand := range s.Commands {
    42  		command := make([]string, len(originalCommand))
    43  		copy(command, originalCommand)
    44  
    45  		for i, arg := range command {
    46  			var err error
    47  			command[i], err = s.Tpl.Process(arg, tplData)
    48  			if err != nil {
    49  				err := fmt.Errorf("Error preparing vboxmanage command: %s", err)
    50  				state.Put("error", err)
    51  				ui.Error(err.Error())
    52  				return multistep.ActionHalt
    53  			}
    54  		}
    55  
    56  		ui.Message(fmt.Sprintf("Executing: %s", strings.Join(command, " ")))
    57  		if err := driver.VBoxManage(command...); err != nil {
    58  			err := fmt.Errorf("Error executing command: %s", err)
    59  			state.Put("error", err)
    60  			ui.Error(err.Error())
    61  			return multistep.ActionHalt
    62  		}
    63  	}
    64  
    65  	return multistep.ActionContinue
    66  }
    67  
    68  func (s *StepVBoxManage) Cleanup(state multistep.StateBag) {}