//DiscoverPeers queries DockerCloud API for the list of nodes and checks if nodes must be attached or forgotten func DiscoverPeers() error { tries := 0 counter := 1 log.Println("[NODE DISCOVERY STARTED]") for { nodeList, err := dockercloud.ListNodes() if err != nil { e, ok := err.(dockercloud.HttpError) if ok && e.StatusCode == 401 { log.Println("Not authorized. Retry in 1 hour") time.Sleep(1 * time.Hour) } if err.Error() == "Couldn't find any DockerCloud credentials in ~/.docker/config.json or environment variables DOCKERCLOUD_USER and DOCKERCLOUD_APIKEY" { return err } if counter > 100 { log.Println("Too many retries, give up") return err } counter *= 2 log.Printf("%s: Retry in %d seconds", err, counter) time.Sleep(time.Duration(counter) * time.Second) } else { if len(nodeList.Objects) == 0 { return nil } NodePublicIPs, nodePrivateIps := NodeAppend(nodeList) log.Println("[NODE DISCOVERY]: Current nodes available") log.Printf("Private Network: %s", nodePrivateIps) log.Printf("Public Network: %s", NodePublicIPs) var diff1 []string //Checking if there are nodes that are not in the peerIps list diff1 = tools.CompareArrays(nodePrivateIps, peerIps, diff1) for _, i := range diff1 { err := connectToPeers(i) if err != nil { tries++ if tries > 3 { return err } } } var diff3 []string //Checking if there are nodes that are not in the peerIps list diff3 = tools.CompareArrays(NodePublicIPs, peerIpsPublic, diff3) for _, i := range diff3 { err := connectToPeers(i) if err != nil { tries++ if tries > 3 { return err } } } //IF TERMINATED EVENT var diff2 []string //Checking if there are peers that are not in the nodePrivateIps list diff2 = tools.CompareArrays(peerIps, nodePrivateIps, diff2) for _, i := range diff2 { err := forgetPeers(i) if err != nil { tries++ if tries > 3 { return err } } } var diff4 []string //Checking if there are peers that are not in the nodePrivateIps list diff4 = tools.CompareArrays(peerIpsPublic, NodePublicIPs, diff4) for _, i := range diff4 { err := forgetPeers(i) if err != nil { tries++ if tries > 3 { return err } } } peerIps = nodePrivateIps peerIpsPublic = NodePublicIPs break } } log.Println("[NODE DISCOVERY STOPPED]") return nil }
func DiscoverPeers() error { tries := 0 log.Println("[NODE DISCOVERY STARTED]") for { nodeList, err := dockercloud.ListNodes() if err != nil { time.Sleep(60 * time.Second) return err } if len(nodeList.Objects) == 0 { return nil } node_public_ips, node_private_ips := NodeAppend(nodeList) log.Println("[NODE DISCOVERY]: Current nodes available") log.Printf("Private Network: %s", node_private_ips) log.Printf("Public Network: %s", node_public_ips) var diff1 []string //Checking if there are nodes that are not in the peer_ips list diff1 = compareNodePeer(node_private_ips, peer_ips, diff1) for _, i := range diff1 { err := connectToPeers(i) if err != nil { tries++ if tries > 3 { return err } } } var diff3 []string //Checking if there are nodes that are not in the peer_ips list diff3 = compareNodePeer(node_public_ips, peer_ips_public, diff3) for _, i := range diff3 { err := connectToPeers(i) if err != nil { tries++ if tries > 3 { return err } } } //IF TERMINATED EVENT var diff2 []string //Checking if there are peers that are not in the node_private_ips list diff2 = compareNodePeer(peer_ips, node_private_ips, diff2) for _, i := range diff2 { err := forgetPeers(i) if err != nil { tries++ if tries > 3 { return err } } } var diff4 []string //Checking if there are peers that are not in the node_private_ips list diff4 = compareNodePeer(peer_ips_public, node_public_ips, diff4) for _, i := range diff4 { err := forgetPeers(i) if err != nil { tries++ if tries > 3 { return err } } } peer_ips = node_private_ips peer_ips_public = node_public_ips break } log.Println("[NODE DISCOVERY STOPPED]") return nil }