aboutsummaryrefslogtreecommitdiffstats
path: root/charms/trusty/cassandra/hooks/helpers.py
blob: b86a6b135d24ca7080876560c080f36ca5dceb68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
# Copyright 2015 Canonical Ltd.
#
# This file is part of the Cassandra Charm for Juju.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import configparser
from contextlib import contextmanager
from datetime import timedelta
from distutils.version import LooseVersion
import errno
from functools import wraps
import io
import json
import os.path
import re
import shutil
import subprocess
import sys
import tempfile
from textwrap import dedent
import time

import bcrypt
from cassandra import ConsistencyLevel
import cassandra.auth
import cassandra.cluster
import cassandra.query
import yaml

from charmhelpers.core import hookenv, host
from charmhelpers.core.hookenv import DEBUG, ERROR, WARNING
from charmhelpers import fetch

from coordinator import coordinator


RESTART_TIMEOUT = 600


def logged(func):
    @wraps(func)
    def wrapper(*args, **kw):
        hookenv.log("* Helper {}/{}".format(hookenv.hook_name(),
                                            func.__name__))
        return func(*args, **kw)
    return wrapper


def backoff(what_for, max_pause=60):
    i = 0
    while True:
        yield True
        i += 1
        pause = min(max_pause, 2 ** i)
        time.sleep(pause)
        if pause > 10:
            hookenv.log('Recheck {} for {}'.format(i, what_for))


# FOR CHARMHELPERS
@contextmanager
def autostart_disabled(services=None, _policy_rc='/usr/sbin/policy-rc.d'):
    '''Tell well behaved Debian packages to not start services when installed.
    '''
    script = ['#!/bin/sh']
    if services is not None:
        for service in services:
            script.append(
                'if [ "$1" = "{}" ]; then exit 101; fi'.format(service))
        script.append('exit 0')
    else:
        script.append('exit 101')  # By default, all daemons disabled.
    try:
        if os.path.exists(_policy_rc):
            shutil.move(_policy_rc, "{}-orig".format(_policy_rc))
        host.write_file(_policy_rc, '\n'.join(script).encode('ASCII'),
                        perms=0o555)
        yield
    finally:
        os.unlink(_policy_rc)
        if os.path.exists("{}-orig".format(_policy_rc)):
            shutil.move("{}-orig".format(_policy_rc), _policy_rc)


# FOR CHARMHELPERS
@logged
def install_packages(packages):
    packages = list(packages)
    if hookenv.config('extra_packages'):
        packages.extend(hookenv.config('extra_packages').split())
    packages = fetch.filter_installed_packages(packages)
    if packages:
        # The DSE packages are huge, so this might take some time.
        status_set('maintenance', 'Installing packages')
        with autostart_disabled(['cassandra']):
            fetch.apt_install(packages, fatal=True)


# FOR CHARMHELPERS
@logged
def ensure_package_status(packages):
    config_dict = hookenv.config()

    package_status = config_dict['package_status']

    if package_status not in ['install', 'hold']:
        raise RuntimeError("package_status must be 'install' or 'hold', "
                           "not {!r}".format(package_status))

    selections = []
    for package in packages:
        selections.append('{} {}\n'.format(package, package_status))
    dpkg = subprocess.Popen(['dpkg', '--set-selections'],
                            stdin=subprocess.PIPE)
    dpkg.communicate(input=''.join(selections).encode('US-ASCII'))


def get_seed_ips():
    '''Return the set of seed ip addresses.

    We use ip addresses rather than unit names, as we may need to use
    external seed ips at some point.
    '''
    return set((hookenv.leader_get('seeds') or '').split(','))


def actual_seed_ips():
    '''Return the seeds currently in cassandra.yaml'''
    cassandra_yaml = read_cassandra_yaml()
    s = cassandra_yaml['seed_provider'][0]['parameters'][0]['seeds']
    return set(s.split(','))


def get_database_directory(config_path):
    '''Convert a database path from the service config to an absolute path.

    Entries in the config file may be absolute, relative to
    /var/lib/cassandra, or relative to the mountpoint.
    '''
    import relations
    storage = relations.StorageRelation()
    if storage.mountpoint:
        root = os.path.join(storage.mountpoint, 'cassandra')
    else:
        root = '/var/lib/cassandra'
    return os.path.join(root, config_path)


