github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/parallels/common/step_prlctl.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  // StepPrlctl is a step that executes additional `prlctl` commands as specified.
    17  // by the template.
    18  //
    19  // Uses:
    20  //   driver Driver
    21  //   ui packer.Ui
    22  //   vmName string
    23  //
    24  // Produces:
    25  type StepPrlctl struct {
    26  	Commands [][]string
    27  	Ctx      interpolate.Context
    28  }
    29  
    30  // Run executes `prlctl` commands.
    31  func (s *StepPrlctl) Run(state multistep.StateBag) multistep.StepAction {
    32  	driver := state.Get("driver").(Driver)
    33  	ui := state.Get("ui").(packer.Ui)
    34  	vmName := state.Get("vmName").(string)
    35  
    36  	if len(s.Commands) > 0 {
    37  		ui.Say("Executing custom prlctl commands...")
    38  	}
    39  
    40  	s.Ctx.Data = &commandTemplate{
    41  		Name: vmName,
    42  	}
    43  
    44  	for _, originalCommand := range s.Commands {
    45  		command := make([]string, len(originalCommand))
    46  		copy(command, originalCommand)
    47  
    48  		for i, arg := range command {
    49  			var err error
    50  			command[i], err = interpolate.Render(arg, &s.Ctx)
    51  			if err != nil {
    52  				err = fmt.Errorf("Error preparing prlctl command: %s", err)
    53  				state.Put("error", err)
    54  				ui.Error(err.Error())
    55  				return multistep.ActionHalt
    56  			}
    57  		}
    58  
    59  		ui.Message(fmt.Sprintf("Executing: prlctl %s", strings.Join(command, " ")))
    60  		if err := driver.Prlctl(command...); err != nil {
    61  			err = fmt.Errorf("Error executing command: %s", err)
    62  			state.Put("error", err)
    63  			ui.Error(err.Error())
    64  			return multistep.ActionHalt
    65  		}
    66  	}
    67  
    68  	return multistep.ActionContinue
    69  }
    70  
    71  // Cleanup does nothing.
    72  func (s *StepPrlctl) Cleanup(state multistep.StateBag) {}