Merge ~mastier1/charm-ubuntu-advantage:master into charm-ubuntu-advantage:master

Proposed by Bartosz Woronicz
Status: Work in progress
Proposed branch: ~mastier1/charm-ubuntu-advantage:master
Merge into: charm-ubuntu-advantage:master
Diff against target: 159 lines (+36/-24)
1 file modified
src/charm.py (+36/-24)
Reviewer Review Type Date Requested Status
Tom Haddon Needs Fixing
Canonical IS Reviewers Pending
Review via email: mp+412774@code.launchpad.net

Commit message

Add support for proxy settings from the model

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
Tom Haddon (mthaddon) wrote :

One comment inline about import sorting, but also this causes test failures, and doesn't include any tests for the new _setup_proxy_env method.

Instructions for running the tests are in the README.md.

review: Needs Fixing
Revision history for this message
Bartosz Woronicz (mastier1) wrote :

@Tom Haddon, thanks,

I may help you out to push that merge in January. Depending on time.
Anytime feel free to cherry-pick from that as this feature is very important.
Many clients got proxy, and now that's only way to activate Livepatch On-Prem with that charm.
Cheers and Happy New Year :-)

Revision history for this message
Tom Haddon (mthaddon) wrote :

I think this has been superseded by https://code.launchpad.net/~jfguedez/charm-ubuntu-advantage/+git/charm-ubuntu-advantage/+merge/415946. Will mark as WIP, please mark back to needs review if you want to follow up.

Revision history for this message
Bartosz Woronicz (mastier1) wrote :

@Tom So it seems that functionality was already implemented.

So this one might be dropped when the latter is merged.

Unmerged commits

670a954... by Bartosz Woronicz

Add support for proxy settings from the model