def ensure_database_directory(config_path):
    '''Create the database directory if it doesn't exist, resetting
    ownership and other settings while we are at it.

    Returns the absolute path.
    '''
    absdir = get_database_directory(config_path)

    # Work around Bug #1427150 by ensuring components of the path are
    # created with the required permissions, if necessary.
    component = os.sep
    for p in absdir.split(os.sep)[1:-1]:
        component = os.path.join(component, p)
        if not os.path.exists(p):
            host.mkdir(component)
    assert component == os.path.split(absdir)[0]
    host.mkdir(absdir, owner='cassandra', group='cassandra', perms=0o750)
    return absdir


def get_all_database_directories():
    config = hookenv.config()
    dirs = dict(
        data_file_directories=[get_database_directory(d)
                               for d in (config['data_file_directories'] or
                                         'data').split()],
        commitlog_directory=get_database_directory(
            config['commitlog_directory'] or 'commitlog'),
        saved_caches_directory=get_database_directory(
            config['saved_caches_directory'] or 'saved_caches'))
    if has_cassandra_version('3.0'):
        # Not yet configurable. Make configurable with Juju native storage.
        dirs['hints_directory'] = get_database_directory('hints')
    return dirs


def mountpoint(path):
    '''Return the mountpoint that path exists on.'''
    path = os.path.realpath(path)
    while path != '/' and not os.path.ismount(path):
        path = os.path.dirname(path)
    return path


# FOR CHARMHELPERS
def is_lxc():
    '''Return True if we are running inside an LXC container.'''
    with open('/proc/1/cgroup', 'r') as f:
        return ':/lxc/' in f.readline()


# FOR CHARMHELPERS
def set_io_scheduler(io_scheduler, directory):
    '''Set the block device io scheduler.'''

    assert os.path.isdir(directory)

    # The block device regex may be a tad simplistic.
    block_regex = re.compile('\/dev\/([a-z]*)', re.IGNORECASE)

    output = subprocess.check_output(['df', directory],
                                     universal_newlines=True)

    if not is_lxc():
        hookenv.log("Setting block device of {} to IO scheduler {}"
                    "".format(directory, io_scheduler))
        try:
            block_dev = re.findall(block_regex, output)[0]
        except IndexError:
            hookenv.log("Unable to locate block device of {} (in container?)"
                        "".format(directory))
            return
        sys_file = os.path.join("/", "sys", "block", block_dev,
                                "queue", "scheduler")
        try:
            host.write_file(sys_file, io_scheduler.encode('ascii'),
                            perms=0o644)
        except OSError as e:
            if e.errno == errno.EACCES:
                hookenv.log("Got Permission Denied trying to set the "
                            "IO scheduler at {}. We may be in an LXC. "
                            "Exiting gracefully".format(sys_file),
                            WARNING)
            elif e.errno == errno.ENOENT:
                hookenv.log("Got no such file or directory trying to "
                            "set the IO scheduler at {}. It may be "
                            "this is an LXC, the device name is as "
                            "yet unknown to the charm, or LVM/RAID is "
                            "hiding the underlying device name. "
                            "Exiting gracefully".format(sys_file),
                            WARNING)
            else:
                raise e
    else:
        # Make no change if we are in an LXC
        hookenv.log("In an LXC. Cannot set io scheduler {}"
                    "".format(io_scheduler))


# FOR CHARMHELPERS
def recursive_chown(directory, owner="root", group="root"):
    '''Change ownership of all files and directories in 'directory'.

    Ownership of 'directory' is also reset.
    '''
    shutil.chown(directory, owner, group)
    for root, dirs, files in os.walk(directory):
        for dirname in dirs:
            shutil.chown(os.path.join(root, dirname), owner, group)
        for filename in files:
            shutil.chown(os.path.join(root, filename), owner, group)


def maybe_backup(path):
    '''Copy a file to file.orig, if file.orig does not already exist.'''
    backup_path = path + '.orig'
    if not os.path.exists(backup_path):
        with open(path, 'rb') as f:
            host.write_file(backup_path, f.read(), perms=0o600)


# FOR CHARMHELPERS
def get_package_version(package):
    cache = fetch.apt_cache()
    if package not in cache:
        return None
    pkgver = cache[package].current_ver
    if pkgver is not None:
        return pkgver.ver_str
    return None


