~hopem/charm-helpers/fix-ssl-disable

« back to all changes in this revision

Viewing changes to charmhelpers/contrib/database/mysql.py

  • Committer: Jorge Niedbalski
  • Date: 2015-02-27 16:10:02 UTC
  • mfrom: (323.1.3 charm-helpers)
  • Revision ID: jorge.niedbalski@canonical.com-20150227161002-zdnccjlg6nl4y0x4
[hopem, r=niedbalski] Fixes Bug LP: #1425999

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Helper for working with a MySQL database"""
2
2
import json
3
 
import socket
4
3
import re
5
4
import sys
6
5
import platform
15
14
    write_file
16
15
)
17
16
from charmhelpers.core.hookenv import (
 
17
    config as config_get,
18
18
    relation_get,
19
19
    related_units,
20
20
    unit_get,
22
22
    DEBUG,
23
23
    INFO,
24
24
)
25
 
from charmhelpers.core.hookenv import config as config_get
26
25
from charmhelpers.fetch import (
27
26
    apt_install,
28
27
    apt_update,
32
31
    peer_store,
33
32
    peer_retrieve,
34
33
)
 
34
from charmhelpers.contrib.network.ip import get_host_ip
35
35
 
36
36
try:
37
37
    import MySQLdb
220
220
        """Retrieve or generate mysql root password for service units."""
221
221
        return self.get_mysql_password(username=None, password=password)
222
222
 
 
223
    def normalize_address(self, hostname):
 
224
        """Ensure that address returned is an IP address (i.e. not fqdn)"""
 
225
        if config_get('prefer-ipv6'):
 
226
            # TODO: add support for ipv6 dns
 
227
            return hostname
 
228
 
 
229
        if hostname != unit_get('private-address'):
 
230
            return get_host_ip(hostname, fallback=hostname)
 
231
 
 
232
        # Otherwise assume localhost
 
233
        return '127.0.0.1'
 
234
 
223
235
    def get_allowed_units(self, database, username, relation_id=None):
224
236
        """Get list of units with access grants for database with username.
225
237
 
247
259
 
248
260
            if hosts:
249
261
                for host in hosts:
 
262
                    host = self.normalize_address(host)
250
263
                    if self.grant_exists(database, username, host):
251
264
                        log("Grant exists for host '%s' on db '%s'" %
252
265
                            (host, database), level=DEBUG)
262
275
 
263
276
    def configure_db(self, hostname, database, username, admin=False):
264
277
        """Configure access to database for username from hostname."""
265
 
        if config_get('prefer-ipv6'):
266
 
            remote_ip = hostname
267
 
        elif hostname != unit_get('private-address'):
268
 
            try:
269
 
                remote_ip = socket.gethostbyname(hostname)
270
 
            except Exception:
271
 
                # socket.gethostbyname doesn't support ipv6
272
 
                remote_ip = hostname
273
 
        else:
274
 
            remote_ip = '127.0.0.1'
275
 
 
276
278
        self.connect(password=self.get_mysql_root_password())
277
279
        if not self.database_exists(database):
278
280
            self.create_database(database)
279
281
 
 
282
        remote_ip = self.normalize_address(hostname)
280
283
        password = self.get_mysql_password(username)
281
284
        if not self.grant_exists(database, username, remote_ip):
282
285
            if not admin: