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 logger.ToStderr("-- WARNING -- \n"
34 "Performing this operation is going to replace the ssh daemon keypair\n"
35 "on the target machine (%s) with the ones of the current one\n"
36 "and grant full intra-cluster ssh root access to/from it\n" % args[0])
37 op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip)
41 def ListNodes(opts, args):
42 """List nodes and their properties.
45 if opts.output is None:
46 selected_fields = ["name", "dtotal", "dfree",
47 "mtotal", "mnode", "mfree",
48 "pinst_cnt", "sinst_cnt"]
50 selected_fields = opts.output.split(",")
52 op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[])
53 output = SubmitOpCode(op)
55 if not opts.no_headers:
56 headers = {"name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
57 "pinst_list": "PriInstances", "sinst_list": "SecInstances",
58 "pip": "PrimaryIP", "sip": "SecondaryIP",
59 "dtotal": "DTotal", "dfree": "DFree",
60 "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree"}
64 if opts.human_readable:
65 unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
69 numfields = ["dtotal", "dfree",
70 "mtotal", "mnode", "mfree",
71 "pinst_cnt", "sinst_cnt"]
73 # change raw values to nicer strings
75 for idx, field in enumerate(selected_fields):
77 if field == "pinst_list":
79 elif field == "sinst_list":
85 data = GenerateTable(separator=opts.separator, headers=headers,
86 fields=selected_fields, unitfields=unitfields,
87 numfields=numfields, data=output)
94 def ShowNodeConfig(opts, args):
95 """Show node information.
98 op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
99 "pinst_list", "sinst_list"],
101 result = SubmitOpCode(op)
103 for name, primary_ip, secondary_ip, pinst, sinst in result:
104 logger.ToStdout("Node name: %s" % name)
105 logger.ToStdout(" primary ip: %s" % primary_ip)
106 logger.ToStdout(" secondary ip: %s" % secondary_ip)
108 logger.ToStdout(" primary for instances:")
110 logger.ToStdout(" - %s" % iname)
112 logger.ToStdout(" primary for no instances")
114 logger.ToStdout(" secondary for instances:")
116 logger.ToStdout(" - %s" % iname)
118 logger.ToStdout(" secondary for no instances")
123 def RemoveNode(opts, args):
124 """Remove node cli-to-processor bridge."""
125 op = opcodes.OpRemoveNode(node_name=args[0])
129 def ListVolumes(opts, args):
130 """List logical volumes on node(s).
133 if opts.output is None:
134 selected_fields = ["node", "phys", "vg",
135 "name", "size", "instance"]
137 selected_fields = opts.output.split(",")
139 op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
140 output = SubmitOpCode(op)
142 if not opts.no_headers:
143 headers = {"node": "Node", "phys": "PhysDev",
144 "vg": "VG", "name": "Name",
145 "size": "Size", "instance": "Instance"}
149 if opts.human_readable:
150 unitfields = ["size"]
156 data = GenerateTable(separator=opts.separator, headers=headers,
157 fields=selected_fields, unitfields=unitfields,
158 numfields=numfields, data=output)
161 logger.ToStdout(line)
167 'add': (AddNode, ARGS_ONE,
169 make_option("-s", "--secondary-ip", dest="secondary_ip",
170 help="Specify the secondary ip for the node",
171 metavar="ADDRESS", default=None),],
172 "<node_name>", "Add a node to the cluster"),
173 'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
174 "[<node_name>...]", "Show information about the node(s)"),
175 'list': (ListNodes, ARGS_NONE,
176 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
177 "", "Lists the nodes in the cluster"),
178 'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
179 "<node_name>", "Removes a node from the cluster"),
180 'volumes': (ListVolumes, ARGS_ANY,
181 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
182 "[<node_name>...]", "List logical volumes on node(s)"),
186 if __name__ == '__main__':
187 sys.exit(GenericMain(commands))