Merge lp:~canonical-is-sa/charms/trusty/elasticsearch/trunk into lp:charms/trusty/elasticsearch

Proposed by Michael Foley
Status: Merged
Merged at revision: 40
Proposed branch: lp:~canonical-is-sa/charms/trusty/elasticsearch/trunk
Merge into: lp:charms/trusty/elasticsearch
Diff against target: 137 lines (+71/-0)
4 files modified
hooks/hooks.py (+55/-0)
metadata.yaml (+3/-0)
playbook.yaml (+10/-0)
roles/nrpe/tasks/main.yml (+3/-0)
To merge this branch: bzr merge lp:~canonical-is-sa/charms/trusty/elasticsearch/trunk
Reviewer Review Type Date Requested Status
Michael Nelson (community) Approve
charmers Pending
Review via email: mp+260027@code.launchpad.net

Description of the change

Add data relations for using block-storage-broker.

To post a comment you must log in.
Revision history for this message
Michael Nelson (michael.nelson) wrote :

Thanks Michael. Sorry for the delay, I've just ran the existing tests with this branch to ensure everything works as normal without block storage, and will be testing the blockstorage with a deployment.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added symlink 'hooks/data-relation-changed'
2=== target is u'hooks.py'
3=== added symlink 'hooks/data-relation-departed'
4=== target is u'hooks.py'
5=== added symlink 'hooks/data-relation-joined'
6=== target is u'hooks.py'
7=== modified file 'hooks/hooks.py'
8--- hooks/hooks.py 2014-07-08 13:38:52 +0000
9+++ hooks/hooks.py 2015-05-25 04:21:12 +0000
10@@ -5,13 +5,21 @@
11 import charmhelpers.contrib.ansible
12 import charmhelpers.payload.execd
13 import charmhelpers.core.host
14+from charmhelpers.core import hookenv
15+import os
16+import shutil
17
18+mountpoint = '/srv/elasticsearch'
19
20 hooks = charmhelpers.contrib.ansible.AnsibleHooks(
21 playbook_path='playbook.yaml',
22 default_hooks=[
23 'config-changed',
24 'cluster-relation-joined',
25+ 'data-relation-joined',
26+ 'data-relation-changed',
27+ 'data-relation-departed',
28+ 'data-relation-broken',
29 'peer-relation-joined',
30 'peer-relation-departed',
31 'nrpe-external-master-relation-changed',
32@@ -39,5 +47,52 @@
33 '/usr/share/ansible')
34
35
36+@hooks.hook('data-relation-joined', 'data-relation-changed')
37+def data_relation():
38+ if hookenv.relation_get('mountpoint') == mountpoint:
39+ # Other side of relation is ready
40+ migrate_to_mount(mountpoint)
41+ else:
42+ # Other side not ready yet, provide mountpoint
43+ hookenv.log('Requesting storage for {}'.format(mountpoint))
44+ hookenv.relation_set(mountpoint=mountpoint)
45+
46+
47+@hooks.hook('data-relation-departed', 'data-relation-broken')
48+def data_relation_gone():
49+ hookenv.log('Data relation no longer present, stopping elasticsearch.')
50+ charmhelpers.core.host.service_stop('elasticsearch')
51+
52+
53+def migrate_to_mount(new_path):
54+ """Invoked when new mountpoint appears. This function safely migrates
55+ elasticsearch data from local disk to persistent storage (only if needed)
56+ """
57+ old_path = '/var/lib/elasticsearch'
58+ if os.path.islink(old_path):
59+ hookenv.log('{} is already a symlink, skipping migration'.format(
60+ old_path))
61+ return True
62+ # Ensure our new mountpoint is empty. Otherwise error and allow
63+ # users to investigate and migrate manually
64+ files = os.listdir(new_path)
65+ try:
66+ files.remove('lost+found')
67+ except ValueError:
68+ pass
69+ if files:
70+ raise RuntimeError('Persistent storage contains old data. '
71+ 'Please investigate and migrate data manually '
72+ 'to: {}'.format(new_path))
73+ os.chmod(new_path, 0700)
74+ charmhelpers.core.host.service_stop('elasticsearch')
75+ charmhelpers.core.host.rsync(os.path.join(old_path, ''), # Ensure we have trailing slashes
76+ os.path.join(new_path, ''),
77+ options=['--archive'])
78+ shutil.rmtree(old_path)
79+ os.symlink(new_path, old_path)
80+ charmhelpers.core.host.service_start('elasticsearch')
81+
82+
83 if __name__ == "__main__":
84 hooks.execute(sys.argv)
85
86=== modified file 'metadata.yaml'
87--- metadata.yaml 2014-09-11 18:34:45 +0000
88+++ metadata.yaml 2015-05-25 04:21:12 +0000
89@@ -16,3 +16,6 @@
90 nrpe-external-master:
91 interface: nrpe-external-master
92 scope: container
93+ data:
94+ interface: block-storage
95+ scope: container
96
97=== modified file 'playbook.yaml'
98--- playbook.yaml 2014-10-31 03:43:31 +0000
99+++ playbook.yaml 2015-05-25 04:21:12 +0000
100@@ -60,3 +60,13 @@
101 cluster-name={{ cluster_name }}
102 host={{ ansible_default_ipv4.address }}
103 port=9200
104+
105+ # A bug in the ansible hooks() helper requires at least
106+ # one task to be tagged.
107+ - name: Empty task to keep ansible helper satisfied.
108+ debug: msg="Noop ansible task."
109+ tags:
110+ - data-relation-joined
111+ - data-relation-changed
112+ - data-relation-departed
113+ - data-relation-broken
114
115=== modified file 'roles/nrpe/tasks/main.yml'
116--- roles/nrpe/tasks/main.yml 2014-04-08 13:25:50 +0000
117+++ roles/nrpe/tasks/main.yml 2015-05-25 04:21:12 +0000
118@@ -7,6 +7,7 @@
119 owner: nagios
120 group: nagios
121 mode: 0644
122+ when: "'nagios_hostname' in current_relation"
123
124 - name: Write nagios check service definition for export.
125 tags:
126@@ -17,9 +18,11 @@
127 owner: nagios
128 group: nagios
129 mode: 0644
130+ when: "'nagios_hostname' in current_relation"
131
132 - name: Trigger nrpe-external-master-relation-changed to restart.
133 tags:
134 - nrpe-external-master-relation-changed
135 command: >
136 relation-set timestamp={{ ansible_date_time.iso8601_micro }}
137+ when: "'nagios_hostname' in current_relation"

Subscribers

People subscribed via source and target branches