diff options
author | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-03-31 15:33:04 +0000 |
---|---|---|
committer | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-03-31 15:33:04 +0000 |
commit | f57cb74a5b541d1b34c7af66a564794ef3edb0da (patch) | |
tree | bb4ffd7ddf9258f26721ba08d7b9ff99f9a3f9ae | |
parent | af31a8c0de239f9b8ae2197c7d206e1573306c14 (diff) | |
download | lorry-controller-f57cb74a5b541d1b34c7af66a564794ef3edb0da.tar.gz |
Add HTTP support to GitanoCommand
Code originally written by Adam Coldrick, but manually inserted here
because my code is so different from the code Adam wrote it for.
-rw-r--r-- | lorrycontroller/gitano.py | 53 |
1 files changed, 48 insertions, 5 deletions
diff --git a/lorrycontroller/gitano.py b/lorrycontroller/gitano.py index 5121a54..c1347d5 100644 --- a/lorrycontroller/gitano.py +++ b/lorrycontroller/gitano.py @@ -17,6 +17,8 @@ import collections import logging import re +import urllib2 +import urlparse import cliapp @@ -36,14 +38,28 @@ class GitanoCommand(object): '''Run a Gitano command on a Trove.''' - def __init__(self, trovehost): + def __init__(self, trovehost, protocol, username, password): self.trovehost = trovehost + self.protocol = protocol + self.username = username + self.password = password + + if protocol == 'ssh': + self._command = self._ssh_command + elif protocol in ('http', 'https'): + self._command = self._http_command + else: + raise GitanoCommandFailure( + self.trovehost, '__init__', 'unknown protocol %s' % protocol) + + def whoami(self): + return self._command(['whoami']) def create(self, repo_path): - self._ssh_command(['create', repo_path]) + self._command(['create', repo_path]) def get_gitano_config(self, repo_path): - stdout = self._ssh_command(['config', repo_path, 'show']) + stdout = self._command(['config', repo_path, 'show']) # "config REPO show" outputs a sequence of lines of the form "key: value". # Extract those into a collections.defaultdict. @@ -57,10 +73,10 @@ class GitanoCommand(object): return result def set_gitano_config(self, path, key, value): - self._ssh_command(['config', path, 'set', key, value]) + self._command(['config', path, 'set', key, value]) def ls(self): - return self._ssh_command(['ls']) + return self._command(['ls']) def _ssh_command(self, gitano_args): quoted_args = [cliapp.shell_quote(x) for x in gitano_args] @@ -78,3 +94,30 @@ class GitanoCommand(object): stdout + stderr) return stdout + + def _http_command(self, gitano_args): + quoted_args = urllib2.quote(' '.join(gitano_args)) + url = urlparse.urlunsplit(( + self.protocol, + self.trovehost, + '/gitano-command.cgi', + 'cmd=%s' % quoted_args, + '')) + logging.debug('url=%r', url) + + try: + request = urllib2.Request(url, None, {}) + logging.debug('request=%r', request.get_full_url()) + if self.username and self.password: + password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() + password_mgr.add_password(None, url, self.username, self.password) + auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr) + opener = urllib2.build_opener(auth_handler) + response = opener.open(url) + else: + response = urllib2.urlopen(request) + except urllib2.URLError as e: + raise GitanoCommandFailure( + self.trovehost, ' '.join(gitano_args), str(e)) + + return response.read() |