예제 #1
0
파일: ui.go 프로젝트: weldpua2008/deployer
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
}
예제 #2
0
파일: ui.go 프로젝트: weldpua2008/deployer
func UiNetworks(ui *gui.DialogUi, data *xmlinput.XMLInputData, allowedNics host.NICList, gconf *guest.Config) error {
	guestPciSlotCounter := data.GuestNic.PCI.FirstSlot
	lastGuestPciSlotCounter := guestPciSlotCounter
	portCounter := 1
	lastPortCounter := portCounter
	i := 0

MainLoop:
	for i < len(data.Networks.Configs) {
		net := data.Networks.Configs[i]
	PolicyLoop:
		for {
			var modes []xmlinput.ConnectionMode

			if net.UiModeBinding == nil || len(net.UiModeBinding) == 0 {
				for _, mode := range net.Modes {
					modes = append(modes, mode.Type)
				}
			} else {
				var err error
				modes, err = uiNetworkPolicySelector(ui, net)
				if err != nil {
					switch err.Error() {
					case gui.DialogMoveBack:
						gconf.Networks = gconf.Networks[:i]
						gconf.NICLists = gconf.NICLists[:i]
						portCounter = lastPortCounter - 1
						guestPciSlotCounter = lastGuestPciSlotCounter - 1
						if i == 0 {
							return err
						}
						i--
						continue MainLoop
					case gui.DialogNext:
						i++
						continue MainLoop
					case gui.DialogExit:
						os.Exit(1)
					default:
						return err
					}

				}
			}

			retainedNics, err := host_hwfilter.NicsByType(allowedNics, modes)
			if err != nil {
				return utils.FormatError(err)
			}
			if len(retainedNics) == 0 {
				ui.Output(gui.Warning, "No interfaces have been found.", "Press <OK> to return to menu.")
				continue MainLoop
			}
			if net.UiResetCounter {
				portCounter = 1
			}
			list, err := uiNicSelectMenu(ui, data, &portCounter, &guestPciSlotCounter, retainedNics, net, i)
			if err != nil {
				switch err.Error() {
				case gui.DialogMoveBack:
					if i == 0 {
						return err
					}
					gconf.Networks = gconf.Networks[:i]
					gconf.NICLists = gconf.NICLists[:i]
					continue PolicyLoop
				case gui.DialogExit:
					os.Exit(1)
				}
			}

			gconf.Networks = append(gconf.Networks, net)
			gconf.NICLists = append(gconf.NICLists, list)
			lastPortCounter = portCounter
			lastGuestPciSlotCounter = guestPciSlotCounter
			i++
			break
		}
	}
	return nil
}