def get_jre():
    # DataStax Enterprise requires the Oracle JRE.
    if get_cassandra_edition() == 'dse':
        return 'oracle'

    config = hookenv.config()
    jre = config['jre'].lower()
    if jre not in ('openjdk', 'oracle'):
        hookenv.log('Unknown JRE {!r} specified. Using OpenJDK'.format(jre),
                    ERROR)
        jre = 'openjdk'
    return jre


def get_cassandra_edition():
    config = hookenv.config()
    edition = config['edition'].lower()
    if edition not in ('community', 'dse'):
        hookenv.log('Unknown edition {!r}. Using community.'.format(edition),
                    ERROR)
        edition = 'community'
    return edition


def get_cassandra_service():
    '''Cassandra upstart service'''
    if get_cassandra_edition() == 'dse':
        return 'dse'
    return 'cassandra'


def get_cassandra_version():
    if get_cassandra_edition() == 'dse':
        dse_ver = get_package_version('dse-full')
        if not dse_ver:
            return None
        elif LooseVersion(dse_ver) >= LooseVersion('5.0'):
            return '3.0'
        elif LooseVersion(dse_ver) >= LooseVersion('4.7'):
            return '2.1'
        else:
            return '2.0'
    return get_package_version('cassandra')


def has_cassandra_version(minimum_ver):
    cassandra_version = get_cassandra_version()
    assert cassandra_version is not None, 'Cassandra package not yet installed'
    return LooseVersion(cassandra_version) >= LooseVersion(minimum_ver)


def get_cassandra_config_dir():
    if get_cassandra_edition() == 'dse':
        return '/etc/dse/cassandra'
    else:
        return '/etc/cassandra'


def get_cassandra_yaml_file():
    return os.path.join(get_cassandra_config_dir(), "cassandra.yaml")


def get_cassandra_env_file():
    return os.path.join(get_cassandra_config_dir(), "cassandra-env.sh")


def get_cassandra_rackdc_file():
    return os.path.join(get_cassandra_config_dir(),
                        "cassandra-rackdc.properties")


def get_cassandra_pid_file():
    edition = get_cassandra_edition()
    if edition == 'dse':
        pid_file = "/var/run/dse/dse.pid"
    else:
        pid_file = "/var/run/cassandra/cassandra.pid"
    return pid_file


def get_cassandra_packages():
    edition = get_cassandra_edition()
    if edition == 'dse':
        packages = set(['dse-full'])
    else:
        packages = set(['cassandra'])  # 'cassandra-tools'

    packages.add('ntp')
    packages.add('run-one')
    packages.add('netcat')

    jre = get_jre()
    if jre == 'oracle':
        # We can't use a packaged version of the Oracle JRE, as we
        # are not allowed to bypass Oracle's click through license
        # agreement.
        pass
    else:
        # NB. OpenJDK 8 not available in trusty. This needs to come
        # from a PPA or some other configured source.
        packages.add('openjdk-8-jre-headless')

    return packages


@logged
def stop_cassandra():
    if is_cassandra_running():
        hookenv.log('Shutting down Cassandra')
        host.service_stop(get_cassandra_service())
    if is_cassandra_running():
        hookenv.status_set('blocked', 'Cassandra failed to shut down')
        raise SystemExit(0)


@logged
def start_cassandra():
    if is_cassandra_running():
        return

    actual_seeds = sorted(actual_seed_ips())
    assert actual_seeds, 'Attempting to start cassandra with empty seed list'
    hookenv.config()['configured_seeds'] = actual_seeds

    if is_bootstrapped():
        status_set('maintenance',
                   'Starting Cassandra with seeds {!r}'
                   .format(','.join(actual_seeds)))
    else:
        status_set('maintenance',
                   'Bootstrapping with seeds {}'
                   .format(','.join(actual_seeds)))

    host.service_start(get_cassandra_service())

    # Wait for Cassandra to actually start, or abort.
    timeout = time.time() + RESTART_TIMEOUT
    while time.time() < timeout:
        if is_cassandra_running():
            return
        time.sleep(1)
    status_set('blocked', 'Cassandra failed to start')
    raise SystemExit(0)


