~charmers/charms/trusty/bird/trunk

« back to all changes in this revision

Viewing changes to hooks/relations.py

  • Committer: Cory Benfield
  • Date: 2015-01-29 16:01:55 UTC
  • Revision ID: cory.benfield@metaswitch.com-20150129160155-lzfikgp865acfi45
Add IPv6 BGP support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
    return str(addr)
56
56
 
57
57
 
 
58
def local_ipv6_address():
 
59
    '''
 
60
    Determines the IPv6 address to use to contact this machine. Excludes
 
61
    link-local addresses.
 
62
 
 
63
    Currently only returns the first valid IPv6 address found.
 
64
    '''
 
65
    for iface in netifaces.interfaces():
 
66
        addresses = netifaces.ifaddresses(iface)
 
67
 
 
68
        for addr in addresses.get(netifaces.AF_INET6, []):
 
69
            # Make sure we strip any interface specifier from the address.
 
70
            addr = netaddr.IPAddress(addr['addr'].split('%')[0])
 
71
 
 
72
            if not (addr.is_link_local() or addr.is_loopback()):
 
73
                return str(addr)
 
74
 
 
75
 
58
76
class BgpRRRelation(RelationContext):
59
77
    '''
60
78
    Relation context for the BGP Route Reflector interface.
67
85
        return True
68
86
 
69
87
    def _is_ready(self, data):
70
 
        return set(data.keys()).issuperset(set(['addr']))
 
88
        return set(data.keys()).issuperset(set(['addr', 'addr6']))
71
89
 
72
90
    def get_data(self):
73
91
        peers = []
 
92
        peers6 = []
74
93
 
75
94
        for rid in hookenv.relation_ids(self.name):
76
95
            for unit in hookenv.related_units(rid):
84
103
                    if addr:
85
104
                        peers.append(addr)
86
105
 
 
106
                rel6 = hookenv.relation_get(attribute='addr6',
 
107
                                            rid=rid,
 
108
                                            unit=unit)
 
109
 
 
110
                if rel6 is not None:
 
111
                    peers6.append(rel6)
 
112
 
87
113
        self['bgp_peers'] = peers
 
114
        self['bgp_peers6'] = peers6
88
115
        self['router_id'] = router_id()
89
116
 
90
117
        return
91
118
 
92
119
    def provide_data(self):
93
 
        return {'addr': hookenv.unit_get('private-address')}
 
120
        return {
 
121
            'addr': hookenv.unit_get('private-address'),
 
122
            'addr6': local_ipv6_address()
 
123
        }