2014-05-04 11:56:57 -04:00
|
|
|
package common
|
2013-06-24 02:09:52 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-09-18 21:15:46 -04:00
|
|
|
"log"
|
2013-06-24 02:09:52 -04:00
|
|
|
"os"
|
2015-05-27 17:01:08 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-06-24 02:09:52 -04:00
|
|
|
)
|
|
|
|
|
2013-06-24 02:14:19 -04:00
|
|
|
type guestAdditionsPathTemplate struct {
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
|
|
|
// This step uploads the guest additions ISO to the VM.
|
2014-05-04 11:56:57 -04:00
|
|
|
type StepUploadGuestAdditions struct {
|
|
|
|
GuestAdditionsMode string
|
|
|
|
GuestAdditionsPath string
|
2015-05-27 17:01:08 -04:00
|
|
|
Ctx interpolate.Context
|
2014-05-04 11:56:57 -04:00
|
|
|
}
|
2013-06-24 02:09:52 -04:00
|
|
|
|
2014-05-04 11:56:57 -04:00
|
|
|
func (s *StepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:44:58 -04:00
|
|
|
comm := state.Get("communicator").(packer.Communicator)
|
2014-05-04 11:56:57 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-24 02:09:52 -04:00
|
|
|
|
2013-09-18 21:15:46 -04:00
|
|
|
// If we're attaching then don't do this, since we attached.
|
2014-05-04 11:56:57 -04:00
|
|
|
if s.GuestAdditionsMode != GuestAdditionsModeUpload {
|
2013-11-02 04:22:56 -04:00
|
|
|
log.Println("Not uploading guest additions since mode is not upload")
|
2013-09-18 21:15:46 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-18 14:27:25 -05:00
|
|
|
// Get the guest additions path since we're doing it
|
|
|
|
guestAdditionsPath := state.Get("guest_additions_path").(string)
|
|
|
|
|
2013-06-24 02:14:19 -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 guest additions upload: %s", err))
|
2013-06-24 02:14:19 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-06-24 02:09:52 -04:00
|
|
|
f, err := os.Open(guestAdditionsPath)
|
|
|
|
if err != nil {
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error opening guest additions ISO: %s", err))
|
2013-06-24 02:09:52 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:01:08 -04:00
|
|
|
s.Ctx.Data = &guestAdditionsPathTemplate{
|
2013-06-24 02:14:19 -04:00
|
|
|
Version: version,
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:01:08 -04:00
|
|
|
s.GuestAdditionsPath, err = interpolate.Render(s.GuestAdditionsPath, &s.Ctx)
|
2013-08-08 19:00:47 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error preparing guest additions path: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-08-08 19:00:47 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-24 02:14:19 -04:00
|
|
|
|
2013-06-24 02:44:03 -04:00
|
|
|
ui.Say("Uploading VirtualBox guest additions ISO...")
|
2014-05-10 00:03:35 -04:00
|
|
|
if err := comm.Upload(s.GuestAdditionsPath, f, nil); err != nil {
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error uploading guest additions: %s", err))
|
2013-06-24 02:09:52 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-05-04 11:56:57 -04:00
|
|
|
func (s *StepUploadGuestAdditions) Cleanup(state multistep.StateBag) {}
|