@logged
def reconfigure_and_restart_cassandra(overrides={}):
    stop_cassandra()
    configure_cassandra_yaml(overrides)
    start_cassandra()


@logged
def remount_cassandra():
    '''If a new mountpoint is ready, migrate data across to it.'''
    assert not is_cassandra_running()  # Guard against data loss.
    import relations
    storage = relations.StorageRelation()
    if storage.needs_remount():
        status_set('maintenance', 'Migrating data to new mountpoint')
        hookenv.config()['bootstrapped_into_cluster'] = False
        if storage.mountpoint is None:
            hookenv.log('External storage AND DATA gone. '
                        'Reverting to local storage. '
                        'In danger of resurrecting old data. ',
                        WARNING)
        else:
            storage.migrate('/var/lib/cassandra', 'cassandra')
            root = os.path.join(storage.mountpoint, 'cassandra')
            os.chmod(root, 0o750)


@logged
def ensure_database_directories():
    '''Ensure that directories Cassandra expects to store its data in exist.'''
    # Guard against changing perms on a running db. Although probably
    # harmless, it causes shutil.chown() to fail.
    assert not is_cassandra_running()
    db_dirs = get_all_database_directories()
    ensure_database_directory(db_dirs['commitlog_directory'])
    ensure_database_directory(db_dirs['saved_caches_directory'])
    if 'hints_directory' in db_dirs:
        ensure_database_directory(db_dirs['hints_directory'])
    for db_dir in db_dirs['data_file_directories']:
        ensure_database_directory(db_dir)


CONNECT_TIMEOUT = 10


@contextmanager
def connect(username=None, password=None, timeout=CONNECT_TIMEOUT,
            auth_timeout=CONNECT_TIMEOUT):
    # We pull the currently configured listen address and port from the
    # yaml, rather than the service configuration, as it may have been
    # overridden.
    cassandra_yaml = read_cassandra_yaml()
    address = cassandra_yaml['rpc_address']
    if address == '0.0.0.0':
        address = 'localhost'
    port = cassandra_yaml['native_transport_port']

    if username is None or password is None:
        username, password = superuser_credentials()

    auth = hookenv.config()['authenticator']
    if auth == 'AllowAllAuthenticator':
        auth_provider = None
    else:
        auth_provider = cassandra.auth.PlainTextAuthProvider(username=username,
                                                             password=password)

    # Although we specify a reconnection_policy, it does not apply to
    # the initial connection so we retry in a loop.
    start = time.time()
    until = start + timeout
    auth_until = start + auth_timeout
    while True:
        cluster = cassandra.cluster.Cluster([address], port=port,
                                            auth_provider=auth_provider)
        try:
            session = cluster.connect()
            session.default_timeout = timeout
            break
        except cassandra.cluster.NoHostAvailable as x:
            cluster.shutdown()
            now = time.time()
            # If every node failed auth, reraise one of the
            # AuthenticationFailed exceptions. Unwrapping the exception
            # means call sites don't have to sniff the exception bundle.
            # We don't retry on auth fails; this method should not be
            # called if the system_auth data is inconsistent.
            auth_fails = [af for af in x.errors.values()
                          if isinstance(af, cassandra.AuthenticationFailed)]
            if auth_fails:
                if now > auth_until:
                    raise auth_fails[0]
            if now > until:
                raise
        time.sleep(1)
    try:
        yield session
    finally:
        cluster.shutdown()


QUERY_TIMEOUT = 60


def query(session, statement, consistency_level, args=None):
    q = cassandra.query.SimpleStatement(statement,
                                        consistency_level=consistency_level)

    until = time.time() + QUERY_TIMEOUT
    for _ in backoff('query to execute'):
        try:
            return session.execute(q, args)
        except Exception:
            if time.time() > until:
                raise


def encrypt_password(password):
    return bcrypt.hashpw(password, bcrypt.gensalt())


