gnt-os 3.57 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#!/usr/bin/python
#

# Copyright (C) 2006, 2007 Google Inc.
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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 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
31
from ganeti import constants
Guido Trotter's avatar
Guido Trotter committed
32

33

Iustin Pop's avatar
Iustin Pop committed
34 35 36 37
def ListOS(opts, args):
  """List the OSes existing on this node.

  """
38
  op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
Iustin Pop's avatar
Iustin Pop committed
39 40 41 42 43 44 45
  result = SubmitOpCode(op)

  if not result:
    logger.ToStdout("Can't get the OS list")
    return 1

  if not opts.no_headers:
46 47 48
    headers = {"name": "Name"}
  else:
    headers = None
Iustin Pop's avatar
Iustin Pop committed
49

50
  data = GenerateTable(separator=None, headers=headers, fields=["name"],
51
                       data=[[row[0]] for row in result if row[1]])
52 53 54

  for line in data:
    logger.ToStdout(line)
Iustin Pop's avatar
Iustin Pop committed
55 56 57

  return 0

Guido Trotter's avatar
Guido Trotter committed
58

Iustin Pop's avatar
Iustin Pop committed
59 60 61 62
def DiagnoseOS(opts, args):
  """Analyse all OSes on this cluster.

  """
63 64
  op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "node_status"],
                            names=[])
Iustin Pop's avatar
Iustin Pop committed
65 66 67 68 69 70
  result = SubmitOpCode(op)

  if not result:
    logger.ToStdout("Can't get the OS list")
    return 1

71 72
  has_bad = False

73
  for os_name, os_valid, node_data in result:
Guido Trotter's avatar
Guido Trotter committed
74
    nodes_valid = {}
Iustin Pop's avatar
Iustin Pop committed
75
    nodes_bad = {}
76 77 78 79 80
    nodes_hidden = {}
    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)
81
        first_os_msg = ("%s (path: %s)" %
82 83
                        (first_os_status, first_os_path))
        if first_os_status == constants.OS_VALID_STATUS:
84
          nodes_valid[node_name] = first_os_msg
Guido Trotter's avatar
Guido Trotter committed
85
        else:
86
          nodes_bad[node_name] = first_os_msg
87 88 89
        for hstatus, hpath in node_info:
          nodes_hidden[node_name].append("    [hidden] path: %s, status: %s" %
                                         (hpath, hstatus))
Iustin Pop's avatar
Iustin Pop committed
90
      else:
Guido Trotter's avatar
Guido Trotter committed
91
        nodes_bad[node_name] = "OS not found"
Iustin Pop's avatar
Iustin Pop committed
92 93 94 95 96

    if nodes_valid and not nodes_bad:
      status = "valid"
    elif not nodes_valid and nodes_bad:
      status = "invalid"
97
      has_bad = True
Iustin Pop's avatar
Iustin Pop committed
98 99
    else:
      status = "partial valid"
100
      has_bad = True
101

102 103
    def _OutputPerNodeOSStatus(msg_map):
      map_k = utils.NiceSort(msg_map.keys())
Guido Trotter's avatar
Guido Trotter committed
104
      for node_name in map_k:
105
        logger.ToStdout("  Node: %s, status: %s" %
106
                        (node_name, msg_map[node_name]))
107 108
        for msg in nodes_hidden[node_name]:
          logger.ToStdout(msg)
Guido Trotter's avatar
Guido Trotter committed
109

110
    logger.ToStdout("OS: %s [global status: %s]" % (os_name, status))
Guido Trotter's avatar
Guido Trotter committed
111 112
    _OutputPerNodeOSStatus(nodes_valid)
    _OutputPerNodeOSStatus(nodes_bad)
113
    logger.ToStdout("")
Iustin Pop's avatar
Iustin Pop committed
114

115 116
  return int(has_bad)

Iustin Pop's avatar
Iustin Pop committed
117 118

commands = {
119
  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
Iustin Pop's avatar
Iustin Pop committed
120
           "Lists all valid OSes on the master"),
121
  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
Iustin Pop's avatar
Iustin Pop committed
122 123 124 125
               "Diagnose all OSes"),
  }

if __name__ == '__main__':
126
  sys.exit(GenericMain(commands))