2013-12-24 01:07:43 -05:00
|
|
|
package common
|
2013-12-23 17:38:54 -05:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-12-23 17:38:54 -05:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
2015-08-10 17:52:34 -04:00
|
|
|
"strings"
|
2013-12-23 17:38:54 -05:00
|
|
|
|
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-23 17:38:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step configures a VMX by setting some default settings as well
|
|
|
|
// as taking in custom data to set, attaching a floppy if it exists, etc.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// vmx_path string
|
2018-07-02 07:33:55 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// display_name string - Value of the displayName key set in the VMX file
|
2013-12-23 17:38:54 -05:00
|
|
|
type StepConfigureVMX struct {
|
2018-11-09 13:54:31 -05:00
|
|
|
CustomData map[string]string
|
|
|
|
DisplayName string
|
|
|
|
SkipFloppy bool
|
|
|
|
VMName string
|
2013-12-23 17:38:54 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepConfigureVMX) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-01-27 09:11:55 -05:00
|
|
|
log.Printf("Configuring VMX...\n")
|
|
|
|
|
|
|
|
var err error
|
2013-12-23 17:38:54 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2017-01-27 09:11:55 -05:00
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
2018-01-31 16:19:31 -05:00
|
|
|
vmxData, err := ReadVMX(vmxPath)
|
2013-12-23 17:38:54 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error reading VMX file: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set this so that no dialogs ever appear from Packer.
|
|
|
|
vmxData["msg.autoanswer"] = "true"
|
|
|
|
|
|
|
|
// Create a new UUID for this VM, since it is a new VM
|
|
|
|
vmxData["uuid.action"] = "create"
|
|
|
|
|
|
|
|
// Delete any generated addresses since we want to regenerate
|
|
|
|
// them. Conflicting MAC addresses is a bad time.
|
|
|
|
addrRegex := regexp.MustCompile(`(?i)^ethernet\d+\.generatedAddress`)
|
2016-11-01 17:08:04 -04:00
|
|
|
for k := range vmxData {
|
2013-12-23 17:38:54 -05:00
|
|
|
if addrRegex.MatchString(k) {
|
|
|
|
delete(vmxData, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set custom data
|
|
|
|
for k, v := range s.CustomData {
|
|
|
|
log.Printf("Setting VMX: '%s' = '%s'", k, v)
|
2015-08-10 17:52:34 -04:00
|
|
|
k = strings.ToLower(k)
|
2013-12-23 17:38:54 -05:00
|
|
|
vmxData[k] = v
|
|
|
|
}
|
|
|
|
|
2014-06-08 19:02:21 -04:00
|
|
|
// Set a floppy disk, but only if we should
|
2014-09-04 00:27:54 -04:00
|
|
|
if !s.SkipFloppy {
|
2018-12-03 18:44:23 -05:00
|
|
|
// Grab list of temporary builder devices so we can append the floppy to it
|
|
|
|
tmpBuildDevices := state.Get("temporaryDevices").([]string)
|
|
|
|
|
2014-06-08 19:02:21 -04:00
|
|
|
// Set a floppy disk if we have one
|
|
|
|
if floppyPathRaw, ok := state.GetOk("floppy_path"); ok {
|
|
|
|
log.Println("Floppy path present, setting in VMX")
|
|
|
|
vmxData["floppy0.present"] = "TRUE"
|
|
|
|
vmxData["floppy0.filetype"] = "file"
|
|
|
|
vmxData["floppy0.filename"] = floppyPathRaw.(string)
|
2018-12-03 18:44:23 -05:00
|
|
|
|
|
|
|
// Add it to our list of build devices to later remove
|
|
|
|
tmpBuildDevices = append(tmpBuildDevices, "floppy0")
|
2014-06-08 19:02:21 -04:00
|
|
|
}
|
2018-12-03 18:44:23 -05:00
|
|
|
|
|
|
|
// Build the list back in our statebag
|
|
|
|
state.Put("temporaryDevices", tmpBuildDevices)
|
2013-12-23 17:38:54 -05:00
|
|
|
}
|
|
|
|
|
2018-11-09 13:54:31 -05:00
|
|
|
// If the build is taking place on a remote ESX server, the displayName
|
|
|
|
// will be needed for discovery of the VM's IP address and for export
|
|
|
|
// of the VM. The displayName key should always be set in the VMX file,
|
|
|
|
// so error if we don't find it and the user has not set it in the config.
|
|
|
|
if s.DisplayName != "" {
|
|
|
|
vmxData["displayname"] = s.DisplayName
|
|
|
|
state.Put("display_name", s.DisplayName)
|
|
|
|
} else {
|
|
|
|
displayName, ok := vmxData["displayname"]
|
|
|
|
if !ok { // Packer converts key names to lowercase!
|
|
|
|
err := fmt.Errorf("Error: Could not get value of displayName from VMX data")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
} else {
|
|
|
|
state.Put("display_name", displayName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 21:36:25 -05:00
|
|
|
// Set the extendedConfigFile setting for the .vmxf filename to the VMName
|
2019-03-01 14:16:00 -05:00
|
|
|
// if displayName is not set. This is needed so that when VMWare creates
|
2019-02-28 21:36:25 -05:00
|
|
|
// the .vmxf file it matches the displayName if it is set. When just using
|
|
|
|
// the sisplayName if it was empty VMWare would make a file named ".vmxf".
|
|
|
|
// The ".vmxf" file would not get deleted when the VM got deleted.
|
2019-03-01 14:16:00 -05:00
|
|
|
if s.DisplayName != "" {
|
|
|
|
vmxData["extendedconfigfile"] = fmt.Sprintf("%s.vmxf", s.DisplayName)
|
|
|
|
} else {
|
|
|
|
vmxData["extendedconfigfile"] = fmt.Sprintf("%s.vmxf", s.VMName)
|
|
|
|
}
|
2019-02-28 16:43:47 -05:00
|
|
|
|
2017-01-31 05:05:49 -05:00
|
|
|
err = WriteVMX(vmxPath, vmxData)
|
2017-01-27 09:11:55 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2013-12-23 17:38:54 -05:00
|
|
|
err := fmt.Errorf("Error writing VMX file: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepConfigureVMX) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|