~james-page/charms/trusty/glance/tox

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2015-06-04 08:44:46 UTC
  • mfrom: (117 glance)
  • mto: This revision was merged to the branch mainline in revision 118.
  • Revision ID: liam.young@canonical.com-20150604084446-xsroavxpfefmfynj
Resync le charm helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
    bool_from_string,
54
54
)
55
55
 
 
56
DC_RESOURCE_NAME = 'DC'
 
57
 
56
58
 
57
59
class HAIncompleteConfig(Exception):
58
60
    pass
105
107
    return False
106
108
 
107
109
 
 
110
def is_crm_dc():
 
111
    """
 
112
    Determine leadership by querying the pacemaker Designated Controller
 
113
    """
 
114
    cmd = ['crm', 'status']
 
115
    try:
 
116
        status = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
 
117
        if not isinstance(status, six.text_type):
 
118
            status = six.text_type(status, "utf-8")
 
119
    except subprocess.CalledProcessError:
 
120
        return False
 
121
    current_dc = ''
 
122
    for line in status.split('\n'):
 
123
        if line.startswith('Current DC'):
 
124
            # Current DC: juju-lytrusty-machine-2 (168108163) - partition with quorum
 
125
            current_dc = line.split(':')[1].split()[0]
 
126
    if current_dc == get_unit_hostname():
 
127
        return True
 
128
    return False
 
129
 
 
130
 
108
131
@retry_on_exception(5, base_delay=2, exc_type=CRMResourceNotFound)
109
132
def is_crm_leader(resource, retry=False):
110
133
    """
114
137
    We allow this operation to be retried to avoid the possibility of getting a
115
138
    false negative. See LP #1396246 for more info.
116
139
    """
 
140
    if resource == DC_RESOURCE_NAME:
 
141
        return is_crm_dc()
117
142
    cmd = ['crm', 'resource', 'show', resource]
118
143
    try:
119
144
        status = subprocess.check_output(cmd, stderr=subprocess.STDOUT)