~openstack-charmers-next/charms/trusty/rabbitmq-server/trunk

« back to all changes in this revision

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

  • Committer: James Page
  • Date: 2016-07-06 15:43:20 UTC
  • Revision ID: james.page@ubuntu.com-20160706154320-b493t8fv10dvt99t
Resync charmhelpers for licensing change

The charm-helpers project have re-licensed to Apache 2.0
inline with the agreed licensing approach to intefaces,
layers and charms generally.

Resync helpers to bring charmhelpers inline with charm
codebase.

Change-Id: I4999da180d6bb9d3763367c488469f6fb936f6f2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2014-2015 Canonical Limited.
2
2
#
3
 
# This file is part of charm-helpers.
4
 
#
5
 
# charm-helpers is free software: you can redistribute it and/or modify
6
 
# it under the terms of the GNU Lesser General Public License version 3 as
7
 
# published by the Free Software Foundation.
8
 
#
9
 
# charm-helpers is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU Lesser General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU Lesser General Public License
15
 
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
#  http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
16
14
 
17
15
#
18
16
# Copyright 2012 Canonical Ltd.
41
39
    relation_get,
42
40
    config as config_get,
43
41
    INFO,
44
 
    ERROR,
 
42
    DEBUG,
45
43
    WARNING,
46
44
    unit_get,
47
 
    is_leader as juju_is_leader
 
45
    is_leader as juju_is_leader,
 
46
    status_set,
48
47
)
49
48
from charmhelpers.core.decorators import (
50
49
    retry_on_exception,
60
59
    pass
61
60
 
62
61
 
 
62
class HAIncorrectConfig(Exception):
 
63
    pass
 
64
 
 
65
 
63
66
class CRMResourceNotFound(Exception):
64
67
    pass
65
68
 
274
277
    Obtains all relevant configuration from charm configuration required
275
278
    for initiating a relation to hacluster:
276
279
 
277
 
        ha-bindiface, ha-mcastport, vip
 
280
        ha-bindiface, ha-mcastport, vip, os-internal-hostname,
 
281
        os-admin-hostname, os-public-hostname, os-access-hostname
278
282
 
279
283
    param: exclude_keys: list of setting key(s) to be excluded.
280
284
    returns: dict: A dict containing settings keyed by setting name.
281
 
    raises: HAIncompleteConfig if settings are missing.
 
285
    raises: HAIncompleteConfig if settings are missing or incorrect.
282
286
    '''
283
 
    settings = ['ha-bindiface', 'ha-mcastport', 'vip']
 
287
    settings = ['ha-bindiface', 'ha-mcastport', 'vip', 'os-internal-hostname',
 
288
                'os-admin-hostname', 'os-public-hostname', 'os-access-hostname']
284
289
    conf = {}
285
290
    for setting in settings:
286
291
        if exclude_keys and setting in exclude_keys:
287
292
            continue
288
293
 
289
294
        conf[setting] = config_get(setting)
290
 
    missing = []
291
 
    [missing.append(s) for s, v in six.iteritems(conf) if v is None]
292
 
    if missing:
293
 
        log('Insufficient config data to configure hacluster.', level=ERROR)
294
 
        raise HAIncompleteConfig
 
295
 
 
296
    if not valid_hacluster_config():
 
297
        raise HAIncorrectConfig('Insufficient or incorrect config data to '
 
298
                                'configure hacluster.')
295
299
    return conf
296
300
 
297
301
 
 
302
def valid_hacluster_config():
 
303
    '''
 
304
    Check that either vip or dns-ha is set. If dns-ha then one of os-*-hostname
 
305
    must be set.
 
306
 
 
307
    Note: ha-bindiface and ha-macastport both have defaults and will always
 
308
    be set. We only care that either vip or dns-ha is set.
 
309
 
 
310
    :returns: boolean: valid config returns true.
 
311
    raises: HAIncompatibileConfig if settings conflict.
 
312
    raises: HAIncompleteConfig if settings are missing.
 
313
    '''
 
314
    vip = config_get('vip')
 
315
    dns = config_get('dns-ha')
 
316
    if not(bool(vip) ^ bool(dns)):
 
317
        msg = ('HA: Either vip or dns-ha must be set but not both in order to '
 
318
               'use high availability')
 
319
        status_set('blocked', msg)
 
320
        raise HAIncorrectConfig(msg)
 
321
 
 
322
    # If dns-ha then one of os-*-hostname must be set
 
323
    if dns:
 
324
        dns_settings = ['os-internal-hostname', 'os-admin-hostname',
 
325
                        'os-public-hostname', 'os-access-hostname']
 
326
        # At this point it is unknown if one or all of the possible
 
327
        # network spaces are in HA. Validate at least one is set which is
 
328
        # the minimum required.
 
329
        for setting in dns_settings:
 
330
            if config_get(setting):
 
331
                log('DNS HA: At least one hostname is set {}: {}'
 
332
                    ''.format(setting, config_get(setting)),
 
333
                    level=DEBUG)
 
334
                return True
 
335
 
 
336
        msg = ('DNS HA: At least one os-*-hostname(s) must be set to use '
 
337
               'DNS HA')
 
338
        status_set('blocked', msg)
 
339
        raise HAIncompleteConfig(msg)
 
340
 
 
341
    log('VIP HA: VIP is set {}'.format(vip), level=DEBUG)
 
342
    return True
 
343
 
 
344
 
298
345
def canonical_url(configs, vip_setting='vip'):
299
346
    '''
300
347
    Returns the correct HTTP URL to this host given the state of HTTPS