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)
48 def DestroyCluster(opts, args):
49 """Destroy the cluster.
52 opts - class with options as members
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.")
59 op = opcodes.OpDestroyCluster()
64 def ShowClusterVersion(opts, args):
65 """Write version of ganeti software to the standard output.
68 opts - class with options as members
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"])
81 def ShowClusterMaster(opts, args):
82 """Write name of master node to the standard output.
85 opts - class with options as members
88 op = opcodes.OpQueryClusterInfo()
89 result = SubmitOpCode(op)
90 print (result["master"])
94 def ShowClusterConfig(opts, args):
95 """Shows cluster information.
98 op = opcodes.OpQueryClusterInfo()
99 result = SubmitOpCode(op)
101 print ("Cluster name: %s" % result["name"])
103 print ("Architecture: %s (%s)" %
104 (result["architecture"][0], result["architecture"][1]))
106 print ("Master node: %s" % result["master"])
109 for name, node in result["instances"]:
110 print (" - %s (on %s)" % (name, node))
112 for name in result["nodes"]:
113 print (" - %s" % name)
118 def ClusterCopyFile(opts, args):
119 """Copy a file from master to some nodes.
122 opts - class with options as members
123 args - list containing a single element, the file name
125 nodes - list containing the name of target nodes; if empty, all nodes
128 op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
133 def RunClusterCommand(opts, args):
134 """Run a command on some nodes.
137 opts - class with options as members
138 args - the command list as a list
140 nodes: list containing the name of target nodes; if empty, all nodes
143 command = " ".join(args)
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)
155 def VerifyCluster(opts, args):
156 """Verify integrity of cluster, performing various test on nodes.
159 opts - class with options as members
162 op = opcodes.OpVerifyCluster()
163 result = SubmitOpCode(op)
167 def MasterFailover(opts, args):
168 """Failover the master node.
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
175 op = opcodes.OpMasterFailover()
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>",
187 'init': (InitCluster, ARGS_ONE,
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"
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"],
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",
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]",
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]",
214 "[opts...] <cluster_name>",
215 "Initialises a new cluster configuration"),
216 'destroy': (DestroyCluster, ARGS_NONE,
218 make_option("--yes-do-it", dest="yes_do_it",
219 help="Destroy cluster",
220 action="store_true"),
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"),
241 if __name__ == '__main__':
242 retcode = GenericMain(commands)