github.com/mitchellh/packer@v1.3.2/builder/virtualbox/common/step_upload_guest_additions.go (about)

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