17
21
from charmhelpers.fetch import (
19
filter_installed_packages,
23
filter_installed_packages
25
from charmhelpers.contrib.network.ip import (
21
28
from mysql import get_mysql_root_password, MySQLHelper
25
33
except ImportError:
64
73
subprocess.check_call(['apt-key', 'add', KEY])
66
75
TEMPLATES_DIR = 'templates'
69
79
def render_template(template_name, context, template_dir=TEMPLATES_DIR):
73
83
return template.render(context)
76
# TODO: goto charm-helpers (I use this everywhere)
77
86
def get_host_ip(hostname=None):
87
if config('prefer-ipv6'):
88
# Ensure we have a valid ipv6 address configured
89
get_ipv6_addr(exc_list=[config('vip')], fatal=True)[0]
90
return socket.gethostname()
78
92
hostname = hostname or unit_get('private-address')
80
94
# Test to see if already an IPv4 address
91
105
def get_cluster_hosts():
92
hosts = [get_host_ip()]
107
hostname = get_host_ip()
109
# We need to add this localhost dns name to /etc/hosts along with peer
110
# hosts to ensure percona gets consistently resolved addresses.
111
if config('prefer-ipv6'):
112
addr = get_ipv6_addr(exc_list=[config('vip')], fatal=True)[0]
113
hosts_map = {addr: hostname}
93
115
for relid in relation_ids('cluster'):
94
116
for unit in related_units(relid):
95
hosts.append(get_host_ip(relation_get('private-address',
117
rdata = relation_get(unit=unit, rid=relid)
118
private_address = rdata.get('private-address')
119
if config('prefer-ipv6'):
120
hostname = rdata.get('hostname')
121
if not hostname or hostname in hosts:
122
log("(unit=%s) Ignoring hostname '%s' provided by cluster "
123
"relation for addr %s" %
124
(unit, hostname, private_address))
127
log("(unit=%s) hostname '%s' provided by cluster relation "
128
"for addr %s" % (unit, hostname, private_address))
130
hosts_map[private_address] = hostname
131
hosts.append(hostname)
133
hosts.append(get_host_ip(private_address))
136
update_hosts_file(hosts_map)
99
SQL_SST_USER_SETUP = "GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.*" \
100
" TO 'sstuser'@'localhost' IDENTIFIED BY '{}'"
141
SQL_SST_USER_SETUP = ("GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* "
142
"TO 'sstuser'@'localhost' IDENTIFIED BY '{}'")
144
SQL_SST_USER_SETUP_IPV6 = ("GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT "
145
"ON *.* TO 'sstuser'@'ip6-localhost' IDENTIFIED "
103
149
def configure_sstuser(sst_password):
104
150
m_helper = MySQLHelper()
105
151
m_helper.connect(password=get_mysql_root_password())
106
152
m_helper.execute(SQL_SST_USER_SETUP.format(sst_password))
153
m_helper.execute(SQL_SST_USER_SETUP_IPV6.format(sst_password))
109
156
# TODO: mysql charmhelper
185
def update_hosts_file(map):
186
"""Percona does not currently like ipv6 addresses so we need to use dns
187
names instead. In order to make them resolvable we ensure they are in
190
See https://bugs.launchpad.net/galera/+bug/1130595 for some more info.
192
with open(HOSTS_FILE, 'r') as hosts:
193
lines = hosts.readlines()
195
log("Updating hosts file with: %s (current: %s)" % (map, lines),
199
for ip, hostname in map.items():
200
if not ip or not hostname:
206
if len(line) < 2 or not (_line[0] == ip or hostname in _line[1:]):
209
log("Removing line '%s' from hosts file" % (line))
212
newlines.append("%s %s\n" % (ip, hostname))
216
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
217
with open(tmpfile.name, 'w') as hosts:
221
os.rename(tmpfile.name, HOSTS_FILE)
222
os.chmod(HOSTS_FILE, 0o644)
225
def assert_charm_supports_ipv6():
226
"""Check whether we are able to support charms ipv6."""
227
if lsb_release()['DISTRIB_CODENAME'].lower() < "trusty":
228
raise Exception("IPv6 is not supported in the charms for Ubuntu "
229
"versions less than Trusty 14.04")
138
232
def unit_sorted(units):
139
233
"""Return a sorted list of unit names."""