~openstack-charmers-next/charms/wily/neutron-gateway/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/ha/utils.py

  • Committer: James Page
  • Date: 2016-06-23 08:59:56 UTC
  • Revision ID: james.page@ubuntu.com-20160623085956-6cct6w8bc2vs6qla
Switch to using charm-store for amulet tests

All OpenStack charms are now directly published to the charm store
on landing; switch Amulet helper to resolve charms using the
charm store rather than bzr branches, removing the lag between
charm changes landing and being available for other charms to
use for testing.

This is also important for new layered charms where the charm must
be build and published prior to being consumable.

Change-Id: I543ac243b4e8c3deb7c88d641d0f50bda812d284

Show diffs side-by-side

added added

removed removed

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