Revision 312ac745 scripts/gnt-instance
b/scripts/gnt-instance | ||
---|---|---|
21 | 21 |
|
22 | 22 |
import sys |
23 | 23 |
import os |
24 |
import itertools |
|
24 | 25 |
from optparse import make_option |
25 | 26 |
from cStringIO import StringIO |
26 | 27 |
|
... | ... | |
29 | 30 |
from ganeti import logger |
30 | 31 |
from ganeti import constants |
31 | 32 |
from ganeti import utils |
33 |
from ganeti import errors |
|
34 |
|
|
35 |
|
|
36 |
_SHUTDOWN_CLUSTER = "cluster" |
|
37 |
_SHUTDOWN_NODES_BOTH = "nodes" |
|
38 |
_SHUTDOWN_NODES_PRI = "nodes-pri" |
|
39 |
_SHUTDOWN_NODES_SEC = "nodes-sec" |
|
40 |
_SHUTDOWN_INSTANCES = "instances" |
|
41 |
|
|
42 |
def _ExpandMultiNames(mode, names): |
|
43 |
"""Expand the given names using the passed mode. |
|
44 |
|
|
45 |
Args: |
|
46 |
- mode, which can be one of _SHUTDOWN_CLUSTER, _SHUTDOWN_NODES_BOTH, |
|
47 |
_SHUTDOWN_NODES_PRI, _SHUTDOWN_NODES_SEC or _SHUTDOWN_INSTANCES |
|
48 |
- names, which is a list of names; for cluster, it must be empty, |
|
49 |
and for node and instance it must be a list of valid item |
|
50 |
names (short names are valid as usual, e.g. node1 instead of |
|
51 |
node1.example.com) |
|
52 |
|
|
53 |
For _SHUTDOWN_CLUSTER, all instances will be returned. For |
|
54 |
_SHUTDOWN_NODES_PRI/SEC, all instances having those nodes as |
|
55 |
primary/secondary will be shutdown. For _SHUTDOWN_NODES_BOTH, all |
|
56 |
instances having those nodes as either primary or secondary will be |
|
57 |
returned. For _SHUTDOWN_INSTANCES, the given instances will be |
|
58 |
returned. |
|
59 |
|
|
60 |
""" |
|
61 |
if mode == _SHUTDOWN_CLUSTER: |
|
62 |
if names: |
|
63 |
raise errors.OpPrereqError("Cluster filter mode takes no arguments") |
|
64 |
op = opcodes.OpQueryInstances(output_fields=["name"], names=[]) |
|
65 |
idata = SubmitOpCode(op) |
|
66 |
inames = [row[0] for row in idata] |
|
67 |
|
|
68 |
elif mode in (_SHUTDOWN_NODES_BOTH, |
|
69 |
_SHUTDOWN_NODES_PRI, |
|
70 |
_SHUTDOWN_NODES_SEC): |
|
71 |
if not names: |
|
72 |
raise errors.OpPrereqError("No node names passed") |
|
73 |
op = opcodes.OpQueryNodes(output_fields=["name", "pinst_list", |
|
74 |
"sinst_list"], names=names) |
|
75 |
ndata = SubmitOpCode(op) |
|
76 |
ipri = [row[1] for row in ndata] |
|
77 |
pri_names = list(itertools.chain(*ipri)) |
|
78 |
isec = [row[2] for row in ndata] |
|
79 |
sec_names = list(itertools.chain(*isec)) |
|
80 |
if mode == _SHUTDOWN_NODES_BOTH: |
|
81 |
inames = pri_names + sec_names |
|
82 |
elif mode == _SHUTDOWN_NODES_PRI: |
|
83 |
inames = pri_names |
|
84 |
elif mode == _SHUTDOWN_NODES_SEC: |
|
85 |
inames = sec_names |
|
86 |
else: |
|
87 |
raise errors.ProgrammerError("Unhandled shutdown type") |
|
88 |
|
|
89 |
elif mode == _SHUTDOWN_INSTANCES: |
|
90 |
if not names: |
|
91 |
raise errors.OpPrereqError("No instance names passed") |
|
92 |
op = opcodes.OpQueryInstances(output_fields=["name"], names=names) |
|
93 |
idata = SubmitOpCode(op) |
|
94 |
inames = [row[0] for row in idata] |
|
95 |
|
|
96 |
else: |
|
97 |
raise errors.OpPrereqError("Unknown mode '%s'" % mode) |
|
98 |
|
|
99 |
return inames |
|
32 | 100 |
|
33 | 101 |
|
34 | 102 |
def ListInstances(opts, args): |
... | ... | |
209 | 277 |
args - list containing a single element, the instance name |
210 | 278 |
|
211 | 279 |
""" |
212 |
instance_name = args[0] |
|
213 |
op = opcodes.OpStartupInstance(instance_name=instance_name, force=opts.force, |
|
214 |
extra_args=opts.extra_args) |
|
215 |
SubmitOpCode(op) |
|
280 |
if opts.multi_mode is None: |
|
281 |
opts.multi_mode = _SHUTDOWN_INSTANCES |
|
282 |
inames = _ExpandMultiNames(opts.multi_mode, args) |
|
283 |
multi_on = len(inames) > 1 |
|
284 |
for name in inames: |
|
285 |
op = opcodes.OpStartupInstance(instance_name=name, |
|
286 |
force=opts.force, |
|
287 |
extra_args=opts.extra_args) |
|
288 |
if multi_on: |
|
289 |
logger.ToStdout("Starting up %s" % name) |
|
290 |
SubmitOpCode(op) |
|
216 | 291 |
return 0 |
217 | 292 |
|
218 | 293 |
|
... | ... | |
224 | 299 |
args - list containing a single element, the instance name |
225 | 300 |
|
226 | 301 |
""" |
227 |
instance_name = args[0] |
|
228 |
op = opcodes.OpShutdownInstance(instance_name=instance_name) |
|
229 |
SubmitOpCode(op) |
|
302 |
if opts.multi_mode is None: |
|
303 |
opts.multi_mode = _SHUTDOWN_INSTANCES |
|
304 |
inames = _ExpandMultiNames(opts.multi_mode, args) |
|
305 |
multi_on = len(inames) > 1 |
|
306 |
for name in inames: |
|
307 |
op = opcodes.OpShutdownInstance(instance_name=name) |
|
308 |
if multi_on: |
|
309 |
logger.ToStdout("Shutting down %s" % name) |
|
310 |
SubmitOpCode(op) |
|
230 | 311 |
return 0 |
231 | 312 |
|
232 | 313 |
|
... | ... | |
455 | 536 |
os_opt = cli_option("-o", "--os-type", dest="os", help="What OS to run", |
456 | 537 |
metavar="<os>") |
457 | 538 |
|
539 |
# multi-instance selection options |
|
540 |
m_pri_node_opt = make_option("--primary", dest="multi_mode", |
|
541 |
help="Filter by nodes (primary only)", |
|
542 |
const=_SHUTDOWN_NODES_PRI, action="store_const") |
|
543 |
|
|
544 |
m_sec_node_opt = make_option("--secondary", dest="multi_mode", |
|
545 |
help="Filter by nodes (secondary only)", |
|
546 |
const=_SHUTDOWN_NODES_SEC, action="store_const") |
|
547 |
|
|
548 |
m_node_opt = make_option("--node", dest="multi_mode", |
|
549 |
help="Filter by nodes (primary and secondary)", |
|
550 |
const=_SHUTDOWN_NODES_BOTH, action="store_const") |
|
551 |
|
|
552 |
m_clust_opt = make_option("--all", dest="multi_mode", |
|
553 |
help="Select all instances in the cluster", |
|
554 |
const=_SHUTDOWN_CLUSTER, action="store_const") |
|
555 |
|
|
556 |
m_inst_opt = make_option("--instance", dest="multi_mode", |
|
557 |
help="Filter by instance name [default]", |
|
558 |
const=_SHUTDOWN_INSTANCES, action="store_const") |
|
559 |
|
|
560 |
|
|
458 | 561 |
# this is defined separately due to readability only |
459 | 562 |
add_opts = [ |
460 | 563 |
DEBUG_OPT, |
... | ... | |
555 | 658 |
default=None, type="string", metavar="<bridge>") |
556 | 659 |
], |
557 | 660 |
"<instance>", "Alters the parameters of an instance"), |
558 |
'shutdown': (ShutdownInstance, ARGS_ONE, [DEBUG_OPT], |
|
661 |
'shutdown': (ShutdownInstance, ARGS_ANY, |
|
662 |
[DEBUG_OPT, m_node_opt, m_pri_node_opt, m_sec_node_opt, |
|
663 |
m_clust_opt, m_inst_opt], |
|
559 | 664 |
"<instance>", "Stops an instance"), |
560 |
'startup': (StartupInstance, ARGS_ONE,
|
|
665 |
'startup': (StartupInstance, ARGS_ANY,
|
|
561 | 666 |
[DEBUG_OPT, FORCE_OPT, |
562 | 667 |
make_option("-e", "--extra", dest="extra_args", |
563 | 668 |
help="Extra arguments for the instance's kernel", |
564 | 669 |
default=None, type="string", metavar="<PARAMS>"), |
670 |
m_node_opt, m_pri_node_opt, m_sec_node_opt, |
|
671 |
m_clust_opt, m_inst_opt, |
|
565 | 672 |
], |
566 | 673 |
"<instance>", "Starts an instance"), |
567 | 674 |
'activate-disks': (ActivateDisks, ARGS_ONE, [DEBUG_OPT], |
Also available in: Unified diff