root / scripts / gnt-os @ a8083063
History | View | Annotate | Download (3.8 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 |
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 |
fnode = oses.keys()[0] |
50 |
os_set = set([os_inst.name for os_inst in oses[fnode]]) |
51 |
del oses[fnode] |
52 |
for node in oses: |
53 |
os_set &= set([os_inst.name for os_inst in oses[node]]) |
54 |
|
55 |
format = "%s" |
56 |
|
57 |
if not opts.no_headers: |
58 |
logger.ToStdout(format % 'Name') |
59 |
|
60 |
for os_name in os_set: |
61 |
logger.ToStdout(format % os_name) |
62 |
|
63 |
return 0 |
64 |
|
65 |
def DiagnoseOS(opts, args): |
66 |
"""Analyse all OSes on this cluster. |
67 |
|
68 |
""" |
69 |
op = opcodes.OpDiagnoseOS() |
70 |
result = SubmitOpCode(op) |
71 |
|
72 |
if not result: |
73 |
logger.ToStdout("Can't get the OS list") |
74 |
return 1 |
75 |
|
76 |
format = "%-*s %-*s %s" |
77 |
|
78 |
node_data = result |
79 |
all_os = {} |
80 |
for node_name in node_data: |
81 |
nr = node_data[node_name] |
82 |
if nr: |
83 |
for obj in nr: |
84 |
if isinstance(obj, objects.OS): |
85 |
os_name = obj.name |
86 |
else: |
87 |
os_name = obj.args[0] |
88 |
if os_name not in all_os: |
89 |
all_os[os_name] = {} |
90 |
all_os[os_name][node_name] = obj |
91 |
|
92 |
max_name = len('Name') |
93 |
if all_os: |
94 |
max_name = max(max_name, max([len(name) for name in all_os])) |
95 |
|
96 |
max_node = len('Status/Node') |
97 |
max_node = max(max_node, max([len(name) for name in node_data])) |
98 |
|
99 |
logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node', |
100 |
'Details')) |
101 |
|
102 |
for os_name in all_os: |
103 |
nodes_valid = [] |
104 |
nodes_bad = {} |
105 |
for node_name in node_data: |
106 |
nos = all_os[os_name].get(node_name, None) |
107 |
if isinstance(nos, objects.OS): |
108 |
nodes_valid.append(node_name) |
109 |
elif isinstance(nos, errors.InvalidOS): |
110 |
nodes_bad[node_name] = nos.args[1] |
111 |
else: |
112 |
nodes_bad[node_name] = "os dir not found" |
113 |
|
114 |
if nodes_valid and not nodes_bad: |
115 |
status = "valid" |
116 |
elif not nodes_valid and nodes_bad: |
117 |
status = "invalid" |
118 |
else: |
119 |
status = "partial valid" |
120 |
logger.ToStdout(format % (max_name, os_name, max_node, status, "")) |
121 |
nodes_valid = utils.NiceSort(nodes_valid) |
122 |
for node_name in nodes_valid: |
123 |
logger.ToStdout(format % (max_name, "", max_node, node_name, "valid")) |
124 |
nbk = utils.NiceSort(nodes_bad.keys()) |
125 |
for node_name in nbk: |
126 |
logger.ToStdout(format % (max_name, "", max_node, |
127 |
node_name, nodes_bad[node_name])) |
128 |
|
129 |
|
130 |
commands = { |
131 |
'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "", |
132 |
"Lists all valid OSes on the master"), |
133 |
'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "", |
134 |
"Diagnose all OSes"), |
135 |
} |
136 |
|
137 |
if __name__ == '__main__': |
138 |
retcode = GenericMain(commands) |
139 |
sys.exit(retcode) |