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

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/ha/utils.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
# Copyright 2014-2016 Canonical Limited.
 
2
#
 
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.
 
14
 
 
15
#
 
16
# Copyright 2016 Canonical Ltd.
 
17
#
 
18
# Authors:
 
19
#  Openstack Charmers <
 
20
#
 
21
 
 
22
"""
 
23
Helpers for high availability.
 
24
"""
 
25
 
 
26
import re
 
27
 
 
28
from charmhelpers.core.hookenv import (
 
29
    log,
 
30
    relation_set,
 
31
    charm_name,
 
32
    config,
 
33
    status_set,
 
34
    DEBUG,
 
35
)
 
36
 
 
37
from charmhelpers.core.host import (
 
38
    lsb_release
 
39
)
 
40
 
 
41
from charmhelpers.contrib.openstack.ip import (
 
42
    resolve_address,
 
43
)
 
44
 
 
45
 
 
46
class DNSHAException(Exception):
 
47
    """Raised when an error occurs setting up DNS HA
 
48
    """
 
49
 
 
50
    pass
 
51
 
 
52
 
 
53
def update_dns_ha_resource_params(resources, resource_params,
 
54
                                  relation_id=None,
 
55
                                  crm_ocf='ocf:maas:dns'):
 
56
    """ Check for os-*-hostname settings and update resource dictionaries for
 
57
    the HA relation.
 
58
 
 
59
    @param resources: Pointer to dictionary of resources.
 
60
                      Usually instantiated in ha_joined().
 
61
    @param resource_params: Pointer to dictionary of resource parameters.
 
62
                            Usually instantiated in ha_joined()
 
63
    @param relation_id: Relation ID of the ha relation
 
64
    @param crm_ocf: Corosync Open Cluster Framework resource agent to use for
 
65
                    DNS HA
 
66
    """
 
67
 
 
68
    # Validate the charm environment for DNS HA
 
69
    assert_charm_supports_dns_ha()
 
70
 
 
71
    settings = ['os-admin-hostname', 'os-internal-hostname',
 
72
                'os-public-hostname', 'os-access-hostname']
 
73
 
 
74
    # Check which DNS settings are set and update dictionaries
 
75
    hostname_group = []
 
76
    for setting in settings:
 
77
        hostname = config(setting)
 
78
        if hostname is None:
 
79
            log('DNS HA: Hostname setting {} is None. Ignoring.'
 
80
                ''.format(setting),
 
81
                DEBUG)
 
82
            continue
 
83
        m = re.search('os-(.+?)-hostname', setting)
 
84
        if m:
 
85
            networkspace = m.group(1)
 
86
        else:
 
87
            msg = ('Unexpected DNS hostname setting: {}. '
 
88
                   'Cannot determine network space name'
 
89
                   ''.format(setting))
 
90
            status_set('blocked', msg)
 
91
            raise DNSHAException(msg)
 
92
 
 
93
        hostname_key = 'res_{}_{}_hostname'.format(charm_name(), networkspace)
 
94
        if hostname_key in hostname_group:
 
95
            log('DNS HA: Resource {}: {} already exists in '
 
96
                'hostname group - skipping'.format(hostname_key, hostname),
 
97
                DEBUG)
 
98
            continue
 
99
 
 
100
        hostname_group.append(hostname_key)
 
101
        resources[hostname_key] = crm_ocf
 
102
        resource_params[hostname_key] = (
 
103
            'params fqdn="{}" ip_address="{}" '
 
104
            ''.format(hostname, resolve_address(endpoint_type=networkspace,
 
105
                                                override=False)))
 
106
 
 
107
    if len(hostname_group) >= 1:
 
108
        log('DNS HA: Hostname group is set with {} as members. '
 
109
            'Informing the ha relation'.format(' '.join(hostname_group)),
 
110
            DEBUG)
 
111
        relation_set(relation_id=relation_id, groups={
 
112
            'grp_{}_hostnames'.format(charm_name()): ' '.join(hostname_group)})
 
113
    else:
 
114
        msg = 'DNS HA: Hostname group has no members.'
 
115
        status_set('blocked', msg)
 
116
        raise DNSHAException(msg)
 
117
 
 
118
 
 
119
def assert_charm_supports_dns_ha():
 
120
    """Validate prerequisites for DNS HA
 
121
    The MAAS client is only available on Xenial or greater
 
122
    """
 
123
    if lsb_release().get('DISTRIB_RELEASE') < '16.04':
 
124
        msg = ('DNS HA is only supported on 16.04 and greater '
 
125
               'versions of Ubuntu.')
 
126
        status_set('blocked', msg)
 
127
        raise DNSHAException(msg)
 
128
    return True