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