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
29 from ganeti import constants
32 def AddNode(opts, args):
33 """Add node cli-to-processor bridge."""
34 logger.ToStderr("-- WARNING -- \n"
35 "Performing this operation is going to replace the ssh daemon keypair\n"
36 "on the target machine (%s) with the ones of the current one\n"
37 "and grant full intra-cluster ssh root access to/from it\n" % args[0])
38 op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip)
42 def ListNodes(opts, args):
43 """List nodes and their properties.
46 if opts.output is None:
47 selected_fields = ["name", "dtotal", "dfree",
48 "mtotal", "mnode", "mfree",
49 "pinst_cnt", "sinst_cnt"]
51 selected_fields = opts.output.split(",")
53 op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[])
54 output = SubmitOpCode(op)
56 if not opts.no_headers:
57 headers = {"name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
58 "pinst_list": "PriInstances", "sinst_list": "SecInstances",
59 "pip": "PrimaryIP", "sip": "SecondaryIP",
60 "dtotal": "DTotal", "dfree": "DFree",
61 "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree"}
65 if opts.human_readable:
66 unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
70 numfields = ["dtotal", "dfree",
71 "mtotal", "mnode", "mfree",
72 "pinst_cnt", "sinst_cnt"]
74 # change raw values to nicer strings
76 for idx, field in enumerate(selected_fields):
78 if field == "pinst_list":
80 elif field == "sinst_list":
86 data = GenerateTable(separator=opts.separator, headers=headers,
87 fields=selected_fields, unitfields=unitfields,
88 numfields=numfields, data=output)
95 def ShowNodeConfig(opts, args):
96 """Show node information.
99 op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
100 "pinst_list", "sinst_list"],
102 result = SubmitOpCode(op)
104 for name, primary_ip, secondary_ip, pinst, sinst in result:
105 logger.ToStdout("Node name: %s" % name)
106 logger.ToStdout(" primary ip: %s" % primary_ip)
107 logger.ToStdout(" secondary ip: %s" % secondary_ip)
109 logger.ToStdout(" primary for instances:")
111 logger.ToStdout(" - %s" % iname)
113 logger.ToStdout(" primary for no instances")
115 logger.ToStdout(" secondary for instances:")
117 logger.ToStdout(" - %s" % iname)
119 logger.ToStdout(" secondary for no instances")
124 def RemoveNode(opts, args):
125 """Remove node cli-to-processor bridge."""
126 op = opcodes.OpRemoveNode(node_name=args[0])
130 def ListVolumes(opts, args):
131 """List logical volumes on node(s).
134 if opts.output is None:
135 selected_fields = ["node", "phys", "vg",
136 "name", "size", "instance"]
138 selected_fields = opts.output.split(",")
140 op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
141 output = SubmitOpCode(op)
143 if not opts.no_headers:
144 headers = {"node": "Node", "phys": "PhysDev",
145 "vg": "VG", "name": "Name",
146 "size": "Size", "instance": "Instance"}
150 if opts.human_readable:
151 unitfields = ["size"]
157 data = GenerateTable(separator=opts.separator, headers=headers,
158 fields=selected_fields, unitfields=unitfields,
159 numfields=numfields, data=output)
162 logger.ToStdout(line)
168 'add': (AddNode, ARGS_ONE,
170 make_option("-s", "--secondary-ip", dest="secondary_ip",
171 help="Specify the secondary ip for the node",
172 metavar="ADDRESS", default=None),],
173 "<node_name>", "Add a node to the cluster"),
174 'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
175 "[<node_name>...]", "Show information about the node(s)"),
176 'list': (ListNodes, ARGS_NONE,
177 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
178 "", "Lists the nodes in the cluster"),
179 'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
180 "<node_name>", "Removes a node from the cluster"),
181 'volumes': (ListVolumes, ARGS_ANY,
182 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
183 "[<node_name>...]", "List logical volumes on node(s)"),
184 'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
185 "<node_name>", "List the tags of the given node"),
186 'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT],
187 "<node_name> tag...", "Add tags to the given node"),
188 'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT],
189 "<node_name> tag...", "Remove tags from the given node"),
193 if __name__ == '__main__':
194 sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE}))