Esempio n. 1
0
func UiVmConfig(ui *gui.DialogUi, driver deployer.HostinfoDriver, xidata *xmlinput.XMLInputData,
	pathToMainImage string, sconf *image.Storage, conf *guest.Config) error {
	var installedRamMb int
	var maxRAM int
	var err error

	list := make([]string, 0)
	index := 1

	if xidata.CPU.Configure {
		cpuStr := fmt.Sprintf("  %-9s |  %d-%d", "CPU", xidata.CPU.Min, xidata.CPU.Max)
		list = []string{cpuStr, strconv.Itoa(index), "1", strconv.Itoa(xidata.CPU.Default), "1", "30", "6", "0", "0"}
		index++
	} else if xidata.CPU.Default > 0 {
		conf.CPUs = xidata.CPU.Default
	}
	if xidata.RAM.Configure {
		installedRamMb, err = driver.RAMSize()
		if err != nil {
			return utils.FormatError(err)
		}
		if xidata.RAM.Max > installedRamMb || xidata.RAM.Max == xmlinput.UnlimitedAlloc {
			maxRAM = installedRamMb
		} else {
			maxRAM = xidata.RAM.Max
		}

		ramStr := fmt.Sprintf("  %-9s |  %d-%dG", "RAM", xidata.RAM.Min/1024, maxRAM/1024)
		list = append(list, []string{ramStr, strconv.Itoa(index), "1", strconv.Itoa(xidata.RAM.Default / 1024), "2", "30", "6", "0", "0"}...)
		index++
	} else if xidata.RAM.Default > 0 {
		conf.RamMb = xidata.RAM.Default
	}
	if xidata.Disks.Configure {
		diskName := "Disk"
		for i, disk := range xidata.Disks.Configs {
			if i > 0 {
				diskName = strconv.Itoa(i) + "_" + strconv.Itoa(i)
			}
			diskStr := fmt.Sprintf("  %-9s |  %d-%dG", diskName, disk.Min/1024, disk.Max/1024)
			indexStr := strconv.Itoa(index)
			list = append(list, []string{diskStr, indexStr, "1", strconv.Itoa(disk.Default / 1024), indexStr, "30", "6", "0", "0"}...)
			index++
		}
	}
	str := " ______________________________________\n|	 Resource	 |		Maximum		|		Allocated		|"
	installedCpus, err := driver.CPUs()
	if err != nil {
		return utils.FormatError(err)
	}

	index--

	if index > 1 {

	MainLoop:
		for {
			ui.SetSize(11, 46)
			ui.SetTitle("Virtual Machine configuration")
			ui.HelpButton(true)
			ui.SetHelpLabel("Back")
			resultIndex := 0
			result, err := ui.Mixedform(str, false, list[0:]...)
			if err != nil {
				return err
			}
			if len(result) < index {
				continue
			}
			if xidata.CPU.Configure {
				selectedCpus, err := strconv.Atoi(result[resultIndex])
				if err != nil {
					continue
				}
				if uiCpuNotOK(ui, selectedCpus, installedCpus, xidata.CPU.Min, xidata.CPU.Max) {
					continue
				}
				conf.CPUs = selectedCpus
				resultIndex++
			}
			if xidata.RAM.Configure {
				selectedRamMb, err := utils.FloatStringToInt(result[resultIndex], 1024)
				if err != nil {
					continue MainLoop
				}
				if uiRamNotOK(ui, selectedRamMb, installedRamMb, xidata.RAM.Min, maxRAM) {
					continue
				}
				conf.RamMb = selectedRamMb
				resultIndex++
			}
			if xidata.Disks.Configure {
				disks := make([]int, 0)
				for _, disk := range xidata.Disks.Configs {
					selectedDiskSizeMb, err := utils.FloatStringToInt(result[resultIndex], 1024)
					if err != nil {
						continue MainLoop
					}
					if uiDiskNotOK(ui, selectedDiskSizeMb, disk.Min, disk.Max) {
						continue MainLoop
					}
					disks = append(disks, selectedDiskSizeMb*1024)
					resultIndex++
				}
				if conf.Storage, err = config.StorageConfig(pathToMainImage, 0, sconf, disks); err != nil {
					return err
				}
			}
			break
		}
	}
	return nil
}
Esempio n. 2
0
func RegisterSteps(d *deployer.CommonData, i *InputData, c *Config, metaconf deployer.MetadataConfigurator) error {
	xid, err := xmlinput.ParseXMLInput(i.InputDataConfigFile)
	if err != nil {
		return utils.FormatError(err)
	}

	controller.RegisterSteps(func() func() error {
		return func() error {
			var err error
			if c.Metadata.DomainName, err = gui.UiApplianceName(d.Ui, d.VaName, c.EnvDriver); err != nil {
				return err
			}
			d.VaName = c.Metadata.DomainName
			if err = gui.UiGatherHWInfo(d.Ui, c.Hwdriver, c.RemoteMode); err != nil {
				return utils.FormatError(err)
			}
			return nil
		}
	}())

	// Network configuration
	if xid.Networks.Configure {
		controller.RegisterSteps(func() func() error {
			return func() error {
				c.GuestConfig.Networks = nil
				c.GuestConfig.NICLists = nil

				nics, err := host_hwfilter.GetAllowedNICs(xid, c.Hwdriver)
				if err != nil {
					return utils.FormatError(err)
				}
				if err = gui.UiNetworks(d.Ui, xid, nics, c.GuestConfig); err != nil {
					return err
				}
				c.Metadata.Networks, err = metaconf.SetNetworkData(c.GuestConfig, i.TemplatesDir, nil)
				if err != nil {
					return utils.FormatError(err)
				}
				return nil
			}
		}())
	}

	// guest configuration
	controller.RegisterSteps(func() func() error {
		return func() error {
			if i.BundleParser != nil {
				m, err := i.BundleParser.Parse(d, c.Hwdriver, xid)
				if err != nil {
					return utils.FormatError(err)
				}
				if m != nil {
					c.Bundle = m
					c.GuestConfig.CPUs = m["cpus"].(int)
					c.GuestConfig.RamMb = m["ram_mb"].(int) * 1024
					c.GuestConfig.Storage, err = config.StorageConfig(filepath.Join(c.ExportDir, d.VaName),
						m["storage_config_index"].(image.ConfigIndex), c.StorageConfig, nil)
					if err != nil {
						return utils.FormatError(err)
					}
				}
			}
			if len(c.Bundle) == 0 {
				if xid.CPU.Max == xmlinput.UnlimitedAlloc {
					xid.CPU.Max = c.EnvDriver.MaxVCPUsPerGuest()
				}
				if err = gui.UiVmConfig(d.Ui, c.Hwdriver, xid, filepath.Join(c.ExportDir, d.VaName), c.StorageConfig, c.GuestConfig); err != nil {
					return err
				}
			}

			c.Metadata.CPUs = c.GuestConfig.CPUs
			c.Metadata.RAM = c.GuestConfig.RamMb * 1024
			if c.GuestConfig.Storage == nil {
				if c.GuestConfig.Storage, err = config.StorageConfig(filepath.Join(c.ExportDir, d.VaName), 0, c.StorageConfig, nil); err != nil {
					return err
				}
			}
			c.Metadata.Storage, err = metaconf.SetStorageData(c.GuestConfig, i.TemplatesDir, nil)
			if err != nil {
				return utils.FormatError(err)
			}
			return nil

		}
	}())

	// NUMA configuration
	controller.RegisterSteps(func() func() error {
		return func() error {
			// file, err := os.Create("/tmp/x.txt")
			// defer file.Close()

			c.GuestConfig.NUMAs = nil
			numas, err := c.Hwdriver.NUMAInfo()
			if err != nil {
				return utils.FormatError(err)
			}
			if xid.NUMA.AutoConfig {
				if xid.WarnOnUnpinnedCPUs {
					pinned, err := c.EnvDriver.AllCPUsPinned()
					if err != nil {
						return utils.FormatError(err)
					}
					if !pinned {
						if !gui.UiWarningOnOptimizationFailure(d.Ui, "Not all the virtual machines on the host are configured with CPU pinning.") {
							os.Exit(0)
						}
					}
				}
				if numas.TotalNUMAs() == 1 {
					// file.WriteString("RegisterSteps() numas.TotalNUMAs() == 1\n")
					if err := c.GuestConfig.SetTopologySingleVirtualNUMA(numas, true); err != nil {
						return utils.FormatError(err)
					}
				} else {
					// file.WriteString("RegisterSteps() numas.TotalNUMAs() else \n")
					if err := c.GuestConfig.SetTopologyMultipleVirtualNUMAs(numas); err != nil {
						// file.WriteString("RegisterSteps() c.GuestConfig.SetTopologyMultipleVirtualNUMAs(numas) err != nil " + err.Error() + "\n")
						return utils.FormatError(err)
					}
					if c.GuestConfig.OptimizationFailureMsg != "" {
						// file.WriteString("RegisterSteps() c.GuestConfig.OptimizationFailureMsg  " + c.GuestConfig.OptimizationFailureMsg + "\n")
						if !gui.UiWarningOnOptimizationFailure(d.Ui, c.GuestConfig.OptimizationFailureMsg) {
							os.Exit(0)
						}
					}
				}
			} else {
				if err := c.GuestConfig.SetTopologySingleVirtualNUMA(numas, false); err != nil {
					return utils.FormatError(err)
				}
			}

			processNext, err := gui.UiNumaRamNotOK(d.Ui, c.Hwdriver, c.GuestConfig, c.GuestConfig.RamMb)

			if processNext != true {

				return errors.New(dialog_ui.DialogMoveBack)
			}
			if err != nil {
				// file.WriteString("RegisterSteps() err != nil 1 \n")
				return utils.FormatError(err)
			}
			// file.WriteString("RegisterSteps() xid.UiEditNUMAConfig  \n")
			if xid.UiEditNUMAConfig {
				cpus, err := c.Hwdriver.CPUs()
				// file.WriteString("RegisterSteps() c.Hwdriver.CPUs() \n")
				if err != nil {
					// file.WriteString("RegisterSteps() err  " + err.Error() + " \n")
					return utils.FormatError(err)
				}
				// inject return code if need reconfigure
				isChanged, err := gui.UiNUMATopology(d.Ui, c.GuestConfig, c.EnvDriver, cpus)
				if err != nil {
					// file.WriteString("RegisterSteps() err !=nil gui.UiNUMATopology " + err.Error() + " \n")
					return err
				}
				if isChanged {
					// err := errors.New("CPU j is assigned to more than one vCPU")
					// return utils.FormatError(err)
					if err := c.GuestConfig.ReconfigureMultipleVirtualNUMAs(numas); err != nil {
						// file.WriteString("RegisterSteps() err !=nil c.GuestConfig.ReconfigureMultipleVirtualNUMAs  \n")
						return utils.FormatError(err)
					}
				}
			}
			// file.WriteString("RegisterSteps() c.Hwdriver.CPUInfo()  \n")
			hcpu, err := c.Hwdriver.CPUInfo()
			if err != nil {
				return utils.FormatError(err)
			}
			if _, ok := hcpu.Cap["pdpe1gb"]; ok {
				c.GuestConfig.LargeHugePagesSupported = true
			}
			// file.WriteString("RegisterSteps() metaconf.SetCpuTuneData(  \n")
			c.Metadata.CPUTune, err = metaconf.SetCpuTuneData(c.GuestConfig, i.TemplatesDir, nil)
			if err != nil {
				return utils.FormatError(err)
			}
			// file.WriteString("RegisterSteps() metaconf.SetCpuConfigData(  \n")
			c.Metadata.CPUConfig, err = metaconf.SetCpuConfigData(c.GuestConfig, i.TemplatesDir, nil)
			if err != nil {
				return utils.FormatError(err)
			}
			// file.WriteString("RegisterSteps() metaconf.SetNUMATuneData(  \n")
			c.Metadata.NUMATune, err = metaconf.SetNUMATuneData(c.GuestConfig, i.TemplatesDir, c.EnvDriver)
			if err != nil {
				return utils.FormatError(err)
			}
			// file.WriteString("RegisterSteps() END \n")
			return nil

		}
	}())

	// create default metadata
	controller.RegisterSteps(func() func() error {
		return func() error {
			c.DestMetadataFile = fmt.Sprintf("/tmp/%s-temp-metadata.%d", d.VaName, os.Getpid())
			// always create default metadata
			if err := ioutil.WriteFile(c.DestMetadataFile, metaconf.DefaultMetadata(), 0); err != nil {
				return utils.FormatError(err)
			}
			return controller.SkipStep
		}
	}())

	return nil
}