@logged
def ensure_user(session, username, encrypted_password, superuser=False):
    '''Create the DB user if it doesn't already exist & reset the password.'''
    auth = hookenv.config()['authenticator']
    if auth == 'AllowAllAuthenticator':
        return  # No authentication means we cannot create users

    if superuser:
        hookenv.log('Creating SUPERUSER {}'.format(username))
    else:
        hookenv.log('Creating user {}'.format(username))
    if has_cassandra_version('2.2'):
        query(session,
              'INSERT INTO system_auth.roles '
              '(role, can_login, is_superuser, salted_hash) '
              'VALUES (%s, TRUE, %s, %s)',
              ConsistencyLevel.ALL,
              (username, superuser, encrypted_password))
    else:
        query(session,
              'INSERT INTO system_auth.users (name, super) VALUES (%s, %s)',
              ConsistencyLevel.ALL, (username, superuser))
        query(session,
              'INSERT INTO system_auth.credentials (username, salted_hash) '
              'VALUES (%s, %s)',
              ConsistencyLevel.ALL, (username, encrypted_password))


@logged
def create_unit_superuser_hard():
    '''Create or recreate the unit's superuser account.

    This method is used when there are no known superuser credentials
    to use. We restart the node using the AllowAllAuthenticator and
    insert our credentials directly into the system_auth keyspace.
    '''
    username, password = superuser_credentials()
    pwhash = encrypt_password(password)
    hookenv.log('Creating unit superuser {}'.format(username))

    # Restart cassandra without authentication & listening on localhost.
    reconfigure_and_restart_cassandra(
        dict(authenticator='AllowAllAuthenticator', rpc_address='localhost'))
    for _ in backoff('superuser creation'):
        try:
            with connect() as session:
                ensure_user(session, username, pwhash, superuser=True)
                break
        except Exception as x:
            print(str(x))

    # Restart Cassandra with regular config.
    nodetool('flush')  # Ensure our backdoor updates are flushed.
    reconfigure_and_restart_cassandra()


def get_cqlshrc_path():
    return os.path.expanduser('~root/.cassandra/cqlshrc')


def superuser_username():
    return 'juju_{}'.format(re.subn(r'\W', '_', hookenv.local_unit())[0])


def superuser_credentials():
    '''Return (username, password) to connect to the Cassandra superuser.

    The credentials are persisted in the root user's cqlshrc file,
    making them easily accessible to the command line tools.
    '''
    cqlshrc_path = get_cqlshrc_path()
    cqlshrc = configparser.ConfigParser(interpolation=None)
    cqlshrc.read([cqlshrc_path])

    username = superuser_username()

    try:
        section = cqlshrc['authentication']
        # If there happened to be an existing cqlshrc file, it might
        # contain invalid credentials. Ignore them.
        if section['username'] == username:
            return section['username'], section['password']
    except KeyError:
        hookenv.log('Generating superuser credentials into {}'.format(
            cqlshrc_path))

    config = hookenv.config()

    password = host.pwgen()

    hookenv.log('Generated username {}'.format(username))

    # We set items separately, rather than together, so that we have a
    # defined order for the ConfigParser to preserve and the tests to
    # rely on.
    cqlshrc.setdefault('authentication', {})
    cqlshrc['authentication']['username'] = username
    cqlshrc['authentication']['password'] = password
    cqlshrc.setdefault('connection', {})
    cqlshrc['connection']['hostname'] = hookenv.unit_public_ip()
    if get_cassandra_version().startswith('2.0'):
        cqlshrc['connection']['port'] = str(config['rpc_port'])
    else:
        cqlshrc['connection']['port'] = str(config['native_transport_port'])

    ini = io.StringIO()
    cqlshrc.write(ini)
    host.mkdir(os.path.dirname(cqlshrc_path), perms=0o700)
    host.write_file(cqlshrc_path, ini.getvalue().encode('UTF-8'), perms=0o400)

    return username, password


def emit(*args, **kw):
    # Just like print, but with plumbing and mocked out in the test suite.
    print(*args, **kw)
    sys.stdout.flush()


def nodetool(*cmd, timeout=120):
    cmd = ['nodetool'] + [str(i) for i in cmd]
    i = 0
    until = time.time() + timeout
    for _ in backoff('nodetool to work'):
        i += 1
        try:
            if timeout is not None:
                timeout = max(0, until - time.time())
            raw = subprocess.check_output(cmd, universal_newlines=True,
                                          timeout=timeout,
                                          stderr=subprocess.STDOUT)

            # Work around CASSANDRA-8776.
            if 'status' in cmd and 'Error:' in raw:
                hookenv.log('Error detected but nodetool returned success.',
                            WARNING)
                raise subprocess.CalledProcessError(99, cmd, raw)

            hookenv.log('{} succeeded'.format(' '.join(cmd)), DEBUG)
            out = raw.expandtabs()
            emit(out)
            return out

        except subprocess.CalledProcessError as x:
            if i > 1:
                emit(x.output.expandtabs())  # Expand tabs for juju debug-log.
            if not is_cassandra_running():
                status_set('blocked',
                           'Cassandra has unexpectedly shutdown')
                raise SystemExit(0)
            if time.time() >= until:
                raise


