5
5
import charmhelpers.contrib.ansible
6
6
import charmhelpers.payload.execd
7
7
import charmhelpers.core.host
8
from charmhelpers.core import hookenv
12
mountpoint = '/srv/elasticsearch'
10
14
hooks = charmhelpers.contrib.ansible.AnsibleHooks(
11
15
playbook_path='playbook.yaml',
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')
52
@hooks.hook('data-relation-joined', 'data-relation-changed')
54
if hookenv.relation_get('mountpoint') == mountpoint:
55
# Other side of relation is ready
56
migrate_to_mount(mountpoint)
58
# Other side not ready yet, provide mountpoint
59
hookenv.log('Requesting storage for {}'.format(mountpoint))
60
hookenv.relation_set(mountpoint=mountpoint)
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')
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)
73
old_path = '/var/lib/elasticsearch'
74
if os.path.islink(old_path):
75
hookenv.log('{} is already a symlink, skipping migration'.format(
78
# Ensure our new mountpoint is empty. Otherwise error and allow
79
# users to investigate and migrate manually
80
files = os.listdir(new_path)
82
files.remove('lost+found')
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')
42
99
if __name__ == "__main__":
43
100
hooks.execute(sys.argv)