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",
66 if opts.human_readable:
67 unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
71 numfields = ["dtotal", "dfree",
72 "mtotal", "mnode", "mfree",
73 "pinst_cnt", "sinst_cnt"]
75 # change raw values to nicer strings
77 for idx, field in enumerate(selected_fields):
79 if field == "pinst_list":
81 elif field == "sinst_list":
87 data = GenerateTable(separator=opts.separator, headers=headers,
88 fields=selected_fields, unitfields=unitfields,
89 numfields=numfields, data=output)
96 def ShowNodeConfig(opts, args):
97 """Show node information.
100 op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
101 "pinst_list", "sinst_list"],
103 result = SubmitOpCode(op)
105 for name, primary_ip, secondary_ip, pinst, sinst in result:
106 logger.ToStdout("Node name: %s" % name)
107 logger.ToStdout(" primary ip: %s" % primary_ip)
108 logger.ToStdout(" secondary ip: %s" % secondary_ip)
110 logger.ToStdout(" primary for instances:")
112 logger.ToStdout(" - %s" % iname)
114 logger.ToStdout(" primary for no instances")
116 logger.ToStdout(" secondary for instances:")
118 logger.ToStdout(" - %s" % iname)
120 logger.ToStdout(" secondary for no instances")
125 def RemoveNode(opts, args):
126 """Remove node cli-to-processor bridge."""
127 op = opcodes.OpRemoveNode(node_name=args[0])
131 def ListVolumes(opts, args):
132 """List logical volumes on node(s).
135 if opts.output is None:
136 selected_fields = ["node", "phys", "vg",
137 "name", "size", "instance"]
139 selected_fields = opts.output.split(",")
141 op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
142 output = SubmitOpCode(op)
144 if not opts.no_headers:
145 headers = {"node": "Node", "phys": "PhysDev",
146 "vg": "VG", "name": "Name",
147 "size": "Size", "instance": "Instance"}
151 if opts.human_readable:
152 unitfields = ["size"]
158 data = GenerateTable(separator=opts.separator, headers=headers,
159 fields=selected_fields, unitfields=unitfields,
160 numfields=numfields, data=output)
163 logger.ToStdout(line)
169 'add': (AddNode, ARGS_ONE,
171 make_option("-s", "--secondary-ip", dest="secondary_ip",
172 help="Specify the secondary ip for the node",
173 metavar="ADDRESS", default=None),],
174 "<node_name>", "Add a node to the cluster"),
175 'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
176 "[<node_name>...]", "Show information about the node(s)"),
177 'list': (ListNodes, ARGS_NONE,
178 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
179 "", "Lists the nodes in the cluster"),
180 'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
181 "<node_name>", "Removes a node from the cluster"),
182 'volumes': (ListVolumes, ARGS_ANY,
183 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
184 "[<node_name>...]", "List logical volumes on node(s)"),
185 'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
186 "<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"),
194 if __name__ == '__main__':
195 sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE}))