github.com/alouche/packer@v0.3.7/builder/virtualbox/step_upload_guest_additions.go (about)

     1  package virtualbox
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"os"
     8  )
     9  
    10  type guestAdditionsPathTemplate struct {
    11  	Version string
    12  }
    13  
    14  // This step uploads the guest additions ISO to the VM.
    15  type stepUploadGuestAdditions struct{}
    16  
    17  func (s *stepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
    18  	comm := state.Get("communicator").(packer.Communicator)
    19  	config := state.Get("config").(*config)
    20  	driver := state.Get("driver").(Driver)
    21  	guestAdditionsPath := state.Get("guest_additions_path").(string)
    22  	ui := state.Get("ui").(packer.Ui)
    23  
    24  	version, err := driver.Version()
    25  	if err != nil {
    26  		state.Put("error", fmt.Errorf("Error reading version for guest additions upload: %s", err))
    27  		return multistep.ActionHalt
    28  	}
    29  
    30  	f, err := os.Open(guestAdditionsPath)
    31  	if err != nil {
    32  		state.Put("error", fmt.Errorf("Error opening guest additions ISO: %s", err))
    33  		return multistep.ActionHalt
    34  	}
    35  
    36  	tplData := &guestAdditionsPathTemplate{
    37  		Version: version,
    38  	}
    39  
    40  	config.GuestAdditionsPath, err = config.tpl.Process(config.GuestAdditionsPath, tplData)
    41  	if err != nil {
    42  		err := fmt.Errorf("Error preparing guest additions path: %s", err)
    43  		state.Put("error", err)
    44  		ui.Error(err.Error())
    45  		return multistep.ActionHalt
    46  	}
    47  
    48  	ui.Say("Uploading VirtualBox guest additions ISO...")
    49  	if err := comm.Upload(config.GuestAdditionsPath, f); err != nil {
    50  		state.Put("error", fmt.Errorf("Error uploading guest additions: %s", err))
    51  		return multistep.ActionHalt
    52  	}
    53  
    54  	return multistep.ActionContinue
    55  }
    56  
    57  func (s *stepUploadGuestAdditions) Cleanup(state multistep.StateBag) {}