Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-node @ 2ec08468

History | View | Annotate | Download (4.9 kB)

1 a8083063 Iustin Pop
#!/usr/bin/python
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 a8083063 Iustin Pop
# Copyright (C) 2006, 2007 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 a8083063 Iustin Pop
import sys
23 a8083063 Iustin Pop
from optparse import make_option
24 a8083063 Iustin Pop
25 a8083063 Iustin Pop
from ganeti.cli import *
26 a8083063 Iustin Pop
from ganeti import opcodes
27 a8083063 Iustin Pop
from ganeti import logger
28 a8083063 Iustin Pop
from ganeti import utils
29 a8083063 Iustin Pop
30 a8083063 Iustin Pop
31 a8083063 Iustin Pop
def AddNode(opts, args):
32 a8083063 Iustin Pop
  """Add node cli-to-processor bridge."""
33 a8083063 Iustin Pop
  op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip)
34 a8083063 Iustin Pop
  SubmitOpCode(op)
35 a8083063 Iustin Pop
36 a8083063 Iustin Pop
37 a8083063 Iustin Pop
def ListNodes(opts, args):
38 a8083063 Iustin Pop
  """List nodes and their properties.
39 a8083063 Iustin Pop
40 a8083063 Iustin Pop
  """
41 a8083063 Iustin Pop
  if opts.output is None:
42 a8083063 Iustin Pop
    selected_fields = ["name", "dtotal", "dfree",
43 a8083063 Iustin Pop
                       "mtotal", "mnode", "mfree",
44 a8083063 Iustin Pop
                       "pinst", "sinst"]
45 a8083063 Iustin Pop
  else:
46 a8083063 Iustin Pop
    selected_fields = opts.output.split(",")
47 a8083063 Iustin Pop
48 a8083063 Iustin Pop
  op = opcodes.OpQueryNodes(output_fields=selected_fields)
49 a8083063 Iustin Pop
  output = SubmitOpCode(op)
50 a8083063 Iustin Pop
51 a8083063 Iustin Pop
  if not opts.no_headers:
52 137161c9 Michael Hanselmann
    headers = {"name": "Node", "pinst": "Pinst", "sinst": "Sinst",
53 137161c9 Michael Hanselmann
               "pip": "PrimaryIP", "sip": "SecondaryIP",
54 137161c9 Michael Hanselmann
               "dtotal": "DTotal", "dfree": "DFree",
55 137161c9 Michael Hanselmann
               "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree"}
56 137161c9 Michael Hanselmann
  else:
57 137161c9 Michael Hanselmann
    headers = None
58 137161c9 Michael Hanselmann
59 137161c9 Michael Hanselmann
  if opts.human_readable:
60 137161c9 Michael Hanselmann
    unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
61 137161c9 Michael Hanselmann
  else:
62 137161c9 Michael Hanselmann
    unitfields = None
63 137161c9 Michael Hanselmann
64 137161c9 Michael Hanselmann
  numfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree", "pinst", "sinst"]
65 137161c9 Michael Hanselmann
66 16be8703 Iustin Pop
  data = GenerateTable(separator=opts.separator, headers=headers,
67 16be8703 Iustin Pop
                       fields=selected_fields, unitfields=unitfields,
68 16be8703 Iustin Pop
                       numfields=numfields, data=output)
69 16be8703 Iustin Pop
  for line in data:
70 16be8703 Iustin Pop
    logger.ToStdout(line)
71 a8083063 Iustin Pop
72 a8083063 Iustin Pop
  return 0
73 a8083063 Iustin Pop
74 a8083063 Iustin Pop
75 a8083063 Iustin Pop
def ShowNodeConfig(opts, args):
76 a8083063 Iustin Pop
  """Show node information.
77 a8083063 Iustin Pop
78 a8083063 Iustin Pop
  """
79 a8083063 Iustin Pop
  op = opcodes.OpQueryNodeData(nodes=args)
80 a8083063 Iustin Pop
  result = SubmitOpCode(op)
81 a8083063 Iustin Pop
82 a8083063 Iustin Pop
  for name, primary_ip, secondary_ip, pinst, sinst in result:
83 a8083063 Iustin Pop
    logger.ToStdout("Node name: %s" % name)
84 a8083063 Iustin Pop
    logger.ToStdout("  primary ip: %s" % primary_ip)
85 a8083063 Iustin Pop
    logger.ToStdout("  secondary ip: %s" % secondary_ip)
86 a8083063 Iustin Pop
    if pinst:
87 a8083063 Iustin Pop
      logger.ToStdout("  primary for instances:")
88 a8083063 Iustin Pop
      for iname in pinst:
89 a8083063 Iustin Pop
        logger.ToStdout("    - %s" % iname)
90 a8083063 Iustin Pop
    else:
91 a8083063 Iustin Pop
      logger.ToStdout("  primary for no instances")
92 a8083063 Iustin Pop
    if sinst:
93 a8083063 Iustin Pop
      logger.ToStdout("  secondary for instances:")
