~brad-marshall/charms/trusty/ntp/remove-ntp-status-check

« back to all changes in this revision

Viewing changes to hooks/ntp_hooks.py

  • Committer: Charles Butler
  • Date: 2015-04-16 17:34:09 UTC
  • mfrom: (17.2.20 ntp)
  • Revision ID: charles.butler@ubuntu.com-20150416173409-18pvp0hz1at0exhp
[r=lazypower] Charles Butler 2015-04-16 [lazypower] Ammended unit test to specify private-address
Brad Marshall 2015-04-01 [merge] [bradm] Fixed unit name on the test
Whit Morriss 2015-03-31 specify unit
Brad Marshall 2015-03-31 [bradm] Merge in nrpe fixes
Brad Marshall 2015-03-27 [bradm] Remove the total_source line
Brad Marshall 2015-03-27 [bradm] Tidied up extra unneeded lines.
Brad Marshall 2015-03-27 [bradm] Deploy ntp subordinate before adding relation to ntpmaster, c...
Brad Marshall 2015-03-27 [bradm] Apply the timeout to the sentry
Brad Marshall 2015-03-27 [bradm] Add missing type to nagios_servicegroups
Brad Marshall 2015-03-27 [bradm] Fixed nagios_servicegroup setting
Brad Marshall 2015-03-27 [bradm] Added default empty string to nagios_servicegroup, unit tests...
Brad Marshall 2015-03-25 [merge] [bradm] Fixed conflicts with upstream
Brad Marshall 2015-03-17 [bradm] Add docs on auto peers and peers config options.
Brad Marshall 2015-03-16 [bradm] Fixed use_iburst variable to work properly
Brad Marshall 2015-03-16 [bradm] Added use_iburst config variable, tidy up warning message
Brad Marshall 2015-03-16 [bradm] Fixed formatting on warning
Brad Marshall 2015-03-16 [bradm] Not using brackets inside strings
Brad Marshall 2015-03-16 [bradm] Add backslash for continued line
Brad Marshall 2015-03-16 [bradm] Added restrict line for new ntp sources to ntp.conf. Add peer...
Brad Marshall 2015-03-16 [bradm] Gave source a default value, made auto_peers a boolean
Brad Marshall 2015-03-16 [bradm] Add auto_peers config option
Brad Marshall 2015-03-16 [bradm] First cut of ntp peers relationship

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
hooks = hookenv.Hooks()
20
20
 
21
21
 
 
22
def get_peer_nodes():
 
23
    hosts = []
 
24
    hosts.append(hookenv.unit_get('private-address'))
 
25
    for relid in hookenv.relation_ids('ntp-peers'):
 
26
        for unit in hookenv.related_units(relid):
 
27
            hosts.append(hookenv.relation_get('private-address',
 
28
                         unit, relid))
 
29
    hosts.sort()
 
30
    return hosts
 
31
 
 
32
 
22
33
@hooks.hook('install')
23
34
def install():
24
35
    fetch.apt_update(fatal=True)
30
41
@hooks.hook('config-changed')
31
42
@hooks.hook('master-relation-changed')
32
43
@hooks.hook('master-relation-departed')
 
44
@hooks.hook('ntp-peers-relation-joined',
 
45
            'ntp-peers-relation-changed')
33
46
@host.restart_on_change({NTP_CONF: ['ntp']})
34
47
def write_config():
 
48
    use_iburst = hookenv.config('use_iburst')
35
49
    source = hookenv.config('source')
36
50
    remote_sources = []
37
51
    if source:
38
52
        for s in source.split(" "):
39
53
            if len(s) > 0:
40
 
                remote_sources.append({'name': s})
 
54
                if use_iburst:
 
55
                    remote_sources.append({'name': '%s iburst' % s})
 
56
                else:
 
57
                    remote_sources.append({'name': s})
41
58
    for relid in hookenv.relation_ids('master'):
42
59
        for unit in hookenv.related_units(relid=relid):
43
60
            u_addr = hookenv.relation_get(attribute='private-address',
44
61
                                          unit=unit, rid=relid)
45
62
            remote_sources.append({'name': '%s iburst' % u_addr})
46
63
 
 
64
    auto_peers = hookenv.config('auto_peers')
 
65
    peers = hookenv.config('peers')
 
66
    remote_peers = []
 
67
    if peers:
 
68
        for p in peers.split(" "):
 
69
            if len(p) > 0:
 
70
                remote_peers.append(p)
 
71
    if hookenv.relation_ids('ntp-peers'):
 
72
        if auto_peers:
 
73
            for rp in get_peer_nodes():
 
74
                remote_peers.append(rp)
 
75
 
47
76
    if len(remote_sources) == 0:
48
77
        shutil.copy(NTP_CONF_ORIG, NTP_CONF)
49
78
    else:
50
79
        with open(NTP_CONF, "w") as ntpconf:
51
80
            ntpconf.write(render(os.path.basename(NTP_CONF),
52
 
                                 {'servers': remote_sources}))
53
 
 
 
81
                                 {'servers': remote_sources,
 
82
                                  'peers': remote_peers,
 
83
                                  'use_iburst': use_iburst}))
54
84
    update_nrpe_config()
55
85
 
56
86