~ionutbalutoiu/charms/trusty/neutron-api/next

« back to all changes in this revision

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

  • Committer: James Page
  • Date: 2015-01-13 09:19:48 UTC
  • mfrom: (70 trunk)
  • mto: This revision was merged to the branch mainline in revision 95.
  • Revision ID: james.page@ubuntu.com-20150113091948-2xyc9gnqoc1jkxma
Rebase on next

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
import subprocess
15
15
import os
 
16
 
16
17
from socket import gethostname as get_unit_hostname
17
18
 
18
19
import six
28
29
    WARNING,
29
30
    unit_get,
30
31
)
 
32
from charmhelpers.core.decorators import (
 
33
    retry_on_exception,
 
34
)
31
35
 
32
36
 
33
37
class HAIncompleteConfig(Exception):
34
38
    pass
35
39
 
36
40
 
 
41
class CRMResourceNotFound(Exception):
 
42
    pass
 
43
 
 
44
 
37
45
def is_elected_leader(resource):
38
46
    """
39
47
    Returns True if the charm executing this is the elected cluster leader.
68
76
    return False
69
77
 
70
78
 
71
 
def is_crm_leader(resource):
 
79
@retry_on_exception(5, base_delay=2, exc_type=CRMResourceNotFound)
 
80
def is_crm_leader(resource, retry=False):
72
81
    """
73
82
    Returns True if the charm calling this is the elected corosync leader,
74
83
    as returned by calling the external "crm" command.
 
84
 
 
85
    We allow this operation to be retried to avoid the possibility of getting a
 
86
    false negative. See LP #1396246 for more info.
75
87
    """
76
 
    cmd = [
77
 
        "crm", "resource",
78
 
        "show", resource
79
 
    ]
 
88
    cmd = ['crm', 'resource', 'show', resource]
80
89
    try:
81
 
        status = subprocess.check_output(cmd).decode('UTF-8')
 
90
        status = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
 
91
        if not isinstance(status, six.text_type):
 
92
            status = six.text_type(status, "utf-8")
82
93
    except subprocess.CalledProcessError:
83
 
        return False
84
 
    else:
85
 
        if get_unit_hostname() in status:
86
 
            return True
87
 
        else:
88
 
            return False
 
94
        status = None
 
95
 
 
96
    if status and get_unit_hostname() in status:
 
97
        return True
 
98
 
 
99
    if status and "resource %s is NOT running" % (resource) in status:
 
100
        raise CRMResourceNotFound("CRM resource %s not found" % (resource))
 
101
 
 
102
    return False
89
103
 
90
104
 
91
105
def is_leader(resource):