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 if not opts.lvm_storage and opts.vg_name:
42 print ("Options --no-lvm-storage and --vg-name conflict.")
45 vg_name = opts.vg_name
46 if opts.lvm_storage and not opts.vg_name:
47 vg_name = constants.DEFAULT_VG
49 op = opcodes.OpInitCluster(cluster_name=args[0],
50 secondary_ip=opts.secondary_ip,
51 hypervisor_type=opts.hypervisor_type,
53 mac_prefix=opts.mac_prefix,
54 def_bridge=opts.def_bridge,
55 master_netdev=opts.master_netdev,
56 file_storage_dir=opts.file_storage_dir)
61 def DestroyCluster(opts, args):
62 """Destroy the cluster.
65 opts - class with options as members
68 if not opts.yes_do_it:
69 print ("Destroying a cluster is irreversibly. If you really want destroy"
70 " this cluster, supply the --yes-do-it option.")
73 op = opcodes.OpDestroyCluster()
78 def RenameCluster(opts, args):
79 """Rename the cluster.
82 opts - class with options as members, we use force only
83 args - list of arguments, expected to be [new_name]
88 usertext = ("This will rename the cluster to '%s'. If you are connected"
89 " over the network to the cluster name, the operation is very"
90 " dangerous as the IP address will be removed from the node"
91 " and the change may not go through. Continue?") % name
92 if not AskUser(usertext):
95 op = opcodes.OpRenameCluster(name=name)
100 def ShowClusterVersion(opts, args):
101 """Write version of ganeti software to the standard output.
104 opts - class with options as members
107 op = opcodes.OpQueryClusterInfo()
108 result = SubmitOpCode(op)
109 print ("Software version: %s" % result["software_version"])
110 print ("Internode protocol: %s" % result["protocol_version"])
111 print ("Configuration format: %s" % result["config_version"])
112 print ("OS api version: %s" % result["os_api_version"])
113 print ("Export interface: %s" % result["export_version"])
117 def ShowClusterMaster(opts, args):
118 """Write name of master node to the standard output.
121 opts - class with options as members
124 op = opcodes.OpQueryClusterInfo()
125 result = SubmitOpCode(op)
126 print (result["master"])
130 def ShowClusterConfig(opts, args):
131 """Shows cluster information.
134 op = opcodes.OpQueryClusterInfo()
135 result = SubmitOpCode(op)
137 print ("Cluster name: %s" % result["name"])
139 print ("Master node: %s" % result["master"])
141 print ("Architecture (this node): %s (%s)" %
142 (result["architecture"][0], result["architecture"][1]))
147 def ClusterCopyFile(opts, args):
148 """Copy a file from master to some nodes.
151 opts - class with options as members
152 args - list containing a single element, the file name
154 nodes - list containing the name of target nodes; if empty, all nodes
157 op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
162 def RunClusterCommand(opts, args):
163 """Run a command on some nodes.
166 opts - class with options as members
167 args - the command list as a list
169 nodes: list containing the name of target nodes; if empty, all nodes
172 command = " ".join(args)
174 op = opcodes.OpRunClusterCommand(command=command, nodes=nodes)
175 result = SubmitOpCode(op)
176 for node, output, exit_code in result:
177 print ("------------------------------------------------")
178 print ("node: %s" % node)
179 print ("%s" % output)
180 print ("return code = %s" % exit_code)
183 def VerifyCluster(opts, args):
184 """Verify integrity of cluster, performing various test on nodes.
187 opts - class with options as members
190 op = opcodes.OpVerifyCluster()
191 result = SubmitOpCode(op)
195 def VerifyDisks(opts, args):
196 """Verify integrity of cluster disks.
199 opts - class with options as members
202 op = opcodes.OpVerifyDisks()
203 result = SubmitOpCode(op)
204 if not isinstance(result, tuple) or len(result) != 4:
205 raise errors.ProgrammerError("Unknown result type for OpVerifyDisks")
207 nodes, nlvm, instances, missing = result
210 print "Nodes unreachable or with bad data:"
213 retcode = constants.EXIT_SUCCESS
216 for node, text in nlvm.iteritems():
217 print ("Error on node %s: LVM error: %s" %
218 (node, text[-400:].encode('string_escape')))
220 print "You need to fix these nodes first before fixing instances"
223 for iname in instances:
226 op = opcodes.OpActivateInstanceDisks(instance_name=iname)
228 print "Activating disks for instance '%s'" % iname
230 except errors.GenericError, err:
231 nret, msg = FormatError(err)
233 print >> sys.stderr, ("Error activating disks for instance %s: %s" %
237 for iname, ival in missing.iteritems():
238 all_missing = utils.all(ival, lambda x: x[0] in nlvm)
240 print ("Instance %s cannot be verified as it lives on"
241 " broken nodes" % iname)
243 print "Instance %s has missing logical volumes:" % iname
245 for node, vol in ival:
247 print ("\tbroken node %s /dev/xenvg/%s" % (node, vol))
249 print ("\t%s /dev/xenvg/%s" % (node, vol))
250 print ("You need to run replace_disks for all the above"
251 " instances, if this message persist after fixing nodes.")
257 def MasterFailover(opts, args):
258 """Failover the master node.
260 This command, when run on a non-master node, will cause the current
261 master to cease being master, and the non-master to become new
265 op = opcodes.OpMasterFailover()
269 def SearchTags(opts, args):
270 """Searches the tags on all the cluster.
273 op = opcodes.OpSearchTags(pattern=args[0])
274 result = SubmitOpCode(op)
277 result = list(result)
279 for path, tag in result:
280 print "%s %s" % (path, tag)
283 def SetClusterParams(opts, args):
284 """Modify the cluster.
287 opts - class with options as members
290 if not (not opts.lvm_storage or opts.vg_name):
291 print "Please give at least one of the parameters."
294 vg_name = opts.vg_name
295 if not opts.lvm_storage and opts.vg_name:
296 print ("Options --no-lvm-storage and --vg-name conflict.")
299 op = opcodes.OpSetClusterParams(vg_name=opts.vg_name)
304 # this is an option common to more than one command, so we declare
305 # it here and reuse it
306 node_option = make_option("-n", "--node", action="append", dest="nodes",
307 help="Node to copy to (if not given, all nodes),"
308 " can be given multiple times",
309 metavar="<node>", default=[])
312 'init': (InitCluster, ARGS_ONE,
314 make_option("-s", "--secondary-ip", dest="secondary_ip",
315 help="Specify the secondary ip for this node;"
316 " if given, the entire cluster must have secondary"
318 metavar="ADDRESS", default=None),
319 make_option("-t", "--hypervisor-type", dest="hypervisor_type",
320 help="Specify the hypervisor type "
321 "(xen-3.0, fake, xen-hvm-3.1)",
322 metavar="TYPE", choices=["xen-3.0",
326 make_option("-m", "--mac-prefix", dest="mac_prefix",
327 help="Specify the mac prefix for the instance IP"
328 " addresses, in the format XX:XX:XX",
330 default="aa:00:00",),
331 make_option("-g", "--vg-name", dest="vg_name",
332 help="Specify the volume group name "
333 " (cluster-wide) for disk allocation [xenvg]",
336 make_option("-b", "--bridge", dest="def_bridge",
337 help="Specify the default bridge name (cluster-wide)"
338 " to connect the instances to [%s]" %
339 constants.DEFAULT_BRIDGE,
341 default=constants.DEFAULT_BRIDGE,),
342 make_option("--master-netdev", dest="master_netdev",
343 help="Specify the node interface (cluster-wide)"
344 " on which the master IP address will be added "
345 " [%s]" % constants.DEFAULT_BRIDGE,
347 default=constants.DEFAULT_BRIDGE,),
348 make_option("--file-storage-dir", dest="file_storage_dir",
349 help="Specify the default directory (cluster-wide)"
350 " for storing the file-based disks [%s]" %
351 constants.DEFAULT_FILE_STORAGE_DIR,
353 default=constants.DEFAULT_FILE_STORAGE_DIR,),
354 make_option("--no-lvm-storage", dest="lvm_storage",
355 help="No support for lvm based instances"
357 action="store_false", default=True,),
359 "[opts...] <cluster_name>",
360 "Initialises a new cluster configuration"),
361 'destroy': (DestroyCluster, ARGS_NONE,
363 make_option("--yes-do-it", dest="yes_do_it",
364 help="Destroy cluster",
365 action="store_true"),
367 "", "Destroy cluster"),
368 'rename': (RenameCluster, ARGS_ONE, [DEBUG_OPT, FORCE_OPT],
370 "Renames the cluster"),
371 'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT],
372 "", "Does a check on the cluster configuration"),
373 'verify-disks': (VerifyDisks, ARGS_NONE, [DEBUG_OPT],
374 "", "Does a check on the cluster disk status"),
375 'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT],
376 "", "Makes the current node the master"),
377 'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT],
378 "", "Shows the cluster version"),
379 'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT],
380 "", "Shows the cluster master"),
381 'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option],
382 "[-n node...] <filename>",
383 "Copies a file to all (or only some) nodes"),
384 'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option],
385 "[-n node...] <command>",
386 "Runs a command on all (or only some) nodes"),
387 'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT],
388 "", "Show cluster configuration"),
389 'list-tags': (ListTags, ARGS_NONE,
390 [DEBUG_OPT], "", "List the tags of the cluster"),
391 'add-tags': (AddTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
392 "tag...", "Add tags to the cluster"),
393 'remove-tags': (RemoveTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
394 "tag...", "Remove tags from the cluster"),
395 'search-tags': (SearchTags, ARGS_ONE,
396 [DEBUG_OPT], "", "Searches the tags on all objects on"
397 " the cluster for a given pattern (regex)"),
398 'modify': (SetClusterParams, ARGS_NONE,
400 make_option("-g", "--vg-name", dest="vg_name",
401 help="Specify the volume group name "
402 " (cluster-wide) for disk allocation "
403 "and enable lvm based storage",
405 make_option("--no-lvm-storage", dest="lvm_storage",
406 help="Disable support for lvm based instances"
408 action="store_false", default=True,),
411 "Alters the parameters of the cluster"),
414 if __name__ == '__main__':
415 sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER}))