root / scripts / gnt-os @ 3a24c527
History | View | Annotate | Download (3.5 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 objects |
28 |
from ganeti import utils |
29 |
from ganeti import errors |
30 |
from ganeti import constants |
31 |
|
32 |
|
33 |
def ListOS(opts, args): |
34 |
"""List the OSes existing on this node. |
35 |
|
36 |
""" |
37 |
op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[]) |
38 |
result = SubmitOpCode(op) |
39 |
|
40 |
if not result: |
41 |
ToStderr("Can't get the OS list") |
42 |
return 1 |
43 |
|
44 |
if not opts.no_headers: |
45 |
headers = {"name": "Name"} |
46 |
else: |
47 |
headers = None |
48 |
|
49 |
data = GenerateTable(separator=None, headers=headers, fields=["name"], |
50 |
data=[[row[0]] for row in result if row[1]]) |
51 |
|
52 |
for line in data: |
53 |
ToStdout(line) |
54 |
|
55 |
return 0 |
56 |
|
57 |
|
58 |
def DiagnoseOS(opts, args): |
59 |
"""Analyse all OSes on this cluster. |
60 |
|
61 |
""" |
62 |
op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "node_status"], |
63 |
names=[]) |
64 |
result = SubmitOpCode(op) |
65 |
|
66 |
if not result: |
67 |
ToStderr("Can't get the OS list") |
68 |
return 1 |
69 |
|
70 |
has_bad = False |
71 |
|
72 |
for os_name, os_valid, node_data in result: |
73 |
nodes_valid = {} |
74 |
nodes_bad = {} |
75 |
nodes_hidden = {} |
76 |
for node_name, node_info in node_data.iteritems(): |
77 |
nodes_hidden[node_name] = [] |
78 |
if node_info: # at least one entry in the per-node list |
79 |
first_os_status, first_os_path = node_info.pop(0) |
80 |
first_os_msg = ("%s (path: %s)" % |
81 |
(first_os_status, first_os_path)) |
82 |
if first_os_status == constants.OS_VALID_STATUS: |
83 |
nodes_valid[node_name] = first_os_msg |
84 |
else: |
85 |
nodes_bad[node_name] = first_os_msg |
86 |
for hstatus, hpath in node_info: |
87 |
nodes_hidden[node_name].append(" [hidden] path: %s, status: %s" % |
88 |
(hpath, hstatus)) |
89 |
else: |
90 |
nodes_bad[node_name] = "OS not found" |
91 |
|
92 |
if nodes_valid and not nodes_bad: |
93 |
status = "valid" |
94 |
elif not nodes_valid and nodes_bad: |
95 |
status = "invalid" |
96 |
has_bad = True |
97 |
else: |
98 |
status = "partial valid" |
99 |
has_bad = True |
100 |
|
101 |
def _OutputPerNodeOSStatus(msg_map): |
102 |
map_k = utils.NiceSort(msg_map.keys()) |
103 |
for node_name in map_k: |
104 |
ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name]) |
105 |
for msg in nodes_hidden[node_name]: |
106 |
ToStdout(msg) |
107 |
|
108 |
ToStdout("OS: %s [global status: %s]", os_name, status) |
109 |
_OutputPerNodeOSStatus(nodes_valid) |
110 |
_OutputPerNodeOSStatus(nodes_bad) |
111 |
ToStdout("") |
112 |
|
113 |
return int(has_bad) |
114 |
|
115 |
|
116 |
commands = { |
117 |
'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "", |
118 |
"Lists all valid OSes on the master"), |
119 |
'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "", |
120 |
"Diagnose all OSes"), |
121 |
} |
122 |
|
123 |
if __name__ == '__main__': |
124 |
sys.exit(GenericMain(commands)) |