Revision dcb93971 lib/cmdlib.py

b/lib/cmdlib.py
164 164
    return
165 165

  
166 166

  
167
def _GetWantedNodes(lu, nodes):
168
  if nodes is not None and not isinstance(nodes, list):
169
    raise errors.OpPrereqError, "Invalid argument type 'nodes'"
170

  
171
  if nodes:
172
    wanted_nodes = []
173

  
174
    for name in nodes:
175
      node = lu.cfg.GetNodeInfo(lu.cfg.ExpandNodeName(name))
176
      if node is None:
177
        raise errors.OpPrereqError, ("No such node name '%s'" % name)
178
    wanted_nodes.append(node)
179

  
180
    return wanted_nodes
181
  else:
182
    return [lu.cfg.GetNodeInfo(name) for name in lu.cfg.GetNodeList()]
183

  
184

  
185
def _CheckOutputFields(static, dynamic, selected):
186
    static_fields = frozenset(static)
187
    dynamic_fields = frozenset(dynamic)
188

  
189
    all_fields = static_fields | dynamic_fields
190

  
191
    if not all_fields.issuperset(selected):
192
      raise errors.OpPrereqError, ("Unknown output fields selected: %s"
193
                                   % ",".join(frozenset(selected).
194
                                              difference(all_fields)))
195

  
196

  
167 197
def _UpdateEtcHosts(fullnode, ip):
168 198
  """Ensure a node has a correct entry in /etc/hosts.
169 199

  
......
1028 1058
    This checks that the fields required are valid output fields.
1029 1059

  
1030 1060
    """
1031
    self.static_fields = frozenset(["name", "pinst", "sinst", "pip", "sip"])
1032 1061
    self.dynamic_fields = frozenset(["dtotal", "dfree",
1033 1062
                                     "mtotal", "mnode", "mfree"])
1034
    self.all_fields = self.static_fields | self.dynamic_fields
1035 1063

  
1036
    if not self.all_fields.issuperset(self.op.output_fields):
1037
      raise errors.OpPrereqError, ("Unknown output fields selected: %s"
1038
                                   % ",".join(frozenset(self.op.output_fields).
1039
                                              difference(self.all_fields)))
1064
    _CheckOutputFields(static=["name", "pinst", "sinst", "pip", "sip"],
1065
                       dynamic=self.dynamic_fields,
1066
                       selected=self.op.output_fields)
1040 1067

  
1041 1068

  
1042 1069
  def Exec(self, feedback_fn):
......
1106 1133
    return output
1107 1134

  
1108 1135

  
1136
class LUQueryNodeVolumes(NoHooksLU):
1137
  """Logical unit for getting volumes on node(s).
1138

  
1139
  """
1140
  _OP_REQP = ["nodes", "output_fields"]
1141

  
1142
  def CheckPrereq(self):
1143
    """Check prerequisites.
1144

  
1145
    This checks that the fields required are valid output fields.
1146

  
1147
    """
1148
    self.nodes = _GetWantedNodes(self, self.op.nodes)
1149

  
1150
    _CheckOutputFields(static=["node"],
1151
                       dynamic=["phys", "vg", "name", "size", "instance"],
1152
                       selected=self.op.output_fields)
1153

  
1154

  
1155
  def Exec(self, feedback_fn):
1156
    """Computes the list of nodes and their attributes.
1157

  
1158
    """
1159
    nodenames = utils.NiceSort([node.name for node in self.nodes])
1160
    volumes = rpc.call_node_volumes(nodenames)
1161

  
1162
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
1163
             in self.cfg.GetInstanceList()]
1164

  
1165
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
1166

  
1167
    output = []
1168
    for node in nodenames:
1169
      node_vols = volumes[node][:]
1170
      node_vols.sort(key=lambda vol: vol['dev'])
1171

  
1172
      for vol in node_vols:
1173
        node_output = []
1174
        for field in self.op.output_fields:
1175
          if field == "node":
1176
            val = node
1177
          elif field == "phys":
1178
            val = vol['dev']
1179
          elif field == "vg":
1180
            val = vol['vg']
1181
          elif field == "name":
1182
            val = vol['name']
1183
          elif field == "size":
1184
            val = int(float(vol['size']))
1185
          elif field == "instance":
1186
            for inst in ilist:
1187
              if node not in lv_by_node[inst]:
1188
                continue
1189
              if vol['name'] in lv_by_node[inst][node]:
1190
                val = inst.name
1191
                break
1192
            else:
1193
              val = '-'
1194
          else:
1195
            raise errors.ParameterError, field
1196
          node_output.append(str(val))
1197

  
1198
        output.append(node_output)
1199

  
1200
    return output
1201

  
1202

  
1109 1203
def _CheckNodesDirs(node_list, paths):
1110 1204
  """Verify if the given nodes have the same files.
1111 1205

  
......
1477 1571
    """
1478 1572
    if not os.path.exists(self.op.filename):
1479 1573
      raise errors.OpPrereqError("No such filename '%s'" % self.op.filename)
1480
    if self.op.nodes:
1481
      nodes = self.op.nodes
1482
    else:
1483
      nodes = self.cfg.GetNodeList()
1484
    self.nodes = []
1485
    for node in nodes:
1486
      nname = self.cfg.ExpandNodeName(node)
