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: Paul Gear
Approved revision: 520238983466631f233c6c64f8ac64808612d492
Merged at revision: 74b40b32f58e96cf431425f07e6f166a53e281c0
Proposed branch: ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master
Merge into: ~livepatch-charmers/charm-canonical-livepatch:master
Diff against target: 161 lines (+32/-85)
1 file modified
reactive/canonical_livepatch.py (+32/-85)
Reviewer Review Type Date Requested Status
Paul Gear (community) Approve
Review via email: mp+348671@code.launchpad.net

Commit message

Use livepatch's native config for proxies - we no longer need to write our own YAML or restart the service. Also import os into its own namespace, will cover other imports in a future MP

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
Paul Gear (paulgear) wrote :

LGTM, but if you have a plan to switch to importing modules into separate namespaces, then now's the time to do it. At least time.sleep and yaml.load are trivial to implement now.

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

Change successfully merged at revision 74b40b32f58e96cf431425f07e6f166a53e281c0

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py
index de797ec..21a0f4d 100644
--- a/reactive/canonical_livepatch.py
+++ b/reactive/canonical_livepatch.py
@@ -6,8 +6,8 @@ from charmhelpers.core import hookenv
6from charmhelpers.contrib.charmsupport import nrpe6from charmhelpers.contrib.charmsupport import nrpe
7from subprocess import check_call, check_output, CalledProcessError7from subprocess import check_call, check_output, CalledProcessError
8from time import sleep8from time import sleep
9from os import path, uname9from os import environ, path, uname
10from yaml import load, dump10from yaml import load
11from distutils.version import LooseVersion11from distutils.version import LooseVersion
1212
1313
@@ -83,69 +83,16 @@ def get_patch_details():
83 return kernel, patch_state83 return kernel, patch_state
8484
8585
86def get_yaml_if_exists(path_to_yaml):86def configure_proxies(proxy=None):
87 if path.exists(path_to_yaml):87 for proto in ('http-proxy', 'https-proxy'):
88 with open(path_to_yaml, 'r') as stream:88 if proxy is None:
89 try:89 proxy = ''
90 conf_yaml = load(stream)90 cmd = ['/snap/bin/canonical-livepatch', 'config', '{}={}'.format(proto, proxy)]
91 except Exception:91 hookenv.log('Configuring {} as {}'.format(proto, proxy))
92 hookenv.log('Unable to load YAML from {}'.format(path_to_yaml), hookenv.ERROR)92 try:
93 else:93 check_call(cmd, universal_newlines=True)
94 conf_yaml = {}94 except CalledProcessError:
9595 hookenv.log('Failed to set proxy', hookenv.ERROR)
96 return conf_yaml
97
98
99def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None):
100 config_path = '/var/snap/canonical-livepatch/common/config'
101 conf_yaml = get_yaml_if_exists(config_path)
102
103 if conf_yaml.get('http-proxy'):
104 if http_proxy is None:
105 del conf_yaml['http-proxy']
106 else:
107 conf_yaml['http-proxy'] = http_proxy
108 elif http_proxy:
109 conf_yaml['http-proxy'] = http_proxy
110
111 if conf_yaml.get('https-proxy'):
112 if https_proxy is None:
113 del conf_yaml['https-proxy']
114 else:
115 conf_yaml['https-proxy'] = https_proxy
116 elif https_proxy:
117 conf_yaml['https-proxy'] = https_proxy
118
119 if conf_yaml.get('no-proxy'):
120 if no_proxy is None:
121 del conf_yaml['no-proxy']
122 else:
123 conf_yaml['no-proxy'] = no_proxy
124 elif no_proxy:
125 conf_yaml['no-proxy'] = no_proxy
126
127 stream = open(config_path, 'w')
128 dump(conf_yaml, stream)
129
130
131def restart_livepatch():
132 # do a clean stop of the service first, 'restart' seems fragile right now
133 cmd = ['/bin/systemctl', 'stop', 'snap.canonical-livepatch.canonical-livepatchd.service']
134 hookenv.log('Stopping canonical-livepatch service')
135 try:
136 check_call(cmd, universal_newlines=True)
137 except CalledProcessError:
138 hookenv.log('Failed to stop the service', hookenv.ERROR)
139
140 # and now try to start it again, it may fail the first time!
141 cmd = ['/bin/systemctl', 'start', 'snap.canonical-livepatch.canonical-livepatchd.service']
142 hookenv.log('Starting canonical-livepatch service')
143 try:
144 check_call(cmd, universal_newlines=True)
145 except CalledProcessError:
146 hookenv.log('Failed to start the service - waiting 5s and then trying again', hookenv.ERROR)
147 sleep(5)
148 check_call(cmd, universal_newlines=True)
14996
15097
151def activate_livepatch():98def activate_livepatch():
@@ -172,7 +119,6 @@ def activate_livepatch():
172 unit_update('blocked', 'Activation failed')119 unit_update('blocked', 'Activation failed')
173 else:120 else:
174 set_flag('canonical-livepatch.active')121 set_flag('canonical-livepatch.active')
175 clear_flag('canonical-livepatch.restart-needed')
176 unit_update()122 unit_update()
177 else:123 else:
178 hookenv.log('Unable to activate canonical-livepatch as no key has been set', hookenv.ERROR)124 hookenv.log('Unable to activate canonical-livepatch as no key has been set', hookenv.ERROR)
@@ -197,16 +143,31 @@ def livepatch_supported():
197 else:143 else:
198 set_flag('canonical-livepatch.supported')144 set_flag('canonical-livepatch.supported')
199145
146
200@when('canonical-livepatch.supported')147@when('canonical-livepatch.supported')
201@when_not('livepatch-proxy.configured')148@when_not('livepatch-proxy.configured')
202def proxy_configure():149def proxy_settings():
203 # Configure proxies early, if required - LP#1761661150 # Configure proxies early, if required - LP#1761661
151
152 preferred_proxy = None # default to None
153
154 for key, value in environ.items():
155 # use environment's https_proxy or http_proxy if either are set
156 if key == 'https_proxy':
157 preferred_proxy = value
158 elif key == 'http_proxy':
159 preferred_proxy = value
160
161 # but if we have a charm configured proxy, prefer that
204 config = hookenv.config()162 config = hookenv.config()
205 proxy_url = config.get('livepatch_proxy')163 charm_config_proxy = config.get('livepatch_proxy')
206 if proxy_url:164 if charm_config_proxy:
207 configure_proxies(http_proxy=proxy_url, https_proxy=proxy_url)165 preferred_proxy = charm_config_proxy
166
167 # whatever we prefer (or None), configure it!
168 configure_proxies(preferred_proxy)
169
208 set_flag('livepatch-proxy.configured')170 set_flag('livepatch-proxy.configured')
209 set_flag('canonical-livepatch.restart-needed')
210171
211172
212@when('snap.installed.canonical-livepatch')173@when('snap.installed.canonical-livepatch')
@@ -247,20 +208,6 @@ def update_key():
247 activate_livepatch()208 activate_livepatch()
248209
249210
250@when('canonical-livepatch.connected', 'canonical-livepatch.restart-needed')
251def handle_restart():
252 unit_update('maintenance', 'Restarting client')
253
254 # restart the system service to pick up the new config
255 restart_livepatch()
256
257 # Make sure the service is ready for us
258 wait_for_livepatch()
259
260 # force a key update, we may have been blocked before proxy was set
261 update_key()
262
263
264@when('nrpe-external-master.available')211@when('nrpe-external-master.available')
265def configure_nagios(nagios):212def configure_nagios(nagios):
266 if hookenv.hook_name() == 'update-status':213 if hookenv.hook_name() == 'update-status':

Subscribers

People subscribed via source and target branches