gnt-node 6.21 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
#!/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
29
from ganeti import constants
Iustin Pop's avatar
Iustin Pop committed
30 31 32 33


def AddNode(opts, args):
  """Add node cli-to-processor bridge."""
34 35 36 37
  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])
Iustin Pop's avatar
Iustin Pop committed
38 39 40 41 42 43 44 45 46 47 48
  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",
49
                       "pinst_cnt", "sinst_cnt"]
Iustin Pop's avatar
Iustin Pop committed
50 51 52
  else:
    selected_fields = opts.output.split(",")

53
  op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[])
Iustin Pop's avatar
Iustin Pop committed
54 55 56
  output = SubmitOpCode(op)

  if not opts.no_headers:
57 58
    headers = {"name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
               "pinst_list": "PriInstances", "sinst_list": "SecInstances",
59 60
               "pip": "PrimaryIP", "sip": "SecondaryIP",
               "dtotal": "DTotal", "dfree": "DFree",
61 62
               "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree",
               "bootid": "BootID"}
63 64 65 66 67 68 69 70
  else:
    headers = None

  if opts.human_readable:
    unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
  else:
    unitfields = None

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  numfields = ["dtotal", "dfree",
               "mtotal", "mnode", "mfree",
               "pinst_cnt", "sinst_cnt"]

  # change raw values to nicer strings
  for row in output:
    for idx, field in enumerate(selected_fields):
      val = row[idx]
      if field == "pinst_list":
        val = ",".join(val)
      elif field == "sinst_list":
        val = ",".join(val)
      elif val is None:
        val = "?"
      row[idx] = str(val)
86

87 88 89 90 91
  data = GenerateTable(separator=opts.separator, headers=headers,
                       fields=selected_fields, unitfields=unitfields,
                       numfields=numfields, data=output)
  for line in data:
    logger.ToStdout(line)
Iustin Pop's avatar
Iustin Pop committed
92 93 94 95 96 97 98 99

  return 0


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

  """
100 101
  op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
                                           "pinst_list", "sinst_list"],
102
                            names=args)
Iustin Pop's avatar
Iustin Pop committed
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
  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)


131 132 133 134 135 136 137 138 139 140 141 142 143 144
def ListVolumes(opts, args):
  """List logical volumes on node(s).

  """
  if opts.output is None:
    selected_fields = ["node", "phys", "vg",
                       "name", "size", "instance"]
  else:
    selected_fields = opts.output.split(",")

  op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
  output = SubmitOpCode(op)

  if not opts.no_headers:
145 146 147 148 149 150 151 152 153 154 155 156 157
    headers = {"node": "Node", "phys": "PhysDev",
               "vg": "VG", "name": "Name",
               "size": "Size", "instance": "Instance"}
  else:
    headers = None

  if opts.human_readable:
    unitfields = ["size"]
  else:
    unitfields = None

  numfields = ["size"]

158 159 160 161 162 163
  data = GenerateTable(separator=opts.separator, headers=headers,
                       fields=selected_fields, unitfields=unitfields,
                       numfields=numfields, data=output)

  for line in data:
    logger.ToStdout(line)
164 165 166 167

  return 0


Iustin Pop's avatar
Iustin Pop committed
168 169 170 171 172 173 174 175 176 177
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,
178
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
Iustin Pop's avatar
Iustin Pop committed
179 180 181
           "", "Lists the nodes in the cluster"),
  'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
             "<node_name>", "Removes a node from the cluster"),
182 183 184
  'volumes': (ListVolumes, ARGS_ANY,
              [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
              "[<node_name>...]", "List logical volumes on node(s)"),
185 186
  'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
                "<node_name>", "List the tags of the given node"),
187
  'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
188
               "<node_name> tag...", "Add tags to the given node"),
189
  'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
190
                  "<node_name> tag...", "Remove tags from the given node"),
Iustin Pop's avatar
Iustin Pop committed
191 192 193 194
  }


if __name__ == '__main__':
195
  sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE}))