~hopem/charms/trusty/cinder/ensure-apache-restart

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2015-01-09 16:02:39 UTC
  • mfrom: (65 cinder.next)
  • mto: This revision was merged to the branch mainline in revision 67.
  • Revision ID: liam.young@canonical.com-20150109160239-qldk423wxfno2ao3
Merged next in and resolved conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
from socket import gethostname as get_unit_hostname
18
18
 
 
19
import six
 
20
 
19
21
from charmhelpers.core.hookenv import (
20
22
    log,
21
23
    relation_ids,
27
29
    WARNING,
28
30
    unit_get,
29
31
)
 
32
from charmhelpers.core.decorators import (
 
33
    retry_on_exception,
 
34
)
30
35
 
31
36
 
32
37
class HAIncompleteConfig(Exception):
33
38
    pass
34
39
 
35
40
 
 
41
class CRMResourceNotFound(Exception):
 
42
    pass
 
43
 
 
44
 
36
45
def is_elected_leader(resource):
37
46
    """
38
47
    Returns True if the charm executing this is the elected cluster leader.
67
76
    return False
68
77
 
69
78
 
70
 
def is_crm_leader(resource):
 
79
@retry_on_exception(5, base_delay=2, exc_type=CRMResourceNotFound)
 
80
def is_crm_leader(resource, retry=False):
71
81
    """
72
82
    Returns True if the charm calling this is the elected corosync leader,
73
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.
74
87
    """
75
 
    cmd = [
76
 
        "crm", "resource",
77
 
        "show", resource
78
 
    ]
 
88
    cmd = ['crm', 'resource', 'show', resource]
79
89
    try:
80
 
        status = subprocess.check_output(cmd)
 
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")
81
93
    except subprocess.CalledProcessError:
82
 
        return False
83
 
    else:
84
 
        if get_unit_hostname() in status:
85
 
            return True
86
 
        else:
87
 
            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
88
103
 
89
104
 
90
105
def is_leader(resource):
150
165
    return False
151
166
 
152
167
 
153
 
def determine_api_port(public_port):
 
168
def determine_api_port(public_port, singlenode_mode=False):
154
169
    '''
155
170
    Determine correct API server listening port based on
156
171
    existence of HTTPS reverse proxy and/or haproxy.
157
172
 
158
173
    public_port: int: standard public port for given service
159
174
 
 
175
    singlenode_mode: boolean: Shuffle ports when only a single unit is present
 
176
 
160
177
    returns: int: the correct listening port for the API service
161
178
    '''
162
179
    i = 0
163
 
    if len(peer_units()) > 0 or is_clustered():
 
180
    if singlenode_mode:
 
181
        i += 1
 
182
    elif len(peer_units()) > 0 or is_clustered():
164
183
        i += 1
165
184
    if https():
166
185
        i += 1
167
186
    return public_port - (i * 10)
168
187
 
169
188
 
170
 
def determine_apache_port(public_port):
 
189
def determine_apache_port(public_port, singlenode_mode=False):
171
190
    '''
172
191
    Description: Determine correct apache listening port based on public IP +
173
192
    state of the cluster.
174
193
 
175
194
    public_port: int: standard public port for given service
176
195
 
 
196
    singlenode_mode: boolean: Shuffle ports when only a single unit is present
 
197
 
177
198
    returns: int: the correct listening port for the HAProxy service
178
199
    '''
179
200
    i = 0
180
 
    if len(peer_units()) > 0 or is_clustered():
 
201
    if singlenode_mode:
 
202
        i += 1
 
203
    elif len(peer_units()) > 0 or is_clustered():
181
204
        i += 1
182
205
    return public_port - (i * 10)
183
206
 
197
220
    for setting in settings:
198
221
        conf[setting] = config_get(setting)
199
222
    missing = []
200
 
    [missing.append(s) for s, v in conf.iteritems() if v is None]
 
223
    [missing.append(s) for s, v in six.iteritems(conf) if v is None]
201
224
    if missing:
202
225
        log('Insufficient config data to configure hacluster.', level=ERROR)
203
226
        raise HAIncompleteConfig