~wesmason/charms/trusty/telegraf/bump-charm-helpers

« back to all changes in this revision

Viewing changes to hooks/actions.py

  • Committer: Guillermo Gonzalez
  • Date: 2016-02-03 19:45:30 UTC
  • Revision ID: guillermo.gonzalez@canonical.com-20160203194530-twdt2rf34cbvb8nb
fix UNIT_NAME handling logic to avoid starting telegraf with an empty or wrong hostname value

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
        return old
33
33
 
34
34
    def __call__(self, manager, service_name, event_name):
35
 
        if hookenv.hook_name().endswith('relation-changed'):
36
 
            rel_type = hookenv.relation_type()
 
35
        new_hash = host.file_hash(CONFIG_FILE)
 
36
        old_hash = self._update_persisted_data('config_hash', new_hash)
 
37
        if new_hash != old_hash:
 
38
            hookenv.log('Restarting service, config file changed')
 
39
            host.service_restart(service_name)
 
40
            return
 
41
        types = hookenv.relation_types()
 
42
        for rel_type in types:
 
43
            if rel_type == 'juju-info':
 
44
                continue
37
45
            config_path = os.path.join('{}/{}.conf'.format(CONFIG_DIR, rel_type))
38
46
            new_hash = host.file_hash(config_path)
39
47
            old_hash = self._update_persisted_data(rel_type, new_hash)
41
49
                hookenv.log('Restarting service, {}'.format(hookenv.hook_name()))
42
50
                host.service_restart(service_name)
43
51
                return
44
 
        elif hookenv.hook_name() == 'config-changed':
45
 
            new_hash = host.file_hash(CONFIG_FILE)
46
 
            old_hash = self._update_persisted_data('config_hash', new_hash)
47
 
            if new_hash != old_hash:
48
 
                hookenv.log('Restarting service, config file changed')
49
 
                host.service_restart(service_name)
50
 
                return
51
52
        hookenv.log('Not restarting service, config and relations unchanged')
52
53
 
53
54
 
96
97
        context["outputs"] = ""
97
98
        hookenv.log("No output plugins in main config.")
98
99
    remote_unit_name = get_remote_unit_name()
99
 
    if config["hostname"] == "UNIT_NAME" and remote_unit_name is not None:
100
 
        context["hostname"] = remote_unit_name.replace('/', '-')
 
100
    if config["hostname"] == "UNIT_NAME":
 
101
        if remote_unit_name is not None:
 
102
            context["hostname"] = remote_unit_name.replace('/', '-')
 
103
        else:
 
104
            hookenv.log("Waiting for relation to render config file.")
 
105
            # if UNIT_NAME in hostname config and relation not yet available,
 
106
            # make telegraf unable to start to not get weird metrics names
 
107
            if os.path.exists(CONFIG_FILE):
 
108
                os.unlink(CONFIG_FILE)
 
109
            return
101
110
    hookenv.log("Updating config file")
102
111
    render(source='telegraf.conf.tmpl', target=CONFIG_FILE, context=context)
103
112