Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-os @ 1f05af2b

History | View | Annotate | Download (3.6 kB)

1 a8083063 Iustin Pop
#!/usr/bin/python
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 a8083063 Iustin Pop
# Copyright (C) 2006, 2007 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 a8083063 Iustin Pop
import sys
23 a8083063 Iustin Pop
from optparse import make_option
24 a8083063 Iustin Pop
25 a8083063 Iustin Pop
from ganeti.cli import *
26 a8083063 Iustin Pop
from ganeti import opcodes
27 a8083063 Iustin Pop
from ganeti import logger
28 a8083063 Iustin Pop
from ganeti import objects
29 a8083063 Iustin Pop
from ganeti import utils
30 a8083063 Iustin Pop
from ganeti import errors
31 1f9430d6 Iustin Pop
from ganeti import constants
32 216fe2f3 Guido Trotter
33 fbdb07b9 Guido Trotter
34 a8083063 Iustin Pop
def ListOS(opts, args):
35 a8083063 Iustin Pop
  """List the OSes existing on this node.
36 a8083063 Iustin Pop
37 a8083063 Iustin Pop
  """
38 1f9430d6 Iustin Pop
  op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
39 a8083063 Iustin Pop
  result = SubmitOpCode(op)
40 a8083063 Iustin Pop
41 a8083063 Iustin Pop
  if not result:
42 a8083063 Iustin Pop
    logger.ToStdout("Can't get the OS list")
43 a8083063 Iustin Pop
    return 1
44 a8083063 Iustin Pop
45 a8083063 Iustin Pop
  if not opts.no_headers:
46 606d909d Michael Hanselmann
    headers = {"name": "Name"}
47 606d909d Michael Hanselmann
  else:
48 606d909d Michael Hanselmann
    headers = None
49 a8083063 Iustin Pop
50 16be8703 Iustin Pop
  data = GenerateTable(separator=None, headers=headers, fields=["name"],
51 1f9430d6 Iustin Pop
                       data=[[row[0]] for row in result if row[1]])
52 16be8703 Iustin Pop
53 16be8703 Iustin Pop
  for line in data:
54 16be8703 Iustin Pop
    logger.ToStdout(line)
55 a8083063 Iustin Pop
56 a8083063 Iustin Pop
  return 0
57 a8083063 Iustin Pop
58 b07a9f05 Guido Trotter
59 a8083063 Iustin Pop
def DiagnoseOS(opts, args):
60 a8083063 Iustin Pop
  """Analyse all OSes on this cluster.
61 a8083063 Iustin Pop
62 a8083063 Iustin Pop
  """
63 1f9430d6 Iustin Pop
  op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "node_status"],
64 1f9430d6 Iustin Pop
                            names=[])
65 a8083063 Iustin Pop
  result = SubmitOpCode(op)
66 a8083063 Iustin Pop
67 a8083063 Iustin Pop
  if not result:
68 a8083063 Iustin Pop
    logger.ToStdout("Can't get the OS list")
69 a8083063 Iustin Pop
    return 1
70 a8083063 Iustin Pop
71 a2656173 Michael Hanselmann
  has_bad = False
72 a2656173 Michael Hanselmann
73 1f9430d6 Iustin Pop
  for os_name, os_valid, node_data in result:
74 b07a9f05 Guido Trotter
    nodes_valid = {}
75 a8083063 Iustin Pop
    nodes_bad = {}
76 1f9430d6 Iustin Pop
    nodes_hidden = {}
77 1f9430d6 Iustin Pop
    for node_name, node_info in node_data.iteritems():
78 1f9430d6 Iustin Pop
      nodes_hidden[node_name] = []
79 1f9430d6 Iustin Pop
      if node_info: # at least one entry in the per-node list
80 1f9430d6 Iustin Pop
        first_os_status, first_os_path = node_info.pop(0)
81 48f85f75 Guido Trotter
        first_os_msg = ("%s (path: %s)" %
82 1f9430d6 Iustin Pop
                        (first_os_status, first_os_path))
83 1f9430d6 Iustin Pop
        if first_os_status == constants.OS_VALID_STATUS:
84 48f85f75 Guido Trotter
          nodes_valid[node_name] = first_os_msg
85 b07a9f05 Guido Trotter
        else:
86 48f85f75 Guido Trotter
          nodes_bad[node_name] = first_os_msg
87 1f9430d6 Iustin Pop
        for hstatus, hpath in node_info:
88 1f9430d6 Iustin Pop
          nodes_hidden[node_name].append("    [hidden] path: %s, status: %s" %
89 1f9430d6 Iustin Pop
                                         (hpath, hstatus))
90 a8083063 Iustin Pop
      else:
91 b07a9f05 Guido Trotter
        nodes_bad[node_name] = "OS not found"
92 a8083063 Iustin Pop
93 a8083063 Iustin Pop
    if nodes_valid and not nodes_bad:
94 a8083063 Iustin Pop
      status = "valid"
95 a8083063 Iustin Pop
    elif not nodes_valid and nodes_bad:
96 a8083063 Iustin Pop
      status = "invalid"
97 a2656173 Michael Hanselmann
      has_bad = True
98 a8083063 Iustin Pop
    else:
99 a8083063 Iustin Pop
      status = "partial valid"
100 a2656173 Michael Hanselmann
      has_bad = True
101 694e2444 Guido Trotter
102 48f85f75 Guido Trotter
    def _OutputPerNodeOSStatus(msg_map):
103 48f85f75 Guido Trotter
      map_k = utils.NiceSort(msg_map.keys())
104 b07a9f05 Guido Trotter
      for node_name in map_k:
105 d06565e0 Guido Trotter
        logger.ToStdout("  Node: %s, status: %s" %
106 107b0ccb Guido Trotter
                        (node_name, msg_map[node_name]))
107 1f9430d6 Iustin Pop
        for msg in nodes_hidden[node_name]:
108 1f9430d6 Iustin Pop
          logger.ToStdout(msg)
109 b07a9f05 Guido Trotter
110 107b0ccb Guido Trotter
    logger.ToStdout("OS: %s [global status: %s]" % (os_name, status))
111 b07a9f05 Guido Trotter
    _OutputPerNodeOSStatus(nodes_valid)
112 b07a9f05 Guido Trotter
    _OutputPerNodeOSStatus(nodes_bad)
113 dd96409a Guido Trotter
    logger.ToStdout("")
114 a8083063 Iustin Pop
115 a2656173 Michael Hanselmann
  return int(has_bad)
116 a2656173 Michael Hanselmann
117 a8083063 Iustin Pop
118 a8083063 Iustin Pop
commands = {
119 9a033156 Iustin Pop
  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
120 a8083063 Iustin Pop
           "Lists all valid OSes on the master"),
121 9a033156 Iustin Pop
  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
122 a8083063 Iustin Pop
               "Diagnose all OSes"),
123 a8083063 Iustin Pop
  }
124 a8083063 Iustin Pop
125 a8083063 Iustin Pop
if __name__ == '__main__':
126 3ecf6786 Iustin Pop
  sys.exit(GenericMain(commands))