Fix the "gnt-cluster getmaster" command by making the LuQueryClusterInfo
[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   OutputTable(separator=opts.separator, headers=headers,
67       fields=selected_fields, unitfields=unitfields,
68       numfields=numfields, data=output)
69
70   return 0
71
72
73 def ShowNodeConfig(opts, args):
74   """Show node information.
75
76   """
77   op = opcodes.OpQueryNodeData(nodes=args)
78   result = SubmitOpCode(op)
79
80   for name, primary_ip, secondary_ip, pinst, sinst in result:
81     logger.ToStdout("Node name: %s" % name)
82     logger.ToStdout("  primary ip: %s" % primary_ip)
83     logger.ToStdout("  secondary ip: %s" % secondary_ip)
84     if pinst:
85       logger.ToStdout("  primary for instances:")
86       for iname in pinst:
87         logger.ToStdout("    - %s" % iname)
88     else:
89       logger.ToStdout("  primary for no instances")
90     if sinst:
91       logger.ToStdout("  secondary for instances:")
92       for iname in sinst:
93         logger.ToStdout("    - %s" % iname)
94     else:
95       logger.ToStdout("  secondary for no instances")
96
97   return 0
98
99
100 def RemoveNode(opts, args):
101   """Remove node cli-to-processor bridge."""
102   op = opcodes.OpRemoveNode(node_name=args[0])
103   SubmitOpCode(op)
104
105
106 def ListVolumes(opts, args):
107   """List logical volumes on node(s).
108
109   """
110   if opts.output is None:
111     selected_fields = ["node", "phys", "vg",
112                        "name", "size", "instance"]
113   else:
114     selected_fields = opts.output.split(",")
115
116   op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
117   output = SubmitOpCode(op)
118
119   if not opts.no_headers:
120     headers = {"node": "Node", "phys": "PhysDev",
121                "vg": "VG", "name": "Name",
122                "size": "Size", "instance": "Instance"}
123   else:
124     headers = None
125
126   if opts.human_readable:
127     unitfields = ["size"]
128   else:
129     unitfields = None
130
131   numfields = ["size"]
132
133   OutputTable(separator=opts.separator, headers=headers,
134       fields=selected_fields, unitfields=unitfields,
135       numfields=numfields, data=output)
136
137   return 0
138
139
140 commands = {
141   'add': (AddNode, ARGS_ONE,
142           [DEBUG_OPT,
143            make_option("-s", "--secondary-ip", dest="secondary_ip",
144                        help="Specify the secondary ip for the node",
145                        metavar="ADDRESS", default=None),],
146           "<node_name>", "Add a node to the cluster"),
147   'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
148            "[<node_name>...]", "Show information about the node(s)"),
149   'list': (ListNodes, ARGS_NONE,
150            [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
151            "", "Lists the nodes in the cluster"),
152   'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
153              "<node_name>", "Removes a node from the cluster"),
154   'volumes': (ListVolumes, ARGS_ANY,
155               [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
156               "[<node_name>...]", "List logical volumes on node(s)"),
157   }
158
159
160 if __name__ == '__main__':
161   retcode = GenericMain(commands)
162   sys.exit(retcode)