2013-07-08 23:56:23 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2013-07-20 22:08:20 -04:00
|
|
|
"regexp"
|
2013-07-08 23:56:23 -04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step cleans up the VMX by removing or changing this prior to
|
|
|
|
// being ready for use.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmx_path string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
|
|
|
type stepCleanVMX struct{}
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
func (s stepCleanVMX) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
isoPath := state.Get("iso_path").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
2013-07-08 23:56:23 -04:00
|
|
|
|
2013-07-20 22:08:20 -04:00
|
|
|
ui.Say("Cleaning VMX prior to finishing up...")
|
|
|
|
|
2013-07-08 23:56:23 -04:00
|
|
|
vmxData, err := s.readVMX(vmxPath)
|
|
|
|
if err != nil {
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error reading VMX: %s", err))
|
2013-07-08 23:56:23 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
if _, ok := state.GetOk("floppy_path"); ok {
|
2013-07-19 20:46:26 -04:00
|
|
|
// Delete the floppy0 entries so the floppy is no longer mounted
|
2013-07-20 22:08:20 -04:00
|
|
|
ui.Message("Unmounting floppy from VMX...")
|
2013-07-19 20:46:26 -04:00
|
|
|
for k, _ := range vmxData {
|
|
|
|
if strings.HasPrefix(k, "floppy0.") {
|
|
|
|
log.Printf("Deleting key: %s", k)
|
|
|
|
delete(vmxData, k)
|
|
|
|
}
|
2013-07-08 23:56:23 -04:00
|
|
|
}
|
2013-07-19 20:46:26 -04:00
|
|
|
vmxData["floppy0.present"] = "FALSE"
|
2013-07-08 23:56:23 -04:00
|
|
|
}
|
2013-07-19 20:46:26 -04:00
|
|
|
|
2013-09-02 06:46:47 -04:00
|
|
|
ui.Message("Detaching ISO from CD-ROM device...")
|
2013-07-20 22:08:20 -04:00
|
|
|
devRe := regexp.MustCompile(`^ide\d:\d\.`)
|
|
|
|
for k, _ := range vmxData {
|
|
|
|
match := devRe.FindString(k)
|
|
|
|
if match == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-12-05 16:36:34 -05:00
|
|
|
filenameKey := match + "filename"
|
2013-07-20 22:08:20 -04:00
|
|
|
if filename, ok := vmxData[filenameKey]; ok {
|
|
|
|
if filename == isoPath {
|
|
|
|
// Change the CD-ROM device back to auto-detect to eject
|
|
|
|
vmxData[filenameKey] = "auto detect"
|
2013-12-05 16:36:34 -05:00
|
|
|
vmxData[match+"devicetype"] = "cdrom-raw"
|
2013-07-20 22:08:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-08 23:56:23 -04:00
|
|
|
|
|
|
|
// Rewrite the VMX
|
|
|
|
if err := WriteVMX(vmxPath, vmxData); err != nil {
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error writing VMX: %s", err))
|
2013-07-08 23:56:23 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
func (stepCleanVMX) Cleanup(multistep.StateBag) {}
|
2013-07-08 23:56:23 -04:00
|
|
|
|
|
|
|
func (stepCleanVMX) readVMX(vmxPath string) (map[string]string, error) {
|
|
|
|
vmxF, err := os.Open(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer vmxF.Close()
|
|
|
|
|
|
|
|
vmxBytes, err := ioutil.ReadAll(vmxF)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseVMX(string(vmxBytes)), nil
|
|
|
|
}
|