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
32 def ListOS(opts, args):
33 """List the OSes existing on this node.
36 op = opcodes.OpDiagnoseOS()
37 result = SubmitOpCode(op)
40 logger.ToStdout("Can't get the OS list")
43 # filter non-valid OS-es
45 for node_name in result:
46 oses[node_name] = [obj for obj in result[node_name]
47 if isinstance(obj, objects.OS)]
49 # Get intersection of all OSes
50 fnode = oses.keys()[0]
51 os_set = set([os_inst.name for os_inst in oses[fnode]])
54 os_set &= set([os_inst.name for os_inst in oses[node]])
56 if not opts.no_headers:
57 headers = {"name": "Name"}
61 data = GenerateTable(separator=None, headers=headers, fields=["name"],
62 data=[[os] for os in os_set])
69 def _DiagnoseOSValid(obj):
70 """Verify whether an OS diagnose object represents a valid OS
73 obj: an diagnostic object as returned by OpDiagnoseOS
76 bool: OS validity status
79 if isinstance(obj, objects.OS):
81 elif isinstance(obj, errors.InvalidOS):
84 raise errors.ProgrammerError('unknown OS diagnose type')
86 def _DiagnoseOSStatus(obj):
87 """Generate a status message for an OS diagnose object.
90 obj: an diagnostic object as returned by OpDiagnoseOS
93 string: a description of the OS status
96 if _DiagnoseOSValid(obj):
97 return "valid (path: %s)" % obj.path
99 return "%s (path: %s)" % (obj.args[2], obj.args[1])
101 def DiagnoseOS(opts, args):
102 """Analyse all OSes on this cluster.
105 op = opcodes.OpDiagnoseOS()
106 result = SubmitOpCode(op)
109 logger.ToStdout("Can't get the OS list")
112 format = "%-*s %-*s %s"
116 for node_name in node_data:
117 nr = node_data[node_name]
120 if isinstance(obj, objects.OS):
123 os_name = obj.args[0]
124 if os_name not in all_os:
126 if node_name not in all_os[os_name]:
127 all_os[os_name][node_name] = []
128 all_os[os_name][node_name].append(obj)
130 max_name = len('Name')
132 max_name = max(max_name, max([len(name) for name in all_os]))
134 max_node = len('Status/Node')
135 max_node = max(max_node, max([len(name) for name in node_data]))
137 logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node',
140 for os_name in all_os:
143 for node_name in node_data:
144 if node_name in all_os[os_name]:
145 first_os = all_os[os_name][node_name].pop(0)
146 first_os_status = _DiagnoseOSStatus(first_os)
147 if _DiagnoseOSValid(first_os):
148 nodes_valid[node_name] = first_os_status
150 nodes_bad[node_name] = first_os_status
152 nodes_bad[node_name] = "OS not found"
154 if nodes_valid and not nodes_bad:
156 elif not nodes_valid and nodes_bad:
159 status = "partial valid"
161 def _OutputNodeHiddenOSStatus(dobj_list):
162 for dobj in dobj_list:
163 logger.ToStdout(format % (max_name, "", max_node, "",
165 _DiagnoseOSStatus(dobj)))
167 def _OutputPerNodeOSStatus(status_map):
168 map_k = utils.NiceSort(status_map.keys())
169 for node_name in map_k:
170 logger.ToStdout(format % (max_name, "", max_node,
171 node_name, status_map[node_name]))
172 if node_name in all_os[os_name]:
173 _OutputNodeHiddenOSStatus(all_os[os_name][node_name])
175 logger.ToStdout(format % (max_name, os_name, max_node, status, ""))
176 _OutputPerNodeOSStatus(nodes_valid)
177 _OutputPerNodeOSStatus(nodes_bad)
181 'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
182 "Lists all valid OSes on the master"),
183 'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
184 "Diagnose all OSes"),
187 if __name__ == '__main__':
188 sys.exit(GenericMain(commands))