From 27a28916391396f16dc042319de381f5745bb03c Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Wed, 5 Mar 2014 16:31:12 +0000 Subject: Add support for using an http proxy with lorry-controller. By adding a proxy.conf file to the lorry config repository, lorry controller can be set up to do any http operation over a proxy defined in proxy.conf as follows: { "hostname": "proxy-name", "port": "proxy-port", "username": "proxy-username", "password": "password" } If this file exists in the latest commit of the repository the lorry controller will try to use the proxy. To stop it from using the proxy add a commit which removes the file from the repository. --- lorry-controller | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lorry-controller b/lorry-controller index a3efa2c..88e6436 100755 --- a/lorry-controller +++ b/lorry-controller @@ -17,6 +17,7 @@ import cliapp +import json import logging import os import time @@ -101,6 +102,13 @@ class LorryController(cliapp.Application): logging.error("Unable to find lorry-controller.conf in git") raise SystemExit(4) + if os.path.isfile('git/proxy.conf'): + self.set_proxy('git/proxy.conf') + logging.info('Loaded proxy information') + # make git always ignore ssl certificates + self.maybe_runcmd( + ['git', 'config', '--global', 'http.sslVerify', 'false'], + dry=True) self.conf = LorryControllerConfig(self, 'git/lorry-controller.conf') self.html = HTMLStatusManager(self) if self.settings['dry-run']: @@ -295,5 +303,25 @@ class LorryController(cliapp.Application): logging.debug("DRY-RUN: Not running %r" % cmdline) return 0, 'DRY-RUN', 'DRY-RUN' + def set_proxy(self, proxy_def): + """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. + + """ + with open(proxy_def, 'r') as proxy_info: + proxy = json.load(proxy_info) + + # set the required environment variables + user = '%s:%s' % (proxy['username'], proxy['password']) + url = '%s:%s' % (proxy['hostname'], proxy['port']) + os.environ['http_proxy'] = 'http://%s@%s' % (user, url) + os.environ['https_proxy'] = 'http://%s@%s' % (user, url) + + # install an opener to use the proxy + opener = urllib2.build_opener() + urllib2.install_opener(opener) + if __name__ == '__main__': LorryController(version='1').run() -- cgit v1.2.1