1
# Copyright 2014-2016 Canonical Limited.
3
# This file is part of charm-helpers.
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.
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.
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/>.
18
# Copyright 2016 Canonical Ltd.
21
# Openstack Charmers <
25
Helpers for high availability.
30
from charmhelpers.core.hookenv import (
39
from charmhelpers.core.host import (
43
from charmhelpers.contrib.openstack.ip import (
48
class DNSHAException(Exception):
49
"""Raised when an error occurs setting up DNS HA
55
def update_dns_ha_resource_params(resources, resource_params,
57
crm_ocf='ocf:maas:dns'):
58
""" Check for os-*-hostname settings and update resource dictionaries for
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
70
# Validate the charm environment for DNS HA
71
assert_charm_supports_dns_ha()
73
settings = ['os-admin-hostname', 'os-internal-hostname',
74
'os-public-hostname', 'os-access-hostname']
76
# Check which DNS settings are set and update dictionaries
78
for setting in settings:
79
hostname = config(setting)
81
log('DNS HA: Hostname setting {} is None. Ignoring.'
85
m = re.search('os-(.+?)-hostname', setting)
87
networkspace = m.group(1)
89
msg = ('Unexpected DNS hostname setting: {}. '
90
'Cannot determine network space name'
92
status_set('blocked', msg)
93
raise DNSHAException(msg)
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),
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,
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)),
113
relation_set(relation_id=relation_id, groups={
114
'grp_{}_hostnames'.format(charm_name()): ' '.join(hostname_group)})
116
msg = 'DNS HA: Hostname group has no members.'
117
status_set('blocked', msg)
118
raise DNSHAException(msg)
121
def assert_charm_supports_dns_ha():
122
"""Validate prerequisites for DNS HA
123
The MAAS client is only available on Xenial or greater
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)