Closes-Bug: 1944591

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/charm.py b/src/charm.py
2index 25d7d0a..b94edfb 100755
3--- a/src/charm.py
4+++ b/src/charm.py
5@@ -8,6 +8,7 @@ import json
6 import logging
7 import subprocess
8 import yaml
9+import os
10
11 from ops.charm import CharmBase
12 from ops.framework import StoredState
13@@ -18,25 +19,25 @@ from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus
14 logger = logging.getLogger(__name__)
15
16
17-def install_ppa(ppa):
18+def install_ppa(ppa, env):
19 """Install specified ppa"""
20- subprocess.check_call(["add-apt-repository", "--yes", ppa])
21+ subprocess.check_call(["add-apt-repository", "--yes", ppa], env=env)
22
23
24-def remove_ppa(ppa):
25+def remove_ppa(ppa, env):
26 """Remove specified ppa"""
27- subprocess.check_call(["add-apt-repository", "--remove", "--yes", ppa])
28+ subprocess.check_call(["add-apt-repository", "--remove", "--yes", ppa], env=env)
29
30
31-def install_package(package):
32+def install_package(package, env):
33 """Install specified apt package (after performing an apt update)"""
34- subprocess.check_call(["apt", "update"])
35- subprocess.check_call(["apt", "install", "--yes", "--quiet", package])
36+ subprocess.check_call(["apt", "update"], env=env)
37+ subprocess.check_call(["apt", "install", "--yes", "--quiet", package], env=env)
38
39
40-def remove_package(package):
41+def remove_package(package, env):
42 """Remove specified apt package"""
43- subprocess.check_call(["apt", "remove", "--yes", "--quiet", package])
44+ subprocess.check_call(["apt", "remove", "--yes", "--quiet", package], env=env)
45
46
47 def update_configuration(contract_url):
48@@ -49,19 +50,20 @@ def update_configuration(contract_url):
49 f.truncate()
50
51
52-def detach_subscription():
53+def detach_subscription(env):
54 """Detach from any ubuntu-advantage subscription"""
55- subprocess.check_call(["ubuntu-advantage", "detach", "--assume-yes"])
56+ subprocess.check_call(["ubuntu-advantage", "detach", "--assume-yes"], env=env)
57
58
59-def attach_subscription(token):
60+def attach_subscription(token, env):
61 """Attach an ubuntu-advantage subscription using the specified token"""
62- return subprocess.call(["ubuntu-advantage", "attach", token])
63+ return subprocess.call(["ubuntu-advantage", "attach", token], env=env)
64
65
66-def get_status_output():
67+def get_status_output(env):
68 """Return the parsed output from ubuntu-advantage status"""
69- output = subprocess.check_output(["ubuntu-advantage", "status", "--all", "--format", "json"])
70+ output = subprocess.check_output(
71+ ["ubuntu-advantage", "status", "--all", "--format", "json"], env=env)
72 # handle different return type from xenial
73 if isinstance(output, bytes):
74 output = output.decode("utf-8")
75@@ -74,13 +76,23 @@ class UbuntuAdvantageCharm(CharmBase):
76
77 def __init__(self, *args):
78 super().__init__(*args)
79+ self._setup_proxy_env()
80 self._state.set_default(
81 contract_url=None,
82 hashed_token=None,
83 package_needs_installing=True,
84- ppa=None)
85+ ppa=None,
86+ env=self.env)
87+
88 self.framework.observe(self.on.config_changed, self.config_changed)
89
90+ def _setup_proxy_env(self):
91+ """Setup proxy variables from model"""
92+ self.env = dict(os.environ)
93+ self.env['http_proxy'] = self.env.get('JUJU_CHARM_HTTP_PROXY', '')
94+ self.env['https_proxy'] = self.env.get('JUJU_CHARM_HTTPS_PROXY', '')
95+ self.env['no_proxy'] = self.env.get('JUJU_CHARM_NO_PROXY', '')
96+
97 def config_changed(self, event):
98 """Install and configure ubuntu-advantage tools and attachment"""
99 logger.info("Beginning config_changed")
100@@ -100,14 +112,14 @@ class UbuntuAdvantageCharm(CharmBase):
101
102 if old_ppa and old_ppa != ppa:
103 logger.info("Removing previously installed ppa (%s)", old_ppa)
104- remove_ppa(old_ppa)
105+ remove_ppa(old_ppa, self._state.env)
106 self._state.ppa = None
107 # If ppa is changed, want to remove the previous version of the package for consistency
108 self._state.package_needs_installing = True
109
110 if ppa and ppa != old_ppa:
111 logger.info("Installing ppa: %s", ppa)
112- install_ppa(ppa)
113+ install_ppa(ppa, self._state.env)
114 self._state.ppa = ppa
115 # If ppa is changed, want to force an install of the package for potential updates
116 self._state.package_needs_installing = True
117@@ -116,9 +128,9 @@ class UbuntuAdvantageCharm(CharmBase):
118 """Install apt package if necessary"""
119 if self._state.package_needs_installing:
120 logger.info("Removing package ubuntu-advantage-tools")
121- remove_package("ubuntu-advantage-tools")
122+ remove_package("ubuntu-advantage-tools", self._state.env)
123 logger.info("Installing package ubuntu-advantage-tools")
124- install_package("ubuntu-advantage-tools")
125+ install_package("ubuntu-advantage-tools", self._state.env)
126 self._state.package_needs_installing = False
127
128 def _handle_subscription_state(self):
129@@ -132,10 +144,10 @@ class UbuntuAdvantageCharm(CharmBase):
130 old_contract_url = self._state.contract_url
131 config_changed = contract_url != old_contract_url
132
133- status = get_status_output()
134+ status = get_status_output(self._state.env)
135 if status["attached"] and (config_changed or token_changed):
136 logger.info("Detaching ubuntu-advantage subscription")
137- detach_subscription()
138+ detach_subscription(self._state.env)
139 self._state.hashed_token = None
140
141 if config_changed:
142@@ -148,7 +160,7 @@ class UbuntuAdvantageCharm(CharmBase):
143 return
144 elif config_changed or token_changed:
145 logger.info("Attaching ubuntu-advantage subscription")
146- return_code = attach_subscription(token)
147+ return_code = attach_subscription(token, self._state.env)
148 if return_code != 0:
149 message = "Error attaching, possibly an invalid token or contract_url?"
150 self.unit.status = BlockedStatus(message)
151@@ -157,7 +169,7 @@ class UbuntuAdvantageCharm(CharmBase):
152
153 def _handle_status_state(self):
154 """Parse status output to determine which services are active"""
155- status = get_status_output()
156+ status = get_status_output(self._state.env)
157 services = []
158 for service in status.get("services"):
159 if service.get("status") == "enabled":

Subscribers

People subscribed via source and target branches