当前位置: 首页>>代码示例>>Golang>>正文


Golang VirConnection.IsAlive方法代码示例

本文整理汇总了Golang中dmzhang/catkeeper/libvirt.VirConnection.IsAlive方法的典型用法代码示例。如果您正苦于以下问题:Golang VirConnection.IsAlive方法的具体用法?Golang VirConnection.IsAlive怎么用?Golang VirConnection.IsAlive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dmzhang/catkeeper/libvirt.VirConnection的用法示例。


在下文中一共展示了VirConnection.IsAlive方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: readLibvirtVM

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://[email protected]" + 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://[email protected]" + 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
}
开发者ID:hopkings2008,项目名称:catkeeper,代码行数:33,代码来源:model.go

示例2: readLibvirtPysicalMachine

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://[email protected]" + 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
	}

}
开发者ID:hopkings2008,项目名称:catkeeper,代码行数:87,代码来源:model.go


注:本文中的dmzhang/catkeeper/libvirt.VirConnection.IsAlive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。