packer-cn/builder/virtualbox/common/step_vboxmanage.go

84 lines
1.9 KiB
Go
Raw Normal View History

2013-12-22 12:24:29 -05:00
package common
import (
"context"
"fmt"
"github.com/hashicorp/packer/common"
"strings"
"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"
)
type commandTemplate struct {
// HTTPIP is the HTTP server's IP address.
HTTPIP string
// HTTPPort is the HTTP server port.
HTTPPort int
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
//
// Produces:
2013-12-22 12:24:29 -05:00
type StepVBoxManage struct {
Commands [][]string
Ctx interpolate.Context
2013-12-22 12:24:29 -05:00
}
func (s *StepVBoxManage) Run(ctx 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-12-22 12:24:29 -05:00
if len(s.Commands) > 0 {
ui.Say("Executing custom VBoxManage commands...")
}
hostIP := common.GetHTTPIP()
httpPort := state.Get("http_port").(int)
s.Ctx.Data = &commandTemplate{
Name: vmName,
HTTPIP: hostIP,
HTTPPort: httpPort,
}
2013-12-22 12:24:29 -05:00
for _, originalCommand := range s.Commands {
command := make([]string, len(originalCommand))
copy(command, originalCommand)
for i, arg := range command {
var err error
command[i], err = interpolate.Render(arg, &s.Ctx)
if err != nil {
err := fmt.Errorf("Error preparing vboxmanage command: %s", err)
2013-08-31 15:44:58 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
ui.Message(fmt.Sprintf("Executing: %s", strings.Join(command, " ")))
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)
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) {}