2013-12-22 12:24:29 -05:00
|
|
|
package common
|
2013-06-23 23:58:22 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-23 23:58:22 -04:00
|
|
|
"fmt"
|
2015-05-27 17:01:08 -04:00
|
|
|
"strings"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-06-23 23:58:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type commandTemplate struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// This step executes additional VBoxManage commands as specified by the
|
|
|
|
// template.
|
|
|
|
//
|
|
|
|
// Uses:
|
2013-12-22 12:24:29 -05:00
|
|
|
// driver Driver
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
2013-06-23 23:58:22 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
2013-12-22 12:24:29 -05:00
|
|
|
type StepVBoxManage struct {
|
|
|
|
Commands [][]string
|
2015-05-27 17:01:08 -04:00
|
|
|
Ctx interpolate.Context
|
2013-12-22 12:24:29 -05:00
|
|
|
}
|
2013-06-23 23:58:22 -04:00
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepVBoxManage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-22 12:24:29 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-23 23:58:22 -04:00
|
|
|
|
2013-12-22 12:24:29 -05:00
|
|
|
if len(s.Commands) > 0 {
|
2013-06-23 23:58:22 -04:00
|
|
|
ui.Say("Executing custom VBoxManage commands...")
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:01:08 -04:00
|
|
|
s.Ctx.Data = &commandTemplate{
|
2013-06-23 23:58:22 -04:00
|
|
|
Name: vmName,
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:24:29 -05:00
|
|
|
for _, originalCommand := range s.Commands {
|
2013-06-23 23:58:22 -04:00
|
|
|
command := make([]string, len(originalCommand))
|
|
|
|
copy(command, originalCommand)
|
|
|
|
|
|
|
|
for i, arg := range command {
|
2013-08-08 19:00:47 -04:00
|
|
|
var err error
|
2015-05-27 17:01:08 -04:00
|
|
|
command[i], err = interpolate.Render(arg, &s.Ctx)
|
2013-08-08 19:00:47 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error preparing vboxmanage command: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-08-08 19:00:47 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-23 23:58:22 -04:00
|
|
|
}
|
|
|
|
|
2013-06-24 00:56:14 -04:00
|
|
|
ui.Message(fmt.Sprintf("Executing: %s", strings.Join(command, " ")))
|
2013-06-23 23:58:22 -04:00
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
|
|
|
err := fmt.Errorf("Error executing command: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-23 23:58:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:24:29 -05:00
|
|
|
func (s *StepVBoxManage) Cleanup(state multistep.StateBag) {}
|