2014-04-06 13:21:22 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-05-27 16:49:31 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2014-04-06 13:21:22 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type commandTemplate struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// StepPrlctl is a step that executes additional `prlctl` commands as specified.
|
2016-12-11 17:37:41 -05:00
|
|
|
// by the template.
|
2014-04-06 13:21:22 -04:00
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// driver Driver
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
type StepPrlctl struct {
|
|
|
|
Commands [][]string
|
2015-05-27 16:49:31 -04:00
|
|
|
Ctx interpolate.Context
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Run executes `prlctl` commands.
|
2014-04-06 13:21:22 -04:00
|
|
|
func (s *StepPrlctl) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
if len(s.Commands) > 0 {
|
|
|
|
ui.Say("Executing custom prlctl commands...")
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:49:31 -04:00
|
|
|
s.Ctx.Data = &commandTemplate{
|
2014-04-06 13:21:22 -04:00
|
|
|
Name: vmName,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, originalCommand := range s.Commands {
|
|
|
|
command := make([]string, len(originalCommand))
|
|
|
|
copy(command, originalCommand)
|
|
|
|
|
|
|
|
for i, arg := range command {
|
|
|
|
var err error
|
2015-05-27 16:49:31 -04:00
|
|
|
command[i], err = interpolate.Render(arg, &s.Ctx)
|
2014-04-06 13:21:22 -04:00
|
|
|
if err != nil {
|
2016-12-11 16:12:55 -05:00
|
|
|
err = fmt.Errorf("Error preparing prlctl command: %s", err)
|
2014-04-06 13:21:22 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Executing: prlctl %s", strings.Join(command, " ")))
|
|
|
|
if err := driver.Prlctl(command...); err != nil {
|
2016-12-11 16:12:55 -05:00
|
|
|
err = fmt.Errorf("Error executing command: %s", err)
|
2014-04-06 13:21:22 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Cleanup does nothing.
|
2014-04-06 13:21:22 -04:00
|
|
|
func (s *StepPrlctl) Cleanup(state multistep.StateBag) {}
|