Fix small typo.
[ganeti-local] / scripts / gnt-cluster
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 import pprint
25
26 from ganeti.cli import *
27 from ganeti import opcodes
28
29
30 def InitCluster(opts, args):
31   """Initialize the cluster.
32
33   Args:
34     opts - class with options as members
35     args - list of arguments, expected to be [clustername]
36
37   """
38   op = opcodes.OpInitCluster(cluster_name=args[0],
39                              secondary_ip=opts.secondary_ip,
40                              hypervisor_type=opts.hypervisor_type,
41                              vg_name=opts.vg_name,
42                              mac_prefix=opts.mac_prefix,
43                              def_bridge=opts.def_bridge)
44   SubmitOpCode(op)
45   return 0
46
47
48 def DestroyCluster(opts, args):
49   """Destroy the cluster.
50
51   Args:
52     opts - class with options as members
53   """
54   if not opts.yes_do_it:
55     print ("Destroying a cluster is irreversibly. If you really want destroy"
56            " this cluster, supply the --yes-do-it option.")
57     return 1
58
59   op = opcodes.OpDestroyCluster()
60   SubmitOpCode(op)
61   return 0
62
63
64 def ShowClusterVersion(opts, args):
65   """Write version of ganeti software to the standard output.
66
67   Args:
68     opts - class with options as members
69
70   """
71   op = opcodes.OpQueryClusterInfo()
72   result = SubmitOpCode(op)
73   print ("Software version: %s" % result["software_version"])
74   print ("Internode protocol: %s" % result["protocol_version"])
75   print ("Configuration format: %s" % result["config_version"])
76   print ("OS api version: %s" % result["os_api_version"])
77   print ("Export interface: %s" % result["export_version"])
78   return 0
79
80
81 def ShowClusterMaster(opts, args):
82   """Write name of master node to the standard output.
83
84   Args:
85     opts - class with options as members
86
87   """
88   op = opcodes.OpQueryClusterInfo()
89   result = SubmitOpCode(op)
90   print (result["master"])
91   return 0
92
93
94 def ShowClusterConfig(opts, args):
95   """Shows cluster information.
96
97   """
98   op = opcodes.OpQueryClusterInfo()
99   result = SubmitOpCode(op)
100
101   print ("Cluster name: %s" % result["name"])
102
103   print ("Architecture: %s (%s)" %
104             (result["architecture"][0], result["architecture"][1]))
105
106   print ("Master node: %s" % result["master"])
107
108   print ("Instances:")
109   for name, node in result["instances"]:
110     print ("  - %s (on %s)" % (name, node))
111   print ("Nodes:")
112   for name in result["nodes"]:
113     print ("  - %s" % name)
114
115   return 0
116
117
118 def ClusterCopyFile(opts, args):
119   """Copy a file from master to some nodes.
120
121   Args:
122     opts - class with options as members
123     args - list containing a single element, the file name
124   Opts used:
125     nodes - list containing the name of target nodes; if empty, all nodes
126
127   """
128   op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
129   SubmitOpCode(op)
130   return 0
131
132
133 def RunClusterCommand(opts, args):
134   """Run a command on some nodes.
135
136   Args:
137     opts - class with options as members
138     args - the command list as a list
139   Opts used:
140     nodes: list containing the name of target nodes; if empty, all nodes
141
142   """
143   command = " ".join(args)
144   nodes = opts.nodes
145   op = opcodes.OpRunClusterCommand(command=command, nodes=nodes)
146   result = SubmitOpCode(op)
147   for node, sshcommand, output, exit_code in result:
148     print ("------------------------------------------------")
149     print ("node: %s" % node)
150     print ("command: %s" % sshcommand)
151     print ("%s" % output)
152     print ("return code = %s" % exit_code)
153
154
155 def VerifyCluster(opts, args):
156   """Verify integrity of cluster, performing various test on nodes.
157
158   Args:
159     opts - class with options as members
160
161   """
162   op = opcodes.OpVerifyCluster()
163   result = SubmitOpCode(op)
164   return result
165
166
167 def MasterFailover(opts, args):
168   """Failover the master node.
169
170   This command, when run on a non-master node, will cause the current
171   master to cease being master, and the non-master to become new
172   master.
173
174   """
175   op = opcodes.OpMasterFailover()
176   SubmitOpCode(op)
177
178
179 # this is an option common to more than one command, so we declare
180 # it here and reuse it
181 node_option = make_option("-n", "--node", action="append", dest="nodes",
182                           help="Node to copy to (if not given, all nodes)"
183                           ", can be given multiple times", metavar="<node>",
184                           default=[])
185
186 commands = {
187   'init': (InitCluster, ARGS_ONE,
188            [DEBUG_OPT,
189             make_option("-s", "--secondary-ip", dest="secondary_ip",
190                         help="Specify the secondary ip for this node;"
191                         " if given, the entire cluster must have secondary"
192                         " addresses",
193                         metavar="ADDRESS", default=None),
194             make_option("-t", "--hypervisor-type", dest="hypervisor_type",
195                         help="Specify the hypervisor type (xen-3.0, fake)",
196                         metavar="TYPE", choices=["xen-3.0", "fake"],
197                         default="xen-3.0",),
198             make_option("-m", "--mac-prefix", dest="mac_prefix",
199                         help="Specify the mac prefix for the instance IP"
200                         " addresses, in the format XX:XX:XX",
201                         metavar="PREFIX",
202                         default="aa:00:00",),
203             make_option("-g", "--vg-name", dest="vg_name",
204                         help="Specify the volume group name "
205                         " (cluster-wide) for disk allocation [xenvg]",
206                         metavar="VG",
207                         default="xenvg",),
208             make_option("-b", "--bridge", dest="def_bridge",
209                         help="Specify the default bridge name (cluster-wide)"
210                         " to connect the instances to [xen-br0]",
211                         metavar="BRIDGE",
212                         default="xen-br0",),
213             ],
214            "[opts...] <cluster_name>",
215            "Initialises a new cluster configuration"),
216   'destroy': (DestroyCluster, ARGS_NONE,
217               [DEBUG_OPT,
218                make_option("--yes-do-it", dest="yes_do_it",
219                            help="Destroy cluster",
220                            action="store_true"),
221               ],
222               "", "Destroy cluster"),
223   'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT],
224              "", "Does a check on the cluster configuration"),
225   'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT],
226                      "", "Makes the current node the master"),
227   'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT],
228               "", "Shows the cluster version"),
229   'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT],
230                 "", "Shows the cluster master"),
231   'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option],
232                "[-n node...] <filename>",
233                "Copies a file to all (or only some) nodes"),
234   'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option],
235               "[-n node...] <command>",
236               "Runs a command on all (or only some) nodes"),
237   'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT],
238                  "", "Show cluster configuration"),
239   }
240
241 if __name__ == '__main__':
242   retcode = GenericMain(commands)
243   sys.exit(retcode)