root / scripts / gnt-os @ 694e2444
History | View | Annotate | Download (4.1 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 |
# 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 DiagnoseOS(opts, args): |
70 |
"""Analyse all OSes on this cluster. |
71 |
|
72 |
""" |
73 |
op = opcodes.OpDiagnoseOS() |
74 |
result = SubmitOpCode(op) |
75 |
|
76 |
if not result: |
77 |
logger.ToStdout("Can't get the OS list") |
78 |
return 1 |
79 |
|
80 |
format = "%-*s %-*s %s" |
81 |
|
82 |
node_data = result |
83 |
all_os = {} |
84 |
for node_name in node_data: |
85 |
nr = node_data[node_name] |
86 |
if nr: |
87 |
for obj in nr: |
88 |
if isinstance(obj, objects.OS): |
89 |
os_name = obj.name |
90 |
else: |
91 |
os_name = obj.args[0] |
92 |
if os_name not in all_os: |
93 |
all_os[os_name] = {} |
94 |
if node_name not in all_os[os_name]: |
95 |
all_os[os_name][node_name] = [] |
96 |
all_os[os_name][node_name].append(obj) |
97 |
|
98 |
max_name = len('Name') |
99 |
if all_os: |
100 |
max_name = max(max_name, max([len(name) for name in all_os])) |
101 |
|
102 |
max_node = len('Status/Node') |
103 |
max_node = max(max_node, max([len(name) for name in node_data])) |
104 |
|
105 |
logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node', |
106 |
'Details')) |
107 |
|
108 |
for os_name in all_os: |
109 |
nodes_valid = [] |
110 |
nodes_bad = {} |
111 |
for node_name in node_data: |
112 |
if node_name in all_os[os_name]: |
113 |
nos = all_os[os_name][node_name] |
114 |
if isinstance(nos[0], objects.OS): |
115 |
nodes_valid.append(node_name) |
116 |
elif isinstance(nos[0], errors.InvalidOS): |
117 |
nodes_bad[node_name] = nos[0].args[1] |
118 |
else: |
119 |
nodes_bad[node_name] = "os dir not found" |
120 |
|
121 |
if nodes_valid and not nodes_bad: |
122 |
status = "valid" |
123 |
elif not nodes_valid and nodes_bad: |
124 |
status = "invalid" |
125 |
else: |
126 |
status = "partial valid" |
127 |
|
128 |
logger.ToStdout(format % (max_name, os_name, max_node, status, "")) |
129 |
nodes_valid = utils.NiceSort(nodes_valid) |
130 |
for node_name in nodes_valid: |
131 |
logger.ToStdout(format % (max_name, "", max_node, node_name, |
132 |
"valid (%s)" % all_os[os_name][node_name][0].path)) |
133 |
nbk = utils.NiceSort(nodes_bad.keys()) |
134 |
for node_name in nbk: |
135 |
logger.ToStdout(format % (max_name, "", max_node, |
136 |
node_name, nodes_bad[node_name])) |
137 |
|
138 |
|
139 |
commands = { |
140 |
'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "", |
141 |
"Lists all valid OSes on the master"), |
142 |
'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "", |
143 |
"Diagnose all OSes"), |
144 |
} |
145 |
|
146 |
if __name__ == '__main__': |
147 |
sys.exit(GenericMain(commands)) |