Beispiel #1
0
func readLibvirtVM(HostIpAddress string, UUIDString string) (VirtualMachine, error) {
	var conn libvirt.VirConnection
	var err error
	ok := ipaddressConnectionCache.Check(HostIpAddress)
	if ok == false {
		conn, err = libvirt.NewVirConnection("qemu+ssh://root@" + HostIpAddress + "/system")
		if err != nil {
			return VirtualMachine{}, err
		}
		ipaddressConnectionCache.Set(HostIpAddress, conn)
	} else {
		//?How to deal with connection's not alive
		conn = ipaddressConnectionCache.Get(HostIpAddress).(libvirt.VirConnection)
		if ok, _ := conn.IsAlive(); !ok {
			log.Printf("remote %s is not alive", HostIpAddress)
			conn, err = libvirt.NewVirConnection("qemu+ssh://root@" + HostIpAddress + "/system")
			if err != nil {
				ipaddressConnectionCache.Delete(HostIpAddress)
				return VirtualMachine{}, err
			}
			/*TODO Write Lock*/
			ipaddressConnectionCache.Set(HostIpAddress, conn)
		}
	}

	domain, err := conn.LookupByUUIDString(UUIDString)
	if err != nil {
		return VirtualMachine{}, err
	}
	vm := fillVmData(domain, conn)

	return vm, nil
}
func TestVmInstall(t *testing.T) {
	// create remote pool
	fmt.Println("Creating connection")
	conn, err := libvirt.NewVirConnection("qemu+ssh://[email protected]/system")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer conn.CloseConnection()

	ch := make(chan string)

	url := "http://mirror.bej.suse.com/dist/install/SLP/SLE-12-Server-Beta10/x86_64/DVD1"
	name := "dmzhang-test-lifecyle"
	imageSize := 8589934592
	go VmInstall(conn, name, url, "", uint64(imageSize), ch)

	for m := range ch {
		fmt.Println(m)
	}
}
Beispiel #3
0
func main() {

	var (
		hostPtr      = flag.String("host", "", "remote host IP address")
		repoPtr      = flag.String("repo", "", "installation repository")
		autoyastPtr  = flag.String("autoyast", "", "location of autoyast xml")
		imageSizePtr = flag.Uint64("size", 10, "image size (G)")
		namePtr      = flag.String("name", "", "name of the Virtual Machine")
	)

	flag.Parse()

	var remoteURL string
	if *hostPtr == "" {
		remoteURL = "qemu+ssh:///system"
	} else {
		remoteURL = "qemu+ssh://root@" + *hostPtr + "/system"
	}

	if *repoPtr == "" {
		fmt.Println("MISSING repo")
		usage()
		return
	}
	repo := *repoPtr

	if *namePtr == "" {
		fmt.Println("MISSING name")
		usage()
		return
	}
	name := *namePtr

	if *autoyastPtr == "" {
		fmt.Println("You did not have autoyast.xml")
	}
	autoinst := *autoyastPtr

	// GB > Byte
	imageSize := *imageSizePtr << 30

	fmt.Printf("Install From :%s \n", remoteURL)
	fmt.Printf("Name         :%s \n", name)
	fmt.Printf("Disk Size    :%dG\n", *imageSizePtr)
	fmt.Printf("Repository   :%s \n", repo)
	fmt.Printf("AutoYast     :%s \n", autoinst)

	startListen()
	// create remote pool
	fmt.Printf("Creating connection to %s\n", *hostPtr)
	conn, err := libvirt.NewVirConnection(remoteURL)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer conn.CloseConnection()

	ch := make(chan string)

	go vminstall.VmInstall(conn, name, repo, autoinst, uint64(imageSize), ch)

	for m := range ch {
		if m == vminstall.VMINSTALL_SUCCESS {
			break
		}
		fmt.Println(m)
	}
	startVNCviewer(conn, name, *hostPtr)

}
Beispiel #4
0
func readLibvirtPysicalMachine(hosts []*PhysicalMachine) {
	/* get libvirt connections */
	numLiveHost := 0
	var conn libvirt.VirConnection

	/* use this type in chanStruct */
	type connResult struct {
		host     *PhysicalMachine
		conn     libvirt.VirConnection
		existing bool
	}
	connChan := make(chan connResult)
	var numGoroutines = 0

	for _, host := range hosts {
		ok := ipaddressConnectionCache.Check(host.IpAddress)
		if ok == false {
			numGoroutines++
			go func(host *PhysicalMachine) {
				conn, err := libvirt.NewVirConnection("qemu+ssh://root@" + host.IpAddress + "/system")
				if err != nil {
					checkErr(err, fmt.Sprintf("failed to connect to %s", host.IpAddress))
					host.Existing = false
					connChan <- connResult{host: host, existing: false}
					return
				}
				connChan <- connResult{host: host, conn: conn, existing: true}
			}(host)
		} else {
			/* existing a conn which is alive */
			conn = ipaddressConnectionCache.Get(host.IpAddress).(libvirt.VirConnection)
			if ok, _ := conn.IsAlive(); ok {
				host.VirConn = conn
				host.Existing = true
				numLiveHost++
				/* existing a conn which is dead */
			} else {
				log.Printf("remove %s is not alive", host.IpAddress)
				host.Existing = false
				ipaddressConnectionCache.Delete(host.IpAddress)
				/* TODO ?if close the connectin */
				conn.CloseConnection()
			}
		}
	}

	for i := 0; i < numGoroutines; i++ {
		r := <-connChan
		if r.existing {
			r.host.VirConn = r.conn
			r.host.Existing = true
			/*Write Lock*/
			ipaddressConnectionCache.Set(r.host.IpAddress, r.conn)
			numLiveHost++
		}
	}

	/* all the PhysicalMachines are ready, VirConnection was connected now */
	/* receive data from VirConnections */

	done := make(chan bool)
	for _, host := range hosts {
		if host.Existing == false {
			continue
		}

		go func(host *PhysicalMachine) {
			domains, _ := host.VirConn.ListAllDomains()
			for _, virdomain := range domains {
				vm := fillVmData(virdomain, conn)
				vm.HostIpAddress = host.IpAddress
				if vm.Active == true {
					vm.VNCAddress = host.IpAddress
				}
				//will not have any operations on vm, virdomain could be freeed
				virdomain.Free()
				host.VirtualMachines = append(host.VirtualMachines, &vm)
			}
			done <- true
		}(host)
	}
	/* wait for all ListAllDomains finish */
	for i := 0; i < numLiveHost; i++ {
		<-done
	}

}