basic_layer (is included to all nodes on the deployment)
from charmhelpers.core.unitdata import kv
from charmhelpers.contrib.network.ip import get_ipv6_addr
cache = kv()
ipv6_addr = get_ipv6_addr()[0]
cache.set('ipv6_addr', ipv6_addr)
Then on my own interface-layer (peers.py):
from charms.reactive import RelationBase
from charms.reactive import hook
from charms.reactive import scopes
@hook('{peers:myapp}-relation-joined')
def peer_joined(self):
'''A new peer has joined, set the state on the unit so we can track
when they are departed. '''
conv = self.conversation()
ipv6_addr = get_ipv6_addr()[0]
hookenv
conv.set_local('ipv6_addr', ipv6_addr)
conv.set_state('{relation_name}.joined')
def list_peer_data(self):
for conv in self.conversations():
yield {'address': conv.get_remote('address'),
'port': conv.get_remote('port'),
'ipv6_addr': conv.get_remote('ipv6_addr')}
def get_peers(self):
'''Return a list of names for the peers participating in this
conversation scope. '''
peers = []
# Iterate over all the conversations of this type.
for conversation in self.conversations():
peers.append(conversation.scope)
return peers
My actual charm which connects the two or more peers:
@when ('myapp.joined')
def set_cluster(myapp):
cluster_rid = hookenv.relation_ids('myapp')
print ("Cluster id %s" %cluster_rid)
if cluster_rid:
peers = myapp.get_peers()
hookenv.log("CONSUL PEERS: %s" %peers)
address = ""
#Go through current peers and join with them
if peers:
for peer in myapp.list_peer_data():
#If peer has ipv6 address use it
#hookenv.log("IPV6 addr: %s" %peer[ipv6_addr])
hookenv.log("BLAAH: %s" %peer)
#hookenv.log("IPV6: " %peer_address)
if peer['ipv6_addr'] is not None:
peer_address = peer['ipv6_addr'][0]
hookenv.log("IPV6: " %peer_address)
address = '[' + peer_address +']'
#Append the join command with address
hookenv.log(output)
#If no ipv6 use ipv4
else:
address = peer['address']
join_command.append(address)
hookenv.log("Join command: %s" %join_command)
output = subprocess.check_output(join_command)
hookenv.log(output)
append_address(address)