github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/virtualbox/common/step_upload_guest_additions.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"log"
     8  	"os"
     9  )
    10  
    11  type guestAdditionsPathTemplate struct {
    12  	Version string
    13  }
    14  
    15  // This step uploads the guest additions ISO to the VM.
    16  type StepUploadGuestAdditions struct {
    17  	GuestAdditionsMode string
    18  	GuestAdditionsPath string
    19  	Tpl                *packer.ConfigTemplate
    20  }
    21  
    22  func (s *StepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
    23  	comm := state.Get("communicator").(packer.Communicator)
    24  	driver := state.Get("driver").(Driver)
    25  	ui := state.Get("ui").(packer.Ui)
    26  
    27  	// If we're attaching then don't do this, since we attached.
    28  	if s.GuestAdditionsMode != GuestAdditionsModeUpload {
    29  		log.Println("Not uploading guest additions since mode is not upload")
    30  		return multistep.ActionContinue
    31  	}
    32  
    33  	// Get the guest additions path since we're doing it
    34  	guestAdditionsPath := state.Get("guest_additions_path").(string)
    35  
    36  	version, err := driver.Version()
    37  	if err != nil {
    38  		state.Put("error", fmt.Errorf("Error reading version for guest additions upload: %s", err))
    39  		return multistep.ActionHalt
    40  	}
    41  
    42  	f, err := os.Open(guestAdditionsPath)
    43  	if err != nil {
    44  		state.Put("error", fmt.Errorf("Error opening guest additions ISO: %s", err))
    45  		return multistep.ActionHalt
    46  	}
    47  
    48  	tplData := &guestAdditionsPathTemplate{
    49  		Version: version,
    50  	}
    51  
    52  	s.GuestAdditionsPath, err = s.Tpl.Process(s.GuestAdditionsPath, tplData)
    53  	if err != nil {
    54  		err := fmt.Errorf("Error preparing guest additions path: %s", err)
    55  		state.Put("error", err)
    56  		ui.Error(err.Error())
    57  		return multistep.ActionHalt
    58  	}
    59  
    60  	ui.Say("Uploading VirtualBox guest additions ISO...")
    61  	if err := comm.Upload(s.GuestAdditionsPath, f, nil); err != nil {
    62  		state.Put("error", fmt.Errorf("Error uploading guest additions: %s", err))
    63  		return multistep.ActionHalt
    64  	}
    65  
    66  	return multistep.ActionContinue
    67  }
    68  
    69  func (s *StepUploadGuestAdditions) Cleanup(state multistep.StateBag) {}