Esempio n. 1
0
func UiRemoteMode(ui *gui.DialogUi) (bool, error) {
	ui.SetTitle("Deployment Mode")
	ui.SetSize(9, 28)
	answer, err := ui.Menu(2, "1", "Local", "2", "Remote")
	if err != nil {
		return false, err
	}
	if answer == "1" {
		return false, nil
	}

	if _, err := exec.LookPath("sshfs"); err != nil {
		ui.Output(gui.Error, "sshfs utility is not installed")
	}
	return true, nil
}
Esempio n. 2
0
func uiNetworkPolicySelector(ui *gui.DialogUi, net *xmlinput.Network) ([]xmlinput.ConnectionMode, error) {
	matrix := make(map[string][]xmlinput.ConnectionMode)
	for _, mode := range net.UiModeBinding {
		if _, ok := matrix[mode.Appear]; !ok {
			matrix[mode.Appear] = make([]xmlinput.ConnectionMode, 0)
		}
		matrix[mode.Appear] = append(matrix[mode.Appear], mode.Type)
	}

	var temp []string
	index := 1
	for appear, _ := range matrix {
		temp = append(temp, strconv.Itoa(index), appear)
		index++
	}

	length := len(matrix)
	ui.SetSize(length+8, 50)
	ui.SetTitle(fmt.Sprintf("Network interface type for network \"%s\"", net.Name))
	ui.HelpButton(true)
	ui.SetHelpLabel("Back")
	if net.Optional {
		ui.SetExtraLabel("Skip")
	}
	val, err := ui.Menu(length, temp[0:]...)
	if err != nil {
		return nil, err
	}
	resultInt, err := strconv.Atoi(val)
	if err != nil {
		return nil, utils.FormatError(err)
	}
	if resultInt != 1 {
		resultInt++
	}
	return matrix[temp[resultInt]], nil
}
Esempio n. 3
0
func uiBundleConfig(ui *gui.DialogUi, configs []*Config, advancedConfig bool) (*Config, error) {
	var temp []string
	index := 0
	for _, c := range configs {
		index += 1
		temp = append(temp, strconv.Itoa(index),
			fmt.Sprintf("%-15s [ vCPU %-2d | RAM %-3dMB]", c.Name, c.CPUs, c.RAM))
	}

	advIndex := index + 1
	temp = append(temp, strconv.Itoa(advIndex), "Custom configuration")

	sliceLength := len(temp)
	var configNumStr string
	var err error
	for {
		ui.SetSize(sliceLength+6, 50)
		ui.SetLabel("Select Virtual Machine configuration")
		configNumStr, err = ui.Menu(sliceLength+6, temp[0:]...)
		if err != nil {
			return nil, err
		}
		if configNumStr != "" {
			break
		}
	}

	configNumInt, err := strconv.Atoi(configNumStr)
	if err != nil {
		return nil, utils.FormatError(err)
	}
	if configNumInt == advIndex {
		return nil, nil
	}
	return configs[configNumInt-1], nil
}
Esempio n. 4
0
func UiNUMATopology(ui *gui.DialogUi, c *guest.Config, d deployer.EnvDriver, totalCpusOnHost int) (bool, error) {
	var list []string

	// file, err := os.Create("/tmp/UiNUMATopology.txt")
	// if err != nil {
	// 	return false, nil
	// }
	// defer file.Close()

	isChanged := false
	hCPUnotOnce := ""
	CheckIfCPUdoubleUsed := true

MainLoop:
	for {
		// file.WriteString("[UiNUMATopology] MainLoop: \n")
		list = make([]string, 0)
		tempData := make(map[int]map[int]string)
		// file.WriteString("[UiNUMATopology] totalCpusOnHost: " + strconv.Itoa(totalCpusOnHost) + " \n")
		for _, n := range c.NUMAs {
			// file.WriteString("[UiNUMATopology] c.NUMAs\n")
			// file.WriteString("[UiNUMATopology] len(n.CPUPin): " + strconv.Itoa(len(n.CPUPin)) + "\n")
			keys := make([]int, 0)
			for vcpu, _ := range n.CPUPin {
				keys = append(keys, vcpu)
				// file.WriteString("[UiNUMATopology] vcpu: " + strconv.Itoa(vcpu) + "\n")
			}

			sort.Ints(keys)
			var hostCpu string
			for _, k := range keys {
				if len(n.CPUPin[k]) > 1 {
					if len(n.CPUPin[k]) == totalCpusOnHost {
						CheckIfCPUdoubleUsed = false
						hostCpu = "0-" + strconv.Itoa(totalCpusOnHost-1)
					} else {
						var tmpStrSlice []string
						for _, c := range n.CPUPin[k] {
							tmpStrSlice = append(tmpStrSlice, strconv.Itoa(c))
						}
						hostCpu = strings.Join(tmpStrSlice, ",")
					}
				} else {
					hostCpu = strconv.Itoa(n.CPUPin[k][0])
				}
				// file.WriteString("[UiNUMATopology] hostCpu: " + hostCpu + "\n")
				tempData[k] = make(map[int]string)
				tempData[k][n.CellID] = hostCpu

				// file.WriteString("tempData[" + strconv.Itoa(k) + "][" + strconv.Itoa(n.CellID) + "] = " + hostCpu + "  \n")
			}
		}

		// we need to represent sorted vCPU IDs and not vNUMA IDs
		keys := make([]int, 0)
		for k, _ := range tempData {
			keys = append(keys, k)
		}

		duplicate_frequency := make(map[string]int)
		sort.Ints(keys)
		for _, key := range keys {
			for k, v := range tempData[key] {
				list = append(list, strconv.Itoa(key), fmt.Sprintf("%-10s%-18d%-7s", " ", k, v))
				temphCPU := v
				_, exist := duplicate_frequency[temphCPU]
				if exist {
					duplicate_frequency[temphCPU] += 1 // increase counter by 1 if already in the map
				} else {
					duplicate_frequency[temphCPU] = 1 // else start counting from 1
				}
				// file.WriteString("k: " + strconv.Itoa(k) + " v: " + v + "  \n")
			}
		}

		hCPUnotOnce = ""
		for k_dup, v_dup := range duplicate_frequency {
			if v_dup > 1 {
				hCPUnotOnce = k_dup
				break
			}
		}

		ui.SetLabel(uiNUMATopologyHeader(ui, c))
		result, err := ui.Menu(len(list), list[0:]...)
		// file.WriteString("[UiNUMATopology] result: " + result + " err: " + err.Error() + " len(list): " + strconv.Itoa(len(list)) + " \n")

		if err == nil {
			if hCPUnotOnce != "" && isChanged && CheckIfCPUdoubleUsed {
				ui.Output(gui.Warning, "CPU "+hCPUnotOnce+" is assigned to more than one vCPU")
				continue
			}
			break
		}
		if err.Error() != gui.DialogNext {
			// file.WriteString("[UiNUMATopology] err.Error() != gui.DialogNext " + err.Error() + " \n")
			return isChanged, err
		}

	InternalLoop:
		for {
			// file.WriteString("[UiNUMATopology] InternalLoop: \n")

			ui.SetTitle("VA vCPU Configuration")
			ui.SetExtraLabel("Help")
			// file.WriteString("result: (" + result + ") \n")
			resultInt, err := strconv.Atoi(result)
			if err != nil {
				// file.WriteString("[UiNUMATopology] strconv.Atoi(result) " + err.Error() + "\n")
				return isChanged, err
			}

			var vnumaStr string
			var cpusStr string
			tempDataLen := len(tempData)
			for k, v := range tempData[resultInt] {
				vnumaStr = strconv.Itoa(k)
				cpusStr = v
				// tempDataLen++
			}
			// file.WriteString("resultInt: (" + result + ") \n")
			// DE11527
			index := resultInt
			resultInt--

			// var vnumaPredecStr string
			// for k, _ := range tempData[resultInt] {
			// 	vnumaPredecStr = strconv.Itoa(k)
			// 	// file.WriteString("vnumaPredecStr: (" + vnumaPredecStr + ") ")
			// }

			var lst []string
			label := "Set affinity for vCPU " + result

			var vnumaMinus1 string
			var vnumaPlus1 string
			var vnumaIndex string
			var vnumaTemp string
			if tempDataLen > 3 {
				if index > (tempDataLen - 1) {
					index = index - tempDataLen
				}

				indexMinus1 := index - 1

				if indexMinus1 < 0 {
					indexMinus1 = tempDataLen + indexMinus1
				}
				indexPlus1 := index + 1

				if indexPlus1 > tempDataLen {
					indexPlus1 = indexPlus1 - tempDataLen
				}

				for k, _ := range tempData[indexMinus1] {
					vnumaMinus1 = strconv.Itoa(k)
				}

				for k, _ := range tempData[indexPlus1] {
					vnumaPlus1 = strconv.Itoa(k)
				}
				for k, _ := range tempData[index] {
					vnumaIndex = strconv.Itoa(k)
				}

				if index == (tempDataLen - 1) {
					// vnumaPlus1 = vnumaIndex

					for k, _ := range tempData[0] {
						vnumaTemp = strconv.Itoa(k)
					}
					if vnumaTemp != vnumaIndex {
						vnumaPlus1 = vnumaIndex
					} else {
						vnumaPlus1 = "vnumaPlus1"
					}

				}
				if index == 0 {
					// vnumaMinus1 = vnumaIndex
					for k, _ := range tempData[tempDataLen-1] {
						vnumaTemp = strconv.Itoa(k)
					}
					if vnumaTemp != vnumaIndex {
						vnumaMinus1 = vnumaIndex
					} else {
						vnumaMinus1 = "vnumaIndex"
					}
				}
			} else {
				vnumaMinus1 = "-1"
				vnumaPlus1 = "+1"
			}

			// xxxxxx := d.Id()
			// file.WriteString("d.Id(): (" + xxxxxx + ") ")
			// file.WriteString("vnumaStr: (" + vnumaStr + ") vnumaPredecStr: (" + vnumaPredecStr + ") \n")
			// file.WriteString("indexMinus1: (" + strconv.Itoa(indexMinus1) + ") index: (" + strconv.Itoa(index) + ")  indexPlus1: (" + strconv.Itoa(indexPlus1) + ")\n")
			// file.WriteString("vnumaMinus1: (" + vnumaMinus1 + ") vnumaPlus1: (" + vnumaPlus1 + ") \n")
			// file.WriteString("tempDataLen: (" + strconv.Itoa(tempDataLen) + ")  \n")
			// if vnumaStr == vnumaPredecStr && d.Id() == "QEMU-Libvirt" {
			if vnumaMinus1 == vnumaPlus1 && d.Id() == "QEMU-Libvirt" {
				lst = []string{"VA vNUMA ID : ", "1", "1", vnumaStr, "1", "15", "2", "0", "2"}
				label += "\n\nIMPORTANT! Some QEMU versions do not support\n" +
					"disjoint NUMA CPU ranges therefore vNUMA configuration\n" +
					"is disabled for this vCPU.\n"
			} else {
				lst = []string{"VA vNUMA ID : ", "1", "1", vnumaStr, "1", "15", "2", "0", "0"}
			}

			lst = append(lst, "Host CPU(s) : ", "2", "1", cpusStr, "2", "15", "30", "0", "0")
			r, err := ui.Mixedform(label, false, lst[0:]...)
			if err != nil {
				if err.Error() == gui.DialogNext {
					uiShowNumaTopologyHelpMsg(ui)
					continue
				}
				return isChanged, err
			}
			if len(r) < 2 {
				continue
			}

			vcpuInt, err := strconv.Atoi(result)
			if err != nil {
				continue
			}

			vnumaInt, err := strconv.Atoi(r[0])
			if err != nil {
				ui.Output(gui.Warning, "Illegal input \""+r[0]+"\"", "Press <OK> to return to menu.")
				continue
			}
			if err := verifyRange(vnumaInt, len(c.NUMAs)); err != nil {
				ui.Output(gui.Warning, err.Error(), "Press <OK> to return to menu.")
				continue
			}

			hostCpus := r[1]
			cpus := make([]int, 0)
			if strings.Contains(hostCpus, ",") {
				for _, e := range strings.Split(hostCpus, ",") {
					if strings.Contains(e, "-") {
						cpus, err = splitByHypen(e, totalCpusOnHost)
						if err != nil {
							ui.Output(gui.Warning, err.Error(), "Press <OK> to return to menu.")
							continue InternalLoop
						}
					} else {
						cpu, err := strconv.Atoi(e)
						if err != nil {
							ui.Output(gui.Warning, "Illegal input \""+e+"\"", "Press <OK> to return to menu.")
							continue InternalLoop
						}
						if err := verifyRange(cpu, totalCpusOnHost); err != nil {
							ui.Output(gui.Warning, err.Error(), "Press <OK> to return to menu.")
							continue InternalLoop
						}
						cpus = append(cpus, cpu)
					}
				}
			} else if strings.Contains(hostCpus, "-") {
				cpus, err = splitByHypen(hostCpus, totalCpusOnHost)
				if err != nil {
					ui.Output(gui.Warning, err.Error(), "Press <OK> to return to menu.")
					continue
				}
			} else {
				cpu, err := strconv.Atoi(hostCpus)
				if err != nil {
					ui.Output(gui.Warning, "Illegal input \""+hostCpus+"\"", "Press <OK> to return to menu.")
					continue
				}
				if err := verifyRange(cpu, totalCpusOnHost); err != nil {
					ui.Output(gui.Warning, err.Error(), "Press <OK> to return to menu.")
					continue
				}
				cpus = append(cpus, cpu)
			}

			// delete the old entry
			isChanged = true
			for _, n := range c.NUMAs {
				for vcpu, _ := range n.CPUPin {
					if vcpu == vcpuInt {
						delete(n.CPUPin, vcpu)
					}
				}
			}

			sort.Ints(cpus)
			// set the new entry
			c.NUMAs[vnumaInt].CPUPin[vcpuInt] = cpus
			continue MainLoop
		}
		break
	}

	// ui.Output(gui.Warning, "CPU "+hCPUnotOnce+" is assigned to more than one vCPU")

	// if isChanged {
	// 	file.WriteString("isChanged\n")
	// } else {
	// 	file.WriteString("NotChanged\n")
	// }
	// if isChanged {
	// 	ui.Output(gui.Warning, "CPU j is assigned to more than one vCPU")
	// 	continue MainLoop
	// }
	// file.WriteString("return: \n")
	return isChanged, nil
}
Esempio n. 5
0
func uiNicSelectMenu(ui *gui.DialogUi, data *xmlinput.XMLInputData, guestPortCounter *int,
	guestPciSlotCounter *int, hnics host.NICList, net *xmlinput.Network, index int) (guest.NICList, error) {
	list := make([]string, 0)
	keeper := make(map[string]*host.NIC)
	indexStrToInt := make(map[string][]int)
	indexInt := 1
	for _, hnic := range hnics {
		indexStr := strconv.Itoa(indexInt)
		list = append(list, indexStr, fmt.Sprintf("%-22s%-15s%-68s%-10s", hnic.Name, hnic.PCIAddr, hnic.Desc, uiNUMAIntToString(hnic.NUMANode)))
		keeper[indexStr] = hnic
		if indexInt == 1 {
			// index 0 - element index in the list
			// index 1 - element counter
			// index 2 - PCI slot number represented as integer
			indexStrToInt[indexStr] = []int{1, 0, 0}
		} else {
			indexStrToInt[indexStr] = []int{indexInt*2 - 1, 0, 0}
		}
		indexInt++
	}

	listLength := len(list)
	gnics := guest.NewNICList()
	var disjuncNicVendor string
	var disjuncNicModel string

	for {
		if index > 0 {
			ui.HelpButton(true)
			ui.SetHelpLabel("Back")
		}
		width := uiHeaderSelectNics(ui)
		ui.SetSize(listLength+8, width+5)
		ui.SetTitle(fmt.Sprintf("Select interface for network \"%s\"", net.Name))
		nicNumStr, err := ui.Menu(listLength+5, list[0:]...)
		if err != nil {
			if err.Error() == gui.DialogNext {
				if len(gnics) == 0 && net.Optional == false {
					continue
				}
				break
			}
			*guestPciSlotCounter -= len(gnics)
			*guestPortCounter -= len(gnics)
			return nil, err
		}

		hnic := keeper[nicNumStr]
		// verify that we need to proceed with "disjunction"
		if net.NicsDisjunction && (hnic.Type == host.NicTypePhys || hnic.Type == host.NicTypePhysVF) &&
			(hnic.Vendor != disjuncNicVendor && hnic.Model != disjuncNicModel) {
			// back to menu in case "disjunction" entries already exist
			if host_hwfilter.NicDisjunctionFound(hnic, data.HostNics.Allowed) && disjuncNicVendor != "" {
				msg := fmt.Sprintf("'%s' cannot be selected alongside '%s %s'", hnic.Desc, disjuncNicVendor, disjuncNicModel)
				ui.Output(gui.Warning, msg, "Press <OK> to return to menu.")
				continue
			}
			// set the new entries
			disjuncNicVendor = hnic.Vendor
			disjuncNicModel = hnic.Model
		}

		delNicCounter := indexStrToInt[nicNumStr][1]
		// counter for the NIC found,we should remove the NIC object and its references
		if delNicCounter > 0 {
			// find the guest NIC object in the guest NICs list
			if _, index, err := gnics.NicByHostNicObj(hnic); err == nil {
				// if found :
				// - remove the object
				// - decrement guestPortCounter
				// - decriment guestPciSlotCounter
				gnics.Remove(index)
				if *guestPortCounter > 0 {
					*guestPortCounter--
				}
				if *guestPciSlotCounter > data.GuestNic.PCI.FirstSlot {
					*guestPciSlotCounter--
				}
				// update the list with the new entry containing deselected NIC
				list[indexStrToInt[nicNumStr][0]] = fmt.Sprintf("%-22s%-15s%-68s%-10s", hnic.Name, hnic.PCIAddr, hnic.Desc, uiNUMAIntToString(hnic.NUMANode))
				// - reset element counter
				// - reset PCI slot number
				indexStrToInt[nicNumStr][1] = 0
				indexStrToInt[nicNumStr][2] = 0
			}
			// iterate over the map and update entries
			for nicIndex, data := range indexStrToInt {
				nicCounter := data[1]
				if nicCounter > delNicCounter {
					tmpNic := keeper[nicIndex]
					nicCounter--
					list[data[0]] = fmt.Sprintf("%-22s%-15s%-68s%-9s%d", tmpNic.Name, tmpNic.PCIAddr,
						tmpNic.Desc, uiNUMAIntToString(tmpNic.NUMANode), nicCounter)
					indexStrToInt[nicIndex][1] = nicCounter
					indexStrToInt[nicIndex][2]--
					if gnic, _, err := gnics.NicByHostNicObj(tmpNic); err == nil {
						gnic.PCIAddr.Slot = utils.IntToHexString(indexStrToInt[nicIndex][2])
					}
				}
			}
			// clear "disjunction" entries in case gnics slice is empty
			if gnics.Length() == 0 {
				disjuncNicVendor = ""
				disjuncNicModel = ""
			}
			continue
		}
		// create new guest NIC, set his  number and counter
		indexStrToInt[nicNumStr][1] = *guestPortCounter
		indexStrToInt[nicNumStr][2] = *guestPciSlotCounter
		list[indexStrToInt[nicNumStr][0]] = fmt.Sprintf("%-22s%-15s%-68s%-9s%d", hnic.Name, hnic.PCIAddr,
			hnic.Desc, uiNUMAIntToString(hnic.NUMANode), *guestPortCounter)
		gnic := guest.NewNIC()
		gnic.Network = net.Name
		gnic.PCIAddr.Domain = data.PCI.Domain
		gnic.PCIAddr.Bus = data.PCI.Bus
		gnic.PCIAddr.Slot = utils.IntToHexString(*guestPciSlotCounter)
		gnic.PCIAddr.Function = data.PCI.Function
		gnic.HostNIC = hnic
		gnics.Add(gnic)
		*guestPciSlotCounter++
		*guestPortCounter++
	}
	return gnics, nil
}
Esempio n. 6
0
func UiSshConfig(ui *gui.DialogUi) (*sshconf.Config, error) {
	cfg := new(sshconf.Config)
	cfg.Port = "22"
	cfg.User = "******"
	origlist := []string{"IP      : ", "1", "1", "", "1", "10", "22", "0", "0",
		"SSH Port: ", "2", "1", cfg.Port, "2", "10", "22", "0", "0",
		"Username: "******"3", "1", cfg.User, "3", "10", "22", "0", "0"}

MainLoop:
	for {
		ui.HelpButton(true)
		ui.SetHelpLabel("Back")
		reslist, err := ui.Mixedform("Remote session configuration", false, origlist[0:]...)
		if err != nil {
			return nil, err
		}
		if len(reslist) < 3 {
			continue
		}
		if net.ParseIP(reslist[0]) == nil {
			continue
		}
		cfg.Host = reslist[0]

		portDig, err := strconv.Atoi(reslist[1])
		if err != nil {
			return nil, utils.FormatError(err)
		}
		if portDig > 65535 {
			continue
		}

	AuthLoop:
		for {
			ui.SetTitle("Authentication method")
			ui.SetSize(9, 18)
			ui.HelpButton(true)
			ui.SetHelpLabel("Back")
			val, err := ui.Menu(2, "1", "Password", "2", "Private key")
			if err != nil {
				switch err.Error() {
				case gui.DialogMoveBack:
					continue MainLoop
				case gui.DialogExit:
					os.Exit(1)
				}
			}

			switch val {
			case "1":
				cfg.Password, err = ui.GetPasswordFromInput(cfg.Host, cfg.User, "Back", "", false)
			case "2":
				cfg.PrvtKeyFile, err = ui.GetPathToFileFromInput("Path to ssh private key file", "Back", "")
			}
			if err != nil {
				switch err.Error() {
				case gui.DialogMoveBack:
					continue AuthLoop
				case gui.DialogExit:
					os.Exit(1)
				}
			}
			break MainLoop
		}
	}

	run := utils.RunFunc(cfg)
	errCh := make(chan error)
	defer close(errCh)
	go func() {
		// verifying that user is able execute a command by using sudo
		_, err := run("uname")
		errCh <- err
	}()

	if err := ui.Wait("Trying to establish SSH connection to remote host.\nPlease wait...", time.Second*1, time.Second*5, errCh); err != nil {
		ui.Output(gui.Warning, "Unable to establish SSH connection.", "Press <OK> to return to menu.")
		goto MainLoop
	}
	return cfg, nil
}