def num_nodes():
    return len(get_bootstrapped_ips())


def read_cassandra_yaml():
    cassandra_yaml_path = get_cassandra_yaml_file()
    with open(cassandra_yaml_path, 'rb') as f:
        return yaml.safe_load(f)


@logged
def write_cassandra_yaml(cassandra_yaml):
    cassandra_yaml_path = get_cassandra_yaml_file()
    host.write_file(cassandra_yaml_path,
                    yaml.safe_dump(cassandra_yaml).encode('UTF-8'))


def configure_cassandra_yaml(overrides={}, seeds=None):
    cassandra_yaml_path = get_cassandra_yaml_file()
    config = hookenv.config()

    maybe_backup(cassandra_yaml_path)  # Its comments may be useful.

    cassandra_yaml = read_cassandra_yaml()

    # Most options just copy from config.yaml keys with the same name.
    # Using the same name is preferred to match the actual Cassandra
    # documentation.
    simple_config_keys = ['cluster_name', 'num_tokens',
                          'partitioner', 'authorizer', 'authenticator',
                          'compaction_throughput_mb_per_sec',
                          'stream_throughput_outbound_megabits_per_sec',
                          'tombstone_warn_threshold',
                          'tombstone_failure_threshold',
                          'native_transport_port', 'rpc_port',
                          'storage_port', 'ssl_storage_port']
    cassandra_yaml.update((k, config[k]) for k in simple_config_keys)

    seeds = ','.join(seeds or get_seed_ips())  # Don't include whitespace!
    hookenv.log('Configuring seeds as {!r}'.format(seeds), DEBUG)
    cassandra_yaml['seed_provider'][0]['parameters'][0]['seeds'] = seeds

    cassandra_yaml['listen_address'] = hookenv.unit_private_ip()
    cassandra_yaml['rpc_address'] = '0.0.0.0'
    if not get_cassandra_version().startswith('2.0'):
        cassandra_yaml['broadcast_rpc_address'] = hookenv.unit_public_ip()

    dirs = get_all_database_directories()
    cassandra_yaml.update(dirs)

    # GossipingPropertyFileSnitch is the only snitch recommended for
    # production. It we allow others, we need to consider how to deal
    # with the system_auth keyspace replication settings.
    cassandra_yaml['endpoint_snitch'] = 'GossipingPropertyFileSnitch'

    # Per Bug #1523546 and CASSANDRA-9319, Thrift is disabled by default in
    # Cassandra 2.2. Ensure it is enabled if rpc_port is non-zero.
    if int(config['rpc_port']) > 0:
        cassandra_yaml['start_rpc'] = True

    cassandra_yaml.update(overrides)

    write_cassandra_yaml(cassandra_yaml)


def get_pid_from_file(pid_file):
    try:
        with open(pid_file, 'r') as f:
            pid = int(f.read().strip().split()[0])
            if pid <= 1:
                raise ValueError('Illegal pid {}'.format(pid))
            return pid
    except (ValueError, IndexError) as e:
        hookenv.log("Invalid PID in {}.".format(pid_file))
        raise ValueError(e)


def is_cassandra_running():
    pid_file = get_cassandra_pid_file()

    try:
        for _ in backoff('Cassandra to respond'):
            # We reload the pid every time, in case it has gone away.
            # If it goes away, a FileNotFound exception is raised.
            pid = get_pid_from_file(pid_file)

            # This does not kill the process but checks for its
            # existence. It raises an ProcessLookupError if the process
            # is not running.
            os.kill(pid, 0)

            if subprocess.call(["nodetool", "status"],
                               stdout=subprocess.DEVNULL,
                               stderr=subprocess.DEVNULL) == 0:
                hookenv.log(
                    "Cassandra PID {} is running and responding".format(pid))
                return True
    except FileNotFoundError:
        hookenv.log("Cassandra is not running. PID file does not exist.")
        return False
    except ProcessLookupError:
        if os.path.exists(pid_file):
            # File disappeared between reading the PID and checking if
            # the PID is running.
            hookenv.log("Cassandra is not running, but pid file exists.",
                        WARNING)
        else:
            hookenv.log("Cassandra is not running. PID file does not exist.")
        return False