94 a8083063 Iustin Pop
      for iname in sinst:
95 a8083063 Iustin Pop
        logger.ToStdout("    - %s" % iname)
96 a8083063 Iustin Pop
    else:
97 a8083063 Iustin Pop
      logger.ToStdout("  secondary for no instances")
98 a8083063 Iustin Pop
99 a8083063 Iustin Pop
  return 0
100 a8083063 Iustin Pop
101 a8083063 Iustin Pop
102 a8083063 Iustin Pop
def RemoveNode(opts, args):
103 a8083063 Iustin Pop
  """Remove node cli-to-processor bridge."""
104 a8083063 Iustin Pop
  op = opcodes.OpRemoveNode(node_name=args[0])
105 a8083063 Iustin Pop
  SubmitOpCode(op)
106 a8083063 Iustin Pop
107 a8083063 Iustin Pop
108 dcb93971 Michael Hanselmann
def ListVolumes(opts, args):
109 dcb93971 Michael Hanselmann
  """List logical volumes on node(s).
110 dcb93971 Michael Hanselmann
111 dcb93971 Michael Hanselmann
  """
112 dcb93971 Michael Hanselmann
  if opts.output is None:
113 dcb93971 Michael Hanselmann
    selected_fields = ["node", "phys", "vg",
114 dcb93971 Michael Hanselmann
                       "name", "size", "instance"]
115 dcb93971 Michael Hanselmann
  else:
116 dcb93971 Michael Hanselmann
    selected_fields = opts.output.split(",")
117 dcb93971 Michael Hanselmann
118 dcb93971 Michael Hanselmann
  op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
119 dcb93971 Michael Hanselmann
  output = SubmitOpCode(op)
120 dcb93971 Michael Hanselmann
121 dcb93971 Michael Hanselmann
  if not opts.no_headers:
122 137161c9 Michael Hanselmann
    headers = {"node": "Node", "phys": "PhysDev",
123 137161c9 Michael Hanselmann
               "vg": "VG", "name": "Name",
124 137161c9 Michael Hanselmann
               "size": "Size", "instance": "Instance"}
125 137161c9 Michael Hanselmann
  else:
126 137161c9 Michael Hanselmann
    headers = None
127 137161c9 Michael Hanselmann
128 137161c9 Michael Hanselmann
  if opts.human_readable:
129 137161c9 Michael Hanselmann
    unitfields = ["size"]
130 137161c9 Michael Hanselmann
  else:
131 137161c9 Michael Hanselmann
    unitfields = None
132 137161c9 Michael Hanselmann
133 137161c9 Michael Hanselmann
  numfields = ["size"]
134 137161c9 Michael Hanselmann
135 16be8703 Iustin Pop
  data = GenerateTable(separator=opts.separator, headers=headers,
136 16be8703 Iustin Pop
                       fields=selected_fields, unitfields=unitfields,
137 16be8703 Iustin Pop
                       numfields=numfields, data=output)
138 16be8703 Iustin Pop
139 16be8703 Iustin Pop
  for line in data:
140 16be8703 Iustin Pop
    logger.ToStdout(line)
141 dcb93971 Michael Hanselmann
142 dcb93971 Michael Hanselmann
  return 0
143 dcb93971 Michael Hanselmann
144 dcb93971 Michael Hanselmann
145 a8083063 Iustin Pop
commands = {
146 a8083063 Iustin Pop
  'add': (AddNode, ARGS_ONE,
147 a8083063 Iustin Pop
          [DEBUG_OPT,
148 a8083063 Iustin Pop
           make_option("-s", "--secondary-ip", dest="secondary_ip",
149 a8083063 Iustin Pop
                       help="Specify the secondary ip for the node",
150 a8083063 Iustin Pop
                       metavar="ADDRESS", default=None),],
151 a8083063 Iustin Pop
          "<node_name>", "Add a node to the cluster"),
152 a8083063 Iustin Pop
  'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
153 a8083063 Iustin Pop
           "[<node_name>...]", "Show information about the node(s)"),
154 a8083063 Iustin Pop
  'list': (ListNodes, ARGS_NONE,
155 dcb93971 Michael Hanselmann
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
156 a8083063 Iustin Pop
           "", "Lists the nodes in the cluster"),
157 a8083063 Iustin Pop
  'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
158 a8083063 Iustin Pop
             "<node_name>", "Removes a node from the cluster"),
159 dcb93971 Michael Hanselmann
  'volumes': (ListVolumes, ARGS_ANY,
160 dcb93971 Michael Hanselmann
              [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
161 dcb93971 Michael Hanselmann
              "[<node_name>...]", "List logical volumes on node(s)"),
162 a8083063 Iustin Pop
  }
163 a8083063 Iustin Pop
164 a8083063 Iustin Pop
165 a8083063 Iustin Pop
if __name__ == '__main__':
166 3ecf6786 Iustin Pop
  sys.exit(GenericMain(commands))