root / scripts / gnt-os @ dd96409a
History | View | Annotate | Download (5.7 kB)
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 |
has_bad = False |
178 |
|
179 |
for os_name in all_os: |
180 |
nodes_valid = {} |
181 |
nodes_bad = {} |
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 |
190 |
else: |
191 |
nodes_bad[node_name] = first_os_msg |
192 |
else: |
193 |
nodes_bad[node_name] = "OS not found" |
194 |
|
195 |
if nodes_valid and not nodes_bad: |
196 |
status = "valid" |
197 |
elif not nodes_valid and nodes_bad: |
198 |
status = "invalid" |
199 |
has_bad = True |
200 |
else: |
201 |
status = "partial valid" |
202 |
has_bad = True |
203 |
|
204 |
def _OutputNodeHiddenOSStatus(dobj_list): |
205 |
for dobj in dobj_list: |
206 |
logger.ToStdout(" [hidden] path: %s, status: %s" % |
207 |
(_DiagnoseOSPath(dobj), _DiagnoseOSStatus(dobj))) |
208 |
|
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]) |
216 |
|
217 |
logger.ToStdout("OS: %s [global status: %s]" % (os_name, status)) |
218 |
_OutputPerNodeOSStatus(nodes_valid) |
219 |
_OutputPerNodeOSStatus(nodes_bad) |
220 |
logger.ToStdout("") |
221 |
|
222 |
return int(has_bad) |
223 |
|
224 |
|
225 |
commands = { |
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"), |
230 |
} |
231 |
|
232 |
if __name__ == '__main__': |
233 |
sys.exit(GenericMain(commands)) |