Add warning on gnt-node add operation.
[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   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)
38   SubmitOpCode(op)
39
40
41 def ListNodes(opts, args):
42   """List nodes and their properties.
43
44   """
45   if opts.output is None:
46     selected_fields = ["name", "dtotal", "dfree",
47                        "mtotal", "mnode", "mfree",
48                        "pinst_cnt", "sinst_cnt"]
49   else:
50     selected_fields = opts.output.split(",")
51
52   op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[])
53   output = SubmitOpCode(op)
54
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"}
61   else:
62     headers = None
63
64   if opts.human_readable:
65     unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
66   else:
67     unitfields = None
68
69   numfields = ["dtotal", "dfree",
70                "mtotal", "mnode", "mfree",
71                "pinst_cnt", "sinst_cnt"]
72
73   # change raw values to nicer strings
74   for row in output:
75     for idx, field in enumerate(selected_fields):
76       val = row[idx]
77       if field == "pinst_list":
78         val = ",".join(val)
79       elif field == "sinst_list":
80         val = ",".join(val)
81       elif val is None:
82         val = "?"
83       row[idx] = str(val)
84
85   data = GenerateTable(separator=opts.separator, headers=headers,
86                        fields=selected_fields, unitfields=unitfields,
87                        numfields=numfields, data=output)
88   for line in data:
89     logger.ToStdout(line)
90
91   return 0
92
93
94 def ShowNodeConfig(opts, args):
95   """Show node information.
96
97   """
98   op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
99                                            "pinst_list", "sinst_list"],
100                             names=args)
101   result = SubmitOpCode(op)
102
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)
107     if pinst:
108       logger.ToStdout("  primary for instances:")
109       for iname in pinst:
110         logger.ToStdout("    - %s" % iname)
111     else:
112       logger.ToStdout("  primary for no instances")
113     if sinst:
114       logger.ToStdout("  secondary for instances:")
115       for iname in sinst:
116         logger.ToStdout("    - %s" % iname)
117     else:
118       logger.ToStdout("  secondary for no instances")
119
120   return 0
121
122
123 def RemoveNode(opts, args):
124   """Remove node cli-to-processor bridge."""
125   op = opcodes.OpRemoveNode(node_name=args[0])
126   SubmitOpCode(op)
127
128
129 def ListVolumes(opts, args):
130   """List logical volumes on node(s).
131
132   """
133   if opts.output is None:
134     selected_fields = ["node", "phys", "vg",
135                        "name", "size", "instance"]
136   else:
137     selected_fields = opts.output.split(",")
138
139   op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
140   output = SubmitOpCode(op)
141
142   if not opts.no_headers:
143     headers = {"node": "Node", "phys": "PhysDev",
144                "vg": "VG", "name": "Name",
145                "size": "Size", "instance": "Instance"}
146   else:
147     headers = None
148
149   if opts.human_readable:
150     unitfields = ["size"]
151   else:
152     unitfields = None
153
154   numfields = ["size"]
155
156   data = GenerateTable(separator=opts.separator, headers=headers,
157                        fields=selected_fields, unitfields=unitfields,
158                        numfields=numfields, data=output)
159
160   for line in data:
161     logger.ToStdout(line)
162
163   return 0
164
165
166 commands = {
167   'add': (AddNode, ARGS_ONE,
168           [DEBUG_OPT,
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)"),
183   }
184
185
186 if __name__ == '__main__':
187   sys.exit(GenericMain(commands))