github.com/kafkaliu/etcd@v0.1.2-0.20131007164923-44c16dd30d69/machines.go (about)

     1  /*
     2  Copyright 2013 CoreOS Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  // machineNum returns the number of machines in the cluster
    20  func machineNum() int {
    21  	response, _ := etcdStore.RawGet("_etcd/machines")
    22  
    23  	return len(response)
    24  }
    25  
    26  // getMachines gets the current machines in the cluster
    27  func getMachines(toURL func(string) (string, bool)) []string {
    28  
    29  	peers := r.Peers()
    30  
    31  	machines := make([]string, len(peers)+1)
    32  
    33  	leader, ok := toURL(r.Leader())
    34  	self, _ := toURL(r.Name())
    35  	i := 1
    36  
    37  	if ok {
    38  		machines[0] = leader
    39  		if leader != self {
    40  			machines[1] = self
    41  			i = 2
    42  		}
    43  	} else {
    44  		machines[0] = self
    45  	}
    46  
    47  	// Add all peers to the slice
    48  	for peerName, _ := range peers {
    49  		if machine, ok := toURL(peerName); ok {
    50  			// do not add leader twice
    51  			if machine != leader {
    52  				machines[i] = machine
    53  				i++
    54  			}
    55  		}
    56  	}
    57  	return machines
    58  }