Add support for querying the ctime/mtime
[ganeti-local] / scripts / gnt-os
index 873f09f..4cfa07b 100755 (executable)
 # 02110-1301, USA.
 
 
+# pylint: disable-msg=W0401,W0614
+# W0401: Wildcard import ganeti.cli
+# W0614: Unused import %s from wildcard import (since we need cli)
+
 import sys
 from optparse import make_option
 
 from ganeti.cli import *
 from ganeti import opcodes
-from ganeti import logger
-from ganeti import objects
 from ganeti import utils
-from ganeti import errors
 from ganeti import constants
 
 
 def ListOS(opts, args):
-  """List the OSes existing on this node.
+  """List the valid OSes in the cluster.
+
+  @param opts: the command line options selected by the user
+  @type args: list
+  @param args: should be an empty list
+  @rtype: int
+  @return: the desired exit code
 
   """
   op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
   result = SubmitOpCode(op)
 
   if not result:
-    logger.ToStdout("Can't get the OS list")
+    ToStderr("Can't get the OS list")
     return 1
 
   if not opts.no_headers:
@@ -48,24 +55,47 @@ def ListOS(opts, args):
     headers = None
 
   data = GenerateTable(separator=None, headers=headers, fields=["name"],
-                       data=[[row[0]] for row in result if row[1]])
+                       data=[[row[0]] for row in result if row[1]],
+                       units=None)
 
   for line in data:
-    logger.ToStdout(line)
+    ToStdout(line)
 
   return 0
 
 
+def _OsStatus(status, diagnose):
+  """Beautifier function for OS status.
+
+  @type status: boolean
+  @param status: is the OS valid
+  @type diagnose: string
+  @param diagnose: the error message for invalid OSes
+  @rtype: string
+  @return: a formatted status
+
+  """
+  if status:
+    return "valid"
+  else:
+    return "invalid - %s" % diagnose
+
 def DiagnoseOS(opts, args):
   """Analyse all OSes on this cluster.
 
+  @param opts: the command line options selected by the user
+  @type args: list
+  @param args: should be an empty list
+  @rtype: int
+  @return: the desired exit code
+
   """
   op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "node_status"],
                             names=[])
   result = SubmitOpCode(op)
 
   if not result:
-    logger.ToStdout("Can't get the OS list")
+    ToStderr("Can't get the OS list")
     return 1
 
   has_bad = False
@@ -77,16 +107,17 @@ def DiagnoseOS(opts, args):
     for node_name, node_info in node_data.iteritems():
       nodes_hidden[node_name] = []
       if node_info: # at least one entry in the per-node list
-        first_os_status, first_os_path = node_info.pop(0)
-        first_os_msg = ("%s (path: %s)" %
-                        (first_os_status, first_os_path))
-        if first_os_status == constants.OS_VALID_STATUS:
+        first_os_path, first_os_status, first_os_msg = node_info.pop(0)
+        first_os_msg = ("%s (path: %s)" % (_OsStatus(first_os_status,
+                                                     first_os_msg),
+                                           first_os_path))
+        if first_os_status:
           nodes_valid[node_name] = first_os_msg
         else:
           nodes_bad[node_name] = first_os_msg
-        for hstatus, hpath in node_info:
+        for hpath, hstatus, hmsg in node_info:
           nodes_hidden[node_name].append("    [hidden] path: %s, status: %s" %
-                                         (hpath, hstatus))
+                                         (hpath, _OsStatus(hstatus, hmsg)))
       else:
         nodes_bad[node_name] = "OS not found"
 
@@ -102,23 +133,22 @@ def DiagnoseOS(opts, args):
     def _OutputPerNodeOSStatus(msg_map):
       map_k = utils.NiceSort(msg_map.keys())
       for node_name in map_k:
-        logger.ToStdout("  Node: %s, status: %s" %
-                        (node_name, msg_map[node_name]))
+        ToStdout("  Node: %s, status: %s", node_name, msg_map[node_name])
         for msg in nodes_hidden[node_name]:
-          logger.ToStdout(msg)
+          ToStdout(msg)
 
-    logger.ToStdout("OS: %s [global status: %s]" % (os_name, status))
+    ToStdout("OS: %s [global status: %s]", os_name, status)
     _OutputPerNodeOSStatus(nodes_valid)
     _OutputPerNodeOSStatus(nodes_bad)
-    logger.ToStdout("")
+    ToStdout("")
 
   return int(has_bad)
 
 
 commands = {
-  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT],
+  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
            "Lists all valid OSes on the master"),
-  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT],
+  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
                "Diagnose all OSes"),
   }