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-06 16:16:11 +0000 |
commit | c2c85bbabb632b678b0170375b8d4468af76603c (patch) | |
tree | d7bf18eb494662072dc8955fad67e0b510229aca | |
parent | 2aeeaa3ec7318ede380c596c4dedc9a015aad897 (diff) | |
download | lorry-controller-c2c85bbabb632b678b0170375b8d4468af76603c.tar.gz |
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.
Reviewed By: Lars Wirzenius <lars.wirzenius@codethink.co.uk>
-rwxr-xr-x | lorry-controller | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lorry-controller b/lorry-controller index b8a59c7..d9dc1e0 100755 --- a/lorry-controller +++ b/lorry-controller @@ -17,10 +17,12 @@ import cliapp +import json import logging import os import time import re +import urllib import urllib2 @@ -101,6 +103,9 @@ 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') self.conf = LorryControllerConfig(self, 'git/lorry-controller.conf') self.html = HTMLStatusManager(self) if self.settings['dry-run']: @@ -251,6 +256,11 @@ class LorryController(cliapp.Application): logging.info("Running %d/%d. Lorrying: %s" % ( lorried, len(lorries_to_run),lorry)) self.html.set_processing(lorry) + # Before we run lorry, make sure that Git doesn't verify + # SSL certificates. This is a workaround for the fact that + # we don't yet have a solution for proper SSL certificates + # in Trove yet. + os.environ['GIT_SSL_NO_VERIFY'] = 'true' with mgr.runner(lorry) as runner: runner.run_lorry(*self.lorrycmd) while state['next-due'] <= now: @@ -307,5 +317,31 @@ class LorryController(cliapp.Application): else: return code, response.read(), str(code) + 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 + 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) + if __name__ == '__main__': LorryController(version='1').run() |