Refactor DiagnoseOS
[ganeti-local] / scripts / gnt-os
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2006, 2007 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 import sys
23 from optparse import make_option
24
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
31
32 def ListOS(opts, args):
33   """List the OSes existing on this node.
34
35   """
36   op = opcodes.OpDiagnoseOS()
37   result = SubmitOpCode(op)
38
39   if not result:
40     logger.ToStdout("Can't get the OS list")
41     return 1
42
43   # filter non-valid OS-es
44   oses = {}
45   for node_name in result:
46     oses[node_name] = [obj for obj in result[node_name]
47                        if isinstance(obj, objects.OS)]
48
49   # Get intersection of all OSes
50   fnode = oses.keys()[0]
51   os_set = set([os_inst.name for os_inst in oses[fnode]])
52   del oses[fnode]
53   for node in oses:
54     os_set &= set([os_inst.name for os_inst in oses[node]])
55
56   if not opts.no_headers:
57     headers = {"name": "Name"}
58   else:
59     headers = None
60
61   data = GenerateTable(separator=None, headers=headers, fields=["name"],
62                        data=[[os] for os in os_set])
63
64   for line in data:
65     logger.ToStdout(line)
66
67   return 0
68
69 def _DiagnoseOSValid(obj):
70   """Verify whether an OS diagnose object represents a valid OS
71
72     Args:
73       obj: an diagnostic object as returned by OpDiagnoseOS
74
75     Returns:
76       bool: OS validity status
77   """
78
79   if isinstance(obj, objects.OS):
80     return True
81   elif isinstance(obj, errors.InvalidOS):
82     return False
83   else:
84     raise errors.ProgrammerError('unknown OS diagnose type')
85
86 def _DiagnoseOSStatus(obj):
87   """Generate a status message for an OS diagnose object.
88
89     Args:
90       obj: an diagnostic object as returned by OpDiagnoseOS
91
92     Returns:
93       string: a description of the OS status
94   """
95
96   if _DiagnoseOSValid(obj):
97     return "valid (path: %s)" % obj.path
98   else:
99     return "%s (path: %s)" % (obj.args[2], obj.args[1])
100
101 def DiagnoseOS(opts, args):
102   """Analyse all OSes on this cluster.
103
104   """
105   op = opcodes.OpDiagnoseOS()
106   result = SubmitOpCode(op)
107
108   if not result:
109     logger.ToStdout("Can't get the OS list")
110     return 1
111
112   format = "%-*s %-*s %s"
113
114   node_data = result
115   all_os = {}
116   for node_name in node_data:
117     nr = node_data[node_name]
118     if nr:
119       for obj in nr:
120         if isinstance(obj, objects.OS):
121           os_name = obj.name
122         else:
123           os_name = obj.args[0]
124         if os_name not in all_os:
125           all_os[os_name] = {}
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)
129
130   max_name = len('Name')
131   if all_os:
132     max_name = max(max_name, max([len(name) for name in all_os]))
133
134   max_node = len('Status/Node')
135   max_node = max(max_node, max([len(name) for name in node_data]))
136
137   logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node',
138                             'Details'))
139
140   for os_name in all_os:
141     nodes_valid = {}
142     nodes_bad = {}
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
149         else:
150           nodes_bad[node_name] = first_os_status
151       else:
152         nodes_bad[node_name] = "OS not found"
153
154     if nodes_valid and not nodes_bad:
155       status = "valid"
156     elif not nodes_valid and nodes_bad:
157       status = "invalid"
158     else:
159       status = "partial valid"
160
161     def _OutputNodeHiddenOSStatus(dobj_list):
162       for dobj in dobj_list:
163         logger.ToStdout(format % (max_name, "", max_node, "",
164                                   "[hidden] %s" %
165                                   _DiagnoseOSStatus(dobj)))
166
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])
174
175     logger.ToStdout(format % (max_name, os_name, max_node, status, ""))
176     _OutputPerNodeOSStatus(nodes_valid)
177     _OutputPerNodeOSStatus(nodes_bad)
178
179
180 commands = {
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"),
185   }
186
187 if __name__ == '__main__':
188   sys.exit(GenericMain(commands))