Change cli.OutputTable to cli.GenerateTable
[ganeti-local] / scripts / gnt-node
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2006, 2007 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 import sys
23 from optparse import make_option
24
25 from ganeti.cli import *
26 from ganeti import opcodes
27 from ganeti import logger
28 from ganeti import utils
29
30
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)
34   SubmitOpCode(op)
35
36
37 def ListNodes(opts, args):
38   """List nodes and their properties.
39
40   """
41   if opts.output is None:
42     selected_fields = ["name", "dtotal", "dfree",
43                        "mtotal", "mnode", "mfree",
44                        "pinst", "sinst"]
45   else:
46     selected_fields = opts.output.split(",")
47
48   op = opcodes.OpQueryNodes(output_fields=selected_fields)
49   output = SubmitOpCode(op)
50
51   if not opts.no_headers:
52     headers = {"name": "Node", "pinst": "Pinst", "sinst": "Sinst",
53                "pip": "PrimaryIP", "sip": "SecondaryIP",
54                "dtotal": "DTotal", "dfree": "DFree",
55                "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree"}
56   else:
57     headers = None
58
59   if opts.human_readable:
60     unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
61   else:
62     unitfields = None
63
64   numfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree", "pinst", "sinst"]
65
66   data = GenerateTable(separator=opts.separator, headers=headers,
67                        fields=selected_fields, unitfields=unitfields,
68                        numfields=numfields, data=output)
69   for line in data:
70     logger.ToStdout(line)
71
72   return 0
73
74
75 def ShowNodeConfig(opts, args):
76   """Show node information.
77
78   """
79   op = opcodes.OpQueryNodeData(nodes=args)
80   result = SubmitOpCode(op)
81
82   for name, primary_ip, secondary_ip, pinst, sinst in result:
83     logger.ToStdout("Node name: %s" % name)
84     logger.ToStdout("  primary ip: %s" % primary_ip)
85     logger.ToStdout("  secondary ip: %s" % secondary_ip)
86     if pinst:
87       logger.ToStdout("  primary for instances:")
88       for iname in pinst:
89         logger.ToStdout("    - %s" % iname)
90     else:
91       logger.ToStdout("  primary for no instances")
92     if sinst:
93       logger.ToStdout("  secondary for instances:")
94       for iname in sinst:
95         logger.ToStdout("    - %s" % iname)
96     else:
97       logger.ToStdout("  secondary for no instances")
98
99   return 0
100
101
102 def RemoveNode(opts, args):
103   """Remove node cli-to-processor bridge."""
104   op = opcodes.OpRemoveNode(node_name=args[0])
105   SubmitOpCode(op)
106
107
108 def ListVolumes(opts, args):
109   """List logical volumes on node(s).
110
111   """
112   if opts.output is None:
113     selected_fields = ["node", "phys", "vg",
114                        "name", "size", "instance"]
115   else:
116     selected_fields = opts.output.split(",")
117
118   op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
119   output = SubmitOpCode(op)
120
121   if not opts.no_headers:
122     headers = {"node": "Node", "phys": "PhysDev",
123                "vg": "VG", "name": "Name",
124                "size": "Size", "instance": "Instance"}
125   else:
126     headers = None
127
128   if opts.human_readable:
129     unitfields = ["size"]
130   else:
131     unitfields = None
132
133   numfields = ["size"]
134
135   data = GenerateTable(separator=opts.separator, headers=headers,
136                        fields=selected_fields, unitfields=unitfields,
137                        numfields=numfields, data=output)
138
139   for line in data:
140     logger.ToStdout(line)
141
142   return 0
143
144
145 commands = {
146   'add': (AddNode, ARGS_ONE,
147           [DEBUG_OPT,
148            make_option("-s", "--secondary-ip", dest="secondary_ip",
149                        help="Specify the secondary ip for the node",
150                        metavar="ADDRESS", default=None),],
151           "<node_name>", "Add a node to the cluster"),
152   'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
153            "[<node_name>...]", "Show information about the node(s)"),
154   'list': (ListNodes, ARGS_NONE,
155            [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
156            "", "Lists the nodes in the cluster"),
157   'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
158              "<node_name>", "Removes a node from the cluster"),
159   'volumes': (ListVolumes, ARGS_ANY,
160               [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
161               "[<node_name>...]", "List logical volumes on node(s)"),
162   }
163
164
165 if __name__ == '__main__':
166   sys.exit(GenericMain(commands))