github.com/sneal/packer@v0.5.2/builder/virtualbox/common/step_upload_version.go (about)

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/mitchellh/multistep"
     7  	"github.com/mitchellh/packer/packer"
     8  	"log"
     9  )
    10  
    11  // This step uploads a file containing the VirtualBox version, which
    12  // can be useful for various provisioning reasons.
    13  type StepUploadVersion struct {
    14  	Path string
    15  }
    16  
    17  func (s *StepUploadVersion) Run(state multistep.StateBag) multistep.StepAction {
    18  	comm := state.Get("communicator").(packer.Communicator)
    19  	driver := state.Get("driver").(Driver)
    20  	ui := state.Get("ui").(packer.Ui)
    21  
    22  	if s.Path == "" {
    23  		log.Println("VBoxVersionFile is empty. Not uploading.")
    24  		return multistep.ActionContinue
    25  	}
    26  
    27  	version, err := driver.Version()
    28  	if err != nil {
    29  		state.Put("error", fmt.Errorf("Error reading version for metadata upload: %s", err))
    30  		return multistep.ActionHalt
    31  	}
    32  
    33  	ui.Say(fmt.Sprintf("Uploading VirtualBox version info (%s)", version))
    34  	var data bytes.Buffer
    35  	data.WriteString(version)
    36  	if err := comm.Upload(s.Path, &data); err != nil {
    37  		state.Put("error", fmt.Errorf("Error uploading VirtualBox version: %s", err))
    38  		return multistep.ActionHalt
    39  	}
    40  
    41  	return multistep.ActionContinue
    42  }
    43  
    44  func (s *StepUploadVersion) Cleanup(state multistep.StateBag) {}