github.com/sneal/packer@v0.5.2/builder/virtualbox/iso/step_upload_guest_additions.go (about)

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