packer-cn/builder/vmware/builder.go

114 lines
2.4 KiB
Go
Raw Normal View History

package vmware
import (
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
2013-06-05 20:52:37 -04:00
"log"
"time"
)
const BuilderId = "mitchellh.vmware"
type Builder struct {
config config
2013-06-06 15:19:38 -04:00
driver Driver
runner multistep.Runner
}
type config struct {
DiskName string `mapstructure:"vmdk_name"`
ISOUrl string `mapstructure:"iso_url"`
VMName string `mapstructure:"vm_name"`
OutputDir string `mapstructure:"output_directory"`
HTTPDir string `mapstructure:"http_directory"`
BootCommand []string `mapstructure:"boot_command"`
BootWait time.Duration
SSHUser string `mapstructure:"ssh_user"`
SSHPassword string `mapstructure:"ssh_password"`
SSHWaitTimeout time.Duration
RawBootWait string `mapstructure:"boot_wait"`
RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"`
}
func (b *Builder) Prepare(raw interface{}) (err error) {
err = mapstructure.Decode(raw, &b.config)
if err != nil {
return
}
if b.config.DiskName == "" {
b.config.DiskName = "disk"
}
if b.config.VMName == "" {
b.config.VMName = "packer"
}
if b.config.OutputDir == "" {
b.config.OutputDir = "vmware"
}
if b.config.RawBootWait != "" {
b.config.BootWait, err = time.ParseDuration(b.config.RawBootWait)
if err != nil {
return
}
}
if b.config.RawSSHWaitTimeout == "" {
b.config.RawSSHWaitTimeout = "20m"
}
b.config.SSHWaitTimeout, err = time.ParseDuration(b.config.RawSSHWaitTimeout)
if err != nil {
return
}
2013-06-06 15:19:38 -04:00
b.driver, err = b.newDriver()
if err != nil {
return
}
return nil
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
steps := []multistep.Step{
&stepPrepareOutputDir{},
&stepCreateDisk{},
&stepCreateVMX{},
&stepHTTPServer{},
2013-06-05 18:12:43 -04:00
&stepRun{},
2013-06-05 20:15:16 -04:00
&stepTypeBootCommand{},
&stepWaitForSSH{},
2013-06-06 11:42:38 -04:00
&stepProvision{},
}
// Setup the state bag
state := make(map[string]interface{})
state["config"] = &b.config
2013-06-06 15:19:38 -04:00
state["driver"] = b.driver
state["hook"] = hook
state["ui"] = ui
// Run!
b.runner = &multistep.BasicRunner{Steps: steps}
b.runner.Run(state)
return nil
}
func (b *Builder) Cancel() {
2013-06-05 20:52:37 -04:00
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}
2013-06-06 15:19:38 -04:00
func (b *Builder) newDriver() (Driver, error) {
fusionAppPath := "/Applications/VMware Fusion.app"
return &Fusion5Driver{fusionAppPath}, nil
}