~tanuki/charms/trusty/logstash/trunk

« back to all changes in this revision

Viewing changes to hooks/client-relation-changed

  • Committer: Guillermo Gonzalez
  • Date: 2015-09-08 16:23:29 UTC
  • mto: This revision was merged to the branch mainline in revision 55.
  • Revision ID: guillermo.gonzalez@canonical.com-20150908162329-tmfenasm1f3lmzit
make cache_hosts more generic by iterate over all client relations, and fix identation 

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
    out = os.path.join(BASEPATH, 'conf.d', 'output-elasticsearch.conf')
36
36
    with open(out, 'w') as p:
37
 
      p.write(render(os.path.basename(out), opts))
38
 
 
 
37
        p.write(render(os.path.basename(out), opts))
39
38
 
40
39
 
41
40
def cache_hosts():
42
 
    host = hookenv.relation_get('host')
43
 
    if not host:
44
 
      log('No host received. Assuming nothing to do.')
45
 
      sys.exit(0)
46
 
 
 
41
    rels = hookenv.relations_of_type("client")
 
42
    if not rels:
 
43
        log('No client relations. Assuming nothing to do.')
 
44
        sys.exit(0)
47
45
    if not os.path.exists('host_cache'):
48
46
        open('host_cache', 'a').close()
49
 
 
50
 
    with open('host_cache', 'r') as f:
51
 
        hosts = f.readlines()
52
 
    if not host in hosts:
53
 
        with open('host_cache', 'a') as f:
54
 
            f.write('{}\n'.format(host))
 
47
    for rel in rels:
 
48
        host = rel.get('host')
 
49
        if not host:
 
50
            log('No host received for relation: {}.'.format(rel))
 
51
            continue
 
52
        with open('host_cache', 'r') as f:
 
53
            hosts = f.readlines()
 
54
        if host not in hosts:
 
55
            with open('host_cache', 'a') as f:
 
56
                f.write('{}\n'.format(host))
55
57
 
56
58
 
57
59
if __name__ == "__main__":