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