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