packer-cn/builder/vmware/common/step_upload_tools.go

72 lines
1.8 KiB
Go
Raw Normal View History

package common
import (
"context"
2013-07-01 12:39:19 -04:00
"fmt"
2015-05-27 17:16:28 -04:00
"os"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
)
type toolsUploadPathTemplate struct {
Flavor string
}
2014-05-10 00:12:14 -04:00
type StepUploadTools struct {
RemoteType string
ToolsUploadFlavor string
ToolsUploadPath string
2015-05-27 17:16:28 -04:00
Ctx interpolate.Context
}
func (c *StepUploadTools) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
2015-02-08 20:48:53 -05:00
if c.ToolsUploadFlavor == "" {
return multistep.ActionContinue
}
if c.RemoteType == "esx5" {
if err := driver.ToolsInstall(); err != nil {
state.Put("error", fmt.Errorf("Couldn't mount VMware tools ISO. Please check the 'guest_os_type' in your template.json."))
}
return multistep.ActionContinue
}
2013-08-31 15:50:25 -04:00
comm := state.Get("communicator").(packer.Communicator)
tools_source := state.Get("tools_upload_source").(string)
ui := state.Get("ui").(packer.Ui)
ui.Say(fmt.Sprintf("Uploading the '%s' VMware Tools", c.ToolsUploadFlavor))
f, err := os.Open(tools_source)
if err != nil {
2013-08-31 15:50:25 -04:00
state.Put("error", fmt.Errorf("Error opening VMware Tools ISO: %s", err))
return multistep.ActionHalt
}
2013-06-30 11:09:05 -04:00
defer f.Close()
2015-05-27 17:16:28 -04:00
c.Ctx.Data = &toolsUploadPathTemplate{
Flavor: c.ToolsUploadFlavor,
}
2015-05-27 17:16:28 -04:00
c.ToolsUploadPath, err = interpolate.Render(c.ToolsUploadPath, &c.Ctx)
if err != nil {
err := fmt.Errorf("Error preparing upload path: %s", err)
2013-08-31 15:50:25 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if err := comm.Upload(c.ToolsUploadPath, f, nil); err != nil {
err := fmt.Errorf("Error uploading VMware Tools: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (c *StepUploadTools) Cleanup(multistep.StateBag) {}