summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2014-03-31 16:36:40 +0000
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2014-03-31 16:36:40 +0000
commit947af57daee51f2a9a86be544d93d15d940d51d7 (patch)
treeb912d14202a89293427307263746b13873e5f9d9
parentfc7edcc7670846ed683f2c4d36754f4b55f2b6ff (diff)
downloadlorry-controller-947af57daee51f2a9a86be544d93d15d940d51d7.tar.gz
Setup HTTP proxy config for WEBAPP
-rwxr-xr-xlorry-controller-webapp35
1 files changed, 35 insertions, 0 deletions
diff --git a/lorry-controller-webapp b/lorry-controller-webapp
index 43ec37a..6161e37 100755
--- a/lorry-controller-webapp
+++ b/lorry-controller-webapp
@@ -135,6 +135,8 @@ class WEBAPP(cliapp.Application):
def process_args(self, args):
self.settings.require('statedb')
+ self.setup_proxy()
+
templates = self.load_templates()
webapp = bottle.Bottle()
@@ -205,5 +207,38 @@ class WEBAPP(cliapp.Application):
quiet=True,
debug=True)
+ def setup_proxy(self):
+ """Tell urllib2 to use a proxy for http action by lorry-controller.
+
+ Load the proxy information from the JSON file given by proxy_def, then
+ set urllib2's url opener to open urls via an authenticated proxy.
+
+ """
+
+ config_filename = os.path.join(
+ self.settings['configuration-directory'], 'proxy.conf')
+
+ if not os.path.exists(config_filename):
+ return
+
+ with open(config_filename, 'r') as f:
+ proxy = json.load(f)
+
+ # set the required environment variables
+ hostname = urllib.quote(proxy['hostname'])
+ user = '%s:%s' % (proxy['username'], proxy['password'])
+ url = '%s:%s' % (hostname, proxy['port'])
+ os.environ['http_proxy'] = 'http://%s@%s' % (user, url)
+ os.environ['https_proxy'] = 'https://%s@%s' % (user, url)
+
+ # create a ProxyHandler
+ proxies = {'http_proxy': 'http://%s@%s' % (user, url),
+ 'https_proxy': 'https://%s@%s' % (user, url)}
+ proxy_handler = urllib2.ProxyHandler(proxies)
+
+ # install an opener to use the proxy
+ opener = urllib2.build_opener(proxy_handler)
+ urllib2.install_opener(opener)
+
WEBAPP().run()