Merge ~lutostag/layer-snap:use_juju_proxy into ~stub/layer-snap:master

Proposed by Greg Lutostanski
Status: Merged
Merged at revision: 860fa5723c7e82c919b334181f5c0e0d41ff4ad3
Proposed branch: ~lutostag/layer-snap:use_juju_proxy
Merge into: ~stub/layer-snap:master
Diff against target: 43 lines (+16/-4)
1 file modified
reactive/snap.py (+16/-4)
Reviewer Review Type Date Requested Status
Stuart Bishop Approve
Review via email: mp+323391@code.launchpad.net

Description of the change

Use proxying env vars from juju (with config option to disable).

So we don't have to set the proxy to the same as juju model proxy config for every charm we deploy.

To post a comment you must log in.
Revision history for this message
Stuart Bishop (stub) wrote :

Do we actually want the configuration item?

I can't think of any situation where people set http_proxy but do not want it used to access the snap store (as opposed to apt_http_proxy, where that would be very wrong)

Unless we have actual use cases, I'd like the config option removed. We can add it later if needed, but I'd rather not have it polluting the config namespace of all charms using the layer if it isn't needed.

(and now I see this MP, perhaps the separate snap_proxy config item was a bad idea. Maybe we should deprecate that, if we can one day remove it without breaking actual deployments.)

review: Needs Fixing
Revision history for this message
Stuart Bishop (stub) wrote :

Otherwise all looks good, except that I'd like to avoid mutating the hookenv.config() dictionary and instead keep using the charms.reactive data_changed() helper. charmhelpers.hookenv needs to be reworked into charms.hookenv at some point, and the mutating config has been deprecated in favor of charmhelpers.unitdata (which data_changed uses under the hook).

Revision history for this message
Greg Lutostanski (lutostag) wrote :

Thanks for review. Didn't know about data_changed, good stuff. Made your requested changes. Running a test deployment now...

Revision history for this message
Stuart Bishop (stub) wrote :

Yup.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reactive/snap.py b/reactive/snap.py
2index aa05314..b2ec73e 100644
3--- a/reactive/snap.py
4+++ b/reactive/snap.py
5@@ -83,6 +83,18 @@ def ensure_snapd():
6 subprocess.check_call(cmd, universal_newlines=True)
7
8
9+def proxy_settings():
10+ proxy_vars = ('http_proxy', 'https_proxy', 'no_proxy')
11+ proxy_env = {key: value for key, value in os.environ.items()
12+ if key in proxy_vars}
13+
14+ snap_proxy = hookenv.config()['snap_proxy']
15+ if snap_proxy:
16+ proxy_env['http_proxy'] = snap_proxy
17+ proxy_env['https_proxy'] = snap_proxy
18+ return proxy_env
19+
20+
21 def update_snap_proxy():
22 # This is a hack based on
23 # https://bugs.launchpad.net/layer-snap/+bug/1533899/comments/1
24@@ -90,7 +102,7 @@ def update_snap_proxy():
25 # Note we can't do this in a standard reactive handler as we need
26 # to ensure proxies are configured before attempting installs or
27 # updates.
28- proxy = hookenv.config()['snap_proxy']
29+ proxy = proxy_settings()
30
31 if get_series() == 'trusty':
32 # The hack to configure a snapd proxy only works under
33@@ -123,9 +135,9 @@ def create_snap_proxy_conf(path, proxy):
34 content = dedent('''\
35 # Managed by Juju
36 [Service]
37- Environment=http_proxy={}
38- Environment=https_proxy={}
39- ''').format(proxy, proxy)
40+ ''')
41+ for proxy_key, proxy_value in proxy.items():
42+ content += 'Environment={}={}\n'.format(proxy_key, proxy_value)
43 host.write_file(path, content.encode())
44
45

Subscribers

People subscribed via source and target branches