~gnuoy/charms/trusty/percona-cluster/retry-loner

« back to all changes in this revision

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

  • Committer: Edward Hope-Morley
  • Date: 2015-05-06 16:43:56 UTC
  • mfrom: (56.1.1 percona-cluster.lp1451890)
  • Revision ID: edward.hope-morley@canonical.com-20150506164356-fj8iluvq7lufii91
[hopem,r=gnuoy]

Sync charm-helpers to get fix for LP 1451890.

Fixes issue with passwords stored in peer relation.
Upgrading will no longer ignore passwords stored
using old-style keys.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import os
7
7
import glob
8
8
 
9
 
from string import upper
 
9
# from string import upper
10
10
 
11
11
from charmhelpers.core.host import (
12
12
    mkdir,
21
21
    log,
22
22
    DEBUG,
23
23
    INFO,
 
24
    WARNING,
24
25
)
25
26
from charmhelpers.fetch import (
26
27
    apt_install,
142
143
                log("Excluding %s from peer migration" % (f), level=DEBUG)
143
144
                continue
144
145
 
145
 
            _key = os.path.basename(f)
 
146
            key = os.path.basename(f)
146
147
            with open(f, 'r') as passwd:
147
148
                _value = passwd.read().strip()
148
149
 
149
150
            try:
150
 
                peer_store(_key, _value)
 
151
                peer_store(key, _value)
 
152
 
151
153
                if self.delete_ondisk_passwd_file:
152
154
                    os.unlink(f)
153
155
            except ValueError:
185
187
 
186
188
        return _password
187
189
 
 
190
    def passwd_keys(self, username):
 
191
        """Generator to return keys used to store passwords in peer store.
 
192
 
 
193
        NOTE: we support both legacy and new format to support mysql
 
194
        charm prior to refactor. This is necessary to avoid LP 1451890.
 
195
        """
 
196
        keys = []
 
197
        if username == 'mysql':
 
198
            log("Bad username '%s'" % (username), level=WARNING)
 
199
 
 
200
        if username:
 
201
            # IMPORTANT: *newer* format must be returned first
 
202
            keys.append('mysql-%s.passwd' % (username))
 
203
            keys.append('%s.passwd' % (username))
 
204
        else:
 
205
            keys.append('mysql.passwd')
 
206
 
 
207
        for key in keys:
 
208
            yield key
 
209
 
188
210
    def get_mysql_password(self, username=None, password=None):
189
211
        """Retrieve, generate or store a mysql password for the provided
190
212
        username using peer relation cluster."""
191
213
        excludes = []
192
214
 
193
 
        # First check peer relation
194
 
        if username:
195
 
            _key = 'mysql-{}.passwd'.format(username)
196
 
        else:
197
 
            _key = 'mysql.passwd'
198
 
 
 
215
        # First check peer relation.
199
216
        try:
200
 
            _password = peer_retrieve(_key)
 
217
            for key in self.passwd_keys(username):
 
218
                _password = peer_retrieve(key)
 
219
                if _password:
 
220
                    break
 
221
 
201
222
            # If root password available don't update peer relation from local
202
223
            if _password and not username:
203
224
                excludes.append(self.root_passwd_file_template)
348
369
                key, mem = line.split(':', 2)
349
370
                if key == 'MemTotal':
350
371
                    mtot, modifier = mem.strip().split(' ')
351
 
                    return '%s%s' % (mtot, upper(modifier[0]))
 
372
                    return '%s%s' % (mtot, modifier[0].upper())
352
373
 
353
374
    def parse_config(self):
354
375
        """Parse charm configuration and calculate values for config files."""