github.com/alouche/packer@v0.3.7/builder/virtualbox/step_vboxmanage.go (about)

     1  package virtualbox
     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  //
    19  // Produces:
    20  type stepVBoxManage struct{}
    21  
    22  func (s *stepVBoxManage) Run(state multistep.StateBag) multistep.StepAction {
    23  	config := state.Get("config").(*config)
    24  	driver := state.Get("driver").(Driver)
    25  	ui := state.Get("ui").(packer.Ui)
    26  	vmName := state.Get("vmName").(string)
    27  
    28  	if len(config.VBoxManage) > 0 {
    29  		ui.Say("Executing custom VBoxManage commands...")
    30  	}
    31  
    32  	tplData := &commandTemplate{
    33  		Name: vmName,
    34  	}
    35  
    36  	for _, originalCommand := range config.VBoxManage {
    37  		command := make([]string, len(originalCommand))
    38  		copy(command, originalCommand)
    39  
    40  		for i, arg := range command {
    41  			var err error
    42  			command[i], err = config.tpl.Process(arg, tplData)
    43  			if err != nil {
    44  				err := fmt.Errorf("Error preparing vboxmanage command: %s", err)
    45  				state.Put("error", err)
    46  				ui.Error(err.Error())
    47  				return multistep.ActionHalt
    48  			}
    49  		}
    50  
    51  		ui.Message(fmt.Sprintf("Executing: %s", strings.Join(command, " ")))
    52  		if err := driver.VBoxManage(command...); err != nil {
    53  			err := fmt.Errorf("Error executing command: %s", err)
    54  			state.Put("error", err)
    55  			ui.Error(err.Error())
    56  			return multistep.ActionHalt
    57  		}
    58  	}
    59  
    60  	return multistep.ActionContinue
    61  }
    62  
    63  func (s *stepVBoxManage) Cleanup(state multistep.StateBag) {}