-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
step_upload_tools.go
70 lines (57 loc) · 1.78 KB
/
step_upload_tools.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package common
import (
"fmt"
"os"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/mitchellh/multistep"
)
type toolsUploadPathTemplate struct {
Flavor string
}
type StepUploadTools struct {
RemoteType string
ToolsUploadFlavor string
ToolsUploadPath string
Ctx interpolate.Context
}
func (c *StepUploadTools) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
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
}
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 {
state.Put("error", fmt.Errorf("Error opening VMware Tools ISO: %s", err))
return multistep.ActionHalt
}
defer f.Close()
c.Ctx.Data = &toolsUploadPathTemplate{
Flavor: c.ToolsUploadFlavor,
}
c.ToolsUploadPath, err = interpolate.Render(c.ToolsUploadPath, &c.Ctx)
if err != nil {
err := fmt.Errorf("Error preparing upload path: %s", err)
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) {}