summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Coldrick <adam.coldrick@codethink.co.uk>2014-03-04 09:18:14 +0000
committerAdam Coldrick <adam.coldrick@codethink.co.uk>2014-03-06 16:07:27 +0000
commit1b937406e5127184cc38741020c00b330e432a57 (patch)
treeb17c8eca9ab6af1376c2464d881bf2eeb1ac23dd
parent90cfc7566a962a99890035b948cfc8b155f0e3f9 (diff)
downloadlorry-controller-1b937406e5127184cc38741020c00b330e432a57.tar.gz
Update lorry-controller to allow it to make http requests
-rwxr-xr-xlorry-controller26
1 files changed, 26 insertions, 0 deletions
diff --git a/lorry-controller b/lorry-controller
index 01c0225..b8a59c7 100755
--- a/lorry-controller
+++ b/lorry-controller
@@ -21,6 +21,7 @@ import logging
import os
import time
import re
+import urllib2
from lorrycontroller.confparser import LorryControllerConfig
@@ -274,6 +275,14 @@ class LorryController(cliapp.Application):
self.runcmd(['git']+args, cwd=os.path.join(self.settings['work-area'],
'git'))
+ def maybe_http_request(self, url, dry=False):
+ """If not a dry run, make an HTTP request and return its output."""
+ if (not self.settings['dry-run']) or dry:
+ return self.http_request(url)
+ else:
+ logging.debug('DRY-RUN: Not sending a request to %s' % url)
+ return 0, 'DRY-RUN', 'DRY-RUN'
+
def maybe_runcmd(self, cmdline, dry=False, *args, **kwargs):
if (not self.settings['dry-run']) or dry:
return self.runcmd_unchecked(cmdline, *args, **kwargs)
@@ -281,5 +290,22 @@ class LorryController(cliapp.Application):
logging.debug("DRY-RUN: Not running %r" % cmdline)
return 0, 'DRY-RUN', 'DRY-RUN'
+ def http_request(self, url):
+ """Make an HTTP request to the given url, return the output.
+
+ Make an HTTP request to `url`. If the request succeeds (response code
+ 200) then return an exit code 0, the data from the response and the
+ response code. Otherwise return the response code, any data in the
+ repsonse and a string containing the response code.
+
+ """
+ request = urllib2.Request(url, None, {})
+ response = urllib2.urlopen(request)
+ code = response.getcode()
+ if code == 200:
+ return 0, response.read(), '200'
+ else:
+ return code, response.read(), str(code)
+
if __name__ == '__main__':
LorryController(version='1').run()