4 # Copyright (C) 2012 Google Inc.
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.
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.
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
21 """External Storage related commands"""
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
29 from ganeti.cli import *
30 from ganeti import opcodes
31 from ganeti import utils
34 def ShowExtStorageInfo(opts, args):
35 """List detailed information about ExtStorage providers.
37 @param opts: the command line options selected by the user
39 @param args: empty list or list of ExtStorage providers' names
41 @return: the desired exit code
44 op = opcodes.OpExtStorageDiagnose(output_fields=["name", "nodegroup_status",
48 result = SubmitOpCode(op, opts=opts)
51 ToStderr("Can't get the ExtStorage providers list")
54 do_filter = bool(args)
56 for (name, nodegroup_data, parameters) in result:
64 for nodegroup_name, nodegroup_status in nodegroup_data.iteritems():
66 nodegroups_valid.append(nodegroup_name)
70 if nodegroups_valid != []:
71 ToStdout(" - Valid for nodegroups:")
72 for ndgrp in utils.NiceSort(nodegroups_valid):
73 ToStdout(" %s", ndgrp)
74 ToStdout(" - Supported parameters:")
75 for pname, pdesc in parameters:
76 ToStdout(" %s: %s", pname, pdesc)
78 ToStdout(" - Invalid for all nodegroups")
84 ToStdout("%s: Not Found", name)
90 def _ExtStorageStatus(status, diagnose):
91 """Beautifier function for ExtStorage status.
94 @param status: is the ExtStorage provider valid
95 @type diagnose: string
96 @param diagnose: the error message for invalid ExtStorages
98 @return: a formatted status
104 return "invalid - %s" % diagnose
107 def DiagnoseExtStorage(opts, args):
108 """Analyse all ExtStorage providers.
110 @param opts: the command line options selected by the user
112 @param args: should be an empty list
114 @return: the desired exit code
117 op = opcodes.OpExtStorageDiagnose(output_fields=["name", "node_status",
121 result = SubmitOpCode(op, opts=opts)
124 ToStderr("Can't get the list of ExtStorage providers")
127 for provider_name, node_data, nodegroup_data in result:
131 nodegroups_valid = {}
135 for node_name, node_info in node_data.iteritems():
136 if node_info: # at least one entry in the per-node list
137 (fo_path, fo_status, fo_msg, fo_params) = node_info.pop(0)
138 fo_msg = "%s (path: %s)" % (_ExtStorageStatus(fo_status, fo_msg),
141 fo_msg += (" [parameters: %s]" %
142 utils.CommaJoin([v[0] for v in fo_params]))
144 fo_msg += " [no parameters]"
146 nodes_valid[node_name] = fo_msg
148 nodes_bad[node_name] = fo_msg
150 nodes_bad[node_name] = "ExtStorage provider not found"
152 # Per nodegroup diagnose
153 for nodegroup_name, nodegroup_status in nodegroup_data.iteritems():
154 status = nodegroup_status
156 nodegroups_valid[nodegroup_name] = "valid"
158 nodegroups_bad[nodegroup_name] = "invalid"
160 def _OutputPerNodegroupStatus(msg_map):
161 map_k = utils.NiceSort(msg_map.keys())
162 for nodegroup in map_k:
163 ToStdout(" For nodegroup: %s --> %s", nodegroup,
166 def _OutputPerNodeStatus(msg_map):
167 map_k = utils.NiceSort(msg_map.keys())
168 for node_name in map_k:
169 ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name])
172 st_msg = "Provider: %s" % provider_name
175 _OutputPerNodeStatus(nodes_valid)
176 _OutputPerNodeStatus(nodes_bad)
178 _OutputPerNodegroupStatus(nodegroups_valid)
179 _OutputPerNodegroupStatus(nodegroups_bad)
187 DiagnoseExtStorage, ARGS_NONE, [PRIORITY_OPT],
188 "", "Diagnose all ExtStorage providers"),
190 ShowExtStorageInfo, [ArgOs()], [PRIORITY_OPT],
191 "", "Show info about ExtStorage providers"),
196 return GenericMain(commands)