diff options
author | Adam Coldrick <adam.coldrick@codethink.co.uk> | 2014-03-05 16:31:12 +0000 |
---|---|---|
committer | Adam Coldrick <adam.coldrick@codethink.co.uk> | 2014-03-05 16:42:45 +0000 |
commit | 27a28916391396f16dc042319de381f5745bb03c (patch) | |
tree | abd75a6c5783f521a31ddeebbee3f5369e5d7035 | |
parent | b57f8141536508e83c9b88029948b9cd2e12e790 (diff) | |
download | lorry-controller-adamcoldrick/access-troves-via-http-with-proxy-support.tar.gz |
Add support for using an http proxy with lorry-controller.adamcoldrick/access-troves-via-http-with-proxy-support
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.
-rwxr-xr-x | lorry-controller | 28 |
1 files changed, 28 insertions, 0 deletions
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() |