Ubuntu Pastebin

Paste from ipv6_address at Wed, 9 Nov 2016 10:22:55 +0000

Download as text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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)

   
Download as text