2013-12-22 14:50:29 -05:00
|
|
|
package common
|
2013-06-24 01:44:58 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-24 01:44:58 -04:00
|
|
|
"fmt"
|
2018-01-22 18:32:33 -05:00
|
|
|
"log"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2013-06-24 01:44:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step uploads a file containing the VirtualBox version, which
|
|
|
|
// can be useful for various provisioning reasons.
|
2013-12-22 14:50:29 -05:00
|
|
|
type StepUploadVersion struct {
|
|
|
|
Path string
|
|
|
|
}
|
2013-06-24 01:44:58 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepUploadVersion) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 18:10:00 -05:00
|
|
|
comm := state.Get("communicator").(packersdk.Communicator)
|
2013-12-22 14:50:29 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-06-24 01:44:58 -04:00
|
|
|
|
2013-12-22 14:50:29 -05:00
|
|
|
if s.Path == "" {
|
2013-06-24 01:46:57 -04:00
|
|
|
log.Println("VBoxVersionFile is empty. Not uploading.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-06-24 01:44:58 -04:00
|
|
|
version, err := driver.Version()
|
|
|
|
if err != nil {
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error reading version for metadata upload: %s", err))
|
2013-06-24 01:44:58 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Uploading VirtualBox version info (%s)", version))
|
|
|
|
var data bytes.Buffer
|
|
|
|
data.WriteString(version)
|
2014-05-10 00:03:35 -04:00
|
|
|
if err := comm.Upload(s.Path, &data, nil); err != nil {
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error uploading VirtualBox version: %s", err))
|
2013-06-24 01:44:58 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-22 14:50:29 -05:00
|
|
|
func (s *StepUploadVersion) Cleanup(state multistep.StateBag) {}
|