4 # Copyright (C) 2006, 2007 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 from optparse import make_option
25 from ganeti.cli import *
26 from ganeti import opcodes
27 from ganeti import logger
28 from ganeti import utils
31 def AddNode(opts, args):
32 """Add node cli-to-processor bridge."""
33 op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip)
37 def ListNodes(opts, args):
38 """List nodes and their properties.
41 if opts.output is None:
42 selected_fields = ["name", "dtotal", "dfree",
43 "mtotal", "mnode", "mfree",
46 selected_fields = opts.output.split(",")
48 op = opcodes.OpQueryNodes(output_fields=selected_fields)
49 output = SubmitOpCode(op)
51 mlens = [0 for name in selected_fields]
53 unitformat_fields = ("dtotal", "dfree", "mtotal", "mnode", "mfree")
54 for field in selected_fields:
55 if field in ("dtotal", "dfree", "mtotal", "mnode",
56 "mfree", "pinst", "sinst"):
57 format_fields.append("%*s")
59 format_fields.append("%-*s")
61 separator = opts.separator
63 separator = separator.replace("%", "%%")
64 format = separator.join(format_fields)
67 for idx, val in enumerate(row):
68 if opts.human_readable and selected_fields[idx] in unitformat_fields:
74 val = row[idx] = utils.FormatUnit(val)
75 mlens[idx] = max(mlens[idx], len(val))
77 if not opts.no_headers:
78 header_list = {"name": "Node", "pinst": "Pinst", "sinst": "Sinst",
79 "pip": "PrimaryIP", "sip": "SecondaryIP",
80 "dtotal": "DTotal", "dfree": "DFree",
81 "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree"}
83 for idx, name in enumerate(selected_fields):
84 hdr = header_list[name]
85 mlens[idx] = max(mlens[idx], len(hdr))
86 args.append(mlens[idx])
88 logger.ToStdout(format % tuple(args))
92 for idx, val in enumerate(row):
93 args.append(mlens[idx])
95 logger.ToStdout(format % tuple(args))
100 def ShowNodeConfig(opts, args):
101 """Show node information.
104 op = opcodes.OpQueryNodeData(nodes=args)
105 result = SubmitOpCode(op)
107 for name, primary_ip, secondary_ip, pinst, sinst in result:
108 logger.ToStdout("Node name: %s" % name)
109 logger.ToStdout(" primary ip: %s" % primary_ip)
110 logger.ToStdout(" secondary ip: %s" % secondary_ip)
112 logger.ToStdout(" primary for instances:")
114 logger.ToStdout(" - %s" % iname)
116 logger.ToStdout(" primary for no instances")
118 logger.ToStdout(" secondary for instances:")
120 logger.ToStdout(" - %s" % iname)
122 logger.ToStdout(" secondary for no instances")
127 def RemoveNode(opts, args):
128 """Remove node cli-to-processor bridge."""
129 op = opcodes.OpRemoveNode(node_name=args[0])
133 def ListVolumes(opts, args):
134 """List logical volumes on node(s).
137 if opts.output is None:
138 selected_fields = ["node", "phys", "vg",
139 "name", "size", "instance"]
141 selected_fields = opts.output.split(",")
143 op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
144 output = SubmitOpCode(op)
146 mlens = [0 for name in selected_fields]
148 unitformat_fields = ("size",)
149 for field in selected_fields:
150 if field in unitformat_fields:
151 format_fields.append("%*s")
153 format_fields.append("%-*s")
155 separator = opts.separator
157 separator = separator.replace("%", "%%")
158 format = separator.join(format_fields)
161 for idx, val in enumerate(row):
162 if opts.human_readable and selected_fields[idx] in unitformat_fields:
168 val = row[idx] = utils.FormatUnit(val)
169 mlens[idx] = max(mlens[idx], len(val))
171 if not opts.no_headers:
172 header_list = {"node": "Node", "phys": "PhysDev",
173 "vg": "VG", "name": "Name",
174 "size": "Size", "instance": "Instance"}
176 for idx, name in enumerate(selected_fields):
177 hdr = header_list[name]
178 mlens[idx] = max(mlens[idx], len(hdr))
179 args.append(mlens[idx])
181 logger.ToStdout(format % tuple(args))
185 for idx, val in enumerate(row):
186 args.append(mlens[idx])
188 logger.ToStdout(format % tuple(args))
194 'add': (AddNode, ARGS_ONE,
196 make_option("-s", "--secondary-ip", dest="secondary_ip",
197 help="Specify the secondary ip for the node",
198 metavar="ADDRESS", default=None),],
199 "<node_name>", "Add a node to the cluster"),
200 'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
201 "[<node_name>...]", "Show information about the node(s)"),
202 'list': (ListNodes, ARGS_NONE,
203 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
204 "", "Lists the nodes in the cluster"),
205 'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
206 "<node_name>", "Removes a node from the cluster"),
207 'volumes': (ListVolumes, ARGS_ANY,
208 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
209 "[<node_name>...]", "List logical volumes on node(s)"),
213 if __name__ == '__main__':
214 retcode = GenericMain(commands)