~corey.bryant/charms/trusty/neutron-api/db-stamp

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/hahelpers/cluster.py

  • Committer: james.page at ubuntu
  • Date: 2014-10-01 21:05:24 UTC
  • mfrom: (39.3.25 ipv6)
  • Revision ID: james.page@ubuntu.com-20141001210524-z6uqyljzorphrhy6
[xianghui,dosaboy,r=james-page,t=gema] Add IPv6 support using prefer-ipv6 flag

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
#  Adam Gandelman <adamg@ubuntu.com>
7
7
#
8
8
 
 
9
"""
 
10
Helpers for clustering and determining "cluster leadership" and other
 
11
clustering-related helpers.
 
12
"""
 
13
 
9
14
import subprocess
10
15
import os
11
16
 
19
24
    config as config_get,
20
25
    INFO,
21
26
    ERROR,
 
27
    WARNING,
22
28
    unit_get,
23
29
)
24
30
 
27
33
    pass
28
34
 
29
35
 
 
36
def is_elected_leader(resource):
 
37
    """
 
38
    Returns True if the charm executing this is the elected cluster leader.
 
39
 
 
40
    It relies on two mechanisms to determine leadership:
 
41
        1. If the charm is part of a corosync cluster, call corosync to
 
42
        determine leadership.
 
43
        2. If the charm is not part of a corosync cluster, the leader is
 
44
        determined as being "the alive unit with the lowest unit numer". In
 
45
        other words, the oldest surviving unit.
 
46
    """
 
47
    if is_clustered():
 
48
        if not is_crm_leader(resource):
 
49
            log('Deferring action to CRM leader.', level=INFO)
 
50
            return False
 
51
    else:
 
52
        peers = peer_units()
 
53
        if peers and not oldest_peer(peers):
 
54
            log('Deferring action to oldest service unit.', level=INFO)
 
55
            return False
 
56
    return True
 
57
 
 
58
 
30
59
def is_clustered():
31
60
    for r_id in (relation_ids('ha') or []):
32
61
        for unit in (relation_list(r_id) or []):
38
67
    return False
39
68
 
40
69
 
41
 
def is_leader(resource):
 
70
def is_crm_leader(resource):
 
71
    """
 
72
    Returns True if the charm calling this is the elected corosync leader,
 
73
    as returned by calling the external "crm" command.
 
74
    """
42
75
    cmd = [
43
76
        "crm", "resource",
44
77
        "show", resource
54
87
            return False
55
88
 
56
89
 
57
 
def peer_units():
 
90
def is_leader(resource):
 
91
    log("is_leader is deprecated. Please consider using is_crm_leader "
 
92
        "instead.", level=WARNING)
 
93
    return is_crm_leader(resource)
 
94
 
 
95
 
 
96
def peer_units(peer_relation="cluster"):
58
97
    peers = []
59
 
    for r_id in (relation_ids('cluster') or []):
 
98
    for r_id in (relation_ids(peer_relation) or []):
60
99
        for unit in (relation_list(r_id) or []):
61
100
            peers.append(unit)
62
101
    return peers
63
102
 
64
103
 
 
104
def peer_ips(peer_relation='cluster', addr_key='private-address'):
 
105
    '''Return a dict of peers and their private-address'''
 
106
    peers = {}
 
107
    for r_id in relation_ids(peer_relation):
 
108
        for unit in relation_list(r_id):
 
109
            peers[unit] = relation_get(addr_key, rid=r_id, unit=unit)
 
110
    return peers
 
111
 
 
112
 
65
113
def oldest_peer(peers):
 
114
    """Determines who the oldest peer is by comparing unit numbers."""
66
115
    local_unit_no = int(os.getenv('JUJU_UNIT_NAME').split('/')[1])
67
116
    for peer in peers:
68
117
        remote_unit_no = int(peer.split('/')[1])
72
121
 
73
122
 
74
123
def eligible_leader(resource):
75
 
    if is_clustered():
76
 
        if not is_leader(resource):
77
 
            log('Deferring action to CRM leader.', level=INFO)
78
 
            return False
79
 
    else:
80
 
        peers = peer_units()
81
 
        if peers and not oldest_peer(peers):
82
 
            log('Deferring action to oldest service unit.', level=INFO)
83
 
            return False
84
 
    return True
 
124
    log("eligible_leader is deprecated. Please consider using "
 
125
        "is_elected_leader instead.", level=WARNING)
 
126
    return is_elected_leader(resource)
85
127
 
86
128
 
87
129
def https():
97
139
        return True
98
140
    for r_id in relation_ids('identity-service'):
99
141
        for unit in relation_list(r_id):
 
142
            # TODO - needs fixing for new helper as ssl_cert/key suffixes with CN
100
143
            rel_state = [
101
144
                relation_get('https_keystone', rid=r_id, unit=unit),
102
 
                relation_get('ssl_cert', rid=r_id, unit=unit),
103
 
                relation_get('ssl_key', rid=r_id, unit=unit),
104
145
                relation_get('ca_cert', rid=r_id, unit=unit),
105
146
            ]
106
147
            # NOTE: works around (LP: #1203241)