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)
179 for os_name in all_os:
182 for node_name in node_data:
183 if node_name in all_os[os_name]:
184 first_os = all_os[os_name][node_name].pop(0)
185 first_os_msg = ("%s (path: %s)" %
186 (_DiagnoseOSStatus(first_os),
187 _DiagnoseOSPath(first_os)))
188 if _DiagnoseOSValid(first_os):
189 nodes_valid[node_name] = first_os_msg
191 nodes_bad[node_name] = first_os_msg
193 nodes_bad[node_name] = "OS not found"
195 if nodes_valid and not nodes_bad:
197 elif not nodes_valid and nodes_bad:
201 status = "partial valid"
204 def _OutputNodeHiddenOSStatus(dobj_list):
205 for dobj in dobj_list:
206 logger.ToStdout(" [hidden] path: %s, status: %s" %
207 (_DiagnoseOSPath(dobj), _DiagnoseOSStatus(dobj)))
209 def _OutputPerNodeOSStatus(msg_map):
210 map_k = utils.NiceSort(msg_map.keys())
211 for node_name in map_k:
212 logger.ToStdout(" Node: %s, status: %s" %
213 (node_name, msg_map[node_name]))
214 if node_name in all_os[os_name]:
215 _OutputNodeHiddenOSStatus(all_os[os_name][node_name])
217 logger.ToStdout("OS: %s [global status: %s]" % (os_name, status))
218 _OutputPerNodeOSStatus(nodes_valid)
219 _OutputPerNodeOSStatus(nodes_bad)
226 'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
227 "Lists all valid OSes on the master"),
228 'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
229 "Diagnose all OSes"),
232 if __name__ == '__main__':
233 sys.exit(GenericMain(commands))