Separate OS status and path
[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
33 def _DiagnoseOSValid(obj):
34   """Verify whether an OS diagnose object represents a valid OS
35
36     Args:
37       obj: an diagnostic object as returned by OpDiagnoseOS
38
39     Returns:
40       bool: OS validity status
41   """
42
43   if isinstance(obj, objects.OS):
44     return True
45   elif isinstance(obj, errors.InvalidOS):
46     return False
47   else:
48     raise errors.ProgrammerError("unknown OS diagnose type: '%s'" % type(obj))
49
50
51 def _DiagnoseOSName(obj):
52   """Generate a status message for an OS diagnose object.
53
54     Args:
55       obj: an diagnostic object as returned by OpDiagnoseOS
56
57     Returns:
58       string: the name of the OS in question
59   """
60
61   if _DiagnoseOSValid(obj):
62     return obj.name
63   else:
64     return obj.args[0]
65
66
67 def _DiagnoseOSStatus(obj):
68   """Generate a status message for an OS diagnose object.
69
70     Args:
71       obj: an diagnostic object as returned by OpDiagnoseOS
72
73     Returns:
74       string: a description of the OS status
75   """
76
77   if _DiagnoseOSValid(obj):
78     return "valid"
79   else:
80     return obj.args[2]
81
82
83 def _DiagnoseOSPath(obj):
84   """Get the path out of an OS diagnose object.
85
86     Args:
87       obj: an diagnostic object as returned by OpDiagnoseOS
88
89     Returns:
90       string: the OS path
91
92   """
93   if _DiagnoseOSValid(obj):
94     return obj.path
95   else:
96     return obj.args[1]
97
98
99 def _DiagnoseByOS(rlist):
100   """Remap an OpDiagnoseOS() return list into an a per-os per-node dictionary
101
102     Args:
103       rlist: a map with nodes as keys and diagnoseobjects as values
104
105     Returns:
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>}}
109   """
110
111   all_os = {}
112   for node_name, nr in rlist.iteritems():
113     if not nr:
114       continue
115     for obj in nr:
116       os_name = _DiagnoseOSName(obj)
117       if os_name not in all_os:
118         all_os[os_name] = {}
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)
122
123   return all_os
124
125
126 def ListOS(opts, args):
127   """List the OSes existing on this node.
128
129   """
130   op = opcodes.OpDiagnoseOS()
131   result = SubmitOpCode(op)
132
133   if not result:
134     logger.ToStdout("Can't get the OS list")
135     return 1
136
137   node_data = result
138   num_nodes = len(node_data)
139   all_os = _DiagnoseByOS(node_data)
140
141   valid_os = []
142   for os_name, os_node_data in all_os.iteritems():
143     if len(os_node_data) != num_nodes:
144       continue
145
146     if utils.all(os_node_data.values(), lambda l: _DiagnoseOSValid(l[0])):
147       valid_os.append(os_name)
148
149   if not opts.no_headers:
150     headers = {"name": "Name"}
151   else:
152     headers = None
153
154   data = GenerateTable(separator=None, headers=headers, fields=["name"],
155                        data=[[os] for os in valid_os])
156
157   for line in data:
158     logger.ToStdout(line)
159
160   return 0
161
162
163 def DiagnoseOS(opts, args):
164   """Analyse all OSes on this cluster.
165
166   """
167   op = opcodes.OpDiagnoseOS()
168   result = SubmitOpCode(op)
169
170   if not result:
171     logger.ToStdout("Can't get the OS list")
172     return 1
173
174   node_data = result
175   all_os = _DiagnoseByOS(node_data)
176
177   format = "%-*s %-*s %s"
178
179   max_name = len('Name')
180   if all_os:
181     max_name = max(max_name, max([len(name) for name in all_os]))
182
183   max_node = len('Status/Node')
184   max_node = max(max_node, max([len(name) for name in node_data]))
185
186   logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node',
187                             'Details'))
188
189   has_bad = False
190
191   for os_name in all_os:
192     nodes_valid = {}
193     nodes_bad = {}
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
200         else:
201           nodes_bad[node_name] = first_os_status
202       else:
203         nodes_bad[node_name] = "OS not found"
204
205     if nodes_valid and not nodes_bad:
206       status = "valid"
207     elif not nodes_valid and nodes_bad:
208       status = "invalid"
209       has_bad = True
210     else:
211       status = "partial valid"
212       has_bad = True
213
214     def _OutputNodeHiddenOSStatus(dobj_list):
215       for dobj in dobj_list:
216         logger.ToStdout(format % (max_name, "", max_node, "",
217                                   "[hidden] %s" %
218                                   _DiagnoseOSStatus(dobj)))
219
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])
227
228     logger.ToStdout(format % (max_name, os_name, max_node, status, ""))
229     _OutputPerNodeOSStatus(nodes_valid)
230     _OutputPerNodeOSStatus(nodes_bad)
231
232   return int(has_bad)
233
234
235 commands = {
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"),
240   }
241
242 if __name__ == '__main__':
243   sys.exit(GenericMain(commands))