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