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