diff options
-rwxr-xr-x | lorry-controller | 16 | ||||
-rw-r--r-- | lorrycontroller/confparser.py | 12 |
2 files changed, 16 insertions, 12 deletions
diff --git a/lorry-controller b/lorry-controller index d9dc1e0..0ae4ceb 100755 --- a/lorry-controller +++ b/lorry-controller @@ -285,10 +285,10 @@ 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): + def maybe_http_request(self, url, auth=None, 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) + return self.http_request(url, auth) else: logging.debug('DRY-RUN: Not sending a request to %s' % url) return 0, 'DRY-RUN', 'DRY-RUN' @@ -300,7 +300,7 @@ class LorryController(cliapp.Application): logging.debug("DRY-RUN: Not running %r" % cmdline) return 0, 'DRY-RUN', 'DRY-RUN' - def http_request(self, url): + def http_request(self, url, auth=None): """Make an HTTP request to the given url, return the output. Make an HTTP request to `url`. If the request succeeds (response code @@ -310,7 +310,15 @@ class LorryController(cliapp.Application): """ request = urllib2.Request(url, None, {}) - response = urllib2.urlopen(request) + if auth: + password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() + password_mgr.add_password( + None, url, auth['username'], auth['password']) + auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr) + opener = urllib2.build_opener(auth_handler) + response = opener.open(url) + else: + response = urllib2.urlopen(request) code = response.getcode() if code == 200: return 0, response.read(), '200' diff --git a/lorrycontroller/confparser.py b/lorrycontroller/confparser.py index e8b78d8..403b768 100644 --- a/lorrycontroller/confparser.py +++ b/lorrycontroller/confparser.py @@ -323,15 +323,11 @@ class LorryControllerConfig(object): query_string = '%s %s' % (command, ' '.join(args)) query_string = urllib.quote(query_string) trovehost = urllib.quote(trove['trovehost']) - url = '%s/gitano-command.cgi?cmd=%s' % (trovehost, query_string) - if trove['protocol'] == 'https': - url = ('https://%s:%s@%s' % (trove['auth']['username'], - trove['auth']['password'], - url)) - else: - url = 'http://%s' % url + url = '%s://%s/gitano-command.cgi?cmd=%s' % ( + trove['protocol'], trovehost, query_string) + auth = trove.get('auth', None) # make an http request to the url - exit, out, err = self.app.maybe_http_request(url, dry=dry) + exit, out, err = self.app.maybe_http_request(url, auth=auth, dry=dry) return exit, out, err def _give_up(self, *args, **kwargs): |