github.com/sneal/packer@v0.5.2/builder/vmware/iso/step_create_vmx.go (about)

     1  package iso
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
     7  	"github.com/mitchellh/packer/packer"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  )
    12  
    13  type vmxTemplateData struct {
    14  	Name     string
    15  	GuestOS  string
    16  	DiskName string
    17  	ISOPath  string
    18  }
    19  
    20  // This step creates the VMX file for the VM.
    21  //
    22  // Uses:
    23  //   config *config
    24  //   iso_path string
    25  //   ui     packer.Ui
    26  //
    27  // Produces:
    28  //   vmx_path string - The path to the VMX file.
    29  type stepCreateVMX struct {
    30  	tempDir string
    31  }
    32  
    33  func (s *stepCreateVMX) Run(state multistep.StateBag) multistep.StepAction {
    34  	config := state.Get("config").(*config)
    35  	isoPath := state.Get("iso_path").(string)
    36  	ui := state.Get("ui").(packer.Ui)
    37  
    38  	ui.Say("Building and writing VMX file")
    39  
    40  	tplData := &vmxTemplateData{
    41  		Name:     config.VMName,
    42  		GuestOS:  config.GuestOSType,
    43  		DiskName: config.DiskName,
    44  		ISOPath:  isoPath,
    45  	}
    46  
    47  	vmxTemplate := DefaultVMXTemplate
    48  	if config.VMXTemplatePath != "" {
    49  		f, err := os.Open(config.VMXTemplatePath)
    50  		if err != nil {
    51  			err := fmt.Errorf("Error reading VMX template: %s", err)
    52  			state.Put("error", err)
    53  			ui.Error(err.Error())
    54  			return multistep.ActionHalt
    55  		}
    56  		defer f.Close()
    57  
    58  		rawBytes, err := ioutil.ReadAll(f)
    59  		if err != nil {
    60  			err := fmt.Errorf("Error reading VMX template: %s", err)
    61  			state.Put("error", err)
    62  			ui.Error(err.Error())
    63  			return multistep.ActionHalt
    64  		}
    65  
    66  		vmxTemplate = string(rawBytes)
    67  	}
    68  
    69  	vmxContents, err := config.tpl.Process(vmxTemplate, tplData)
    70  	if err != nil {
    71  		err := fmt.Errorf("Error procesing VMX template: %s", err)
    72  		state.Put("error", err)
    73  		ui.Error(err.Error())
    74  		return multistep.ActionHalt
    75  	}
    76  
    77  	vmxDir := config.OutputDir
    78  	if config.RemoteType != "" {
    79  		// For remote builds, we just put the VMX in a temporary
    80  		// directory since it just gets uploaded anyways.
    81  		vmxDir, err = ioutil.TempDir("", "packer-vmx")
    82  		if err != nil {
    83  			err := fmt.Errorf("Error preparing VMX template: %s", err)
    84  			state.Put("error", err)
    85  			ui.Error(err.Error())
    86  			return multistep.ActionHalt
    87  		}
    88  
    89  		// Set the tempDir so we clean it up
    90  		s.tempDir = vmxDir
    91  	}
    92  
    93  	vmxPath := filepath.Join(vmxDir, config.VMName+".vmx")
    94  	if err := vmwcommon.WriteVMX(vmxPath, vmwcommon.ParseVMX(vmxContents)); err != nil {
    95  		err := fmt.Errorf("Error creating VMX file: %s", err)
    96  		state.Put("error", err)
    97  		ui.Error(err.Error())
    98  		return multistep.ActionHalt
    99  	}
   100  
   101  	state.Put("vmx_path", vmxPath)
   102  
   103  	return multistep.ActionContinue
   104  }
   105  
   106  func (s *stepCreateVMX) Cleanup(multistep.StateBag) {
   107  	if s.tempDir != "" {
   108  		os.RemoveAll(s.tempDir)
   109  	}
   110  }
   111  
   112  // This is the default VMX template used if no other template is given.
   113  // This is hardcoded here. If you wish to use a custom template please
   114  // do so by specifying in the builder configuration.
   115  const DefaultVMXTemplate = `
   116  .encoding = "UTF-8"
   117  bios.bootOrder = "hdd,CDROM"
   118  checkpoint.vmState = ""
   119  cleanShutdown = "TRUE"
   120  config.version = "8"
   121  displayName = "{{ .Name }}"
   122  ehci.pciSlotNumber = "34"
   123  ehci.present = "TRUE"
   124  ethernet0.addressType = "generated"
   125  ethernet0.bsdName = "en0"
   126  ethernet0.connectionType = "nat"
   127  ethernet0.displayName = "Ethernet"
   128  ethernet0.linkStatePropagation.enable = "FALSE"
   129  ethernet0.pciSlotNumber = "33"
   130  ethernet0.present = "TRUE"
   131  ethernet0.virtualDev = "e1000"
   132  ethernet0.wakeOnPcktRcv = "FALSE"
   133  extendedConfigFile = "{{ .Name }}.vmxf"
   134  floppy0.present = "FALSE"
   135  guestOS = "{{ .GuestOS }}"
   136  gui.fullScreenAtPowerOn = "FALSE"
   137  gui.viewModeAtPowerOn = "windowed"
   138  hgfs.linkRootShare = "TRUE"
   139  hgfs.mapRootShare = "TRUE"
   140  ide1:0.present = "TRUE"
   141  ide1:0.fileName = "{{ .ISOPath }}"
   142  ide1:0.deviceType = "cdrom-image"
   143  isolation.tools.hgfs.disable = "FALSE"
   144  memsize = "512"
   145  nvram = "{{ .Name }}.nvram"
   146  pciBridge0.pciSlotNumber = "17"
   147  pciBridge0.present = "TRUE"
   148  pciBridge4.functions = "8"
   149  pciBridge4.pciSlotNumber = "21"
   150  pciBridge4.present = "TRUE"
   151  pciBridge4.virtualDev = "pcieRootPort"
   152  pciBridge5.functions = "8"
   153  pciBridge5.pciSlotNumber = "22"
   154  pciBridge5.present = "TRUE"
   155  pciBridge5.virtualDev = "pcieRootPort"
   156  pciBridge6.functions = "8"
   157  pciBridge6.pciSlotNumber = "23"
   158  pciBridge6.present = "TRUE"
   159  pciBridge6.virtualDev = "pcieRootPort"
   160  pciBridge7.functions = "8"
   161  pciBridge7.pciSlotNumber = "24"
   162  pciBridge7.present = "TRUE"
   163  pciBridge7.virtualDev = "pcieRootPort"
   164  powerType.powerOff = "soft"
   165  powerType.powerOn = "soft"
   166  powerType.reset = "soft"
   167  powerType.suspend = "soft"
   168  proxyApps.publishToHost = "FALSE"
   169  replay.filename = ""
   170  replay.supported = "FALSE"
   171  scsi0.pciSlotNumber = "16"
   172  scsi0.present = "TRUE"
   173  scsi0.virtualDev = "lsilogic"
   174  scsi0:0.fileName = "{{ .DiskName }}.vmdk"
   175  scsi0:0.present = "TRUE"
   176  scsi0:0.redo = ""
   177  sound.startConnected = "FALSE"
   178  tools.syncTime = "TRUE"
   179  tools.upgrade.policy = "upgradeAtPowerCycle"
   180  usb.pciSlotNumber = "32"
   181  usb.present = "FALSE"
   182  virtualHW.productCompatibility = "hosted"
   183  virtualHW.version = "9"
   184  vmci0.id = "1861462627"
   185  vmci0.pciSlotNumber = "35"
   186  vmci0.present = "TRUE"
   187  vmotion.checkpointFBSize = "65536000"
   188  `