@UsesRPC
def AddNode(opts, args):
  """Add a node to the cluster.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the new node name
  @rtype: int
  @return: the desired exit code

  """
  cl = GetClient()
  node = netutils.GetHostname(name=args[0]).name
  readd = opts.readd

  # Retrieve relevant parameters of the node group.
  ssh_port = None
  try:
    # Passing [] to QueryGroups means query the default group:
    node_groups = [opts.nodegroup] if opts.nodegroup is not None else []
    output = cl.QueryGroups(names=node_groups, fields=["ndp/ssh_port"],
                            use_locking=False)
    (ssh_port, ) = output[0]
  except (errors.OpPrereqError, errors.OpExecError):
    pass

  try:
    output = cl.QueryNodes(names=[node],
                           fields=["name", "sip", "master",
                                   "ndp/ssh_port"],
                           use_locking=False)
    if len(output) == 0:
      node_exists = ""
      sip = None
    else:
      node_exists, sip, is_master, ssh_port = 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
    if is_master:
      ToStderr("Node %s is the master, cannot readd", node)
      return 1
  else:
    if node_exists:
      ToStderr("Node %s already in the cluster (as %s)"
               " - please retry with '--readd'", node, node_exists)
      return 1
    sip = opts.secondary_ip

  # read the cluster name from the master
  (cluster_name, ) = cl.QueryConfigValues(["cluster_name"])

  if not opts.node_setup:
    ToStdout("-- WARNING -- \n"
             "The option --no-node-setup is disabled. Whether or not the\n"
             "SSH setup is manipulated while adding a node is determined\n"
             "by the 'modify_ssh_setup' value in the cluster-wide\n"
             "configuration instead.\n")

  (modify_ssh_setup, ) = \
    cl.QueryConfigValues(["modify_ssh_setup"])

  if modify_ssh_setup:
    ToStderr("-- WARNING -- \n"
             "Performing this operation is going to perform the following\n"
             "changes to the target machine (%s) and the current cluster\n"
             "nodes:\n"
             "* A new SSH daemon key pair is generated on the target machine.\n"
             "* The public SSH keys of all master candidates of the cluster\n"
             "  are added to the target machine's 'authorized_keys' file.\n"
             "* In case the target machine is a master candidate, its newly\n"
             "  generated public SSH key will be distributed to all other\n"
             "  cluster nodes.\n", node)

  if modify_ssh_setup:
    _SetupSSH(opts, cluster_name, node, ssh_port, cl)

  bootstrap.SetupNodeDaemon(opts, cluster_name, node, ssh_port)

  if opts.disk_state:
    disk_state = utils.FlatToDict(opts.disk_state)
  else:
    disk_state = {}

  hv_state = dict(opts.hv_state)

  op = opcodes.OpNodeAdd(node_name=args[0], secondary_ip=sip,
                         readd=opts.readd, group=opts.nodegroup,
                         vm_capable=opts.vm_capable, ndparams=opts.ndparams,
                         master_capable=opts.master_capable,
                         disk_state=disk_state,
                         hv_state=hv_state,
                         node_setup=modify_ssh_setup,
                         verbose=opts.verbose,
                         debug=0 > opts.debug)
  SubmitOpCode(op, opts=opts)
dataset/ETHPy150Open ganeti/ganeti/lib/client/gnt_node.py/AddNode/swapped operands of `>`