2013-12-26 10:34:27 -05:00
|
|
|
package vmx
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-12-26 17:44:39 -05:00
|
|
|
"fmt"
|
2013-12-26 10:34:27 -05:00
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
|
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"
|
2013-12-26 10:34:27 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepCloneVMX takes a VMX file and clones the VM into the output directory.
|
|
|
|
type StepCloneVMX struct {
|
|
|
|
OutputDir string
|
|
|
|
Path string
|
|
|
|
VMName string
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepCloneVMX) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-26 16:39:41 -05:00
|
|
|
driver := state.Get("driver").(vmwcommon.Driver)
|
2013-12-26 10:34:27 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
vmxPath := filepath.Join(s.OutputDir, s.VMName+".vmx")
|
|
|
|
|
2013-12-26 16:39:41 -05:00
|
|
|
ui.Say("Cloning source VM...")
|
2013-12-26 10:34:27 -05:00
|
|
|
log.Printf("Cloning from: %s", s.Path)
|
|
|
|
log.Printf("Cloning to: %s", vmxPath)
|
2013-12-26 16:39:41 -05:00
|
|
|
if err := driver.Clone(vmxPath, s.Path); err != nil {
|
2013-12-26 10:34:27 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-12-26 17:44:39 -05:00
|
|
|
vmxData, err := vmwcommon.ReadVMX(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-10-20 23:58:12 -04:00
|
|
|
var diskName string
|
2015-08-10 17:52:34 -04:00
|
|
|
if _, ok := vmxData["scsi0:0.filename"]; ok {
|
|
|
|
diskName = vmxData["scsi0:0.filename"]
|
2014-10-27 19:53:25 -04:00
|
|
|
}
|
2015-08-10 17:52:34 -04:00
|
|
|
if _, ok := vmxData["sata0:0.filename"]; ok {
|
|
|
|
diskName = vmxData["sata0:0.filename"]
|
2014-10-27 19:53:25 -04:00
|
|
|
}
|
2015-08-10 17:52:34 -04:00
|
|
|
if _, ok := vmxData["ide0:0.filename"]; ok {
|
|
|
|
diskName = vmxData["ide0:0.filename"]
|
2014-10-28 03:58:46 -04:00
|
|
|
}
|
2014-10-27 19:53:25 -04:00
|
|
|
if diskName == "" {
|
2013-12-26 17:44:39 -05:00
|
|
|
err := fmt.Errorf("Root disk filename could not be found!")
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
var networkType string
|
|
|
|
if _, ok := vmxData["ethernet0.connectionType"]; ok {
|
|
|
|
networkType = vmxData["ethernet0.connectionType"]
|
|
|
|
}
|
|
|
|
if networkType == "" {
|
|
|
|
networkType = "nat"
|
|
|
|
log.Printf("Defaulting to network type : nat")
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("vmnetwork", networkType)
|
2013-12-26 17:44:39 -05:00
|
|
|
state.Put("full_disk_path", filepath.Join(s.OutputDir, diskName))
|
2013-12-26 10:34:27 -05:00
|
|
|
state.Put("vmx_path", vmxPath)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCloneVMX) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|