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
29 from ganeti import errors
30 from ganeti import utils
33 def InitCluster(opts, args):
34 """Initialize the cluster.
37 opts - class with options as members
38 args - list of arguments, expected to be [clustername]
41 op = opcodes.OpInitCluster(cluster_name=args[0],
42 secondary_ip=opts.secondary_ip,
43 hypervisor_type=opts.hypervisor_type,
45 mac_prefix=opts.mac_prefix,
46 def_bridge=opts.def_bridge,
47 master_netdev=opts.master_netdev)
52 def DestroyCluster(opts, args):
53 """Destroy the cluster.
56 opts - class with options as members
59 if not opts.yes_do_it:
60 print ("Destroying a cluster is irreversibly. If you really want destroy"
61 " this cluster, supply the --yes-do-it option.")
64 op = opcodes.OpDestroyCluster()
69 def RenameCluster(opts, args):
70 """Rename the cluster.
73 opts - class with options as members, we use force only
74 args - list of arguments, expected to be [new_name]
79 usertext = ("This will rename the cluster to '%s'. If you are connected"
80 " over the network to the cluster name, the operation is very"
81 " dangerous as the IP address will be removed from the node"
82 " and the change may not go through. Continue?") % name
83 if not AskUser(usertext):
86 op = opcodes.OpRenameCluster(name=name)
91 def ShowClusterVersion(opts, args):
92 """Write version of ganeti software to the standard output.
95 opts - class with options as members
98 op = opcodes.OpQueryClusterInfo()
99 result = SubmitOpCode(op)
100 print ("Software version: %s" % result["software_version"])
101 print ("Internode protocol: %s" % result["protocol_version"])
102 print ("Configuration format: %s" % result["config_version"])
103 print ("OS api version: %s" % result["os_api_version"])
104 print ("Export interface: %s" % result["export_version"])
108 def ShowClusterMaster(opts, args):
109 """Write name of master node to the standard output.
112 opts - class with options as members
115 op = opcodes.OpQueryClusterInfo()
116 result = SubmitOpCode(op)
117 print (result["master"])
121 def ShowClusterConfig(opts, args):
122 """Shows cluster information.
125 op = opcodes.OpQueryClusterInfo()
126 result = SubmitOpCode(op)
128 print ("Cluster name: %s" % result["name"])
130 print ("Master node: %s" % result["master"])
132 print ("Architecture (this node): %s (%s)" %
133 (result["architecture"][0], result["architecture"][1]))
138 def ClusterCopyFile(opts, args):
139 """Copy a file from master to some nodes.
142 opts - class with options as members
143 args - list containing a single element, the file name
145 nodes - list containing the name of target nodes; if empty, all nodes
148 op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
153 def RunClusterCommand(opts, args):
154 """Run a command on some nodes.
157 opts - class with options as members
158 args - the command list as a list
160 nodes: list containing the name of target nodes; if empty, all nodes
163 command = " ".join(args)
165 op = opcodes.OpRunClusterCommand(command=command, nodes=nodes)
166 result = SubmitOpCode(op)
167 for node, output, exit_code in result:
168 print ("------------------------------------------------")
169 print ("node: %s" % node)
170 print ("%s" % output)
171 print ("return code = %s" % exit_code)
174 def VerifyCluster(opts, args):
175 """Verify integrity of cluster, performing various test on nodes.
178 opts - class with options as members
181 op = opcodes.OpVerifyCluster()
182 result = SubmitOpCode(op)
186 def VerifyDisks(opts, args):
187 """Verify integrity of cluster disks.
190 opts - class with options as members
193 op = opcodes.OpVerifyDisks()
194 result = SubmitOpCode(op)
195 if not isinstance(result, tuple) or len(result) != 4:
196 raise errors.ProgrammerError("Unknown result type for OpVerifyDisks")
198 nodes, nlvm, instances, missing = result
201 print "Nodes unreachable or with bad data:"
204 retcode = constants.EXIT_SUCCESS
207 for node, text in nlvm.iteritems():
208 print ("Error on node %s: LVM error: %s" %
209 (node, text[-400:].encode('string_escape')))
211 print "You need to fix these nodes first before fixing instances"
214 for iname in instances:
217 op = opcodes.OpActivateInstanceDisks(instance_name=iname)
219 print "Activating disks for instance '%s'" % iname
221 except errors.GenericError, err:
222 nret, msg = FormatError(err)
224 print >> sys.stderr, ("Error activating disks for instance %s: %s" %
228 for iname, ival in missing.iteritems():
229 all_missing = utils.all(ival, lambda x: x[0] in nlvm)
231 print ("Instance %s cannot be verified as it lives on"
232 " broken nodes" % iname)
234 print "Instance %s has missing logical volumes:" % iname
236 for node, vol in ival:
238 print ("\tbroken node %s /dev/xenvg/%s" % (node, vol))
240 print ("\t%s /dev/xenvg/%s" % (node, vol))
241 print ("You need to run replace_disks for all the above"
242 " instances, if this message persist after fixing nodes.")
248 def MasterFailover(opts, args):
249 """Failover the master node.
251 This command, when run on a non-master node, will cause the current
252 master to cease being master, and the non-master to become new
256 op = opcodes.OpMasterFailover()
260 def SearchTags(opts, args):
261 """Searches the tags on all the cluster.
264 op = opcodes.OpSearchTags(pattern=args[0])
265 result = SubmitOpCode(op)
268 result = list(result)
270 for path, tag in result:
271 print "%s %s" % (path, tag)
274 # this is an option common to more than one command, so we declare
275 # it here and reuse it
276 node_option = make_option("-n", "--node", action="append", dest="nodes",
277 help="Node to copy to (if not given, all nodes),"
278 " can be given multiple times",
279 metavar="<node>", default=[])
282 'init': (InitCluster, ARGS_ONE,
284 make_option("-s", "--secondary-ip", dest="secondary_ip",
285 help="Specify the secondary ip for this node;"
286 " if given, the entire cluster must have secondary"
288 metavar="ADDRESS", default=None),
289 make_option("-t", "--hypervisor-type", dest="hypervisor_type",
290 help="Specify the hypervisor type "
291 "(xen-3.0, fake, xen-hvm-3.1)",
292 metavar="TYPE", choices=["xen-3.0",
296 make_option("-m", "--mac-prefix", dest="mac_prefix",
297 help="Specify the mac prefix for the instance IP"
298 " addresses, in the format XX:XX:XX",
300 default="aa:00:00",),
301 make_option("-g", "--vg-name", dest="vg_name",
302 help="Specify the volume group name "
303 " (cluster-wide) for disk allocation [xenvg]",
306 make_option("-b", "--bridge", dest="def_bridge",
307 help="Specify the default bridge name (cluster-wide)"
308 " to connect the instances to [%s]" %
309 constants.DEFAULT_BRIDGE,
311 default=constants.DEFAULT_BRIDGE,),
312 make_option("--master-netdev", dest="master_netdev",
313 help="Specify the node interface (cluster-wide)"
314 " on which the master IP address will be added "
315 " [%s]" % constants.DEFAULT_BRIDGE,
317 default=constants.DEFAULT_BRIDGE,),
319 "[opts...] <cluster_name>",
320 "Initialises a new cluster configuration"),
321 'destroy': (DestroyCluster, ARGS_NONE,
323 make_option("--yes-do-it", dest="yes_do_it",
324 help="Destroy cluster",
325 action="store_true"),
327 "", "Destroy cluster"),
328 'rename': (RenameCluster, ARGS_ONE, [DEBUG_OPT, FORCE_OPT],
330 "Renames the cluster"),
331 'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT],
332 "", "Does a check on the cluster configuration"),
333 'verify-disks': (VerifyDisks, ARGS_NONE, [DEBUG_OPT],
334 "", "Does a check on the cluster disk status"),
335 'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT],
336 "", "Makes the current node the master"),
337 'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT],
338 "", "Shows the cluster version"),
339 'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT],
340 "", "Shows the cluster master"),
341 'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option],
342 "[-n node...] <filename>",
343 "Copies a file to all (or only some) nodes"),
344 'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option],
345 "[-n node...] <command>",
346 "Runs a command on all (or only some) nodes"),
347 'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT],
348 "", "Show cluster configuration"),
349 'list-tags': (ListTags, ARGS_NONE,
350 [DEBUG_OPT], "", "List the tags of the cluster"),
351 'add-tags': (AddTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
352 "tag...", "Add tags to the cluster"),
353 'remove-tags': (RemoveTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
354 "tag...", "Remove tags from the cluster"),
355 'search-tags': (SearchTags, ARGS_ONE,
356 [DEBUG_OPT], "", "Searches the tags on all objects on"
357 " the cluster for a given pattern (regex)"),
360 if __name__ == '__main__':
361 sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER}))