4 # Copyright (C) 2006, 2007 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 # pylint: disable-msg=W0401,W0614
23 # W0401: Wildcard import ganeti.cli
24 # W0614: Unused import %s from wildcard import (since we need cli)
27 from optparse import make_option
29 from ganeti.cli import *
30 from ganeti import opcodes
31 from ganeti import utils
32 from ganeti import constants
35 def ListOS(opts, args):
36 """List the valid OSes in the cluster.
38 @param opts: the command line options selected by the user
40 @param args: should be an empty list
42 @return: the desired exit code
45 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
46 result = SubmitOpCode(op)
49 ToStderr("Can't get the OS list")
52 if not opts.no_headers:
53 headers = {"name": "Name"}
57 data = GenerateTable(separator=None, headers=headers, fields=["name"],
58 data=[[row[0]] for row in result if row[1]],
67 def DiagnoseOS(opts, args):
68 """Analyse all OSes on this cluster.
70 @param opts: the command line options selected by the user
72 @param args: should be an empty list
74 @return: the desired exit code
77 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "node_status"],
79 result = SubmitOpCode(op)
82 ToStderr("Can't get the OS list")
87 for os_name, os_valid, node_data in result:
91 for node_name, node_info in node_data.iteritems():
92 nodes_hidden[node_name] = []
93 if node_info: # at least one entry in the per-node list
94 first_os_status, first_os_path = node_info.pop(0)
95 first_os_msg = ("%s (path: %s)" %
96 (first_os_status, first_os_path))
97 if first_os_status == constants.OS_VALID_STATUS:
98 nodes_valid[node_name] = first_os_msg
100 nodes_bad[node_name] = first_os_msg
101 for hstatus, hpath in node_info:
102 nodes_hidden[node_name].append(" [hidden] path: %s, status: %s" %
105 nodes_bad[node_name] = "OS not found"
107 if nodes_valid and not nodes_bad:
109 elif not nodes_valid and nodes_bad:
113 status = "partial valid"
116 def _OutputPerNodeOSStatus(msg_map):
117 map_k = utils.NiceSort(msg_map.keys())
118 for node_name in map_k:
119 ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name])
120 for msg in nodes_hidden[node_name]:
123 ToStdout("OS: %s [global status: %s]", os_name, status)
124 _OutputPerNodeOSStatus(nodes_valid)
125 _OutputPerNodeOSStatus(nodes_bad)
132 'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
133 "Lists all valid OSes on the master"),
134 'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
135 "Diagnose all OSes"),
138 if __name__ == '__main__':
139 sys.exit(GenericMain(commands))