1487
      if nname is None:
1488
        raise errors.OpPrereqError, ("Node '%s' is unknown." % node)
1489
      self.nodes.append(nname)
1574

  
1575
    self.nodes = _GetWantedNodes(self, self.op.nodes)
1490 1576

  
1491 1577
  def Exec(self, feedback_fn):
1492 1578
    """Copy a file from master to some nodes.
......
1540 1626
    It checks that the given list of nodes is valid.
1541 1627

  
1542 1628
    """
1543
    if self.op.nodes:
1544
      nodes = self.op.nodes
1545
    else:
1546
      nodes = self.cfg.GetNodeList()
1547
    self.nodes = []
1548
    for node in nodes:
1549
      nname = self.cfg.ExpandNodeName(node)
1550
      if nname is None:
1551
        raise errors.OpPrereqError, ("Node '%s' is unknown." % node)
1552
      self.nodes.append(nname)
1629
    self.nodes = _GetWantedNodes(self, self.op.nodes)
1553 1630

  
1554 1631
  def Exec(self, feedback_fn):
1555 1632
    """Run a command on some nodes.
......
1557 1634
    """
1558 1635
    data = []
1559 1636
    for node in self.nodes:
1560
      result = utils.RunCmd(["ssh", node, self.op.command])
1561
      data.append((node, result.cmd, result.output, result.exit_code))
1637
      result = utils.RunCmd(["ssh", node.name, self.op.command])
1638
      data.append((node.name, result.cmd, result.output, result.exit_code))
1562 1639

  
1563 1640
    return data
1564 1641

  
......
1884 1961
  """Logical unit for querying instances.
1885 1962

  
1886 1963
  """
1887
  OP_REQP = ["output_fields"]
1964
  _OP_REQP = ["output_fields"]
1888 1965

  
1889 1966
  def CheckPrereq(self):
1890 1967
    """Check prerequisites.
......
1892 1969
    This checks that the fields required are valid output fields.
1893 1970

  
1894 1971
    """
1895

  
1896
    self.static_fields = frozenset(["name", "os", "pnode", "snodes",
1897
                                    "admin_state", "admin_ram",
1898
                                    "disk_template", "ip", "mac", "bridge"])
1899 1972
    self.dynamic_fields = frozenset(["oper_state", "oper_ram"])
1900
    self.all_fields = self.static_fields | self.dynamic_fields
1901

  
1902
    if not self.all_fields.issuperset(self.op.output_fields):
1903
      raise errors.OpPrereqError, ("Unknown output fields selected: %s"
1904
                                   % ",".join(frozenset(self.op.output_fields).
1905
                                              difference(self.all_fields)))
1973
    _CheckOutputFields(static=["name", "os", "pnode", "snodes",
1974
                               "admin_state", "admin_ram",
1975
                               "disk_template", "ip", "mac", "bridge"],
1976
                       dynamic=self.dynamic_fields,
1977
                       selected=self.op.output_fields)
1906 1978

  
1907 1979
  def Exec(self, feedback_fn):
1908 1980
    """Computes the list of nodes and their attributes.
......
3074 3146
    This only checks the optional node list against the existing names.
3075 3147

  
3076 3148
    """
3077
    if not isinstance(self.op.nodes, list):
3078
      raise errors.OpPrereqError, "Invalid argument type 'nodes'"
3079
    if self.op.nodes:
3080
      self.wanted_nodes = []
3081
      names = self.op.nodes
3082
      for name in names:
3083
        node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(name))
3084
        if node is None:
3085
          raise errors.OpPrereqError, ("No such node name '%s'" % name)
3086
      self.wanted_nodes.append(node)
3087
    else:
3088
      self.wanted_nodes = [self.cfg.GetNodeInfo(name) for name
3089
                           in self.cfg.GetNodeList()]
3090
    return
3149
    self.wanted_nodes = _GetWantedNodes(self, self.op.nodes)
3091 3150

  
3092 3151
  def Exec(self, feedback_fn):
3093 3152
    """Compute and return the list of nodes.
......
3214 3273
    """Check that the nodelist contains only existing nodes.
3215 3274

  
3216 3275
    """
3217
    nodes = getattr(self.op, "nodes", None)
3218
    if not nodes:
3219
      self.op.nodes = self.cfg.GetNodeList()
3220
    else:
3221
      expnodes = [self.cfg.ExpandNodeName(node) for node in nodes]
3222
      if expnodes.count(None) > 0:
3223
        raise errors.OpPrereqError, ("At least one of the given nodes %s"
3224
                                     " is unknown" % self.op.nodes)
3225
      self.op.nodes = expnodes
3276
    self.nodes = _GetWantedNodes(self, getattr(self.op, "nodes", None))
3226 3277

  
3227 3278
  def Exec(self, feedback_fn):
3228

  
3229 3279
    """Compute the list of all the exported system images.
3230 3280

  
3231 3281
    Returns:
......
3234 3284
      that node.
3235 3285

  
3236 3286
    """
3237
    return rpc.call_export_list(self.op.nodes)
3287
    return rpc.call_export_list([node.name for node in self.nodes])
3238 3288

  
3239 3289

  
3240 3290
class LUExportInstance(LogicalUnit):

Also available in: Unified diff