Remove quotes from CommaJoin and convert to it
[ganeti-local] / scripts / gnt-node
index 75dca86..7e3e439 100755 (executable)
@@ -41,6 +41,19 @@ _LIST_DEF_FIELDS = [
   "pinst_cnt", "sinst_cnt",
   ]
 
+
+#: default list of field for L{ListStorage}
+_LIST_STOR_DEF_FIELDS = [
+  constants.SF_NODE,
+  constants.SF_TYPE,
+  constants.SF_NAME,
+  constants.SF_SIZE,
+  constants.SF_USED,
+  constants.SF_FREE,
+  constants.SF_ALLOCATABLE,
+  ]
+
+
 #: headers (and full field list for L{ListNodes}
 _LIST_HEADERS = {
   "name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
@@ -56,9 +69,22 @@ _LIST_HEADERS = {
   "master": "IsMaster",
   "offline": "Offline", "drained": "Drained",
   "role": "Role",
-  "ctime": "CTime", "mtime": "MTime",
+  "ctime": "CTime", "mtime": "MTime", "uuid": "UUID"
+  }
+
+
+#: headers (and full field list for L{ListStorage}
+_LIST_STOR_HEADERS = {
+  constants.SF_NODE: "Node",
+  constants.SF_TYPE: "Type",
+  constants.SF_NAME: "Name",
+  constants.SF_SIZE: "Size",
+  constants.SF_USED: "Used",
+  constants.SF_FREE: "Free",
+  constants.SF_ALLOCATABLE: "Allocatable",
   }
 
+
 #: User-facing storage unit types
 _USER_STORAGE_TYPE = {
   constants.ST_FILE: "file",
@@ -67,7 +93,7 @@ _USER_STORAGE_TYPE = {
   }
 
 _STORAGE_TYPE_OPT = \
-  cli_option("--storage-type",
+  cli_option("-t", "--storage-type",
              dest="user_storage_type",
              choices=_USER_STORAGE_TYPE.keys(),
              default=None,
@@ -89,7 +115,8 @@ def ConvertStorageType(user_storage_type):
   try:
     return _USER_STORAGE_TYPE[user_storage_type]
   except KeyError:
-    raise errors.OpPrereqError("Unknown storage type: %s" % user_storage_type)
+    raise errors.OpPrereqError("Unknown storage type: %s" % user_storage_type,
+                               errors.ECODE_INVAL)
 
 
 @UsesRPC
@@ -104,7 +131,7 @@ def AddNode(opts, args):
 
   """
   cl = GetClient()
-  dns_data = utils.HostInfo(args[0])
+  dns_data = utils.GetHostInfo(args[0])
   node = dns_data.name
   readd = opts.readd
 
@@ -224,7 +251,7 @@ def EvacuateNode(opts, args):
   cnt = [dst_node, iallocator].count(None)
   if cnt != 1:
     raise errors.OpPrereqError("One and only one of the -n and -I"
-                               " options must be passed")
+                               " options must be passed", errors.ECODE_INVAL)
 
   selected_fields = ["name", "sinst_list"]
   src_node = args[0]
@@ -245,7 +272,7 @@ def EvacuateNode(opts, args):
     if src_node == dst_node:
       raise errors.OpPrereqError("Evacuate node needs different source and"
                                  " target nodes (node %s given twice)" %
-                                 src_node)
+                                 src_node, errors.ECODE_INVAL)
     txt_msg = "to node %s" % dst_node
   else:
     txt_msg = "using iallocator %s" % iallocator
@@ -459,7 +486,7 @@ def ListVolumes(opts, args):
   return 0
 
 
-def ListPhysicalVolumes(opts, args):
+def ListStorage(opts, args):
   """List physical volumes on node(s).
 
   @param opts: the command line options selected by the user
@@ -477,27 +504,10 @@ def ListPhysicalVolumes(opts, args):
 
   storage_type = ConvertStorageType(opts.user_storage_type)
 
-  default_fields = {
-    constants.ST_FILE: [
-      constants.SF_NAME,
-      constants.SF_USED,
-      constants.SF_FREE,
-      ],
-    constants.ST_LVM_PV: [
-      constants.SF_NAME,
-      constants.SF_SIZE,
-      constants.SF_USED,
-      constants.SF_FREE,
-      ],
-    constants.ST_LVM_VG: [
-      constants.SF_NAME,
-      constants.SF_SIZE,
-      ],
-  }
-
   if opts.output is None:
-    selected_fields = ["node"]
-    selected_fields.extend(default_fields[storage_type])
+    selected_fields = _LIST_STOR_DEF_FIELDS
+  elif opts.output.startswith("+"):
+    selected_fields = _LIST_STOR_DEF_FIELDS + opts.output[1:].split(",")
   else:
     selected_fields = opts.output.split(",")
 
@@ -508,7 +518,8 @@ def ListPhysicalVolumes(opts, args):
 
   if not opts.no_headers:
     headers = {
-      "node": "Node",
+      constants.SF_NODE: "Node",
+      constants.SF_TYPE: "Type",
       constants.SF_NAME: "Name",
       constants.SF_SIZE: "Size",
       constants.SF_USED: "Used",
@@ -521,6 +532,17 @@ def ListPhysicalVolumes(opts, args):
   unitfields = [constants.SF_SIZE, constants.SF_USED, constants.SF_FREE]
   numfields = [constants.SF_SIZE, constants.SF_USED, constants.SF_FREE]
 
+  # change raw values to nicer strings
+  for row in output:
+    for idx, field in enumerate(selected_fields):
+      val = row[idx]
+      if field == constants.SF_ALLOCATABLE:
+        if val:
+          val = "Y"
+        else:
+          val = "N"
+      row[idx] = str(val)
+
   data = GenerateTable(separator=opts.separator, headers=headers,
                        fields=selected_fields, unitfields=unitfields,
                        numfields=numfields, data=output, units=opts.units)
@@ -531,7 +553,7 @@ def ListPhysicalVolumes(opts, args):
   return 0
 
 
-def ModifyVolume(opts, args):
+def ModifyStorage(opts, args):
   """Modify storage volume on a node.
 
   @param opts: the command line options selected by the user
@@ -556,9 +578,11 @@ def ModifyVolume(opts, args):
                                      name=volume_name,
                                      changes=changes)
     SubmitOpCode(op)
+  else:
+    ToStderr("No changes to perform, exiting.")
 
 
-def RepairVolume(opts, args):
+def RepairStorage(opts, args):
   """Repairs a storage volume on a node.
 
   @param opts: the command line options selected by the user
@@ -574,7 +598,8 @@ def RepairVolume(opts, args):
 
   op = opcodes.OpRepairNodeStorage(node_name=node_name,
                                    storage_type=storage_type,
-                                   name=volume_name)
+                                   name=volume_name,
+                                   ignore_consistency=opts.ignore_consistency)
   SubmitOpCode(op)
 
 
@@ -622,101 +647,82 @@ def SetNodeParams(opts, args):
 
 
 commands = {
-  'add': (AddNode, [ArgHost(min=1, max=1)],
-          [DEBUG_OPT,
-           cli_option("-s", "--secondary-ip", dest="secondary_ip",
-                      help="Specify the secondary ip for the node",
-                      metavar="ADDRESS", default=None),
-           cli_option("--readd", dest="readd",
-                      default=False, action="store_true",
-                      help="Readd old node after replacing it"),
-           cli_option("--no-ssh-key-check", dest="ssh_key_check",
-                      default=True, action="store_false",
-                      help="Disable SSH key fingerprint checking"),
-           ],
-          "[-s ip] [--readd] [--no-ssh-key-check] <node_name>",
-          "Add a node to the cluster"),
-  'evacuate': (EvacuateNode, ARGS_ONE_NODE,
-               [DEBUG_OPT, FORCE_OPT, IALLOCATOR_OPT, NEW_SECONDARY_OPT],
-               "[-f] {-I <iallocator> | -n <dst>} <node>",
-               "Relocate the secondary instances from a node"
-               " to other nodes (only for instances with drbd disk template)"),
-  'failover': (FailoverNode, ARGS_ONE_NODE,
-               [DEBUG_OPT, FORCE_OPT, IGNORE_CONSIST_OPT],
-               "[-f] <node>",
-               "Stops the primary instances on a node and start them on their"
-               " secondary node (only for instances with drbd disk template)"),
-  'migrate': (MigrateNode, ARGS_ONE_NODE,
-               [DEBUG_OPT, FORCE_OPT, NONLIVE_OPT],
-               "[-f] <node>",
-               "Migrate all the primary instance on a node away from it"
-               " (only for instances of type drbd)"),
-  'info': (ShowNodeConfig, ARGS_MANY_NODES, [DEBUG_OPT],
-           "[<node_name>...]", "Show information about the node(s)"),
-  'list': (ListNodes, ARGS_MANY_NODES,
-           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, SYNC_OPT],
-           "[nodes...]",
-           "Lists the nodes in the cluster. The available fields"
-           " are (see the man page for details): %s"
-           " The default field list is (in order): %s." %
-           (", ".join(_LIST_HEADERS), ", ".join(_LIST_DEF_FIELDS))),
-  'modify': (SetNodeParams, ARGS_ONE_NODE,
-             [DEBUG_OPT, FORCE_OPT,
-              SUBMIT_OPT,
-              cli_option("-C", "--master-candidate", dest="master_candidate",
-                         choices=('yes', 'no'), default=None,
-                         metavar="yes|no",
-                         help="Set the master_candidate flag on the node"),
-              cli_option("-O", "--offline", dest="offline", metavar="yes|no",
-                         choices=('yes', 'no'), default=None,
-                         help="Set the offline flag on the node"),
-              cli_option("-D", "--drained", dest="drained", metavar="yes|no",
-                         choices=('yes', 'no'), default=None,
-                         help="Set the drained flag on the node"),
-              ],
-             "<node_name>", "Alters the parameters of a node"),
-  'powercycle': (PowercycleNode, ARGS_ONE_NODE,
-                 [DEBUG_OPT, FORCE_OPT, CONFIRM_OPT],
-                 "<node_name>", "Tries to forcefully powercycle a node"),
-  'remove': (RemoveNode, ARGS_ONE_NODE, [DEBUG_OPT],
-             "<node_name>", "Removes a node from the cluster"),
-  'volumes': (ListVolumes, [ArgNode()],
-              [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
-              "[<node_name>...]", "List logical volumes on node(s)"),
-  'physical-volumes': (ListPhysicalVolumes, ARGS_MANY_NODES,
-                       [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT,
-                        FIELDS_OPT, _STORAGE_TYPE_OPT],
-                       "[<node_name>...]",
-                       "List physical volumes on node(s)"),
-  'modify-volume': (ModifyVolume,
-                    [ArgNode(min=1, max=1),
-                     ArgChoice(min=1, max=1,
-                               choices=_MODIFIABLE_STORAGE_TYPES),
-                     ArgFile(min=1, max=1)],
-                    [DEBUG_OPT,
-                     cli_option("--allocatable", dest="allocatable",
-                                choices=["yes", "no"], default=None,
-                                metavar="yes|no",
-                                help="Set the allocatable flag on a volume"),
-                     ],
-                    "<node_name> <storage_type> <name>",
-                    "Modify storage volume on a node"),
-  'repair-volume': (RepairVolume,
-                    [ArgNode(min=1, max=1),
-                     ArgChoice(min=1, max=1,
-                               choices=_REPAIRABLE_STORAGE_TYPES),
-                     ArgFile(min=1, max=1)],
-                    [DEBUG_OPT],
-                    "<node_name> <storage_type> <name>",
-                    "Repairs a storage volume on a node"),
-  'list-tags': (ListTags, ARGS_ONE_NODE, [DEBUG_OPT],
-                "<node_name>", "List the tags of the given node"),
-  'add-tags': (AddTags, [ArgNode(min=1, max=1), ArgUnknown()],
-               [DEBUG_OPT, TAG_SRC_OPT],
-               "<node_name> tag...", "Add tags to the given node"),
-  'remove-tags': (RemoveTags, [ArgNode(min=1, max=1), ArgUnknown()],
-                  [DEBUG_OPT, TAG_SRC_OPT],
-                  "<node_name> tag...", "Remove tags from the given node"),
+  'add': (
+    AddNode, [ArgHost(min=1, max=1)],
+    [SECONDARY_IP_OPT, READD_OPT, NOSSH_KEYCHECK_OPT],
+    "[-s ip] [--readd] [--no-ssh-key-check] <node_name>",
+    "Add a node to the cluster"),
+  'evacuate': (
+    EvacuateNode, ARGS_ONE_NODE,
+    [FORCE_OPT, IALLOCATOR_OPT, NEW_SECONDARY_OPT],
+    "[-f] {-I <iallocator> | -n <dst>} <node>",
+    "Relocate the secondary instances from a node"
+    " to other nodes (only for instances with drbd disk template)"),
+  'failover': (
+    FailoverNode, ARGS_ONE_NODE, [FORCE_OPT, IGNORE_CONSIST_OPT],
+    "[-f] <node>",
+    "Stops the primary instances on a node and start them on their"
+    " secondary node (only for instances with drbd disk template)"),
+  'migrate': (
+    MigrateNode, ARGS_ONE_NODE, [FORCE_OPT, NONLIVE_OPT],
+    "[-f] <node>",
+    "Migrate all the primary instance on a node away from it"
+    " (only for instances of type drbd)"),
+  'info': (
+    ShowNodeConfig, ARGS_MANY_NODES, [],
+    "[<node_name>...]", "Show information about the node(s)"),
+  'list': (
+    ListNodes, ARGS_MANY_NODES,
+    [NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, SYNC_OPT],
+    "[nodes...]",
+    "Lists the nodes in the cluster. The available fields are (see the man"
+    " page for details): %s. The default field list is (in order): %s." %
+    (utils.CommaJoin(_LIST_HEADERS), utils.CommaJoin(_LIST_DEF_FIELDS))),
+  'modify': (
+    SetNodeParams, ARGS_ONE_NODE,
+    [FORCE_OPT, SUBMIT_OPT, MC_OPT, DRAINED_OPT, OFFLINE_OPT],
+    "<node_name>", "Alters the parameters of a node"),
+  'powercycle': (
+    PowercycleNode, ARGS_ONE_NODE,
+    [FORCE_OPT, CONFIRM_OPT],
+    "<node_name>", "Tries to forcefully powercycle a node"),
+  'remove': (
+    RemoveNode, ARGS_ONE_NODE, [],
+    "<node_name>", "Removes a node from the cluster"),
+  'volumes': (
+    ListVolumes, [ArgNode()],
+    [NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
+    "[<node_name>...]", "List logical volumes on node(s)"),
+  'list-storage': (
+    ListStorage, ARGS_MANY_NODES,
+    [NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, _STORAGE_TYPE_OPT],
+    "[<node_name>...]", "List physical volumes on node(s). The available"
+    " fields are (see the man page for details): %s." %
+    (utils.CommaJoin(_LIST_STOR_HEADERS))),
+  'modify-storage': (
+    ModifyStorage,
+    [ArgNode(min=1, max=1),
+     ArgChoice(min=1, max=1, choices=_MODIFIABLE_STORAGE_TYPES),
+     ArgFile(min=1, max=1)],
+    [ALLOCATABLE_OPT],
+    "<node_name> <storage_type> <name>", "Modify storage volume on a node"),
+  'repair-storage': (
+    RepairStorage,
+    [ArgNode(min=1, max=1),
+     ArgChoice(min=1, max=1, choices=_REPAIRABLE_STORAGE_TYPES),
+     ArgFile(min=1, max=1)],
+    [IGNORE_CONSIST_OPT],
+    "<node_name> <storage_type> <name>",
+    "Repairs a storage volume on a node"),
+  'list-tags': (
+    ListTags, ARGS_ONE_NODE, [],
+    "<node_name>", "List the tags of the given node"),
+  'add-tags': (
+    AddTags, [ArgNode(min=1, max=1), ArgUnknown()], [TAG_SRC_OPT],
+    "<node_name> tag...", "Add tags to the given node"),
+  'remove-tags': (
+    RemoveTags, [ArgNode(min=1, max=1), ArgUnknown()], [TAG_SRC_OPT],
+    "<node_name> tag...", "Remove tags from the given node"),
   }