~ev/charms/xenial/cassandra/snap

« back to all changes in this revision

Viewing changes to hooks/helpers.py

  • Committer: Cory Johns
  • Date: 2016-02-25 17:35:50 UTC
  • Revision ID: cory.johns@canonical.com-20160225173550-3gl1t7elhruaw84o
Revert previous merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
import configparser
17
17
from contextlib import contextmanager
18
18
from datetime import timedelta
19
 
from distutils.version import LooseVersion
20
19
import errno
21
20
from functools import wraps
22
21
import io
178
177
 
179
178
def get_all_database_directories():
180
179
    config = hookenv.config()
181
 
    dirs = dict(
 
180
    return dict(
182
181
        data_file_directories=[get_database_directory(d)
183
182
                               for d in (config['data_file_directories'] or
184
183
                                         'data').split()],
186
185
            config['commitlog_directory'] or 'commitlog'),
187
186
        saved_caches_directory=get_database_directory(
188
187
            config['saved_caches_directory'] or 'saved_caches'))
189
 
    if has_cassandra_version('3.0'):
190
 
        # Not yet configurable. Make configurable with Juju native storage.
191
 
        dirs['hints_directory'] = get_database_directory('hints')
192
 
    return dirs
193
188
 
194
189
 
195
190
# FOR CHARMHELPERS
308
303
 
309
304
def get_cassandra_version():
310
305
    if get_cassandra_edition() == 'dse':
311
 
        dse_ver = get_package_version('dse-full')
312
 
        if not dse_ver:
313
 
            return None
314
 
        elif LooseVersion(dse_ver) >= LooseVersion('4.7'):
315
 
            return '2.1'
316
 
        else:
317
 
            return '2.0'
 
306
        return '2.1' if get_package_version('dse-full') else None
318
307
    return get_package_version('cassandra')
319
308
 
320
309
 
321
 
def has_cassandra_version(minimum_ver):
322
 
    cassandra_version = get_cassandra_version()
323
 
    assert cassandra_version is not None, 'Cassandra package not yet installed'
324
 
    return LooseVersion(cassandra_version) >= LooseVersion(minimum_ver)
325
 
 
326
 
 
327
310
def get_cassandra_config_dir():
328
311
    if get_cassandra_edition() == 'dse':
329
312
        return '/etc/dse/cassandra'
371
354
        # agreement.
372
355
        pass
373
356
    else:
374
 
        # NB. OpenJDK 8 not available in trusty. This needs to come
375
 
        # from a PPA or some other configured source.
376
 
        packages.add('openjdk-8-jre-headless')
 
357
        # NB. OpenJDK 8 not available in trusty.
 
358
        packages.add('openjdk-7-jre-headless')
377
359
 
378
360
    return packages
379
361
 
452
434
    # harmless, it causes shutil.chown() to fail.
453
435
    assert not is_cassandra_running()
454
436
    db_dirs = get_all_database_directories()
455
 
    ensure_database_directory(db_dirs['commitlog_directory'])
456
 
    ensure_database_directory(db_dirs['saved_caches_directory'])
457
 
    if 'hints_directory' in db_dirs:
458
 
        ensure_database_directory(db_dirs['hints_directory'])
459
 
    for db_dir in db_dirs['data_file_directories']:
 
437
    unpacked_db_dirs = (db_dirs['data_file_directories'] +
 
438
                        [db_dirs['commitlog_directory']] +
 
439
                        [db_dirs['saved_caches_directory']])
 
440
    for db_dir in unpacked_db_dirs:
460
441
        ensure_database_directory(db_dir)
461
442
 
462
443
 
550
531
        hookenv.log('Creating SUPERUSER {}'.format(username))
551
532
    else:
552
533
        hookenv.log('Creating user {}'.format(username))
553
 
    if has_cassandra_version('2.2'):
554
 
        query(session,
555
 
              'INSERT INTO system_auth.roles '
556
 
              '(role, can_login, is_superuser, salted_hash) '
557
 
              'VALUES (%s, TRUE, %s, %s)',
558
 
              ConsistencyLevel.ALL,
559
 
              (username, superuser, encrypted_password))
560
 
    else:
561
 
        query(session,
562
 
              'INSERT INTO system_auth.users (name, super) VALUES (%s, %s)',
563
 
              ConsistencyLevel.ALL, (username, superuser))
564
 
        query(session,
565
 
              'INSERT INTO system_auth.credentials (username, salted_hash) '
566
 
              'VALUES (%s, %s)',
567
 
              ConsistencyLevel.ALL, (username, encrypted_password))
 
534
    query(session,
 
535
          'INSERT INTO system_auth.users (name, super) VALUES (%s, %s)',
 
536
          ConsistencyLevel.ALL, (username, superuser))
 
537
    query(session,
 
538
          'INSERT INTO system_auth.credentials (username, salted_hash) '
 
539
          'VALUES (%s, %s)',
 
540
          ConsistencyLevel.ALL, (username, encrypted_password))
568
541
 
569
542
 
570
543
@logged
748
721
    # with the system_auth keyspace replication settings.
749
722
    cassandra_yaml['endpoint_snitch'] = 'GossipingPropertyFileSnitch'
750
723
 
751
 
    # Per Bug #1523546 and CASSANDRA-9319, Thrift is disabled by default in
752
 
    # Cassandra 2.2. Ensure it is enabled if rpc_port is non-zero.
753
 
    if int(config['rpc_port']) > 0:
754
 
        cassandra_yaml['start_rpc'] = True
755
 
 
756
724
    cassandra_yaml.update(overrides)
757
725
 
758
726
    write_cassandra_yaml(cassandra_yaml)
805
773
 
806
774
 
807
775
def get_auth_keyspace_replication(session):
808
 
    if has_cassandra_version('3.0'):
809
 
        statement = dedent('''\
810
 
            SELECT replication FROM system_schema.keyspaces
811
 
            WHERE keyspace_name='system_auth'
812
 
            ''')
813
 
        r = query(session, statement, ConsistencyLevel.QUORUM)
814
 
        return dict(r[0][0])
815
 
    else:
816
 
        statement = dedent('''\
817
 
            SELECT strategy_options FROM system.schema_keyspaces
818
 
            WHERE keyspace_name='system_auth'
819
 
            ''')
820
 
        r = query(session, statement, ConsistencyLevel.QUORUM)
821
 
        return json.loads(r[0][0])
 
776
    statement = dedent('''\
 
777
        SELECT strategy_options FROM system.schema_keyspaces
 
778
        WHERE keyspace_name='system_auth'
 
779
        ''')
 
780
    r = query(session, statement, ConsistencyLevel.QUORUM)
 
781
    return json.loads(r[0][0])
822
782
 
823
783
 
824
784
@logged