Merge lp:~danilo/charms/trusty/glance-simplestreams-sync/leader-only into lp:~landscape/charms/trusty/glance-simplestreams-sync/landscape

Proposed by Данило Шеган
Status: Rejected
Rejected by: Данило Шеган
Proposed branch: lp:~danilo/charms/trusty/glance-simplestreams-sync/leader-only
Merge into: lp:~landscape/charms/trusty/glance-simplestreams-sync/landscape
Diff against target: 107 lines (+40/-7)
2 files modified
hooks/hooks.py (+12/-0)
scripts/glance-simplestreams-sync.py (+28/-7)
To merge this branch: bzr merge lp:~danilo/charms/trusty/glance-simplestreams-sync/leader-only
Reviewer Review Type Date Requested Status
Bogdana Vereha (community) Approve
Chris Glass (community) Approve
Review via email: mp+293753@code.launchpad.net

Description of the change

Make glance-simplestreams-sync.py script no-op when not run on the leader unit

To avoid changing the structure too much, we do not set-up and destroy cronjobs in leader-change hooks, but instead simply make the cronjob script no-op when not run on the leader unit. Cronjob setup is a bit intricate and involved (there's a per-minute cronjob set initially, along with the cronjob depending on the frequency configured for the service).

Testing instructions:

This charm has been published at cs:~danilo/trusty/glance-simplestreams-sync-21

See Landscape branch that integrates this charm https://code.launchpad.net/~danilo/landscape/ha-image-refresh/+merge/293751

To post a comment you must log in.
70. By Данило Шеган

Strip the returned value from is-leader.

Revision history for this message
Chris Glass (tribaal) wrote :

Looks good, one nitpick inline.

review: Approve
71. By Данило Шеган

Return explicit False (instead of implicit None) in is_leader() on exception.

Revision history for this message
Данило Шеган (danilo) :
Revision history for this message
Bogdana Vereha (bogdana) wrote :

+1

review: Approve
72. By Данило Шеган

Add leader hooks.

73. By Данило Шеган

Merge landscape charm tip.

74. By Данило Шеган

Do not drop the glance-simplestreams-sync per-minute cronjob when not leader inside the script, but manage that from leader-related hooks.

Revision history for this message
Данило Шеган (danilo) wrote :

As discussed with David, we'll be shelving this work for now, waiting for glance-simplestreams-sync stuff to be integrated into glance itself, when we'll get HA as well.

Unmerged revisions

74. By Данило Шеган

Do not drop the glance-simplestreams-sync per-minute cronjob when not leader inside the script, but manage that from leader-related hooks.

73. By Данило Шеган

Merge landscape charm tip.

72. By Данило Шеган

Add leader hooks.

71. By Данило Шеган

Return explicit False (instead of implicit None) in is_leader() on exception.

70. By Данило Шеган

Strip the returned value from is-leader.

69. By Данило Шеган

Merge lint fixes from trunk.

68. By Данило Шеган

Lint fix.

67. By Данило Шеган

Revert accidental change.

66. By Данило Шеган

Use a better check for is-leader.

65. By Данило Шеган

Do not validate full configuration when getting unit_name for juju_run_cmd().

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 2016-04-14 07:03:35 +0000
3+++ hooks/hooks.py 2016-05-05 09:04:26 +0000
4@@ -246,6 +246,18 @@
5 config.save()
6
7
8+@hooks.hook('leader-elected')
9+def leader_elected():
10+ hookenv.log('elected as leader.')
11+ install_cron_poll()
12+
13+
14+@hooks.hook('leader-settings-changed')
15+def leader_settings_changed():
16+ hookenv.log('leader settings changed.')
17+ uninstall_cron_poll()
18+
19+
20 @hooks.hook('upgrade-charm')
21 def upgrade_charm():
22 install()
23
24=== modified file 'scripts/glance-simplestreams-sync.py'
25--- scripts/glance-simplestreams-sync.py 2016-04-27 13:04:08 +0000
26+++ scripts/glance-simplestreams-sync.py 2016-05-05 09:04:26 +0000
27@@ -133,7 +133,7 @@
28 return confobj
29
30
31-def get_conf():
32+def get_conf(validate=True):
33 conf_files = [ID_CONF_FILE_NAME, CHARM_CONF_FILE_NAME]
34 for conf_file_name in conf_files:
35 if not os.path.exists(conf_file_name):
36@@ -141,12 +141,12 @@
37 sys.exit(1)
38
39 id_conf = read_conf(ID_CONF_FILE_NAME)
40- if None in id_conf.values():
41+ if validate and None in id_conf.values():
42 log.info("Configuration value missing in {}:\n"
43 "{}".format(ID_CONF_FILE_NAME, id_conf))
44 sys.exit(1)
45 charm_conf = read_conf(CHARM_CONF_FILE_NAME)
46- if None in charm_conf.values():
47+ if validate and None in charm_conf.values():
48 log.info("Configuration value missing in {}:\n"
49 "{}".format(CHARM_CONF_FILE_NAME, charm_conf))
50 sys.exit(1)
51@@ -271,7 +271,7 @@
52
53 def juju_run_cmd(cmd):
54 '''Execute the passed commands under the local unit context'''
55- id_conf, _ = get_conf()
56+ id_conf, _ = get_conf(validate=False)
57 unit_name = id_conf['unit_name']
58 _cmd = ['juju-run', unit_name, ' '.join(cmd)]
59 log.info("Executing command: {}".format(_cmd))
60@@ -286,6 +286,16 @@
61 log.info(message)
62
63
64+def is_leader():
65+ """Returns True if the script is running on the leader unit."""
66+ try:
67+ value = juju_run_cmd(['is-leader']).strip()
68+ return value == 'True'
69+ except subprocess.CalledProcessError as e:
70+ log.info(e.message)
71+ return False
72+
73+
74 def update_endpoint_urls(region, publicurl, adminurl, internalurl):
75 # Notify keystone via the identity service relation about
76 # and endpoint changes
77@@ -381,8 +391,21 @@
78 raise e
79
80
81+def remove_initial_cronjob(should_delete_cron_poll=False):
82+ """
83+ Remove the initial per-minute cron job if should_delete_cron_poll is True.
84+ """
85+ if os.path.exists(CRON_POLL_FILENAME) and should_delete_cron_poll:
86+ os.unlink(CRON_POLL_FILENAME)
87+ log.info("Initial sync attempt done. every-minute cronjob removed.")
88+
89+
90 def main():
91
92+ if not is_leader():
93+ log.info("glance-simplestreams-sync can only run on the leader unit.")
94+ sys.exit(0)
95+
96 log.info("glance-simplestreams-sync started.")
97
98 lockfile = open(SYNC_RUNNING_FLAG_FILE_NAME, 'w')
99@@ -476,9 +499,7 @@
100
101 status_exchange.close()
102
103- if os.path.exists(CRON_POLL_FILENAME) and should_delete_cron_poll:
104- os.unlink(CRON_POLL_FILENAME)
105- log.info("Initial sync attempt done. every-minute cronjob removed.")
106+ remove_initial_cronjob(should_delete_cron_poll)
107 log.info("sync done.")
108
109

Subscribers

People subscribed via source and target branches