1
# Copyright 2014-2016 Canonical Limited.
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
7
# http://www.apache.org/licenses/LICENSE-2.0
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
# Copyright 2016 Canonical Ltd.
19
# Openstack Charmers <
23
Helpers for high availability.
28
from charmhelpers.core.hookenv import (
37
from charmhelpers.core.host import (
41
from charmhelpers.contrib.openstack.ip import (
46
class DNSHAException(Exception):
47
"""Raised when an error occurs setting up DNS HA
53
def update_dns_ha_resource_params(resources, resource_params,
55
crm_ocf='ocf:maas:dns'):
56
""" Check for os-*-hostname settings and update resource dictionaries for
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
68
# Validate the charm environment for DNS HA
69
assert_charm_supports_dns_ha()
71
settings = ['os-admin-hostname', 'os-internal-hostname',
72
'os-public-hostname', 'os-access-hostname']
74
# Check which DNS settings are set and update dictionaries
76
for setting in settings:
77
hostname = config(setting)
79
log('DNS HA: Hostname setting {} is None. Ignoring.'
83
m = re.search('os-(.+?)-hostname', setting)
85
networkspace = m.group(1)
87
msg = ('Unexpected DNS hostname setting: {}. '
88
'Cannot determine network space name'
90
status_set('blocked', msg)
91
raise DNSHAException(msg)
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),
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,
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)),
111
relation_set(relation_id=relation_id, groups={
112
'grp_{}_hostnames'.format(charm_name()): ' '.join(hostname_group)})
114
msg = 'DNS HA: Hostname group has no members.'
115
status_set('blocked', msg)
116
raise DNSHAException(msg)
119
def assert_charm_supports_dns_ha():
120
"""Validate prerequisites for DNS HA
121
The MAAS client is only available on Xenial or greater
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)