Merge lp:~michael.nelson/charms/trusty/logstash/fix-es-hosts into lp:~canonical-is-sa/charms/trusty/logstash/logstash2

Proposed by Michael Nelson
Status: Merged
Merged at revision: 56
Proposed branch: lp:~michael.nelson/charms/trusty/logstash/fix-es-hosts
Merge into: lp:~canonical-is-sa/charms/trusty/logstash/logstash2
Diff against target: 76 lines (+9/-30)
2 files modified
hooks/client-relation-changed (+8/-29)
templates/output-elasticsearch.conf (+1/-1)
To merge this branch: bzr merge lp:~michael.nelson/charms/trusty/logstash/fix-es-hosts
Reviewer Review Type Date Requested Status
Michael Foley (community) Approve
Review via email: mp+276841@code.launchpad.net

Commit message

Include all ES hosts in the output-elasticsearch.conf

Description of the change

I've used json.dumps to create a string list compatible with that used in the ruby file. A python list is rendered with u"10.x.x.x:9200" unicode strings which logstash obviously doesn't like.

Tested on local doployment.

To post a comment you must log in.
59. By Michael Nelson

Add client-relation-departed

60. By Michael Nelson

Add client-relation-departed to hooks python file.

Revision history for this message
Michael Foley (foli) wrote :

looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/client-relation-changed'
2--- hooks/client-relation-changed 2015-09-08 19:45:13 +0000
3+++ hooks/client-relation-changed 2015-11-06 04:06:33 +0000
4@@ -1,5 +1,6 @@
5 #!/usr/bin/python
6
7+import json
8 import os
9 import sys
10
11@@ -19,45 +20,23 @@
12 BASEPATH = os.path.join(os.path.sep, 'opt', 'logstash')
13
14
15-@hooks.hook('client-relation-changed')
16+@hooks.hook('client-relation-changed', 'client-relation-departed')
17 def rest_changed():
18- cache_hosts()
19 write_config()
20 host.service_restart(SERVICE) or host.service_start(SERVICE)
21
22
23 def write_config():
24- with open('host_cache', 'r') as f:
25- hosts = f.readlines()
26- if not hosts:
27- sys.exit(0)
28-
29- # Use last host in list as it will be the most recently added
30- # and first host in list may not exist anymore! TODO fix that.
31- opts = {'hosts': hosts[-1].rstrip()}
32-
33- out = os.path.join(BASEPATH, 'conf.d', 'output-elasticsearch.conf')
34- with open(out, 'w') as p:
35- p.write(render(os.path.basename(out), opts))
36-
37-
38-def cache_hosts():
39 rels = hookenv.relations_of_type("client")
40 if not rels:
41 log('No client relations. Assuming nothing to do.')
42 sys.exit(0)
43- if not os.path.exists('host_cache'):
44- open('host_cache', 'a').close()
45- for rel in rels:
46- host = rel.get('host')
47- if not host:
48- log('No host received for relation: {}.'.format(rel))
49- continue
50- with open('host_cache', 'r') as f:
51- hosts = f.readlines()
52- if host not in hosts:
53- with open('host_cache', 'a') as f:
54- f.write('{}\n'.format(host))
55+ hosts = json.dumps(
56+ ["%s:%s" % (client['host'], client['port']) for client in rels])
57+
58+ out = os.path.join(BASEPATH, 'conf.d', 'output-elasticsearch.conf')
59+ with open(out, 'w') as p:
60+ p.write(render(os.path.basename(out), dict(hosts=hosts)))
61
62
63 if __name__ == "__main__":
64
65=== added symlink 'hooks/client-relation-departed'
66=== target is u'client-relation-changed'
67=== modified file 'templates/output-elasticsearch.conf'
68--- templates/output-elasticsearch.conf 2015-11-02 23:00:01 +0000
69+++ templates/output-elasticsearch.conf 2015-11-06 04:06:33 +0000
70@@ -1,5 +1,5 @@
71 output {
72 elasticsearch {
73- hosts => "{{hosts}}"
74+ hosts => {{ hosts }}
75 }
76 }

Subscribers

People subscribed via source and target branches