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

     1  package virtualbox
     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  
    15  func (s *stepUploadVersion) Run(state multistep.StateBag) multistep.StepAction {
    16  	comm := state.Get("communicator").(packer.Communicator)
    17  	config := state.Get("config").(*config)
    18  	driver := state.Get("driver").(Driver)
    19  	ui := state.Get("ui").(packer.Ui)
    20  
    21  	if config.VBoxVersionFile == "" {
    22  		log.Println("VBoxVersionFile is empty. Not uploading.")
    23  		return multistep.ActionContinue
    24  	}
    25  
    26  	version, err := driver.Version()
    27  	if err != nil {
    28  		state.Put("error", fmt.Errorf("Error reading version for metadata upload: %s", err))
    29  		return multistep.ActionHalt
    30  	}
    31  
    32  	ui.Say(fmt.Sprintf("Uploading VirtualBox version info (%s)", version))
    33  	var data bytes.Buffer
    34  	data.WriteString(version)
    35  	if err := comm.Upload(config.VBoxVersionFile, &data); err != nil {
    36  		state.Put("error", fmt.Errorf("Error uploading VirtualBox version: %s", err))
    37  		return multistep.ActionHalt
    38  	}
    39  
    40  	return multistep.ActionContinue
    41  }
    42  
    43  func (s *stepUploadVersion) Cleanup(state multistep.StateBag) {}