master daemon: allow skipping the voting process
[ganeti-local] / scripts / gnt-node
index 606b7c8..a49eb3d 100755 (executable)
@@ -50,12 +50,12 @@ _LIST_HEADERS = {
   "dtotal": "DTotal", "dfree": "DFree",
   "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree",
   "bootid": "BootID",
-  "ctotal": "CTotal",
+  "ctotal": "CTotal", "cnodes": "CNodes", "csockets": "CSockets",
   "tags": "Tags",
   "serial_no": "SerialNo",
   "master_candidate": "MasterC",
   "master": "IsMaster",
-  "offline": "Offline",
+  "offline": "Offline", "drained": "Drained",
   }
 
 
@@ -73,31 +73,54 @@ def AddNode(opts, args):
   cl = GetClient()
   dns_data = utils.HostInfo(args[0])
   node = dns_data.name
-
-  if not opts.readd:
-    try:
-      output = cl.QueryNodes(names=[node], fields=['name'], use_locking=True)
-    except (errors.OpPrereqError, errors.OpExecError):
-      pass
-    else:
+  readd = opts.readd
+
+  try:
+    output = cl.QueryNodes(names=[node], fields=['name', 'sip'],
+                           use_locking=True)
+    node_exists, sip = output[0]
+  except (errors.OpPrereqError, errors.OpExecError):
+    node_exists = ""
+    sip = None
+
+  if readd:
+    if not node_exists:
+      ToStderr("Node %s not in the cluster"
+               " - please retry without '--readd'", node)
+      return 1
+  else:
+    if node_exists:
       ToStderr("Node %s already in the cluster (as %s)"
-               " - please use --readd", args[0], output[0][0])
+               " - please retry with '--readd'", node, node_exists)
       return 1
+    sip = opts.secondary_ip
 
   # read the cluster name from the master
   output = cl.QueryConfigValues(['cluster_name'])
   cluster_name = output[0]
 
-  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", node)
+  if readd:
+    # clear the offline and drain flags on the node
+    ToStdout("Resetting the 'offline' and 'drained' flags due to re-add")
+    op = opcodes.OpSetNodeParams(node_name=node, force=True,
+                                 offline=False, drained=False)
+
+    result = SubmitOpCode(op, cl=cl)
+    if result:
+      ToStdout("Modified:")
+      for param, data in result:
+        ToStdout(" - %-5s -> %s", param, data)
+  else:
+    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", node)
 
   bootstrap.SetupNodeDaemon(cluster_name, node, opts.ssh_key_check)
 
-  op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip,
+  op = opcodes.OpAddNode(node_name=args[0], secondary_ip=sip,
                          readd=opts.readd)
   SubmitOpCode(op)
 
@@ -140,7 +163,7 @@ def ListNodes(opts, args):
       val = row[idx]
       if field in list_type_fields:
         val = ",".join(val)
-      elif field in ('master', 'master_candidate', 'offline'):
+      elif field in ('master', 'master_candidate', 'offline', 'drained'):
         if val:
           val = 'Y'
         else:
@@ -323,22 +346,27 @@ def ShowNodeConfig(opts, args):
   """
   cl = GetClient()
   result = cl.QueryNodes(fields=["name", "pip", "sip",
-                                 "pinst_list", "sinst_list"],
+                                 "pinst_list", "sinst_list",
+                                 "master_candidate", "drained", "offline"],
                          names=args, use_locking=True)
 
-  for name, primary_ip, secondary_ip, pinst, sinst in result:
+  for (name, primary_ip, secondary_ip, pinst, sinst,
+       is_mc, drained, offline) in result:
     ToStdout("Node name: %s", name)
     ToStdout("  primary ip: %s", primary_ip)
     ToStdout("  secondary ip: %s", secondary_ip)
+    ToStdout("  master candidate: %s", is_mc)
+    ToStdout("  drained: %s", drained)
+    ToStdout("  offline: %s", offline)
     if pinst:
       ToStdout("  primary for instances:")
-      for iname in pinst:
+      for iname in utils.NiceSort(pinst):
         ToStdout("    - %s", iname)
     else:
       ToStdout("  primary for no instances")
     if sinst:
       ToStdout("  secondary for instances:")
-      for iname in sinst:
+      for iname in utils.NiceSort(sinst):
         ToStdout("    - %s", iname)
     else:
       ToStdout("  secondary for no instances")
@@ -414,7 +442,7 @@ def SetNodeParams(opts, args):
   @return: the desired exit code
 
   """
-  if opts.master_candidate is None and opts.offline is None:
+  if [opts.master_candidate, opts.drained, opts.offline].count(None) == 3:
     ToStderr("Please give at least one of the parameters.")
     return 1
 
@@ -426,9 +454,15 @@ def SetNodeParams(opts, args):
     offline = opts.offline == 'yes'
   else:
     offline = None
+
+  if opts.drained is not None:
+    drained = opts.drained == 'yes'
+  else:
+    drained = None
   op = opcodes.OpSetNodeParams(node_name=args[0],
                                master_candidate=candidate,
                                offline=offline,
+                               drained=drained,
                                force=opts.force)
 
   # even if here we process the result, we allow submit only
@@ -510,6 +544,9 @@ commands = {
               make_option("-O", "--offline", dest="offline",
                           choices=('yes', 'no'), default=None,
                           help="Set the offline flag on the node"),
+              make_option("-D", "--drained", dest="drained",
+                          choices=('yes', 'no'), default=None,
+                          help="Set the drained flag on the node"),
               ],
              "<instance>", "Alters the parameters of an instance"),
   'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],