Merge pull request #6987 from hashicorp/6624

6624
This commit is contained in:
Adrien Delorme 2018-11-12 15:31:36 +01:00 committed by GitHub
commit 78ac943679
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"
"time"
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
@ -162,6 +163,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs, fmt.Errorf("vmx_template_path is invalid: %s", err))
}
} else {
warn := b.checkForVMXTemplateAndVMXDataCollisions()
if warn != "" {
warnings = append(warnings, warn)
}
}
if b.config.Network == "" {
@ -399,6 +405,44 @@ func (b *Builder) Cancel() {
}
}
// Validate the vmx_data option against the default vmx template to warn
// user if anything is being overridden.
func (b *Builder) checkForVMXTemplateAndVMXDataCollisions() string {
if b.config.VMXTemplatePath != "" {
return ""
}
var overridden []string
tplLines := strings.Split(DefaultVMXTemplate, "\n")
tplLines = append(tplLines,
fmt.Sprintf("%s0:0.present", strings.ToLower(b.config.DiskAdapterType)),
fmt.Sprintf("%s0:0.fileName", strings.ToLower(b.config.DiskAdapterType)),
fmt.Sprintf("%s0:0.deviceType", strings.ToLower(b.config.DiskAdapterType)),
fmt.Sprintf("%s0:1.present", strings.ToLower(b.config.DiskAdapterType)),
fmt.Sprintf("%s0:1.fileName", strings.ToLower(b.config.DiskAdapterType)),
fmt.Sprintf("%s0:1.deviceType", strings.ToLower(b.config.DiskAdapterType)),
)
for _, line := range tplLines {
if strings.Contains(line, `{{`) {
key := line[:strings.Index(line, " =")]
if _, ok := b.config.VMXData[key]; ok {
overridden = append(overridden, key)
}
}
}
if len(overridden) > 0 {
warnings := fmt.Sprintf("Your vmx data contains the following "+
"variable(s), which Packer normally sets when it generates its "+
"own default vmx template. This may cause your build to fail or "+
"behave unpredictably: %s", strings.Join(overridden, ", "))
return warnings
}
return ""
}
// Make sure custom vmx template exists and that data can be read from it
func (b *Builder) validateVMXTemplatePath() error {
f, err := os.Open(b.config.VMXTemplatePath)
if err != nil {

View File

@ -441,6 +441,31 @@ func TestBuilderPrepare_VNCPort(t *testing.T) {
}
}
func TestBuilderCheckCollisions(t *testing.T) {
config := testConfig()
config["vmx_data"] = map[string]string{
"no.collision": "awesomesauce",
"ide0:0.fileName": "is a collision",
"displayName": "also a collision",
}
{
var b Builder
warns, _ := b.Prepare(config)
if len(warns) != 1 {
t.Fatalf("Should have warning about two collisions.")
}
}
{
config["vmx_template_path"] = "some/path.vmx"
var b Builder
warns, _ := b.Prepare(config)
if len(warns) != 0 {
t.Fatalf("Should not check for collisions with custom template.")
}
}
}
func TestBuilderPrepare_CommConfig(t *testing.T) {
// Test Winrm
{

View File

@ -28,10 +28,10 @@ type vmxTemplateData struct {
SATA_Present string
NVME_Present string
DiskName string
DiskType string
CDROMType string
CDROMType_MasterSlave string
DiskName string
DiskType string
CDROMType string
CDROMType_PrimarySecondary string
Network_Type string
Network_Device string
@ -383,10 +383,10 @@ func (s *stepCreateVMX) Run(_ context.Context, state multistep.StateBag) multist
SATA_Present: "FALSE",
NVME_Present: "FALSE",
DiskType: "scsi",
HDD_BootOrder: "scsi0:0",
CDROMType: "ide",
CDROMType_MasterSlave: "0",
DiskType: "scsi",
HDD_BootOrder: "scsi0:0",
CDROMType: "ide",
CDROMType_PrimarySecondary: "0",
Network_Adapter: "e1000",
@ -406,20 +406,20 @@ func (s *stepCreateVMX) Run(_ context.Context, state multistep.StateBag) multist
case "ide":
templateData.DiskType = "ide"
templateData.CDROMType = "ide"
templateData.CDROMType_MasterSlave = "1"
templateData.CDROMType_PrimarySecondary = "1"
templateData.HDD_BootOrder = "ide0:0"
case "sata":
templateData.SATA_Present = "TRUE"
templateData.DiskType = "sata"
templateData.CDROMType = "sata"
templateData.CDROMType_MasterSlave = "1"
templateData.CDROMType_PrimarySecondary = "1"
templateData.HDD_BootOrder = "sata0:0"
case "nvme":
templateData.NVME_Present = "TRUE"
templateData.DiskType = "nvme"
templateData.SATA_Present = "TRUE"
templateData.CDROMType = "sata"
templateData.CDROMType_MasterSlave = "0"
templateData.CDROMType_PrimarySecondary = "0"
templateData.HDD_BootOrder = "nvme0:0"
case "scsi":
diskAdapterType = "lsilogic"
@ -429,20 +429,20 @@ func (s *stepCreateVMX) Run(_ context.Context, state multistep.StateBag) multist
templateData.SCSI_diskAdapterType = diskAdapterType
templateData.DiskType = "scsi"
templateData.CDROMType = "ide"
templateData.CDROMType_MasterSlave = "0"
templateData.CDROMType_PrimarySecondary = "0"
templateData.HDD_BootOrder = "scsi0:0"
}
/// Handle the cdrom adapter type. If the disk adapter type and the
// cdrom adapter type are the same, then ensure that the cdrom is the
// slave device on whatever bus the disk adapter is on.
// secondary device on whatever bus the disk adapter is on.
cdromAdapterType := strings.ToLower(config.CdromAdapterType)
if cdromAdapterType == "" {
cdromAdapterType = templateData.CDROMType
} else if cdromAdapterType == diskAdapterType {
templateData.CDROMType_MasterSlave = "1"
templateData.CDROMType_PrimarySecondary = "1"
} else {
templateData.CDROMType_MasterSlave = "0"
templateData.CDROMType_PrimarySecondary = "0"
}
switch cdromAdapterType {
@ -686,9 +686,9 @@ nvme0.present = "{{ .NVME_Present }}"
{{ .DiskType }}0:0.present = "TRUE"
{{ .DiskType }}0:0.fileName = "{{ .DiskName }}.vmdk"
{{ .CDROMType }}0:{{ .CDROMType_MasterSlave }}.present = "TRUE"
{{ .CDROMType }}0:{{ .CDROMType_MasterSlave }}.fileName = "{{ .ISOPath }}"
{{ .CDROMType }}0:{{ .CDROMType_MasterSlave }}.deviceType = "cdrom-image"
{{ .CDROMType }}0:{{ .CDROMType_PrimarySecondary }}.present = "TRUE"
{{ .CDROMType }}0:{{ .CDROMType_PrimarySecondary }}.fileName = "{{ .ISOPath }}"
{{ .CDROMType }}0:{{ .CDROMType_PrimarySecondary }}.deviceType = "cdrom-image"
isolation.tools.hgfs.disable = "FALSE"
memsize = "512"