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
diff --git a/reactive/snap.py b/reactive/snap.py
index aa05314..b2ec73e 100644
--- a/reactive/snap.py
+++ b/reactive/snap.py
@@ -83,6 +83,18 @@ def ensure_snapd():
83 subprocess.check_call(cmd, universal_newlines=True)83 subprocess.check_call(cmd, universal_newlines=True)
8484
8585
86def proxy_settings():
87 proxy_vars = ('http_proxy', 'https_proxy', 'no_proxy')
88 proxy_env = {key: value for key, value in os.environ.items()
89 if key in proxy_vars}
90
91 snap_proxy = hookenv.config()['snap_proxy']
92 if snap_proxy:
93 proxy_env['http_proxy'] = snap_proxy
94 proxy_env['https_proxy'] = snap_proxy
95 return proxy_env
96
97
86def update_snap_proxy():98def update_snap_proxy():
87 # This is a hack based on99 # This is a hack based on
88 # https://bugs.launchpad.net/layer-snap/+bug/1533899/comments/1100 # https://bugs.launchpad.net/layer-snap/+bug/1533899/comments/1
@@ -90,7 +102,7 @@ def update_snap_proxy():
90 # Note we can't do this in a standard reactive handler as we need102 # Note we can't do this in a standard reactive handler as we need
91 # to ensure proxies are configured before attempting installs or103 # to ensure proxies are configured before attempting installs or
92 # updates.104 # updates.
93 proxy = hookenv.config()['snap_proxy']105 proxy = proxy_settings()
94106
95 if get_series() == 'trusty':107 if get_series() == 'trusty':
96 # The hack to configure a snapd proxy only works under108 # The hack to configure a snapd proxy only works under
@@ -123,9 +135,9 @@ def create_snap_proxy_conf(path, proxy):
123 content = dedent('''\135 content = dedent('''\
124 # Managed by Juju136 # Managed by Juju
125 [Service]137 [Service]
126 Environment=http_proxy={}138 ''')
127 Environment=https_proxy={}139 for proxy_key, proxy_value in proxy.items():
128 ''').format(proxy, proxy)140 content += 'Environment={}={}\n'.format(proxy_key, proxy_value)
129 host.write_file(path, content.encode())141 host.write_file(path, content.encode())
130142
131143

Subscribers

People subscribed via source and target branches