github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/parallels/common/step_upload_parallels_tools.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  
     8  	"github.com/mitchellh/multistep"
     9  	"github.com/mitchellh/packer/packer"
    10  	"github.com/mitchellh/packer/template/interpolate"
    11  )
    12  
    13  // This step uploads the Parallels Tools ISO to the virtual machine.
    14  //
    15  // Uses:
    16  //   communicator packer.Communicator
    17  //   parallels_tools_path string
    18  //   ui packer.Ui
    19  //
    20  // Produces:
    21  type toolsPathTemplate struct {
    22  	Flavor string
    23  }
    24  
    25  // StepUploadParallelsTools is a step that uploads the Parallels Tools ISO
    26  // to the VM.
    27  //
    28  // Uses:
    29  //   communicator packer.Communicator
    30  //   parallels_tools_path string
    31  //   ui packer.Ui
    32  type StepUploadParallelsTools struct {
    33  	ParallelsToolsFlavor    string
    34  	ParallelsToolsGuestPath string
    35  	ParallelsToolsMode      string
    36  	Ctx                     interpolate.Context
    37  }
    38  
    39  // Run uploads the Parallels Tools ISO to the VM.
    40  func (s *StepUploadParallelsTools) Run(state multistep.StateBag) multistep.StepAction {
    41  	comm := state.Get("communicator").(packer.Communicator)
    42  	ui := state.Get("ui").(packer.Ui)
    43  
    44  	// If we're attaching then don't do this, since we attached.
    45  	if s.ParallelsToolsMode != ParallelsToolsModeUpload {
    46  		log.Println("Not uploading Parallels Tools since mode is not upload")
    47  		return multistep.ActionContinue
    48  	}
    49  
    50  	// Get the Paralells Tools path on the host machine
    51  	parallelsToolsPath := state.Get("parallels_tools_path").(string)
    52  
    53  	f, err := os.Open(parallelsToolsPath)
    54  	if err != nil {
    55  		state.Put("error", fmt.Errorf("Error opening Parallels Tools ISO: %s", err))
    56  		return multistep.ActionHalt
    57  	}
    58  	defer f.Close()
    59  
    60  	s.Ctx.Data = &toolsPathTemplate{
    61  		Flavor: s.ParallelsToolsFlavor,
    62  	}
    63  
    64  	s.ParallelsToolsGuestPath, err = interpolate.Render(s.ParallelsToolsGuestPath, &s.Ctx)
    65  	if err != nil {
    66  		err = fmt.Errorf("Error preparing Parallels Tools path: %s", err)
    67  		state.Put("error", err)
    68  		ui.Error(err.Error())
    69  		return multistep.ActionHalt
    70  	}
    71  
    72  	ui.Say(fmt.Sprintf("Uploading Parallels Tools for '%s' to path: '%s'",
    73  		s.ParallelsToolsFlavor, s.ParallelsToolsGuestPath))
    74  	if err := comm.Upload(s.ParallelsToolsGuestPath, f, nil); err != nil {
    75  		err = fmt.Errorf("Error uploading Parallels Tools: %s", err)
    76  		state.Put("error", err)
    77  		ui.Error(err.Error())
    78  		return multistep.ActionHalt
    79  	}
    80  
    81  	return multistep.ActionContinue
    82  }
    83  
    84  // Cleanup does nothing.
    85  func (s *StepUploadParallelsTools) Cleanup(state multistep.StateBag) {}