Merge ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master into ~livepatch-charmers/charm-canonical-livepatch:master

Proposed by Barry Price
Status: Merged
Approved by: Barry Price
Approved revision: a4ecf65ef7ea5bc480f230bfd4d602461cba895b
Merged at revision: 9635b94b1f64a01b2d51cd13f542d8b4cbc6587b
Proposed branch: ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master
Merge into: ~livepatch-charmers/charm-canonical-livepatch:master
Diff against target: 98 lines (+26/-17)
1 file modified
reactive/canonical_livepatch.py (+26/-17)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Review via email: mp+347668@code.launchpad.net

Commit message

Use reactive triggers to handle proxy setting, avoid handling the reconfig/restart logic twice

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
Stuart Bishop (stub) wrote :

Looks good to me.

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

Change successfully merged at revision 9635b94b1f64a01b2d51cd13f542d8b4cbc6587b

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py
2index f71923d..7c0b559 100644
3--- a/reactive/canonical_livepatch.py
4+++ b/reactive/canonical_livepatch.py
5@@ -1,5 +1,6 @@
6 from charms import layer
7-from charms.reactive import when, when_not, set_state, remove_state
8+from charms.reactive import when, when_not, set_flag, clear_flag
9+from charms.reactive.flags import register_trigger
10 from charmhelpers.core.host import write_file, is_container
11 from charmhelpers.core import hookenv
12 from charmhelpers.contrib.charmsupport import nrpe
13@@ -167,17 +168,21 @@ def activate_livepatch():
14 check_output(cmd, universal_newlines=True)
15 except CalledProcessError as e:
16 hookenv.log('Unable to activate: {}'.format(str(e)), hookenv.ERROR)
17- remove_state('canonical-livepatch.active')
18+ clear_flag('canonical-livepatch.active')
19 unit_update('blocked', 'Activation failed')
20 else:
21- set_state('canonical-livepatch.active')
22+ set_flag('canonical-livepatch.active')
23+ clear_flag('canonical-livepatch.restart-needed')
24 unit_update()
25 else:
26 hookenv.log('Unable to activate canonical-livepatch as no key has been set', hookenv.ERROR)
27- remove_state('canonical-livepatch.active')
28+ clear_flag('canonical-livepatch.active')
29 unit_update('blocked', 'Service disabled, please set livepatch_key to activate')
30
31
32+register_trigger(when='config.changed.livepatch_proxy', clear_flag='livepatch-proxy.configured')
33+
34+
35 @when_not('snap.installed.canonical-livepatch')
36 def livepatch_supported():
37 arch = uname()[4]
38@@ -191,7 +196,19 @@ def livepatch_supported():
39 unit_update('blocked', 'Livepatch is not needed in OS containers')
40
41
42+@when_not('livepatch-proxy.configured')
43+def proxy_configure():
44+ # Configure proxies early, if required - LP#1761661
45+ config = hookenv.config()
46+ proxy_url = config.get('livepatch_proxy')
47+ if proxy_url:
48+ configure_proxies(http_proxy=proxy_url, https_proxy=proxy_url)
49+ set_flag('livepatch-proxy.configured')
50+ set_flag('canonical-livepatch.restart-needed')
51+
52+
53 @when('snap.installed.canonical-livepatch')
54+@when('livepatch-proxy.configured')
55 @when_not('canonical-livepatch.connected')
56 def canonical_livepatch_connect():
57 # So if we've just installed snapd on a trusty system, we will not be on
58@@ -207,14 +224,9 @@ def canonical_livepatch_connect():
59 unit_update('blocked', 'A reboot is required')
60 else:
61 unit_update('maintenance', 'Connecting to the livepatch service')
62- # Configure proxies early, if required - LP#1761661
63- config = hookenv.config()
64- proxy_url = config.get('livepatch_proxy')
65- if proxy_url:
66- configure_proxies(http_proxy=proxy_url, https_proxy=proxy_url)
67 # Make sure the service is ready for us
68 wait_for_livepatch()
69- set_state('canonical-livepatch.connected')
70+ set_flag('canonical-livepatch.connected')
71 unit_update('blocked', 'Service disabled, please set livepatch_key to activate')
72
73
74@@ -233,12 +245,9 @@ def update_key():
75 activate_livepatch()
76
77
78-@when('canonical-livepatch.connected', 'config.changed.livepatch_proxy')
79-def update_livepatch_proxy():
80- unit_update('maintenance', 'Configuring proxy servers')
81- config = hookenv.config()
82- proxy_url = config.get('livepatch_proxy')
83- configure_proxies(http_proxy=proxy_url, https_proxy=proxy_url)
84+@when('canonical-livepatch.connected', 'canonical-livepatch.restart-needed')
85+def handle_restart():
86+ unit_update('maintenance', 'Restarting client')
87
88 # restart the system service to pick up the new config
89 restart_livepatch()
90@@ -290,7 +299,7 @@ def configure_nagios(nagios):
91 nrpe_setup.write()
92
93 # Remove obsolete state from older charm.
94- remove_state('canonical-livepatch.nagios-configured')
95+ clear_flag('canonical-livepatch.nagios-configured')
96
97
98 @when('snap.installed.canonical-livepatch', 'canonical-livepatch.active')

Subscribers

People subscribed via source and target branches