Check that we have a valid export list
[ganeti-local] / scripts / gnt-node
index 9fbc10a..8078af7 100755 (executable)
@@ -26,10 +26,16 @@ from ganeti.cli import *
 from ganeti import opcodes
 from ganeti import logger
 from ganeti import utils
+from ganeti import constants
+from ganeti import errors
 
 
 def AddNode(opts, args):
   """Add node cli-to-processor bridge."""
+  logger.ToStderr("-- WARNING -- \n"
+    "Performing this operation is going to replace the ssh daemon keypair\n"
+    "on the target machine (%s) with the ones of the current one\n"
+    "and grant full intra-cluster ssh root access to/from it\n" % args[0])
   op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip)
   SubmitOpCode(op)
 
@@ -45,7 +51,7 @@ def ListNodes(opts, args):
   else:
     selected_fields = opts.output.split(",")
 
-  op = opcodes.OpQueryNodes(output_fields=selected_fields, nodes=[])
+  op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[])
   output = SubmitOpCode(op)
 
   if not opts.no_headers:
@@ -53,7 +59,8 @@ def ListNodes(opts, args):
                "pinst_list": "PriInstances", "sinst_list": "SecInstances",
                "pip": "PrimaryIP", "sip": "SecondaryIP",
                "dtotal": "DTotal", "dfree": "DFree",
-               "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree"}
+               "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree",
+               "bootid": "BootID"}
   else:
     headers = None
 
@@ -87,11 +94,121 @@ def ListNodes(opts, args):
   return 0
 
 
+def EvacuateNode(opts, args):
+  """Relocate all secondary instance from a node.
+
+  """
+  force = opts.force
+  selected_fields = ["name", "sinst_list"]
+  src_node, dst_node = args
+
+  op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[src_node])
+  result = SubmitOpCode(op)
+  src_node, sinst = result[0]
+  op = opcodes.OpQueryNodes(output_fields=["name"], names=[dst_node])
+  result = SubmitOpCode(op)
+  dst_node = result[0][0]
+
+  if src_node == dst_node:
+    raise errors.OpPrereqError("Evacuate node needs different source and"
+                               " target nodes (node %s given twice)" %
+                               src_node)
+
+  if not sinst:
+    logger.ToStderr("No secondary instances on node %s, exiting." % src_node)
+    return constants.EXIT_SUCCESS
+
+  sinst = utils.NiceSort(sinst)
+
+  retcode = constants.EXIT_SUCCESS
+
+  if not force and not AskUser("Relocate instance(s) %s from node\n"
+                               " %s to node\n %s?" %
+                               (",".join("'%s'" % name for name in sinst),
+                               src_node, dst_node)):
+    return constants.EXIT_CONFIRMATION
+
+  good_cnt = bad_cnt = 0
+  for iname in sinst:
+    op = opcodes.OpReplaceDisks(instance_name=iname,
+                                remote_node=dst_node,
+                                mode=constants.REPLACE_DISK_ALL,
+                                disks=["sda", "sdb"])
+    try:
+      logger.ToStdout("Replacing disks for instance %s" % iname)
+      SubmitOpCode(op)
+      logger.ToStdout("Instance %s has been relocated" % iname)
+      good_cnt += 1
+    except errors.GenericError, err:
+      nret, msg = FormatError(err)
+      retcode |= nret
+      logger.ToStderr("Error replacing disks for instance %s: %s" %
+                      (iname, msg))
+      bad_cnt += 1
+
+  if retcode == constants.EXIT_SUCCESS:
+    logger.ToStdout("All %d instance(s) relocated successfully." % good_cnt)
+  else:
+    logger.ToStdout("There were errors during the relocation:\n"
+                    "%d error(s) out of %d instance(s)." %
+                    (bad_cnt, good_cnt + bad_cnt))
+  return retcode
+
+
+def FailoverNode(opts, args):
+  """Failover all primary instance on a node.
+
+  """
+  force = opts.force
+  selected_fields = ["name", "pinst_list"]
+
+  op = opcodes.OpQueryNodes(output_fields=selected_fields, names=args)
+  result = SubmitOpCode(op)
+  node, pinst = result[0]
+
+  if not pinst:
+    logger.ToStderr("No primary instances on node %s, exiting." % node)
+    return 0
+
+  pinst = utils.NiceSort(pinst)
+
+  retcode = 0
+
+  if not force and not AskUser("Fail over instance(s) %s?" %
+                               (",".join("'%s'" % name for name in pinst))):
+    return 2
+
+  good_cnt = bad_cnt = 0
+  for iname in pinst:
+    op = opcodes.OpFailoverInstance(instance_name=iname,
+                                    ignore_consistency=opts.ignore_consistency)
+    try:
+      logger.ToStdout("Failing over instance %s" % iname)
+      SubmitOpCode(op)
+      logger.ToStdout("Instance %s has been failed over" % iname)
+      good_cnt += 1
+    except errors.GenericError, err:
+      nret, msg = FormatError(err)
+      retcode |= nret
+      logger.ToStderr("Error failing over instance %s: %s" % (iname, msg))
+      bad_cnt += 1
+
+  if retcode == 0:
+    logger.ToStdout("All %d instance(s) failed over successfully." % good_cnt)
+  else:
+    logger.ToStdout("There were errors during the failover:\n"
+                    "%d error(s) out of %d instance(s)." %
+                    (bad_cnt, good_cnt + bad_cnt))
+  return retcode
+
+
 def ShowNodeConfig(opts, args):
   """Show node information.
 
   """
-  op = opcodes.OpQueryNodeData(nodes=args)
+  op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
+                                           "pinst_list", "sinst_list"],
+                            names=args)
   result = SubmitOpCode(op)
 
   for name, primary_ip, secondary_ip, pinst, sinst in result:
@@ -163,7 +280,22 @@ commands = {
            make_option("-s", "--secondary-ip", dest="secondary_ip",
                        help="Specify the secondary ip for the node",
                        metavar="ADDRESS", default=None),],
-          "<node_name>", "Add a node to the cluster"),
+          "[-s ip] <node_name>", "Add a node to the cluster"),
+  'evacuate': (EvacuateNode, ARGS_FIXED(2),
+               [DEBUG_OPT, FORCE_OPT],
+               "[-f] <src_node> <dst_node>",
+               "Relocate the secondary instances from the first node"
+               " to the second one (only for instances of type remote_raid1)"),
+  'failover': (FailoverNode, ARGS_ONE,
+               [DEBUG_OPT, FORCE_OPT,
+                make_option("--ignore-consistency", dest="ignore_consistency",
+                            action="store_true", default=False,
+                            help="Ignore the consistency of the disks on"
+                            " the secondary"),
+                ],
+               "[-f] <node>",
+               "Stops the primary instances on a node and start them on their"
+               " secondary node (only for instances of type remote_raid1)"),
   'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
            "[<node_name>...]", "Show information about the node(s)"),
   'list': (ListNodes, ARGS_NONE,
@@ -174,8 +306,14 @@ commands = {
   'volumes': (ListVolumes, ARGS_ANY,
               [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
               "[<node_name>...]", "List logical volumes on node(s)"),
+  'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
+                "<node_name>", "List the tags of the given node"),
+  'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
+               "<node_name> tag...", "Add tags to the given node"),
+  'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
+                  "<node_name> tag...", "Remove tags from the given node"),
   }
 
 
 if __name__ == '__main__':
-  sys.exit(GenericMain(commands))
+  sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE}))