Package ganeti :: Package client :: Module gnt_storage
[hide private]
[frames] | no frames]

Source Code for Module ganeti.client.gnt_storage

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2012 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29   
 30  """External Storage related commands""" 
 31   
 32  # pylint: disable=W0401,W0613,W0614,C0103 
 33  # W0401: Wildcard import ganeti.cli 
 34  # W0613: Unused argument, since all functions follow the same API 
 35  # W0614: Unused import %s from wildcard import (since we need cli) 
 36  # C0103: Invalid name gnt-storage 
 37   
 38  from ganeti.cli import * 
 39  from ganeti import opcodes 
 40  from ganeti import utils 
 41   
 42   
43 -def ShowExtStorageInfo(opts, args):
44 """List detailed information about ExtStorage providers. 45 46 @param opts: the command line options selected by the user 47 @type args: list 48 @param args: empty list or list of ExtStorage providers' names 49 @rtype: int 50 @return: the desired exit code 51 52 """ 53 op = opcodes.OpExtStorageDiagnose(output_fields=["name", "nodegroup_status", 54 "parameters"], 55 names=[]) 56 57 result = SubmitOpCode(op, opts=opts) 58 59 if not result: 60 ToStderr("Can't get the ExtStorage providers list") 61 return 1 62 63 do_filter = bool(args) 64 65 for (name, nodegroup_data, parameters) in result: 66 if do_filter: 67 if name not in args: 68 continue 69 else: 70 args.remove(name) 71 72 nodegroups_valid = [] 73 for nodegroup_name, nodegroup_status in nodegroup_data.iteritems(): 74 if nodegroup_status: 75 nodegroups_valid.append(nodegroup_name) 76 77 ToStdout("%s:", name) 78 79 if nodegroups_valid != []: 80 ToStdout(" - Valid for nodegroups:") 81 for ndgrp in utils.NiceSort(nodegroups_valid): 82 ToStdout(" %s", ndgrp) 83 ToStdout(" - Supported parameters:") 84 for pname, pdesc in parameters: 85 ToStdout(" %s: %s", pname, pdesc) 86 else: 87 ToStdout(" - Invalid for all nodegroups") 88 89 ToStdout("") 90 91 if args: 92 for name in args: 93 ToStdout("%s: Not Found", name) 94 ToStdout("") 95 96 return 0
97 98
99 -def _ExtStorageStatus(status, diagnose):
100 """Beautifier function for ExtStorage status. 101 102 @type status: boolean 103 @param status: is the ExtStorage provider valid 104 @type diagnose: string 105 @param diagnose: the error message for invalid ExtStorages 106 @rtype: string 107 @return: a formatted status 108 109 """ 110 if status: 111 return "valid" 112 else: 113 return "invalid - %s" % diagnose
114 115
116 -def DiagnoseExtStorage(opts, args):
117 """Analyse all ExtStorage providers. 118 119 @param opts: the command line options selected by the user 120 @type args: list 121 @param args: should be an empty list 122 @rtype: int 123 @return: the desired exit code 124 125 """ 126 op = opcodes.OpExtStorageDiagnose(output_fields=["name", "node_status", 127 "nodegroup_status"], 128 names=[]) 129 130 result = SubmitOpCode(op, opts=opts) 131 132 if not result: 133 ToStderr("Can't get the list of ExtStorage providers") 134 return 1 135 136 for provider_name, node_data, nodegroup_data in result: 137 138 nodes_valid = {} 139 nodes_bad = {} 140 nodegroups_valid = {} 141 nodegroups_bad = {} 142 143 # Per node diagnose 144 for node_name, node_info in node_data.iteritems(): 145 if node_info: # at least one entry in the per-node list 146 (fo_path, fo_status, fo_msg, fo_params) = node_info.pop(0) 147 fo_msg = "%s (path: %s)" % (_ExtStorageStatus(fo_status, fo_msg), 148 fo_path) 149 if fo_params: 150 fo_msg += (" [parameters: %s]" % 151 utils.CommaJoin([v[0] for v in fo_params])) 152 else: 153 fo_msg += " [no parameters]" 154 if fo_status: 155 nodes_valid[node_name] = fo_msg 156 else: 157 nodes_bad[node_name] = fo_msg 158 else: 159 nodes_bad[node_name] = "ExtStorage provider not found" 160 161 # Per nodegroup diagnose 162 for nodegroup_name, nodegroup_status in nodegroup_data.iteritems(): 163 status = nodegroup_status 164 if status: 165 nodegroups_valid[nodegroup_name] = "valid" 166 else: 167 nodegroups_bad[nodegroup_name] = "invalid" 168 169 def _OutputPerNodegroupStatus(msg_map): 170 map_k = utils.NiceSort(msg_map.keys()) 171 for nodegroup in map_k: 172 ToStdout(" For nodegroup: %s --> %s", nodegroup, 173 msg_map[nodegroup])
174 175 def _OutputPerNodeStatus(msg_map): 176 map_k = utils.NiceSort(msg_map.keys()) 177 for node_name in map_k: 178 ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name]) 179 180 # Print the output 181 st_msg = "Provider: %s" % provider_name 182 ToStdout(st_msg) 183 ToStdout("---") 184 _OutputPerNodeStatus(nodes_valid) 185 _OutputPerNodeStatus(nodes_bad) 186 ToStdout(" --") 187 _OutputPerNodegroupStatus(nodegroups_valid) 188 _OutputPerNodegroupStatus(nodegroups_bad) 189 ToStdout("") 190 191 return 0 192 193 194 commands = { 195 "diagnose": ( 196 DiagnoseExtStorage, ARGS_NONE, [PRIORITY_OPT], 197 "", "Diagnose all ExtStorage providers"), 198 "info": ( 199 ShowExtStorageInfo, [ArgOs()], [PRIORITY_OPT], 200 "", "Show info about ExtStorage providers"), 201 } 202 203
204 -def Main():
205 return GenericMain(commands)
206