summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2014-03-31 14:11:05 +0000
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2014-03-31 14:11:05 +0000
commitb5b8e949e8c46b02cd0487aae1fe8e677a751f38 (patch)
tree021510124baedbebab094ae4ffe790ff14731896
parent6e2d8167093500b7aa8bad9ddd1dc09aaf56d1b6 (diff)
downloadlorry-controller-b5b8e949e8c46b02cd0487aae1fe8e677a751f38.tar.gz
Add GitanoCommand class
-rw-r--r--lorrycontroller/__init__.py1
-rw-r--r--lorrycontroller/gitano.py74
2 files changed, 75 insertions, 0 deletions
diff --git a/lorrycontroller/__init__.py b/lorrycontroller/__init__.py
index fec1866..1340160 100644
--- a/lorrycontroller/__init__.py
+++ b/lorrycontroller/__init__.py
@@ -36,6 +36,7 @@ from removejob import RemoveJob
from lstroves import LsTroves, ForceLsTrove
from pretendtime import PretendTime
from maxjobs import GetMaxJobs, SetMaxJobs
+from gitano import GitanoCommand, GitanoCommandFailure
from static import StaticFile
diff --git a/lorrycontroller/gitano.py b/lorrycontroller/gitano.py
new file mode 100644
index 0000000..375e518
--- /dev/null
+++ b/lorrycontroller/gitano.py
@@ -0,0 +1,74 @@
+# Copyright (C) 2014 Codethink Limited
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+
+import cliapp
+
+include lorrycontroller
+
+
+class GitanoCommandFailure(Exception):
+
+ def __init__(self, trovehost, command):
+ Exception.__init__(
+ self, 'Failed to run "%s" on Gitano on %s' % (command, trovehost))
+
+
+class GitanoCommand(object):
+
+ '''Run a Gitano command on a Trove.'''
+
+ def __init__(self, trovehost):
+ self.trovehost = trovehost
+
+ def get_gitano_config_for_repo(self, repo_path):
+ exit, stdout, stderr = cliapp.runcmd_unchecked(
+ ['ssh', 'git@%s' % self.trovehost,
+ 'config', cliapp.shell_quote(repo_path), 'show'])
+
+ if exit:
+ raise GitanoCommandFailure(self.trovehost, 'config show')
+
+ # "config REPO show" outputs a sequence of lines of the form "key: value".
+ # Extract those into a collections.defaultdict.
+
+ result = collections.defaultdict(str)
+ for line in stdout.splitlines():
+ m = re.match(r'^([^:])+:\s*(.*)$', line)
+ if m:
+ result[m.group(0)] = m.group(1).strip()
+
+ return result
+
+ def set_gitano_config_for_repo(self, path, key, value):
+ exit, stdout, stderr = cliapp.runcmd_unchecked(
+ ['ssh', 'git@%s' % self.trovehost,
+ 'config', cliapp.shell_quote(repo_path),
+ 'set', cliapp.shell_quote(key), cliapp.shell_quote(value)])
+
+ if exit:
+ raise GitanoCommandFailure(self.trovehost, 'config set')
+
+ def ls(self):
+ exit, stdout, stderr = cliapp.runcmd_unchecked(
+ ['ssh', 'git@%s' % self.trovehost, 'ls'])
+
+ if exit != 0:
+ logging.error(
+ 'Failed to run "gitano ls" for %s:\n%s',
+ self.trovehost, stdout + stderr)
+ raise GitanoLsError(self.trovehost, stdout + stderr)
+
+ return stdout