]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Add a label kwarg to run.run
authorAndrew Schoen <aschoen@redhat.com>
Fri, 30 Jan 2015 21:55:18 +0000 (15:55 -0600)
committerAndrew Schoen <aschoen@redhat.com>
Wed, 4 Feb 2015 15:54:33 +0000 (09:54 -0600)
This can be used to label or annotate commands to remotes so we can
print a meaningful log message if the command fails.

Signed-off-by: Andrew Schoen <aschoen@redhat.com>
teuthology/exceptions.py
teuthology/orchestra/run.py

index d2f257af39c61dc6125d57df92c352de84f73474..efc8e71f849077a5e26dd876ac23454be32a5851 100644 (file)
@@ -33,16 +33,21 @@ class CommandFailedError(Exception):
     """
     Exception thrown on command failure
     """
-    def __init__(self, command, exitstatus, node=None):
+    def __init__(self, command, exitstatus, node=None, label=None):
         self.command = command
         self.exitstatus = exitstatus
         self.node = node
+        self.label = label
 
     def __str__(self):
-        return "Command failed on {node} with status {status}: {cmd!r}".format(
+        prefix = "Command failed"
+        if self.label:
+            prefix = "Command failed ({label})".format(label=self.label)
+        return "{prefix} on {node} with status {status}: {cmd!r}".format(
             node=self.node,
             status=self.exitstatus,
             cmd=self.command,
+            prefix=prefix,
             )
 
 
index fc4502ea33f6e8a785b6390e1b742dd8d8393872..0af7db5f3a49c5f44ac1f499fb56029011ae90b0 100644 (file)
@@ -30,9 +30,10 @@ class RemoteProcess(object):
         'greenlets',
         # for orchestra.remote.Remote to place a backreference
         'remote',
+        'label',
         ]
 
-    def __init__(self, client, args, check_status=True, hostname=None):
+    def __init__(self, client, args, check_status=True, hostname=None, label=None):
         """
         Create the object. Does not initiate command execution.
 
@@ -42,7 +43,9 @@ class RemoteProcess(object):
         :param check_status: Whether to raise CommandFailedError on non-zero
                              exit status, and . Defaults to True. All signals
                              and connection loss are made to look like SIGHUP.
-        :param hostname: Name of remote host (optional)
+        :param hostname:     Name of remote host (optional)
+        :param label:        Can be used to label or describe what the
+                             command is doing.
         """
         self.client = client
         self.args = args
@@ -52,6 +55,7 @@ class RemoteProcess(object):
             self.command = quote(args)
 
         self.check_status = check_status
+        self.label = label
 
         if hostname:
             self.hostname = hostname
@@ -66,8 +70,11 @@ class RemoteProcess(object):
         """
         Execute remote command
         """
-        log.getChild(self.hostname).info(u"Running: {cmd!r}".format(
-            cmd=self.command))
+        prefix = "Running:"
+        if self.label:
+            prefix = "Running ({label}):".format(label=self.label)
+        log.getChild(self.hostname).info(u"{prefix} {cmd!r}".format(
+            cmd=self.command, prefix=prefix))
 
         (self._stdin_buf, self._stdout_buf, self._stderr_buf) = \
             self.client.exec_command(self.command)
@@ -103,7 +110,8 @@ class RemoteProcess(object):
                 raise CommandCrashedError(command=self.command)
             if status != 0:
                 raise CommandFailedError(command=self.command,
-                                         exitstatus=status, node=self.hostname)
+                                         exitstatus=status, node=self.hostname,
+                                         label=self.label)
         return status
 
     def _get_exitstatus(self):
@@ -287,7 +295,8 @@ def run(
     logger=None,
     check_status=True,
     wait=True,
-    name=None
+    name=None,
+    label=None
 ):
     """
     Run a command remotely.  If any of 'args' contains shell metacharacters
@@ -316,6 +325,7 @@ def run(
                  actual status is available via ``.get()``.
     :param name: Human readable name (probably hostname) of the destination
                  host
+    :param label: Can be used to label or describe what the command is doing.
     """
     try:
         (host, port) = client.get_transport().getpeername()
@@ -325,7 +335,7 @@ def run(
     if name is None:
         name = host
 
-    r = RemoteProcess(client, args, check_status=check_status, hostname=name)
+    r = RemoteProcess(client, args, check_status=check_status, hostname=name, label=label)
     r.execute()
 
     r.stdin = KludgeFile(wrapped=r.stdin)