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
30 def InitCluster(opts, args):
31 """Initialize the cluster.
34 opts - class with options as members
35 args - list of arguments, expected to be [clustername]
38 op = opcodes.OpInitCluster(cluster_name=args[0],
39 secondary_ip=opts.secondary_ip,
40 hypervisor_type=opts.hypervisor_type,
42 mac_prefix=opts.mac_prefix,
43 def_bridge=opts.def_bridge,
44 master_netdev=opts.master_netdev)
49 def DestroyCluster(opts, args):
50 """Destroy the cluster.
53 opts - class with options as members
56 if not opts.yes_do_it:
57 print ("Destroying a cluster is irreversibly. If you really want destroy"
58 " this cluster, supply the --yes-do-it option.")
61 op = opcodes.OpDestroyCluster()
66 def ShowClusterVersion(opts, args):
67 """Write version of ganeti software to the standard output.
70 opts - class with options as members
73 op = opcodes.OpQueryClusterInfo()
74 result = SubmitOpCode(op)
75 print ("Software version: %s" % result["software_version"])
76 print ("Internode protocol: %s" % result["protocol_version"])
77 print ("Configuration format: %s" % result["config_version"])
78 print ("OS api version: %s" % result["os_api_version"])
79 print ("Export interface: %s" % result["export_version"])
83 def ShowClusterMaster(opts, args):
84 """Write name of master node to the standard output.
87 opts - class with options as members
90 op = opcodes.OpQueryClusterInfo()
91 result = SubmitOpCode(op)
92 print (result["master"])
96 def ShowClusterConfig(opts, args):
97 """Shows cluster information.
100 op = opcodes.OpQueryClusterInfo()
101 result = SubmitOpCode(op)
103 print ("Cluster name: %s" % result["name"])
105 print ("Master node: %s" % result["master"])
107 print ("Architecture (this node): %s (%s)" %
108 (result["architecture"][0], result["architecture"][1]))
113 def ClusterCopyFile(opts, args):
114 """Copy a file from master to some nodes.
117 opts - class with options as members
118 args - list containing a single element, the file name
120 nodes - list containing the name of target nodes; if empty, all nodes
123 op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
128 def RunClusterCommand(opts, args):
129 """Run a command on some nodes.
132 opts - class with options as members
133 args - the command list as a list
135 nodes: list containing the name of target nodes; if empty, all nodes
138 command = " ".join(args)
140 op = opcodes.OpRunClusterCommand(command=command, nodes=nodes)
141 result = SubmitOpCode(op)
142 for node, sshcommand, output, exit_code in result:
143 print ("------------------------------------------------")
144 print ("node: %s" % node)
145 print ("command: %s" % sshcommand)
146 print ("%s" % output)
147 print ("return code = %s" % exit_code)
150 def VerifyCluster(opts, args):
151 """Verify integrity of cluster, performing various test on nodes.
154 opts - class with options as members
157 op = opcodes.OpVerifyCluster()
158 result = SubmitOpCode(op)
162 def MasterFailover(opts, args):
163 """Failover the master node.
165 This command, when run on a non-master node, will cause the current
166 master to cease being master, and the non-master to become new
170 op = opcodes.OpMasterFailover()
174 # this is an option common to more than one command, so we declare
175 # it here and reuse it
176 node_option = make_option("-n", "--node", action="append", dest="nodes",
177 help="Node to copy to (if not given, all nodes)"
178 ", can be given multiple times", metavar="<node>",
182 'init': (InitCluster, ARGS_ONE,
184 make_option("-s", "--secondary-ip", dest="secondary_ip",
185 help="Specify the secondary ip for this node;"
186 " if given, the entire cluster must have secondary"
188 metavar="ADDRESS", default=None),
189 make_option("-t", "--hypervisor-type", dest="hypervisor_type",
190 help="Specify the hypervisor type (xen-3.0, fake)",
191 metavar="TYPE", choices=["xen-3.0", "fake"],
193 make_option("-m", "--mac-prefix", dest="mac_prefix",
194 help="Specify the mac prefix for the instance IP"
195 " addresses, in the format XX:XX:XX",
197 default="aa:00:00",),
198 make_option("-g", "--vg-name", dest="vg_name",
199 help="Specify the volume group name "
200 " (cluster-wide) for disk allocation [xenvg]",
203 make_option("-b", "--bridge", dest="def_bridge",
204 help="Specify the default bridge name (cluster-wide)"
205 " to connect the instances to [%s]" %
206 constants.DEFAULT_BRIDGE,
208 default=constants.DEFAULT_BRIDGE,),
209 make_option("--master-netdev", dest="master_netdev",
210 help="Specify the node interface (cluster-wide)"
211 " on which the master IP address will be added "
212 " [%s]" % constants.DEFAULT_BRIDGE,
214 default=constants.DEFAULT_BRIDGE,),
216 "[opts...] <cluster_name>",
217 "Initialises a new cluster configuration"),
218 'destroy': (DestroyCluster, ARGS_NONE,
220 make_option("--yes-do-it", dest="yes_do_it",
221 help="Destroy cluster",
222 action="store_true"),
224 "", "Destroy cluster"),
225 'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT],
226 "", "Does a check on the cluster configuration"),
227 'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT],
228 "", "Makes the current node the master"),
229 'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT],
230 "", "Shows the cluster version"),
231 'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT],
232 "", "Shows the cluster master"),
233 'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option],
234 "[-n node...] <filename>",
235 "Copies a file to all (or only some) nodes"),
236 'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option],
237 "[-n node...] <command>",
238 "Runs a command on all (or only some) nodes"),
239 'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT],
240 "", "Show cluster configuration"),
243 if __name__ == '__main__':
244 retcode = GenericMain(commands)