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
23 from optparse import make_option
25 from ganeti.cli import *
26 from ganeti import opcodes
27 from ganeti import logger
28 from ganeti import objects
29 from ganeti import utils
30 from ganeti import errors
33 def _DiagnoseByOS(rlist):
34 """Remap an OpDiagnoseOS() return list into an a per-os per-node dictionary
37 rlist: a map with nodes as keys and diagnoseobjects as values
40 map: a map with osnames as keys and as value another map, with nodes as
41 keys and diagnoseobjects as values
42 e.g. {"debian-etch": {"node1": <object>, "node2": <object>}}
46 for node_name, nr in rlist.iteritems():
50 if os.name not in all_os:
52 if node_name not in all_os[os.name]:
53 all_os[os.name][node_name] = []
54 all_os[os.name][node_name].append(os)
59 def ListOS(opts, args):
60 """List the OSes existing on this node.
63 op = opcodes.OpDiagnoseOS()
64 result = SubmitOpCode(op)
67 logger.ToStdout("Can't get the OS list")
71 num_nodes = len(node_data)
72 all_os = _DiagnoseByOS(node_data)
75 for os_name, os_node_data in all_os.iteritems():
76 if len(os_node_data) != num_nodes:
79 if utils.all(os_node_data.values(), lambda l: l[0]):
80 valid_os.append(os_name)
82 if not opts.no_headers:
83 headers = {"name": "Name"}
87 data = GenerateTable(separator=None, headers=headers, fields=["name"],
88 data=[[os] for os in valid_os])
96 def DiagnoseOS(opts, args):
97 """Analyse all OSes on this cluster.
100 op = opcodes.OpDiagnoseOS()
101 result = SubmitOpCode(op)
104 logger.ToStdout("Can't get the OS list")
108 all_os = _DiagnoseByOS(node_data)
112 for os_name in all_os:
115 for node_name in node_data:
116 if node_name in all_os[os_name]:
117 first_os = all_os[os_name][node_name].pop(0)
118 first_os_msg = ("%s (path: %s)" %
119 (first_os.status, first_os.path))
121 nodes_valid[node_name] = first_os_msg
123 nodes_bad[node_name] = first_os_msg
125 nodes_bad[node_name] = "OS not found"
127 if nodes_valid and not nodes_bad:
129 elif not nodes_valid and nodes_bad:
133 status = "partial valid"
136 def _OutputNodeHiddenOSStatus(dobj_list):
137 for dobj in dobj_list:
138 logger.ToStdout(" [hidden] path: %s, status: %s" %
139 (dobj.path, dobj.status))
141 def _OutputPerNodeOSStatus(msg_map):
142 map_k = utils.NiceSort(msg_map.keys())
143 for node_name in map_k:
144 logger.ToStdout(" Node: %s, status: %s" %
145 (node_name, msg_map[node_name]))
146 if node_name in all_os[os_name]:
147 _OutputNodeHiddenOSStatus(all_os[os_name][node_name])
149 logger.ToStdout("OS: %s [global status: %s]" % (os_name, status))
150 _OutputPerNodeOSStatus(nodes_valid)
151 _OutputPerNodeOSStatus(nodes_bad)
158 'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
159 "Lists all valid OSes on the master"),
160 'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
161 "Diagnose all OSes"),
164 if __name__ == '__main__':
165 sys.exit(GenericMain(commands))