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

	f, err := os.Open(vmxPath)
	if err != nil {
		err := fmt.Errorf("Error reading VMX data: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	vmxBytes, err := ioutil.ReadAll(f)
	if err != nil {
		err := fmt.Errorf("Error reading VMX data: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	var vncFinder VNCAddressFinder
	if finder, ok := driver.(VNCAddressFinder); ok {
		vncFinder = finder
	} else {
		vncFinder = s
	}
	log.Printf("Looking for available port between %d and %d", config.VNCPortMin, config.VNCPortMax)
	vncIp, vncPort := vncFinder.VNCAddress(config.VNCPortMin, config.VNCPortMax)
	if vncPort == 0 {
		err := fmt.Errorf("Unable to find available VNC port between %d and %d",
			config.VNCPortMin, config.VNCPortMax)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	log.Printf("Found available VNC port: %d", vncPort)

	vmxData := vmwcommon.ParseVMX(string(vmxBytes))
	vmxData["remotedisplay.vnc.enabled"] = "TRUE"
	vmxData["remotedisplay.vnc.port"] = fmt.Sprintf("%d", vncPort)

	if err := vmwcommon.WriteVMX(vmxPath, vmxData); err != nil {
		err := fmt.Errorf("Error writing VMX data: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	state.Put("vnc_port", vncPort)
	state.Put("vnc_ip", vncIp)

	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
}
Esempio n. 6
0
func (s *stepCreateVMX) Run(state multistep.StateBag) multistep.StepAction {
	config := state.Get("config").(*Config)
	isoPath := state.Get("iso_path").(string)
	ui := state.Get("ui").(packer.Ui)

	ui.Say("Building and writing VMX file")

	vmxTemplate := DefaultVMXTemplate
	if config.VMXTemplatePath != "" {
		f, err := os.Open(config.VMXTemplatePath)
		if err != nil {
			err := fmt.Errorf("Error reading VMX template: %s", err)
			state.Put("error", err)
			ui.Error(err.Error())
			return multistep.ActionHalt
		}
		defer f.Close()

		rawBytes, err := ioutil.ReadAll(f)
		if err != nil {
			err := fmt.Errorf("Error reading VMX template: %s", err)
			state.Put("error", err)
			ui.Error(err.Error())
			return multistep.ActionHalt
		}

		vmxTemplate = string(rawBytes)
	}

	ctx := config.ctx

	if len(config.AdditionalDiskSize) > 0 {
		for i := range config.AdditionalDiskSize {
			ctx.Data = &additionalDiskTemplateData{
				DiskNumber: i + 1,
				DiskName:   config.DiskName,
			}

			diskTemplate := DefaultAdditionalDiskTemplate
			if config.VMXDiskTemplatePath != "" {
				f, err := os.Open(config.VMXDiskTemplatePath)
				if err != nil {
					err := fmt.Errorf("Error reading VMX disk template: %s", err)
					state.Put("error", err)
					ui.Error(err.Error())
					return multistep.ActionHalt
				}
				defer f.Close()

				rawBytes, err := ioutil.ReadAll(f)
				if err != nil {
					err := fmt.Errorf("Error reading VMX disk template: %s", err)
					state.Put("error", err)
					ui.Error(err.Error())
					return multistep.ActionHalt
				}

				diskTemplate = string(rawBytes)
			}

			diskContents, err := interpolate.Render(diskTemplate, &ctx)
			if err != nil {
				err := fmt.Errorf("Error preparing VMX template for additional disk: %s", err)
				state.Put("error", err)
				ui.Error(err.Error())
				return multistep.ActionHalt
			}

			vmxTemplate += diskContents
		}
	}

	ctx.Data = &vmxTemplateData{
		Name:     config.VMName,
		GuestOS:  config.GuestOSType,
		DiskName: config.DiskName,
		Version:  config.Version,
		ISOPath:  isoPath,
	}

	vmxContents, err := interpolate.Render(vmxTemplate, &ctx)
	if err != nil {
		err := fmt.Errorf("Error procesing VMX template: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	vmxDir := config.OutputDir
	if config.RemoteType != "" {
		// For remote builds, we just put the VMX in a temporary
		// directory since it just gets uploaded anyways.
		vmxDir, err = ioutil.TempDir("", "packer-vmx")
		if err != nil {
			err := fmt.Errorf("Error preparing VMX template: %s", err)
			state.Put("error", err)
			ui.Error(err.Error())
			return multistep.ActionHalt
		}

		// Set the tempDir so we clean it up
		s.tempDir = vmxDir
	}

	vmxPath := filepath.Join(vmxDir, config.VMName+".vmx")
	if err := vmwcommon.WriteVMX(vmxPath, vmwcommon.ParseVMX(vmxContents)); err != nil {
		err := fmt.Errorf("Error creating VMX file: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	state.Put("vmx_path", vmxPath)

	return multistep.ActionContinue
}
Esempio n. 7
0
func (s *stepCreateVMX) Run(state multistep.StateBag) multistep.StepAction {
	config := state.Get("config").(*config)
	isoPath := state.Get("iso_path").(string)
	ui := state.Get("ui").(packer.Ui)

	ui.Say("Building and writing VMX file")

	tplData := &vmxTemplateData{
		Name:     config.VMName,
		GuestOS:  config.GuestOSType,
		DiskName: config.DiskName,
		ISOPath:  isoPath,
	}

	vmxTemplate := DefaultVMXTemplate
	if config.VMXTemplatePath != "" {
		f, err := os.Open(config.VMXTemplatePath)
		if err != nil {
			err := fmt.Errorf("Error reading VMX template: %s", err)
			state.Put("error", err)
			ui.Error(err.Error())
			return multistep.ActionHalt
		}
		defer f.Close()

		rawBytes, err := ioutil.ReadAll(f)
		if err != nil {
			err := fmt.Errorf("Error reading VMX template: %s", err)
			state.Put("error", err)
			ui.Error(err.Error())
			return multistep.ActionHalt
		}

		vmxTemplate = string(rawBytes)
	}

	vmxContents, err := config.tpl.Process(vmxTemplate, tplData)
	if err != nil {
		err := fmt.Errorf("Error procesing VMX template: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	vmxDir := config.OutputDir
	if config.RemoteType != "" {
		// For remote builds, we just put the VMX in a temporary
		// directory since it just gets uploaded anyways.
		vmxDir, err = ioutil.TempDir("", "packer-vmx")
		if err != nil {
			err := fmt.Errorf("Error preparing VMX template: %s", err)
			state.Put("error", err)
			ui.Error(err.Error())
			return multistep.ActionHalt
		}

		// Set the tempDir so we clean it up
		s.tempDir = vmxDir
	}

	vmxPath := filepath.Join(vmxDir, config.VMName+".vmx")
	if err := vmwcommon.WriteVMX(vmxPath, vmwcommon.ParseVMX(vmxContents)); err != nil {
		err := fmt.Errorf("Error creating VMX file: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	state.Put("vmx_path", vmxPath)

	return multistep.ActionContinue
}