Merge lp:~tcuthbert/turku-charms/turku-api-charm into lp:turku-charms/turku-api-charm

Proposed by Thomas Cuthbert
Status: Merged
Approved by: Barry Price
Approved revision: 11
Merged at revision: 10
Proposed branch: lp:~tcuthbert/turku-charms/turku-api-charm
Merge into: lp:turku-charms/turku-api-charm
Diff against target: 53 lines (+37/-1)
1 file modified
hooks/hooks.py (+37/-1)
To merge this branch: bzr merge lp:~tcuthbert/turku-charms/turku-api-charm
Reviewer Review Type Date Requested Status
Barry Price Approve
Review via email: mp+397544@code.launchpad.net

Commit message

Charm changes required to monitor turku backups with Prometheus

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Unable to determine commit message from repository - please click "Set commit message" and enter the commit message manually.

Revision history for this message
Barry Price (barryprice) wrote :

LGTM +1

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 10

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/hooks.py'
2--- hooks/hooks.py 2015-10-30 05:09:23 +0000
3+++ hooks/hooks.py 2021-02-05 02:25:50 +0000
4@@ -14,6 +14,26 @@
5 playbook_path='playbook.yml')
6
7
8+def am_i_leader():
9+ """Calls the is-leader juju helper to determine if server is current leader"""
10+ string_to_boolean = {'True': True, 'False': False}
11+ call_out = None
12+ is_leader = False
13+ try:
14+ call_out = subprocess.run(['is-leader'], check=True, text=True, timeout=15.0)
15+ is_leader = string_to_boolean[call_out.STDOUT]
16+ except KeyError:
17+ juju_log('ERROR', 'is-leader juju helper returned unexpected value: {}'.format(call_out))
18+ except subprocess.CalledProcessError:
19+ juju_log('ERROR', 'is-leader juju helper failed to execute: {}'.format(call_out.STDOUT + call_out.STDERR))
20+ return is_leader
21+
22+
23+def juju_log(severity, message):
24+ cmd = ['juju-log', '--log-level', severity, message]
25+ subprocess.check_call(cmd)
26+
27+
28 @hooks.hook('install', 'upgrade-charm')
29 def install():
30 for f in glob.glob('exec.d/*/charm-pre-install'):
31@@ -29,5 +49,21 @@
32 charmhelpers.contrib.ansible.install_ansible_support(from_ppa=True)
33
34
35+@hooks.hook('leader-elected', 'leader-settings-changed')
36+def leader_elected():
37+ # This manual hook exists because calling ansible every leadership event is a bad idea.
38+ sick_sources_script = '/srv/turku.admin.canonical.com/code/current/turku_api/scripts/turku_sick_sources'
39+ cron_daily_sick_sources = '/etc/cron.daily/sick_sources'
40+ is_leader = am_i_leader()
41+ if is_leader:
42+ # install the script to cron.daily.
43+ os.symlink(sick_sources_script, cron_daily_sick_sources)
44+ else:
45+ # check if script in cron.daily, remove if so.
46+ cron_daily_sick_sources = "/etc/cron.daily/sick_sources"
47+ if os.path.isfile(cron_daily_sick_sources):
48+ os.unlink(cron_daily_sick_sources)
49+
50+
51 if __name__ == "__main__":
52- hooks.execute(sys.argv)
53+ hooks.execute(sys.argv)

Subscribers

People subscribed via source and target branches