gnt-node 4.74 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#!/usr/bin/python
#

# Copyright (C) 2006, 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


import sys
from optparse import make_option

from ganeti.cli import *
from ganeti import opcodes
from ganeti import logger
from ganeti import utils


def AddNode(opts, args):
  """Add node cli-to-processor bridge."""
  op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip)
  SubmitOpCode(op)


def ListNodes(opts, args):
  """List nodes and their properties.

  """
  if opts.output is None:
    selected_fields = ["name", "dtotal", "dfree",
                       "mtotal", "mnode", "mfree",
                       "pinst", "sinst"]
  else:
    selected_fields = opts.output.split(",")

  op = opcodes.OpQueryNodes(output_fields=selected_fields)
  output = SubmitOpCode(op)

  mlens = [0 for name in selected_fields]
  format_fields = []
  unitformat_fields = ("dtotal", "dfree", "mtotal", "mnode", "mfree")
  for field in selected_fields:
    if field in ("dtotal", "dfree", "mtotal", "mnode",
                 "mfree", "pinst", "sinst"):
      format_fields.append("%*s")
    else:
      format_fields.append("%-*s")

  separator = opts.separator
  if "%" in separator:
    separator = separator.replace("%", "%%")
  format = separator.join(format_fields)

  for row in output:
    for idx, val in enumerate(row):
      if opts.human_readable and selected_fields[idx] in unitformat_fields:
        try:
          val = int(val)
        except ValueError:
          pass
        else:
          val = row[idx] = utils.FormatUnit(val)
      mlens[idx] = max(mlens[idx], len(val))

  if not opts.no_headers:
    header_list = {"name": "Node", "pinst": "Pinst", "sinst": "Sinst",
                   "pip": "PrimaryIP", "sip": "SecondaryIP",
                   "dtotal": "DTotal", "dfree": "DFree",
                   "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree"}
    args = []
    for idx, name in enumerate(selected_fields):
      hdr = header_list[name]
      mlens[idx] = max(mlens[idx], len(hdr))
      args.append(mlens[idx])
      args.append(hdr)
    logger.ToStdout(format % tuple(args))

  for row in output:
    args = []
    for idx, val in enumerate(row):
      args.append(mlens[idx])
      args.append(val)
    logger.ToStdout(format % tuple(args))

  return 0


def ShowNodeConfig(opts, args):
  """Show node information.

  """
  op = opcodes.OpQueryNodeData(nodes=args)
  result = SubmitOpCode(op)

  for name, primary_ip, secondary_ip, pinst, sinst in result:
    logger.ToStdout("Node name: %s" % name)
    logger.ToStdout("  primary ip: %s" % primary_ip)
    logger.ToStdout("  secondary ip: %s" % secondary_ip)
    if pinst:
      logger.ToStdout("  primary for instances:")
      for iname in pinst:
        logger.ToStdout("    - %s" % iname)
    else:
      logger.ToStdout("  primary for no instances")
    if sinst:
      logger.ToStdout("  secondary for instances:")
      for iname in sinst:
        logger.ToStdout("    - %s" % iname)
    else:
      logger.ToStdout("  secondary for no instances")

  return 0


def RemoveNode(opts, args):
  """Remove node cli-to-processor bridge."""
  op = opcodes.OpRemoveNode(node_name=args[0])
  SubmitOpCode(op)


commands = {
  'add': (AddNode, ARGS_ONE,
          [DEBUG_OPT,
           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"),
  'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
           "[<node_name>...]", "Show information about the node(s)"),
  'list': (ListNodes, ARGS_NONE,
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT,
            make_option("-o", "--output", dest="output", action="store",
                        type="string", help="Select output fields",
                        metavar="FIELDS")
            ],
           "", "Lists the nodes in the cluster"),
  'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
             "<node_name>", "Removes a node from the cluster"),
  }


if __name__ == '__main__':
  retcode = GenericMain(commands)
  sys.exit(retcode)