github.com/rothwerx/packer@v0.9.0/builder/virtualbox/common/step_upload_guest_additions.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  type guestAdditionsPathTemplate struct {
    14  	Version string
    15  }
    16  
    17  // This step uploads the guest additions ISO to the VM.
    18  type StepUploadGuestAdditions struct {
    19  	GuestAdditionsMode string
    20  	GuestAdditionsPath string
    21  	Ctx                interpolate.Context
    22  }
    23  
    24  func (s *StepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
    25  	comm := state.Get("communicator").(packer.Communicator)
    26  	driver := state.Get("driver").(Driver)
    27  	ui := state.Get("ui").(packer.Ui)
    28  
    29  	// If we're attaching then don't do this, since we attached.
    30  	if s.GuestAdditionsMode != GuestAdditionsModeUpload {
    31  		log.Println("Not uploading guest additions since mode is not upload")
    32  		return multistep.ActionContinue
    33  	}
    34  
    35  	// Get the guest additions path since we're doing it
    36  	guestAdditionsPath := state.Get("guest_additions_path").(string)
    37  
    38  	version, err := driver.Version()
    39  	if err != nil {
    40  		state.Put("error", fmt.Errorf("Error reading version for guest additions upload: %s", err))
    41  		return multistep.ActionHalt
    42  	}
    43  
    44  	f, err := os.Open(guestAdditionsPath)
    45  	if err != nil {
    46  		state.Put("error", fmt.Errorf("Error opening guest additions ISO: %s", err))
    47  		return multistep.ActionHalt
    48  	}
    49  
    50  	s.Ctx.Data = &guestAdditionsPathTemplate{
    51  		Version: version,
    52  	}
    53  
    54  	s.GuestAdditionsPath, err = interpolate.Render(s.GuestAdditionsPath, &s.Ctx)
    55  	if err != nil {
    56  		err := fmt.Errorf("Error preparing guest additions path: %s", err)
    57  		state.Put("error", err)
    58  		ui.Error(err.Error())
    59  		return multistep.ActionHalt
    60  	}
    61  
    62  	ui.Say("Uploading VirtualBox guest additions ISO...")
    63  	if err := comm.Upload(s.GuestAdditionsPath, f, nil); err != nil {
    64  		state.Put("error", fmt.Errorf("Error uploading guest additions: %s", err))
    65  		return multistep.ActionHalt
    66  	}
    67  
    68  	return multistep.ActionContinue
    69  }
    70  
    71  func (s *StepUploadGuestAdditions) Cleanup(state multistep.StateBag) {}