Ejemplo n.º 1
0
func DiscoverPeers() error {
	tries := 0
	log.Println("[NODE DISCOVERY STARTED]")
	for {
		nodeList, err := tutum.ListNodes()
		if err != nil {
			time.Sleep(60 * time.Second)
			return err
		}

		if len(nodeList.Objects) == 0 {
			return nil
		}

		node_ips := nodeAppend(nodeList)

		log.Println("[NODE DISCOVERY]: Current nodes available")
		log.Println(node_ips)

		var diff1 []string

		//Checking if there are nodes that are not in the peer_ips list

		diff1 = compareNodePeer(node_ips, peer_ips, diff1)

		for _, i := range diff1 {
			err := connectToPeers(i)
			if err != nil {
				tries++
				if tries > 3 {
					return err
				}
			}
		}

		var diff2 []string

		//Checking if there are peers that are not in the node_ips list
		diff2 = compareNodePeer(peer_ips, node_ips, diff2)

		for _, i := range diff2 {
			err := forgetPeers(i)
			if err != nil {
				tries++
				if tries > 3 {
					return err
				}
			}
		}

		peer_ips = node_ips
		break
	}
	log.Println("[NODE DISCOVERY STOPPED]")
	return nil
}
Ejemplo n.º 2
0
func DiscoverPeers() error {
	tries := 0
	log.Println("[NODE DISCOVERY STARTED]")
	for {
		node_ips := []string{}
		nodeList, err := tutum.ListNodes()
		if err != nil {
			return err
		}

		if len(nodeList.Objects) == 0 {
			return nil
		}

		for i := range nodeList.Objects {
			state := nodeList.Objects[i].State

			if state == "Deployed" || state == "Unreachable" {
				if nodeList.Objects[i].Public_ip != Tutum_Node_Public_Ip {
					node_ips = append(node_ips, nodeList.Objects[i].Public_ip)
				}
			}
		}
		log.Println("[NODE DISCOVERY]: Current nodes available")
		log.Println(node_ips)

		var diff1 []string

		//Checking if there are nodes that are not in the peer_ips list

		for _, s1 := range node_ips {
			found := false
			for _, s2 := range peer_ips {
				if s1 == s2 {
					found = true
					break
				}
			}
			if !found {
				log.Println("[NODE DISCOVERY UPDATE]: Some nodes are not peers")
				diff1 = append(diff1, s1)
			}
		}

		for _, i := range diff1 {
			err := connectToPeers(i)
			if err != nil {
				tries++
				if tries > 3 {
					return err
				}
			}
		}

		var diff2 []string

		//Checking if there are peers that are not in the node_ips list

		for _, s1 := range peer_ips {
			found := false
			for _, s2 := range node_ips {
				if s1 == s2 {
					found = true
					break
				}
			}
			if !found {
				log.Println("[NODE DISCOVERY UPDATE]: Some peers are not nodes anymore")
				diff2 = append(diff2, s1)
			}
		}

		for _, i := range diff2 {
			err := forgetPeers(i)
			if err != nil {
				tries++
				if tries > 3 {
					return err
				}
			}
		}

		peer_ips = node_ips
		break
	}
	log.Println("[NODE DISCOVERY STOPPED]")
	return nil
}