4 # Copyright (C) 2006, 2007 Google Inc.
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.
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.
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
23 from optparse import make_option
26 from ganeti.cli import *
27 from ganeti import opcodes
28 from ganeti import constants
31 def InitCluster(opts, args):
32 """Initialize the cluster.
35 opts - class with options as members
36 args - list of arguments, expected to be [clustername]
39 op = opcodes.OpInitCluster(cluster_name=args[0],
40 secondary_ip=opts.secondary_ip,
41 hypervisor_type=opts.hypervisor_type,
43 mac_prefix=opts.mac_prefix,
44 def_bridge=opts.def_bridge,
45 master_netdev=opts.master_netdev)
50 def DestroyCluster(opts, args):
51 """Destroy the cluster.
54 opts - class with options as members
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.")
62 op = opcodes.OpDestroyCluster()
67 def ShowClusterVersion(opts, args):
68 """Write version of ganeti software to the standard output.
71 opts - class with options as members
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"])
84 def ShowClusterMaster(opts, args):
85 """Write name of master node to the standard output.
88 opts - class with options as members
91 op = opcodes.OpQueryClusterInfo()
92 result = SubmitOpCode(op)
93 print (result["master"])
97 def ShowClusterConfig(opts, args):
98 """Shows cluster information.
101 op = opcodes.OpQueryClusterInfo()
102 result = SubmitOpCode(op)
104 print ("Cluster name: %s" % result["name"])
106 print ("Master node: %s" % result["master"])
108 print ("Architecture (this node): %s (%s)" %
109 (result["architecture"][0], result["architecture"][1]))
114 def ClusterCopyFile(opts, args):
115 """Copy a file from master to some nodes.
118 opts - class with options as members
119 args - list containing a single element, the file name
121 nodes - list containing the name of target nodes; if empty, all nodes
124 op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
129 def RunClusterCommand(opts, args):
130 """Run a command on some nodes.
133 opts - class with options as members
134 args - the command list as a list
136 nodes: list containing the name of target nodes; if empty, all nodes
139 command = " ".join(args)
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)
151 def VerifyCluster(opts, args):
152 """Verify integrity of cluster, performing various test on nodes.
155 opts - class with options as members
158 op = opcodes.OpVerifyCluster()
159 result = SubmitOpCode(op)
163 def MasterFailover(opts, args):
164 """Failover the master node.
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
171 op = opcodes.OpMasterFailover()
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>",
183 'init': (InitCluster, ARGS_ONE,
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"
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"],
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",
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]",
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,
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,
215 default=constants.DEFAULT_BRIDGE,),
217 "[opts...] <cluster_name>",
218 "Initialises a new cluster configuration"),
219 'destroy': (DestroyCluster, ARGS_NONE,
221 make_option("--yes-do-it", dest="yes_do_it",
222 help="Destroy cluster",
223 action="store_true"),
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"),
244 if __name__ == '__main__':
245 sys.exit(GenericMain(commands))