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')
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):
78 return "valid (path: %s)" % obj.path
80 return "%s (path: %s)" % (obj.args[2], obj.args[1])
83 def _DiagnoseByOS(rlist):
84 """Remap an OpDiagnoseOS() return list into an a per-os per-node dictionary
87 rlist: a map with nodes as keys and diagnoseobjects as values
90 map: a map with osnames as keys and as value another map, with nodes as
91 keys and diagnoseobjects as values
92 e.g. {"debian-etch": {"node1": <object>, "node2": <object>}}
96 for node_name, nr in rlist.iteritems():
100 os_name = _DiagnoseOSName(obj)
101 if os_name not in all_os:
103 if node_name not in all_os[os_name]:
104 all_os[os_name][node_name] = []
105 all_os[os_name][node_name].append(obj)
110 def ListOS(opts, args):
111 """List the OSes existing on this node.
114 op = opcodes.OpDiagnoseOS()
115 result = SubmitOpCode(op)
118 logger.ToStdout("Can't get the OS list")
122 num_nodes = len(node_data)
123 all_os = _DiagnoseByOS(node_data)
126 for os_name, os_node_data in all_os.iteritems():
127 if len(os_node_data) != num_nodes:
130 for l in os_node_data.values():
131 if not _DiagnoseOSValid(l[0]):
135 valid_os.append(os_name)
137 if not opts.no_headers:
138 headers = {"name": "Name"}
142 data = GenerateTable(separator=None, headers=headers, fields=["name"],
143 data=[[os] for os in valid_os])
146 logger.ToStdout(line)
151 def DiagnoseOS(opts, args):
152 """Analyse all OSes on this cluster.
155 op = opcodes.OpDiagnoseOS()
156 result = SubmitOpCode(op)
159 logger.ToStdout("Can't get the OS list")
163 all_os = _DiagnoseByOS(node_data)
165 format = "%-*s %-*s %s"
167 max_name = len('Name')
169 max_name = max(max_name, max([len(name) for name in all_os]))
171 max_node = len('Status/Node')
172 max_node = max(max_node, max([len(name) for name in node_data]))
174 logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node',
177 for os_name in all_os:
180 for node_name in node_data:
181 if node_name in all_os[os_name]:
182 first_os = all_os[os_name][node_name].pop(0)
183 first_os_status = _DiagnoseOSStatus(first_os)
184 if _DiagnoseOSValid(first_os):
185 nodes_valid[node_name] = first_os_status
187 nodes_bad[node_name] = first_os_status
189 nodes_bad[node_name] = "OS not found"
191 if nodes_valid and not nodes_bad:
193 elif not nodes_valid and nodes_bad:
196 status = "partial valid"
198 def _OutputNodeHiddenOSStatus(dobj_list):
199 for dobj in dobj_list:
200 logger.ToStdout(format % (max_name, "", max_node, "",
202 _DiagnoseOSStatus(dobj)))
204 def _OutputPerNodeOSStatus(status_map):
205 map_k = utils.NiceSort(status_map.keys())
206 for node_name in map_k:
207 logger.ToStdout(format % (max_name, "", max_node,
208 node_name, status_map[node_name]))
209 if node_name in all_os[os_name]:
210 _OutputNodeHiddenOSStatus(all_os[os_name][node_name])
212 logger.ToStdout(format % (max_name, os_name, max_node, status, ""))
213 _OutputPerNodeOSStatus(nodes_valid)
214 _OutputPerNodeOSStatus(nodes_bad)
218 'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
219 "Lists all valid OSes on the master"),
220 'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
221 "Diagnose all OSes"),
224 if __name__ == '__main__':
225 sys.exit(GenericMain(commands))