def get_auth_keyspace_replication(session):
    if has_cassandra_version('3.0'):
        statement = dedent('''\
            SELECT replication FROM system_schema.keyspaces
            WHERE keyspace_name='system_auth'
            ''')
        r = query(session, statement, ConsistencyLevel.QUORUM)
        return dict(r[0][0])
    else:
        statement = dedent('''\
            SELECT strategy_options FROM system.schema_keyspaces
            WHERE keyspace_name='system_auth'
            ''')
        r = query(session, statement, ConsistencyLevel.QUORUM)
        return json.loads(r[0][0])


@logged
def set_auth_keyspace_replication(session, settings):
    # Live operation, so keep status the same.
    status_set(hookenv.status_get()[0],
               'Updating system_auth rf to {!r}'.format(settings))
    statement = 'ALTER KEYSPACE system_auth WITH REPLICATION = %s'
    query(session, statement, ConsistencyLevel.ALL, (settings,))


@logged
def repair_auth_keyspace():
    # Repair takes a long time, and may need to be retried due to 'snapshot
    # creation' errors, but should certainly complete within an hour since
    # the keyspace is tiny.
    status_set(hookenv.status_get()[0],
               'Repairing system_auth keyspace')
    nodetool('repair', 'system_auth', timeout=3600)


def is_bootstrapped(unit=None):
    '''Return True if the node has already bootstrapped into the cluster.'''
    if unit is None or unit == hookenv.local_unit():
        return hookenv.config().get('bootstrapped', False)
    elif coordinator.relid:
        return bool(hookenv.relation_get(rid=coordinator.relid,
                                         unit=unit).get('bootstrapped'))
    else:
        return False


def set_bootstrapped():
    # We need to store this flag in two locations. The peer relation,
    # so peers can see it, and local state, for when we haven't joined
    # the peer relation yet. actions.publish_bootstrapped_flag()
    # calls this method again when necessary to ensure that state is
    # propagated # if/when the peer relation is joined.
    config = hookenv.config()
    config['bootstrapped'] = True
    if coordinator.relid is not None:
        hookenv.relation_set(coordinator.relid, bootstrapped="1")
    if config.changed('bootstrapped'):
        hookenv.log('Bootstrapped')
    else:
        hookenv.log('Already bootstrapped')


def get_bootstrapped():
    units = [hookenv.local_unit()]
    if coordinator.relid is not None:
        units.extend(hookenv.related_units(coordinator.relid))
    return set([unit for unit in units if is_bootstrapped(unit)])


def get_bootstrapped_ips():
    return set([unit_to_ip(unit) for unit in get_bootstrapped()])


def unit_to_ip(unit):
    if unit is None or unit == hookenv.local_unit():
        return hookenv.unit_private_ip()
    elif coordinator.relid:
        pa = hookenv.relation_get(rid=coordinator.relid,
                                  unit=unit).get('private-address')
        return hookenv._ensure_ip(pa)
    else:
        return None


def get_node_status():
    '''Return the Cassandra node status.

    May be NORMAL, JOINING, DECOMMISSIONED etc., or None if we can't tell.
    '''
    if not is_cassandra_running():
        return None
    raw = nodetool('netstats')
    m = re.search(r'(?m)^Mode:\s+(\w+)$', raw)
    if m is None:
        return None
    return m.group(1).upper()


def is_decommissioned():
    status = get_node_status()
    if status in ('DECOMMISSIONED', 'LEAVING'):
        hookenv.log('This node is {}'.format(status), WARNING)
        return True
    return False


@logged
def emit_describe_cluster():
    '''Run nodetool describecluster for the logs.'''
    nodetool('describecluster')  # Implicit emit


@logged
def emit_status():
    '''Run 'nodetool status' for the logs.'''
    nodetool('status')  # Implicit emit


@logged
def emit_netstats():
    '''Run 'nodetool netstats' for the logs.'''
    nodetool('netstats')  # Implicit emit


