~onlineservices-charmers/charms/trusty/elasticsearch/elasticsearch2

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Michael Nelson
  • Date: 2015-10-27 23:27:18 UTC
  • mfrom: (39.1.3 merge-new-relations)
  • Revision ID: michael.nelson@canonical.com-20151027232718-26ewhz2bgb13m7pw
Merged lp:~evarlast/charms/trusty/elasticsearch/add-logs-relation
Merged lp:~canonical-is-sa/charms/trusty/elasticsearch/trunk
Updated so that peer-relation-joined will not fail, but -changed will , if the unit isn't part of the cluster.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import charmhelpers.contrib.ansible
6
6
import charmhelpers.payload.execd
7
7
import charmhelpers.core.host
 
8
from charmhelpers.core import hookenv
 
9
import os
 
10
import shutil
8
11
 
 
12
mountpoint = '/srv/elasticsearch'
9
13
 
10
14
hooks = charmhelpers.contrib.ansible.AnsibleHooks(
11
15
    playbook_path='playbook.yaml',
12
16
    default_hooks=[
13
17
        'config-changed',
14
18
        'cluster-relation-joined',
 
19
        'logs-relation-joined',
 
20
        'data-relation-joined',
 
21
        'data-relation-changed',
 
22
        'data-relation-departed',
 
23
        'data-relation-broken',
15
24
        'peer-relation-joined',
 
25
        'peer-relation-changed',
16
26
        'peer-relation-departed',
17
27
        'nrpe-external-master-relation-changed',
18
28
        'rest-relation-joined',
39
49
        '/usr/share/ansible')
40
50
 
41
51
 
 
52
@hooks.hook('data-relation-joined', 'data-relation-changed')
 
53
def data_relation():
 
54
    if hookenv.relation_get('mountpoint') == mountpoint:
 
55
        # Other side of relation is ready
 
56
        migrate_to_mount(mountpoint)
 
57
    else:
 
58
        # Other side not ready yet, provide mountpoint
 
59
        hookenv.log('Requesting storage for {}'.format(mountpoint))
 
60
        hookenv.relation_set(mountpoint=mountpoint)
 
61
 
 
62
 
 
63
@hooks.hook('data-relation-departed', 'data-relation-broken')
 
64
def data_relation_gone():
 
65
    hookenv.log('Data relation no longer present, stopping elasticsearch.')
 
66
    charmhelpers.core.host.service_stop('elasticsearch')
 
67
 
 
68
 
 
69
def migrate_to_mount(new_path):
 
70
    """Invoked when new mountpoint appears. This function safely migrates
 
71
    elasticsearch data from local disk to persistent storage (only if needed)
 
72
    """
 
73
    old_path = '/var/lib/elasticsearch'
 
74
    if os.path.islink(old_path):
 
75
        hookenv.log('{} is already a symlink, skipping migration'.format(
 
76
            old_path))
 
77
        return True
 
78
    # Ensure our new mountpoint is empty. Otherwise error and allow
 
79
    # users to investigate and migrate manually
 
80
    files = os.listdir(new_path)
 
81
    try:
 
82
        files.remove('lost+found')
 
83
    except ValueError:
 
84
        pass
 
85
    if files:
 
86
        raise RuntimeError('Persistent storage contains old data. '
 
87
                           'Please investigate and migrate data manually '
 
88
                           'to: {}'.format(new_path))
 
89
    os.chmod(new_path, 0700)
 
90
    charmhelpers.core.host.service_stop('elasticsearch')
 
91
    charmhelpers.core.host.rsync(os.path.join(old_path, ''),  # Ensure we have trailing slashes
 
92
        os.path.join(new_path, ''),
 
93
        options=['--archive'])
 
94
    shutil.rmtree(old_path)
 
95
    os.symlink(new_path, old_path)
 
96
    charmhelpers.core.host.service_start('elasticsearch')
 
97
 
 
98
 
42
99
if __name__ == "__main__":
43
100
    hooks.execute(sys.argv)