// SelectPeerAddress returns the address to use as the mongo replica set peer // address by selecting it from the given addresses. If no addresses are // available an empty string is returned. func SelectPeerAddress(addrs []network.Address) string { logger.Debugf("selecting mongo peer address from %+v", addrs) // ScopeMachineLocal addresses are OK if we can't pick by space, also the // second bool return is ignored intentionally. addr, _ := network.SelectControllerAddress(addrs, true) return addr.Value }
// controllerAddresses returns the list of internal addresses of the state // server machines. func (st *State) controllerAddresses() ([]string, error) { ssState := st model, err := st.ControllerModel() if err != nil { return nil, errors.Trace(err) } if st.ModelTag() != model.ModelTag() { // We are not using the controller model, so get one. logger.Debugf("getting a controller state connection, current env: %s", st.ModelTag()) ssState, err = st.ForModel(model.ModelTag()) if err != nil { return nil, errors.Trace(err) } defer ssState.Close() logger.Debugf("ssState env: %s", ssState.ModelTag()) } type addressMachine struct { Addresses []address } var allAddresses []addressMachine // TODO(rog) 2013/10/14 index machines on jobs. machines, closer := ssState.getCollection(machinesC) defer closer() err = machines.Find(bson.D{{"jobs", JobManageModel}}).All(&allAddresses) if err != nil { return nil, err } if len(allAddresses) == 0 { return nil, errors.New("no controller machines found") } apiAddrs := make([]string, 0, len(allAddresses)) for _, addrs := range allAddresses { naddrs := networkAddresses(addrs.Addresses) addr, ok := network.SelectControllerAddress(naddrs, false) if ok { apiAddrs = append(apiAddrs, addr.Value) } } if len(apiAddrs) == 0 { return nil, errors.New("no controller machines with addresses found") } return apiAddrs, nil }