def emit_cluster_info():
    emit_describe_cluster()
    emit_status()
    emit_netstats()


# FOR CHARMHELPERS (and think of a better name)
def week_spread(unit_num):
    '''Pick a time for a unit's weekly job.

    Jobs are spread out evenly throughout the week as best we can.
    The chosen time only depends on the unit number, and does not change
    if other units are added and removed; while the chosen time will not
    be perfect, we don't have to worry about skipping a weekly job if
    units are added or removed at the wrong moment.

    Returns (dow, hour, minute) suitable for cron.
    '''
    def vdc(n, base=2):
        '''Van der Corpet sequence. 0, 0.5, 0.25, 0.75, 0.125, 0.625, ...

        http://rosettacode.org/wiki/Van_der_Corput_sequence#Python
        '''
        vdc, denom = 0, 1
        while n:
            denom *= base
            n, remainder = divmod(n, base)
            vdc += remainder / denom
        return vdc
    # We could use the vdc() function to distribute jobs evenly throughout
    # the week, so unit 0==0, unit 1==3.5days, unit 2==1.75 etc. But
    # plain modulo for the day of week is easier for humans and what
    # you expect for 7 units or less.
    sched_dow = unit_num % 7
    # We spread time of day so each batch of 7 units gets the same time,
    # as far spread out from the other batches of 7 units as possible.
    minutes_in_day = 24 * 60
    sched = timedelta(minutes=int(minutes_in_day * vdc(unit_num // 7)))
    sched_hour = sched.seconds // (60 * 60)
    sched_minute = sched.seconds // 60 - sched_hour * 60
    return (sched_dow, sched_hour, sched_minute)


# FOR CHARMHELPERS. This should be a constant in nrpe.py
def local_plugins_dir():
    return '/usr/local/lib/nagios/plugins'


def leader_ping():
    '''Make a change in the leader settings, waking the non-leaders.'''
    assert hookenv.is_leader()
    last = int(hookenv.leader_get('ping') or 0)
    hookenv.leader_set(ping=str(last + 1))


def get_unit_superusers():
    '''Return the set of units that have had their superuser accounts created.
    '''
    raw = hookenv.leader_get('superusers')
    return set(json.loads(raw or '[]'))


def set_unit_superusers(superusers):
    hookenv.leader_set(superusers=json.dumps(sorted(superusers)))


def status_set(state, message):
    '''Set the unit status and log a message.'''
    hookenv.status_set(state, message)
    hookenv.log('{} unit state: {}'.format(state, message))


def service_status_set(state, message):
    '''Set the service status and log a message.'''
    subprocess.check_call(['status-set', '--service', state, message])
    hookenv.log('{} service state: {}'.format(state, message))


def get_service_name(relid):
    '''Return the service name for the other end of relid.'''
    units = hookenv.related_units(relid)
    if units:
        return units[0].split('/', 1)[0]
    else:
        return None


def peer_relid():
    return coordinator.relid


@logged
def set_active():
    '''Set happy state'''
    if hookenv.unit_private_ip() in get_seed_ips():
        msg = 'Live seed'
    else:
        msg = 'Live node'
    status_set('active', msg)

    if hookenv.is_leader():
        n = num_nodes()
        if n == 1:
            n = 'Single'
        service_status_set('active', '{} node cluster'.format(n))


def update_hosts_file(hosts_file, hosts_map):
    """Older versions of Cassandra need own hostname resolution."""
    with open(hosts_file, 'r') as hosts:
        lines = hosts.readlines()

    newlines = []
    for ip, hostname in hosts_map.items():
        if not ip or not hostname:
            continue

        keepers = []
        for line in lines:
            _line = line.split()
            if len(_line) < 2 or not (_line[0] == ip or hostname in _line[1:]):
                keepers.append(line)
            else:
                hookenv.log('Marking line {!r} for update or removal'
                            ''.format(line.strip()), level=DEBUG)

        lines = keepers
        newlines.append('{} {}\n'.format(ip, hostname))

    lines += newlines

    with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
        with open(tmpfile.name, 'w') as hosts:
            for line in lines:
                hosts.write(line)

    os.rename(tmpfile.name, hosts_file)
    os.chmod(hosts_file, 0o644)