Colours and warnings.
authorMichael Hanselmann <hansmi@google.com>
Wed, 10 Oct 2007 15:51:44 +0000 (15:51 +0000)
committerMichael Hanselmann <hansmi@google.com>
Wed, 10 Oct 2007 15:51:44 +0000 (15:51 +0000)
- Implement colours in qa_utils.
- Print warning for cron script.

Reviewed-by: iustinp

qa/ganeti-qa.py
qa/qa_daemon.py
qa/qa_utils.py

index c801c0a..021e1d9 100755 (executable)
@@ -136,11 +136,19 @@ def main():
       if qa_config.TestEnabled('instance-info'):
         RunTest(qa_instance.TestInstanceInfo, instance)
 
-      if qa_config.TestEnabled('instance-automatic-restart'):
-        RunTest(qa_daemon.TestInstanceAutomaticRestart, node, instance)
+      automatic_restart = \
+        qa_config.TestEnabled('instance-automatic-restart')
+      consecutive_failures = \
+        qa_config.TestEnabled('instance-consecutive-failures')
 
-      if qa_config.TestEnabled('instance-consecutive-failures'):
-        RunTest(qa_daemon.TestInstanceConsecutiveFailures, node, instance)
+      if automatic_restart or consecutive_failures:
+        qa_daemon.PrintCronWarning()
+
+        if automatic_restart:
+          RunTest(qa_daemon.TestInstanceAutomaticRestart, node, instance)
+
+        if consecutive_failures:
+          RunTest(qa_daemon.TestInstanceConsecutiveFailures, node, instance)
 
       if qa_config.TestEnabled('instance-export'):
         expnode = qa_config.AcquireNode(exclude=node)
index e6e79fd..8b40edf 100644 (file)
@@ -78,6 +78,15 @@ def _ResetWatcherDaemon(node):
                        utils.ShellQuoteArgs(cmd)).wait(), 0)
 
 
+def PrintCronWarning():
+  """Shows a warning about the required cron job.
+
+  """
+  print
+  qa_utils.PrintWarning("The following tests require the cron script for"
+                        " ganeti-watcher to be set up.")
+
+
 def TestInstanceAutomaticRestart(node, instance):
   """Test automatic restart of instance by ganeti-watcher.
 
index 8e5a8fc..1ad596f 100644 (file)
@@ -21,6 +21,7 @@
 """
 
 import os
+import sys
 import subprocess
 
 from ganeti import utils
@@ -29,6 +30,37 @@ import qa_config
 import qa_error
 
 
+_INFO_SEQ = None
+_WARNING_SEQ = None
+_ERROR_SEQ = None
+_RESET_SEQ = None
+
+
+def _SetupColours():
+  """Initializes the colour constants.
+
+  """
+  global _INFO_SEQ, _WARNING_SEQ, _ERROR_SEQ, _RESET_SEQ
+
+  try:
+    import curses
+  except ImportError:
+    # Don't use colours if curses module can't be imported
+    return
+
+  curses.setupterm()
+
+  _RESET_SEQ = curses.tigetstr("op")
+
+  setaf = curses.tigetstr("setaf")
+  _INFO_SEQ = curses.tparm(setaf, curses.COLOR_GREEN)
+  _WARNING_SEQ = curses.tparm(setaf, curses.COLOR_YELLOW)
+  _ERROR_SEQ = curses.tparm(setaf, curses.COLOR_RED)
+
+
+_SetupColours()
+
+
 def AssertEqual(first, second, msg=None):
   """Raises an error when values aren't equal.
 
@@ -113,3 +145,31 @@ def ResolveInstanceName(instance):
   AssertEqual(p.wait(), 0)
 
   return p.stdout.read().strip()
+
+
+def _PrintWithColor(text, seq):
+  f = sys.stdout
+
+  if not f.isatty():
+    seq = None
+
+  if seq:
+    f.write(seq)
+
+  f.write(text)
+  f.write("\n")
+
+  if seq:
+    f.write(_RESET_SEQ)
+
+
+def PrintWarning(text):
+  return _PrintWithColor(text, _WARNING_SEQ)
+
+
+def PrintError(f, text):
+  return _PrintWithColor(text, _ERROR_SEQ)
+
+
+def PrintInfo(f, text):
+  return _PrintWithColor(text, _INFO_SEQ)