root / lib / client / gnt_storage.py @ 9bcef16f
History | View | Annotate | Download (5.6 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2012 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 |
"""External Storage related commands"""
|
22 |
|
23 |
# pylint: disable=W0401,W0613,W0614,C0103
|
24 |
# W0401: Wildcard import ganeti.cli
|
25 |
# W0613: Unused argument, since all functions follow the same API
|
26 |
# W0614: Unused import %s from wildcard import (since we need cli)
|
27 |
# C0103: Invalid name gnt-storage
|
28 |
|
29 |
from ganeti.cli import * |
30 |
from ganeti import constants |
31 |
from ganeti import opcodes |
32 |
from ganeti import utils |
33 |
|
34 |
|
35 |
def ShowExtStorageInfo(opts, args): |
36 |
"""List detailed information about ExtStorage providers.
|
37 |
|
38 |
@param opts: the command line options selected by the user
|
39 |
@type args: list
|
40 |
@param args: empty list or list of ExtStorage providers' names
|
41 |
@rtype: int
|
42 |
@return: the desired exit code
|
43 |
|
44 |
"""
|
45 |
op = opcodes.OpExtStorageDiagnose(output_fields=["name", "nodegroup_status", |
46 |
"parameters"],
|
47 |
names=[]) |
48 |
|
49 |
result = SubmitOpCode(op, opts=opts) |
50 |
|
51 |
if not result: |
52 |
ToStderr("Can't get the ExtStorage providers list")
|
53 |
return 1 |
54 |
|
55 |
do_filter = bool(args)
|
56 |
|
57 |
for (name, nodegroup_data, parameters) in result: |
58 |
if do_filter:
|
59 |
if name not in args: |
60 |
continue
|
61 |
else:
|
62 |
args.remove(name) |
63 |
|
64 |
nodegroups_valid = [] |
65 |
for nodegroup_name, nodegroup_status in nodegroup_data.iteritems(): |
66 |
if nodegroup_status:
|
67 |
nodegroups_valid.append(nodegroup_name) |
68 |
|
69 |
ToStdout("%s:", name)
|
70 |
|
71 |
if nodegroups_valid != []:
|
72 |
ToStdout(" - Valid for nodegroups:")
|
73 |
for ndgrp in utils.NiceSort(nodegroups_valid): |
74 |
ToStdout(" %s", ndgrp)
|
75 |
ToStdout(" - Supported parameters:")
|
76 |
for pname, pdesc in parameters: |
77 |
ToStdout(" %s: %s", pname, pdesc)
|
78 |
else:
|
79 |
ToStdout(" - Invalid for all nodegroups")
|
80 |
|
81 |
ToStdout("")
|
82 |
|
83 |
if args:
|
84 |
for name in args: |
85 |
ToStdout("%s: Not Found", name)
|
86 |
ToStdout("")
|
87 |
|
88 |
return 0 |
89 |
|
90 |
|
91 |
def _ExtStorageStatus(status, diagnose): |
92 |
"""Beautifier function for ExtStorage status.
|
93 |
|
94 |
@type status: boolean
|
95 |
@param status: is the ExtStorage provider valid
|
96 |
@type diagnose: string
|
97 |
@param diagnose: the error message for invalid ExtStorages
|
98 |
@rtype: string
|
99 |
@return: a formatted status
|
100 |
|
101 |
"""
|
102 |
if status:
|
103 |
return "valid" |
104 |
else:
|
105 |
return "invalid - %s" % diagnose |
106 |
|
107 |
|
108 |
def DiagnoseExtStorage(opts, args): |
109 |
"""Analyse all ExtStorage providers.
|
110 |
|
111 |
@param opts: the command line options selected by the user
|
112 |
@type args: list
|
113 |
@param args: should be an empty list
|
114 |
@rtype: int
|
115 |
@return: the desired exit code
|
116 |
|
117 |
"""
|
118 |
op = opcodes.OpExtStorageDiagnose(output_fields=["name", "node_status", |
119 |
"nodegroup_status"],
|
120 |
names=[]) |
121 |
|
122 |
result = SubmitOpCode(op, opts=opts) |
123 |
|
124 |
if not result: |
125 |
ToStderr("Can't get the list of ExtStorage providers")
|
126 |
return 1 |
127 |
|
128 |
for provider_name, node_data, nodegroup_data in result: |
129 |
|
130 |
nodes_valid = {} |
131 |
nodes_bad = {} |
132 |
nodegroups_valid = {} |
133 |
nodegroups_bad = {} |
134 |
|
135 |
# Per node diagnose
|
136 |
for node_name, node_info in node_data.iteritems(): |
137 |
if node_info: # at least one entry in the per-node list |
138 |
(fo_path, fo_status, fo_msg, fo_params) = node_info.pop(0)
|
139 |
fo_msg = "%s (path: %s)" % (_ExtStorageStatus(fo_status, fo_msg),
|
140 |
fo_path) |
141 |
if fo_params:
|
142 |
fo_msg += (" [parameters: %s]" %
|
143 |
utils.CommaJoin([v[0] for v in fo_params])) |
144 |
else:
|
145 |
fo_msg += " [no parameters]"
|
146 |
if fo_status:
|
147 |
nodes_valid[node_name] = fo_msg |
148 |
else:
|
149 |
nodes_bad[node_name] = fo_msg |
150 |
else:
|
151 |
nodes_bad[node_name] = "ExtStorage provider not found"
|
152 |
|
153 |
# Per nodegroup diagnose
|
154 |
for nodegroup_name, nodegroup_status in nodegroup_data.iteritems(): |
155 |
status = nodegroup_status |
156 |
if status:
|
157 |
nodegroups_valid[nodegroup_name] = "valid"
|
158 |
else:
|
159 |
nodegroups_bad[nodegroup_name] = "invalid"
|
160 |
|
161 |
def _OutputPerNodegroupStatus(msg_map): |
162 |
map_k = utils.NiceSort(msg_map.keys()) |
163 |
for nodegroup in map_k: |
164 |
ToStdout(" For nodegroup: %s --> %s", nodegroup,
|
165 |
msg_map[nodegroup]) |
166 |
|
167 |
def _OutputPerNodeStatus(msg_map): |
168 |
map_k = utils.NiceSort(msg_map.keys()) |
169 |
for node_name in map_k: |
170 |
ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name])
|
171 |
|
172 |
# Print the output
|
173 |
st_msg = "Provider: %s" % provider_name
|
174 |
ToStdout(st_msg) |
175 |
ToStdout("---")
|
176 |
_OutputPerNodeStatus(nodes_valid) |
177 |
_OutputPerNodeStatus(nodes_bad) |
178 |
ToStdout(" --")
|
179 |
_OutputPerNodegroupStatus(nodegroups_valid) |
180 |
_OutputPerNodegroupStatus(nodegroups_bad) |
181 |
ToStdout("")
|
182 |
|
183 |
return 0 |
184 |
|
185 |
|
186 |
commands = { |
187 |
"diagnose": (
|
188 |
DiagnoseExtStorage, ARGS_NONE, [PRIORITY_OPT], |
189 |
"", "Diagnose all ExtStorage providers"), |
190 |
"info": (
|
191 |
ShowExtStorageInfo, [ArgOs()], [PRIORITY_OPT], |
192 |
"", "Show info about ExtStorage providers"), |
193 |
} |
194 |
|
195 |
|
196 |
def Main(): |
197 |
return GenericMain(commands)
|