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