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
1diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py
2index de797ec..21a0f4d 100644
3--- a/reactive/canonical_livepatch.py
4+++ b/reactive/canonical_livepatch.py
5@@ -6,8 +6,8 @@ from charmhelpers.core import hookenv
6 from charmhelpers.contrib.charmsupport import nrpe
7 from subprocess import check_call, check_output, CalledProcessError
8 from time import sleep
9-from os import path, uname
10-from yaml import load, dump
11+from os import environ, path, uname
12+from yaml import load
13 from distutils.version import LooseVersion
14
15
16@@ -83,69 +83,16 @@ def get_patch_details():
17 return kernel, patch_state
18
19
20-def get_yaml_if_exists(path_to_yaml):
21- if path.exists(path_to_yaml):
22- with open(path_to_yaml, 'r') as stream:
23- try:
24- conf_yaml = load(stream)
25- except Exception:
26- hookenv.log('Unable to load YAML from {}'.format(path_to_yaml), hookenv.ERROR)
27- else:
28- conf_yaml = {}
29-
30- return conf_yaml
31-
32-
33-def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None):
34- config_path = '/var/snap/canonical-livepatch/common/config'
35- conf_yaml = get_yaml_if_exists(config_path)
36-
37- if conf_yaml.get('http-proxy'):
38- if http_proxy is None:
39- del conf_yaml['http-proxy']
40- else:
41- conf_yaml['http-proxy'] = http_proxy
42- elif http_proxy:
43- conf_yaml['http-proxy'] = http_proxy
44-
45- if conf_yaml.get('https-proxy'):
46- if https_proxy is None:
47- del conf_yaml['https-proxy']
48- else:
49- conf_yaml['https-proxy'] = https_proxy
50- elif https_proxy:
51- conf_yaml['https-proxy'] = https_proxy
52-
53- if conf_yaml.get('no-proxy'):
54- if no_proxy is None:
55- del conf_yaml['no-proxy']
56- else:
57- conf_yaml['no-proxy'] = no_proxy
58- elif no_proxy:
59- conf_yaml['no-proxy'] = no_proxy
60-
61- stream = open(config_path, 'w')
62- dump(conf_yaml, stream)
63-
64-
65-def restart_livepatch():
66- # do a clean stop of the service first, 'restart' seems fragile right now
67- cmd = ['/bin/systemctl', 'stop', 'snap.canonical-livepatch.canonical-livepatchd.service']
68- hookenv.log('Stopping canonical-livepatch service')
69- try:
70- check_call(cmd, universal_newlines=True)
71- except CalledProcessError:
72- hookenv.log('Failed to stop the service', hookenv.ERROR)
73-
74- # and now try to start it again, it may fail the first time!
75- cmd = ['/bin/systemctl', 'start', 'snap.canonical-livepatch.canonical-livepatchd.service']
76- hookenv.log('Starting canonical-livepatch service')
77- try:
78- check_call(cmd, universal_newlines=True)
79- except CalledProcessError:
80- hookenv.log('Failed to start the service - waiting 5s and then trying again', hookenv.ERROR)
81- sleep(5)
82- check_call(cmd, universal_newlines=True)
83+def configure_proxies(proxy=None):
84+ for proto in ('http-proxy', 'https-proxy'):
85+ if proxy is None:
86+ proxy = ''
87+ cmd = ['/snap/bin/canonical-livepatch', 'config', '{}={}'.format(proto, proxy)]
88+ hookenv.log('Configuring {} as {}'.format(proto, proxy))
89+ try:
90+ check_call(cmd, universal_newlines=True)
91+ except CalledProcessError:
92+ hookenv.log('Failed to set proxy', hookenv.ERROR)
93
94
95 def activate_livepatch():
96@@ -172,7 +119,6 @@ def activate_livepatch():
97 unit_update('blocked', 'Activation failed')
98 else:
99 set_flag('canonical-livepatch.active')
100- clear_flag('canonical-livepatch.restart-needed')
101 unit_update()
102 else:
103 hookenv.log('Unable to activate canonical-livepatch as no key has been set', hookenv.ERROR)
104@@ -197,16 +143,31 @@ def livepatch_supported():
105 else:
106 set_flag('canonical-livepatch.supported')
107
108+
109 @when('canonical-livepatch.supported')
110 @when_not('livepatch-proxy.configured')
111-def proxy_configure():
112+def proxy_settings():
113 # Configure proxies early, if required - LP#1761661
114+
115+ preferred_proxy = None # default to None
116+
117+ for key, value in environ.items():
118+ # use environment's https_proxy or http_proxy if either are set
119+ if key == 'https_proxy':
120+ preferred_proxy = value
121+ elif key == 'http_proxy':
122+ preferred_proxy = value
123+
124+ # but if we have a charm configured proxy, prefer that
125 config = hookenv.config()
126- proxy_url = config.get('livepatch_proxy')
127- if proxy_url:
128- configure_proxies(http_proxy=proxy_url, https_proxy=proxy_url)
129+ charm_config_proxy = config.get('livepatch_proxy')
130+ if charm_config_proxy:
131+ preferred_proxy = charm_config_proxy
132+
133+ # whatever we prefer (or None), configure it!
134+ configure_proxies(preferred_proxy)
135+
136 set_flag('livepatch-proxy.configured')
137- set_flag('canonical-livepatch.restart-needed')
138
139
140 @when('snap.installed.canonical-livepatch')
141@@ -247,20 +208,6 @@ def update_key():
142 activate_livepatch()
143
144
145-@when('canonical-livepatch.connected', 'canonical-livepatch.restart-needed')
146-def handle_restart():
147- unit_update('maintenance', 'Restarting client')
148-
149- # restart the system service to pick up the new config
150- restart_livepatch()
151-
152- # Make sure the service is ready for us
153- wait_for_livepatch()
154-
155- # force a key update, we may have been blocked before proxy was set
156- update_key()
157-
158-
159 @when('nrpe-external-master.available')
160 def configure_nagios(nagios):
161 if hookenv.hook_name() == 'update-status':

Subscribers

People subscribed via source and target branches