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 ("Architecture: %s (%s)" %
106 (result["architecture"][0], result["architecture"][1]))
108 print ("Master node: %s" % result["master"])
111 for name, node in result["instances"]:
112 print (" - %s (on %s)" % (name, node))
114 for name in result["nodes"]:
115 print (" - %s" % name)
120 def ClusterCopyFile(opts, args):
121 """Copy a file from master to some nodes.
124 opts - class with options as members
125 args - list containing a single element, the file name
127 nodes - list containing the name of target nodes; if empty, all nodes
130 op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
135 def RunClusterCommand(opts, args):
136 """Run a command on some nodes.
139 opts - class with options as members
140 args - the command list as a list
142 nodes: list containing the name of target nodes; if empty, all nodes
145 command = " ".join(args)
147 op = opcodes.OpRunClusterCommand(command=command, nodes=nodes)
148 result = SubmitOpCode(op)
149 for node, sshcommand, output, exit_code in result:
150 print ("------------------------------------------------")
151 print ("node: %s" % node)
152 print ("command: %s" % sshcommand)
153 print ("%s" % output)
154 print ("return code = %s" % exit_code)
157 def VerifyCluster(opts, args):
158 """Verify integrity of cluster, performing various test on nodes.
161 opts - class with options as members
164 op = opcodes.OpVerifyCluster()
165 result = SubmitOpCode(op)
169 def MasterFailover(opts, args):
170 """Failover the master node.
172 This command, when run on a non-master node, will cause the current
173 master to cease being master, and the non-master to become new
177 op = opcodes.OpMasterFailover()
181 # this is an option common to more than one command, so we declare
182 # it here and reuse it
183 node_option = make_option("-n", "--node", action="append", dest="nodes",
184 help="Node to copy to (if not given, all nodes)"
185 ", can be given multiple times", metavar="<node>",
189 'init': (InitCluster, ARGS_ONE,
191 make_option("-s", "--secondary-ip", dest="secondary_ip",
192 help="Specify the secondary ip for this node;"
193 " if given, the entire cluster must have secondary"
195 metavar="ADDRESS", default=None),
196 make_option("-t", "--hypervisor-type", dest="hypervisor_type",
197 help="Specify the hypervisor type (xen-3.0, fake)",
198 metavar="TYPE", choices=["xen-3.0", "fake"],
200 make_option("-m", "--mac-prefix", dest="mac_prefix",
201 help="Specify the mac prefix for the instance IP"
202 " addresses, in the format XX:XX:XX",
204 default="aa:00:00",),
205 make_option("-g", "--vg-name", dest="vg_name",
206 help="Specify the volume group name "
207 " (cluster-wide) for disk allocation [xenvg]",
210 make_option("-b", "--bridge", dest="def_bridge",
211 help="Specify the default bridge name (cluster-wide)"
212 " to connect the instances to [xen-br0]",
215 make_option("--master-netdev", dest="master_netdev",
216 help="Specify the node interface (cluster-wide)"
217 " on which the master IP address will be added "
222 "[opts...] <cluster_name>",
223 "Initialises a new cluster configuration"),
224 'destroy': (DestroyCluster, ARGS_NONE,
226 make_option("--yes-do-it", dest="yes_do_it",
227 help="Destroy cluster",
228 action="store_true"),
230 "", "Destroy cluster"),
231 'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT],
232 "", "Does a check on the cluster configuration"),
233 'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT],
234 "", "Makes the current node the master"),
235 'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT],
236 "", "Shows the cluster version"),
237 'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT],
238 "", "Shows the cluster master"),
239 'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option],
240 "[-n node...] <filename>",
241 "Copies a file to all (or only some) nodes"),
242 'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option],
243 "[-n node...] <command>",
244 "Runs a command on all (or only some) nodes"),
245 'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT],
246 "", "Show cluster configuration"),
249 if __name__ == '__main__':
250 retcode = GenericMain(commands)