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 _DiagnoseOSValid(obj):
34 """Verify whether an OS diagnose object represents a valid OS
37 obj: an diagnostic object as returned by OpDiagnoseOS
40 bool: OS validity status
43 if isinstance(obj, objects.OS):
45 elif isinstance(obj, errors.InvalidOS):
48 raise errors.ProgrammerError("unknown OS diagnose type: '%s'" % type(obj))
51 def _DiagnoseOSName(obj):
52 """Generate a status message for an OS diagnose object.
55 obj: an diagnostic object as returned by OpDiagnoseOS
58 string: the name of the OS in question
61 if _DiagnoseOSValid(obj):
67 def _DiagnoseOSStatus(obj):
68 """Generate a status message for an OS diagnose object.
71 obj: an diagnostic object as returned by OpDiagnoseOS
74 string: a description of the OS status
77 if _DiagnoseOSValid(obj):
83 def _DiagnoseOSPath(obj):
84 """Get the path out of an OS diagnose object.
87 obj: an diagnostic object as returned by OpDiagnoseOS
93 if _DiagnoseOSValid(obj):
99 def _DiagnoseByOS(rlist):
100 """Remap an OpDiagnoseOS() return list into an a per-os per-node dictionary
103 rlist: a map with nodes as keys and diagnoseobjects as values
106 map: a map with osnames as keys and as value another map, with nodes as
107 keys and diagnoseobjects as values
108 e.g. {"debian-etch": {"node1": <object>, "node2": <object>}}
112 for node_name, nr in rlist.iteritems():
116 os_name = _DiagnoseOSName(obj)
117 if os_name not in all_os:
119 if node_name not in all_os[os_name]:
120 all_os[os_name][node_name] = []
121 all_os[os_name][node_name].append(obj)
126 def ListOS(opts, args):
127 """List the OSes existing on this node.
130 op = opcodes.OpDiagnoseOS()
131 result = SubmitOpCode(op)
134 logger.ToStdout("Can't get the OS list")
138 num_nodes = len(node_data)
139 all_os = _DiagnoseByOS(node_data)
142 for os_name, os_node_data in all_os.iteritems():
143 if len(os_node_data) != num_nodes:
146 if utils.all(os_node_data.values(), lambda l: _DiagnoseOSValid(l[0])):
147 valid_os.append(os_name)
149 if not opts.no_headers:
150 headers = {"name": "Name"}
154 data = GenerateTable(separator=None, headers=headers, fields=["name"],
155 data=[[os] for os in valid_os])
158 logger.ToStdout(line)
163 def DiagnoseOS(opts, args):
164 """Analyse all OSes on this cluster.
167 op = opcodes.OpDiagnoseOS()
168 result = SubmitOpCode(op)
171 logger.ToStdout("Can't get the OS list")
175 all_os = _DiagnoseByOS(node_data)
177 format = "%-*s %-*s %s"
179 max_name = len('Name')
181 max_name = max(max_name, max([len(name) for name in all_os]))
183 max_node = len('Status/Node')
184 max_node = max(max_node, max([len(name) for name in node_data]))
186 logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node',
191 for os_name in all_os:
194 for node_name in node_data:
195 if node_name in all_os[os_name]:
196 first_os = all_os[os_name][node_name].pop(0)
197 first_os_status = _DiagnoseOSStatus(first_os)
198 if _DiagnoseOSValid(first_os):
199 nodes_valid[node_name] = first_os_status
201 nodes_bad[node_name] = first_os_status
203 nodes_bad[node_name] = "OS not found"
205 if nodes_valid and not nodes_bad:
207 elif not nodes_valid and nodes_bad:
211 status = "partial valid"
214 def _OutputNodeHiddenOSStatus(dobj_list):
215 for dobj in dobj_list:
216 logger.ToStdout(format % (max_name, "", max_node, "",
218 _DiagnoseOSStatus(dobj)))
220 def _OutputPerNodeOSStatus(status_map):
221 map_k = utils.NiceSort(status_map.keys())
222 for node_name in map_k:
223 logger.ToStdout(format % (max_name, "", max_node,
224 node_name, status_map[node_name]))
225 if node_name in all_os[os_name]:
226 _OutputNodeHiddenOSStatus(all_os[os_name][node_name])
228 logger.ToStdout(format % (max_name, os_name, max_node, status, ""))
229 _OutputPerNodeOSStatus(nodes_valid)
230 _OutputPerNodeOSStatus(nodes_bad)
236 'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
237 "Lists all valid OSes on the master"),
238 'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
239 "Diagnose all OSes"),
242 if __name__ == '__main__':
243 sys.exit(GenericMain(commands))