Esempio n. 1
0
func (s *StepCloneVMX) Run(state multistep.StateBag) multistep.StepAction {
	driver := state.Get("driver").(vmwcommon.Driver)
	ui := state.Get("ui").(packer.Ui)

	vmxPath := filepath.Join(s.OutputDir, s.VMName+".vmx")

	ui.Say("Cloning source VM...")
	log.Printf("Cloning from: %s", s.Path)
	log.Printf("Cloning to: %s", vmxPath)
	if err := driver.Clone(vmxPath, s.Path); err != nil {
		state.Put("error", err)
		return multistep.ActionHalt
	}

	vmxData, err := vmwcommon.ReadVMX(vmxPath)
	if err != nil {
		state.Put("error", err)
		return multistep.ActionHalt
	}

	diskName, ok := vmxData["scsi0:0.filename"]
	if !ok {
		err := fmt.Errorf("Root disk filename could not be found!")
		state.Put("error", err)
		return multistep.ActionHalt
	}

	state.Put("full_disk_path", filepath.Join(s.OutputDir, diskName))
	state.Put("vmx_path", vmxPath)
	return multistep.ActionContinue
}
func (p *PostProcessor) RemoveFloppy(vmx string, ui packer.Ui) error {
	ui.Message(fmt.Sprintf("Removing floppy from %s", vmx))
	vmxData, err := vmwarecommon.ReadVMX(vmx)
	if err != nil {
		return err
	}
	for k, _ := range vmxData {
		if strings.HasPrefix(k, "floppy0.") {
			delete(vmxData, k)
		}
	}
	vmxData["floppy0.present"] = "FALSE"
	if err := vmwarecommon.WriteVMX(vmx, vmxData); err != nil {
		return err
	}
	return nil
}
func (p *OVFPostProcessor) stripDrives(vmx string) error {
	vmxData, err := vmwcommon.ReadVMX(vmx)
	if err != nil {
		return err
	}
	for k, _ := range vmxData {
		if strings.HasPrefix(k, "floppy0.") {
			delete(vmxData, k)
		}
		if strings.HasPrefix(k, "ide1:0.file") {
			delete(vmxData, k)
		}
	}
	vmxData["floppy0.present"] = "FALSE"
	vmxData["ide1:0.present"] = "FALSE"
	if err := vmwcommon.WriteVMX(vmx, vmxData); err != nil {
		return err
	}
	return nil
}
func (p *PostProcessor) RemoveOpticalDrive(vmx string, ui packer.Ui, artifact packer.Artifact) error {
	ui.Message(fmt.Sprintf("Removing optical drive from %s", vmx))
	vmxData, err := vmwarecommon.ReadVMX(vmx)
	if err != nil {
		return err
	}

	for k, _ := range vmxData {
		if strings.HasPrefix(k, "ide1:0.file") {
			delete(vmxData, k)
		}
	}

	vmxData["ide1:0.present"] = "FALSE"

	if err := vmwarecommon.WriteVMX(vmx, vmxData); err != nil {
		return err
	}
	return nil
}
func (p *VMwarevCenterProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
	// Create the metadata
	metadata = map[string]interface{}{"provider": "vcenter"}

	vmx := ""
	ovf := ""
	basepath := ""
	for _, path := range artifact.Files() {
		if strings.HasSuffix(path, ".vmx") {
			vmx = path
			ovf = filepath.Base(vmx[:len(vmx)-4] + ".ovf")
			basepath = filepath.Dir(path) + "/ovf"
		}
	}

	vmxData, err := vmwcommon.ReadVMX(vmx)
	if err != nil {
		ui.Message(fmt.Sprintf("err: %s", err))
	}

	for k, _ := range vmxData {
		if strings.HasPrefix(k, "floppy0.") {
			ui.Message(fmt.Sprintf("Deleting key: %s", k))
			delete(vmxData, k)
		}
		if strings.HasPrefix(k, "ide1:0.") {
			ui.Message(fmt.Sprintf("Deleting key: %s", k))
			delete(vmxData, k)
		}
		if strings.HasPrefix(k, "ide1:1.") {
			ui.Message(fmt.Sprintf("Deleting key: %s", k))
			delete(vmxData, k)
		}
	}

	// remove floppy (again)
	ui.Message(fmt.Sprintf("Setting key: floppy0.present = FALSE"))
	vmxData["floppy0.present"] = "FALSE"

	// detach DVD (again)
	ui.Message(fmt.Sprintf("Setting key: ide1:0.present = FALSE"))
	vmxData["ide1:0.present"] = "FALSE"

	// Rewrite the VMX
	if err := vmwcommon.WriteVMX(vmx, vmxData); err != nil {
		ui.Message(fmt.Sprintf("err: %s", err))
	}

	program, err := FindOvfTool()
	sourcetype := "--sourceType=VMX"
	targettype := "--targetType=OVF"

	ui.Message(fmt.Sprintf("Creating directory: %s", basepath))

	if err := os.Mkdir(basepath, 0755); err != nil {
		ui.Message(fmt.Sprintf("err: %s", err))
	}

	cmd := exec.Command(program, sourcetype, targettype, vmx, basepath+"/"+ovf)

	ui.Message(fmt.Sprintf("Starting ovftool"))

	cmd.Start()
	cmd.Wait()

	ui.Message(fmt.Sprintf("Reading files in %s", basepath))
	files, _ := ioutil.ReadDir(basepath)
	for _, path := range files {
		ui.Message(fmt.Sprintf("Copying: %s", path.Name()))

		dstPath := filepath.Join(dir, path.Name())
		if err = CopyContents(dstPath, basepath+"/"+path.Name()); err != nil {
			return
		}
	}

	return
}