Revision 7232c04c scripts/gnt-instance
b/scripts/gnt-instance | ||
---|---|---|
47 | 47 |
|
48 | 48 |
_VALUE_TRUE = "true" |
49 | 49 |
|
50 |
#: default list of options for L{ListInstances} |
|
50 | 51 |
_LIST_DEF_FIELDS = [ |
51 | 52 |
"name", "hypervisor", "os", "pnode", "status", "oper_ram", |
52 | 53 |
] |
... | ... | |
55 | 56 |
def _ExpandMultiNames(mode, names): |
56 | 57 |
"""Expand the given names using the passed mode. |
57 | 58 |
|
58 |
Args: |
|
59 |
- mode, which can be one of _SHUTDOWN_CLUSTER, _SHUTDOWN_NODES_BOTH, |
|
60 |
_SHUTDOWN_NODES_PRI, _SHUTDOWN_NODES_SEC or _SHUTDOWN_INSTANCES |
|
61 |
- names, which is a list of names; for cluster, it must be empty, |
|
62 |
and for node and instance it must be a list of valid item |
|
63 |
names (short names are valid as usual, e.g. node1 instead of |
|
64 |
node1.example.com) |
|
65 |
|
|
66 | 59 |
For _SHUTDOWN_CLUSTER, all instances will be returned. For |
67 | 60 |
_SHUTDOWN_NODES_PRI/SEC, all instances having those nodes as |
68 |
primary/secondary will be shutdown. For _SHUTDOWN_NODES_BOTH, all
|
|
61 |
primary/secondary will be returned. For _SHUTDOWN_NODES_BOTH, all
|
|
69 | 62 |
instances having those nodes as either primary or secondary will be |
70 | 63 |
returned. For _SHUTDOWN_INSTANCES, the given instances will be |
71 | 64 |
returned. |
72 | 65 |
|
66 |
@param mode: one of L{_SHUTDOWN_CLUSTER}, L{_SHUTDOWN_NODES_BOTH}, |
|
67 |
L{_SHUTDOWN_NODES_PRI}, L{_SHUTDOWN_NODES_SEC} or |
|
68 |
L{_SHUTDOWN_INSTANCES} |
|
69 |
@param names: a list of names; for cluster, it must be empty, |
|
70 |
and for node and instance it must be a list of valid item |
|
71 |
names (short names are valid as usual, e.g. node1 instead of |
|
72 |
node1.example.com) |
|
73 |
@rtype: list |
|
74 |
@return: the list of names after the expansion |
|
75 |
@raise errors.ProgrammerError: for unknown selection type |
|
76 |
@raise errors.OpPrereqError: for invalid input parameters |
|
77 |
|
|
73 | 78 |
""" |
74 | 79 |
if mode == _SHUTDOWN_CLUSTER: |
75 | 80 |
if names: |
... | ... | |
117 | 122 |
This function is used to request confirmation for doing an operation |
118 | 123 |
on a given list of instances. |
119 | 124 |
|
120 |
The inames argument is what the selection algorithm computed, and |
|
121 |
the text argument is the operation we should tell the user to |
|
122 |
confirm (e.g. 'shutdown' or 'startup'). |
|
123 |
|
|
124 |
Returns: boolean depending on user's confirmation. |
|
125 |
@type inames: list |
|
126 |
@param inames: the list of names that we display when |
|
127 |
we ask for confirmation |
|
128 |
@type text: str |
|
129 |
@param text: the operation that the user should confirm |
|
130 |
(e.g. I{shutdown} or I{startup}) |
|
131 |
@rtype: boolean |
|
132 |
@return: True or False depending on user's confirmation. |
|
125 | 133 |
|
126 | 134 |
""" |
127 | 135 |
count = len(inames) |
... | ... | |
172 | 180 |
def ListInstances(opts, args): |
173 | 181 |
"""List instances and their properties. |
174 | 182 |
|
183 |
@param opts: the command line options selected by the user |
|
184 |
@type args: list |
|
185 |
@param args: should be an empty list |
|
186 |
@rtype: int |
|
187 |
@return: the desired exit code |
|
188 |
|
|
175 | 189 |
""" |
176 | 190 |
if opts.output is None: |
177 | 191 |
selected_fields = _LIST_DEF_FIELDS |
... | ... | |
263 | 277 |
def AddInstance(opts, args): |
264 | 278 |
"""Add an instance to the cluster. |
265 | 279 |
|
266 |
Args: |
|
267 |
opts - class with options as members |
|
268 |
args - list with a single element, the instance name |
|
269 |
Opts used: |
|
270 |
mem - amount of memory to allocate to instance (MiB) |
|
271 |
size - amount of disk space to allocate to instance (MiB) |
|
272 |
os - which OS to run on instance |
|
273 |
node - node to run new instance on |
|
280 |
@param opts: the command line options selected by the user |
|
281 |
@type args: list |
|
282 |
@param args: should contain only one element, the new instance name |
|
283 |
@rtype: int |
|
284 |
@return: the desired exit code |
|
274 | 285 |
|
275 | 286 |
""" |
276 | 287 |
instance = args[0] |
... | ... | |
319 | 330 |
|
320 | 331 |
|
321 | 332 |
def BatchCreate(opts, args): |
322 |
"""Create instances on a batched base. |
|
323 |
|
|
324 |
This function reads a json with instances defined in the form: |
|
325 |
|
|
326 |
{"instance-name": {"disk_size": 25, |
|
327 |
"swap_size": 1024, |
|
328 |
"template": "drbd", |
|
329 |
"backend": { "memory": 512, |
|
330 |
"vcpus": 1 }, |
|
331 |
"os": "etch-image", |
|
332 |
"primary_node": "firstnode", |
|
333 |
"secondary_node": "secondnode", |
|
334 |
"iallocator": "dumb"}} |
|
335 |
|
|
336 |
primary_node and secondary_node has precedence over iallocator. |
|
337 |
|
|
338 |
Args: |
|
339 |
opts: The parsed command line options |
|
340 |
args: Argument passed to the command in our case the json file |
|
333 |
"""Create instances using a definition file. |
|
334 |
|
|
335 |
This function reads a json file with instances defined |
|
336 |
in the form:: |
|
337 |
|
|
338 |
{"instance-name":{ |
|
339 |
"disk_size": 25, |
|
340 |
"swap_size": 1024, |
|
341 |
"template": "drbd", |
|
342 |
"backend": { |
|
343 |
"memory": 512, |
|
344 |
"vcpus": 1 }, |
|
345 |
"os": "etch-image", |
|
346 |
"primary_node": "firstnode", |
|
347 |
"secondary_node": "secondnode", |
|
348 |
"iallocator": "dumb"} |
|
349 |
} |
|
350 |
|
|
351 |
Note that I{primary_node} and I{secondary_node} have precedence over |
|
352 |
I{iallocator}. |
|
353 |
|
|
354 |
@param opts: the command line options selected by the user |
|
355 |
@type args: list |
|
356 |
@param args: should contain one element, the json filename |
|
357 |
@rtype: int |
|
358 |
@return: the desired exit code |
|
341 | 359 |
|
342 | 360 |
""" |
343 | 361 |
_DEFAULT_SPECS = {"disk_size": 20 * 1024, |
... | ... | |
429 | 447 |
def ReinstallInstance(opts, args): |
430 | 448 |
"""Reinstall an instance. |
431 | 449 |
|
432 |
Args: |
|
433 |
opts - class with options as members |
|
434 |
args - list containing a single element, the instance name |
|
450 |
@param opts: the command line options selected by the user |
|
451 |
@type args: list |
|
452 |
@param args: should contain only one element, the name of the |
|
453 |
instance to be reinstalled |
|
454 |
@rtype: int |
|
455 |
@return: the desired exit code |
|
435 | 456 |
|
436 | 457 |
""" |
437 | 458 |
instance_name = args[0] |
... | ... | |
480 | 501 |
def RemoveInstance(opts, args): |
481 | 502 |
"""Remove an instance. |
482 | 503 |
|
483 |
Args: |
|
484 |
opts - class with options as members |
|
485 |
args - list containing a single element, the instance name |
|
504 |
@param opts: the command line options selected by the user |
|
505 |
@type args: list |
|
506 |
@param args: should contain only one element, the name of |
|
507 |
the instance to be removed |
|
508 |
@rtype: int |
|
509 |
@return: the desired exit code |
|
486 | 510 |
|
487 | 511 |
""" |
488 | 512 |
instance_name = args[0] |
... | ... | |
504 | 528 |
def RenameInstance(opts, args): |
505 | 529 |
"""Rename an instance. |
506 | 530 |
|
507 |
Args: |
|
508 |
opts - class with options as members |
|
509 |
args - list containing two elements, the instance name and the new name |
|
531 |
@param opts: the command line options selected by the user |
|
532 |
@type args: list |
|
533 |
@param args: should contain two elements, the old and the |
|
534 |
new instance names |
|
535 |
@rtype: int |
|
536 |
@return: the desired exit code |
|
510 | 537 |
|
511 | 538 |
""" |
512 | 539 |
op = opcodes.OpRenameInstance(instance_name=args[0], |
... | ... | |
520 | 547 |
"""Activate an instance's disks. |
521 | 548 |
|
522 | 549 |
This serves two purposes: |
523 |
- it allows one (as long as the instance is not running) to mount
|
|
524 |
the disks and modify them from the node
|
|
550 |
- it allows (as long as the instance is not running)
|
|
551 |
mounting the disks and modifying them from the node
|
|
525 | 552 |
- it repairs inactive secondary drbds |
526 | 553 |
|
554 |
@param opts: the command line options selected by the user |
|
555 |
@type args: list |
|
556 |
@param args: should contain only one element, the instance name |
|
557 |
@rtype: int |
|
558 |
@return: the desired exit code |
|
559 |
|
|
527 | 560 |
""" |
528 | 561 |
instance_name = args[0] |
529 | 562 |
op = opcodes.OpActivateInstanceDisks(instance_name=instance_name) |
... | ... | |
534 | 567 |
|
535 | 568 |
|
536 | 569 |
def DeactivateDisks(opts, args): |
537 |
"""Command-line interface for _ShutdownInstanceBlockDevices.
|
|
570 |
"""Deactivate an instance's disks..
|
|
538 | 571 |
|
539 | 572 |
This function takes the instance name, looks for its primary node |
540 | 573 |
and the tries to shutdown its block devices on that node. |
541 | 574 |
|
575 |
@param opts: the command line options selected by the user |
|
576 |
@type args: list |
|
577 |
@param args: should contain only one element, the instance name |
|
578 |
@rtype: int |
|
579 |
@return: the desired exit code |
|
580 |
|
|
542 | 581 |
""" |
543 | 582 |
instance_name = args[0] |
544 | 583 |
op = opcodes.OpDeactivateInstanceDisks(instance_name=instance_name) |
... | ... | |
547 | 586 |
|
548 | 587 |
|
549 | 588 |
def GrowDisk(opts, args): |
550 |
"""Command-line interface for _ShutdownInstanceBlockDevices.
|
|
589 |
"""Grow an instance's disks.
|
|
551 | 590 |
|
552 |
This function takes the instance name, looks for its primary node |
|
553 |
and the tries to shutdown its block devices on that node. |
|
591 |
@param opts: the command line options selected by the user |
|
592 |
@type args: list |
|
593 |
@param args: should contain two elements, the instance name |
|
594 |
whose disks we grow and the disk name, e.g. I{sda} |
|
595 |
@rtype: int |
|
596 |
@return: the desired exit code |
|
554 | 597 |
|
555 | 598 |
""" |
556 | 599 |
instance = args[0] |
... | ... | |
563 | 606 |
|
564 | 607 |
|
565 | 608 |
def StartupInstance(opts, args): |
566 |
"""Startup an instance.
|
|
609 |
"""Startup instances.
|
|
567 | 610 |
|
568 |
Args: |
|
569 |
opts - class with options as members |
|
570 |
args - list containing a single element, the instance name |
|
611 |
Depending on the options given, this will start one or more |
|
612 |
instances. |
|
613 |
|
|
614 |
@param opts: the command line options selected by the user |
|
615 |
@type args: list |
|
616 |
@param args: the instance or node names based on which we |
|
617 |
create the final selection (in conjunction with the |
|
618 |
opts argument) |
|
619 |
@rtype: int |
|
620 |
@return: the desired exit code |
|
571 | 621 |
|
572 | 622 |
""" |
573 | 623 |
if opts.multi_mode is None: |
... | ... | |
594 | 644 |
|
595 | 645 |
|
596 | 646 |
def RebootInstance(opts, args): |
597 |
"""Reboot an instance |
|
647 |
"""Reboot instance(s). |
|
648 |
|
|
649 |
Depending on the parameters given, this will reboot one or more |
|
650 |
instances. |
|
598 | 651 |
|
599 |
Args: |
|
600 |
opts - class with options as members |
|
601 |
args - list containing a single element, the instance name |
|
652 |
@param opts: the command line options selected by the user |
|
653 |
@type args: list |
|
654 |
@param args: the instance or node names based on which we |
|
655 |
create the final selection (in conjunction with the |
|
656 |
opts argument) |
|
657 |
@rtype: int |
|
658 |
@return: the desired exit code |
|
602 | 659 |
|
603 | 660 |
""" |
604 | 661 |
if opts.multi_mode is None: |
... | ... | |
622 | 679 |
def ShutdownInstance(opts, args): |
623 | 680 |
"""Shutdown an instance. |
624 | 681 |
|
625 |
Args: |
|
626 |
opts - class with options as members |
|
627 |
args - list containing a single element, the instance name |
|
682 |
@param opts: the command line options selected by the user |
|
683 |
@type args: list |
|
684 |
@param args: the instance or node names based on which we |
|
685 |
create the final selection (in conjunction with the |
|
686 |
opts argument) |
|
687 |
@rtype: int |
|
688 |
@return: the desired exit code |
|
628 | 689 |
|
629 | 690 |
""" |
630 | 691 |
if opts.multi_mode is None: |
... | ... | |
651 | 712 |
def ReplaceDisks(opts, args): |
652 | 713 |
"""Replace the disks of an instance |
653 | 714 |
|
654 |
Args: |
|
655 |
opts - class with options as members |
|
656 |
args - list with a single element, the instance name |
|
715 |
@param opts: the command line options selected by the user |
|
716 |
@type args: list |
|
717 |
@param args: should contain only one element, the instance name |
|
718 |
@rtype: int |
|
719 |
@return: the desired exit code |
|
657 | 720 |
|
658 | 721 |
""" |
659 | 722 |
instance_name = args[0] |
... | ... | |
687 | 750 |
The failover is done by shutting it down on its present node and |
688 | 751 |
starting it on the secondary. |
689 | 752 |
|
690 |
Args:
|
|
691 |
opts - class with options as members
|
|
692 |
args - list with a single element, the instance name
|
|
693 |
Opts used:
|
|
694 |
force - whether to failover without asking questions.
|
|
753 |
@param opts: the command line options selected by the user
|
|
754 |
@type args: list
|
|
755 |
@param args: should contain only one element, the instance name
|
|
756 |
@rtype: int
|
|
757 |
@return: the desired exit code
|
|
695 | 758 |
|
696 | 759 |
""" |
697 | 760 |
instance_name = args[0] |
... | ... | |
713 | 776 |
def ConnectToInstanceConsole(opts, args): |
714 | 777 |
"""Connect to the console of an instance. |
715 | 778 |
|
716 |
Args: |
|
717 |
opts - class with options as members |
|
718 |
args - list with a single element, the instance name |
|
779 |
@param opts: the command line options selected by the user |
|
780 |
@type args: list |
|
781 |
@param args: should contain only one element, the instance name |
|
782 |
@rtype: int |
|
783 |
@return: the desired exit code |
|
719 | 784 |
|
720 | 785 |
""" |
721 | 786 |
instance_name = args[0] |
... | ... | |
737 | 802 |
def _FormatBlockDevInfo(buf, dev, indent_level, static): |
738 | 803 |
"""Show block device information. |
739 | 804 |
|
740 |
This is only used by ShowInstanceConfig(), but it's too big to be
|
|
805 |
This is only used by L{ShowInstanceConfig}, but it's too big to be
|
|
741 | 806 |
left for an inline definition. |
742 | 807 |
|
808 |
@type buf: StringIO |
|
809 |
@param buf: buffer that will accumulate the output |
|
810 |
@type dev: dict |
|
811 |
@param dev: dictionary with disk information |
|
812 |
@type indent_level: int |
|
813 |
@param indent_level: the indendation level we are at, used for |
|
814 |
the layout of the device tree |
|
815 |
@type static: boolean |
|
816 |
@param static: wheter the device information doesn't contain |
|
817 |
runtime information but only static data |
|
818 |
|
|
743 | 819 |
""" |
744 | 820 |
def helper(buf, dtype, status): |
745 |
"""Format one line for physical device status.""" |
|
821 |
"""Format one line for physical device status. |
|
822 |
|
|
823 |
@type buf: StringIO |
|
824 |
@param buf: buffer that will accumulate the output |
|
825 |
@type dtype: str |
|
826 |
@param dtype: a constant from the L{constants.LDS_BLOCK} set |
|
827 |
@type status: tuple |
|
828 |
@param status: a tuple as returned from L{backend.FindBlockDevice} |
|
829 |
|
|
830 |
""" |
|
746 | 831 |
if not status: |
747 | 832 |
buf.write("not active\n") |
748 | 833 |
else: |
... | ... | |
810 | 895 |
def ShowInstanceConfig(opts, args): |
811 | 896 |
"""Compute instance run-time status. |
812 | 897 |
|
898 |
@param opts: the command line options selected by the user |
|
899 |
@type args: list |
|
900 |
@param args: either an empty list, and then we query all |
|
901 |
instances, or should contain a list of instance names |
|
902 |
@rtype: int |
|
903 |
@return: the desired exit code |
|
904 |
|
|
813 | 905 |
""" |
814 | 906 |
retcode = 0 |
815 | 907 |
op = opcodes.OpQueryInstanceData(instances=args, static=opts.static) |
... | ... | |
895 | 987 |
|
896 | 988 |
All parameters take effect only at the next restart of the instance. |
897 | 989 |
|
898 |
Args:
|
|
899 |
opts - class with options as members
|
|
900 |
args - list with a single element, the instance name
|
|
901 |
Opts used:
|
|
902 |
mac - the new MAC address of the instance
|
|
990 |
@param opts: the command line options selected by the user
|
|
991 |
@type args: list
|
|
992 |
@param args: should contain only one element, the instance name
|
|
993 |
@rtype: int
|
|
994 |
@return: the desired exit code
|
|
903 | 995 |
|
904 | 996 |
""" |
905 | 997 |
if not (opts.ip or opts.bridge or opts.mac or |
... | ... | |
1190 | 1282 |
"<instance_name> tag...", "Remove tags from given instance"), |
1191 | 1283 |
} |
1192 | 1284 |
|
1285 |
#: dictionary with aliases for commands |
|
1193 | 1286 |
aliases = { |
1194 | 1287 |
'activate_block_devs': 'activate-disks', |
1195 | 1288 |
'replace_disks': 'replace-disks', |
Also available in: Unified diff