~bbaqar/charms/trusty/plumgrid-director/config-changed-fix

« back to all changes in this revision

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

  • Committer: bbaqar at plumgrid
  • Date: 2015-07-29 18:07:31 UTC
  • Revision ID: bbaqar@plumgrid.com-20150729180731-ioynar8x3u5pxytc
Addressed reviews by Charmers

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
    ERROR,
45
45
    WARNING,
46
46
    unit_get,
 
47
    is_leader as juju_is_leader
47
48
)
48
49
from charmhelpers.core.decorators import (
49
50
    retry_on_exception,
63
64
    pass
64
65
 
65
66
 
 
67
class CRMDCNotFound(Exception):
 
68
    pass
 
69
 
 
70
 
66
71
def is_elected_leader(resource):
67
72
    """
68
73
    Returns True if the charm executing this is the elected cluster leader.
69
74
 
70
75
    It relies on two mechanisms to determine leadership:
71
 
        1. If the charm is part of a corosync cluster, call corosync to
 
76
        1. If juju is sufficiently new and leadership election is supported,
 
77
        the is_leader command will be used.
 
78
        2. If the charm is part of a corosync cluster, call corosync to
72
79
        determine leadership.
73
 
        2. If the charm is not part of a corosync cluster, the leader is
 
80
        3. If the charm is not part of a corosync cluster, the leader is
74
81
        determined as being "the alive unit with the lowest unit numer". In
75
82
        other words, the oldest surviving unit.
76
83
    """
 
84
    try:
 
85
        return juju_is_leader()
 
86
    except NotImplementedError:
 
87
        log('Juju leadership election feature not enabled'
 
88
            ', using fallback support',
 
89
            level=WARNING)
 
90
 
77
91
    if is_clustered():
78
92
        if not is_crm_leader(resource):
79
93
            log('Deferring action to CRM leader.', level=INFO)
106
120
        status = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
107
121
        if not isinstance(status, six.text_type):
108
122
            status = six.text_type(status, "utf-8")
109
 
    except subprocess.CalledProcessError:
110
 
        return False
 
123
    except subprocess.CalledProcessError as ex:
 
124
        raise CRMDCNotFound(str(ex))
 
125
 
111
126
    current_dc = ''
112
127
    for line in status.split('\n'):
113
128
        if line.startswith('Current DC'):
115
130
            current_dc = line.split(':')[1].split()[0]
116
131
    if current_dc == get_unit_hostname():
117
132
        return True
 
133
    elif current_dc == 'NONE':
 
134
        raise CRMDCNotFound('Current DC: NONE')
 
135
 
118
136
    return False
119
137
 
120
138
 
121
 
@retry_on_exception(5, base_delay=2, exc_type=CRMResourceNotFound)
 
139
@retry_on_exception(5, base_delay=2,
 
140
                    exc_type=(CRMResourceNotFound, CRMDCNotFound))
122
141
def is_crm_leader(resource, retry=False):
123
142
    """
124
143
    Returns True if the charm calling this is the elected corosync leader,