Revision efcfa99d lib/cmdlib.py
b/lib/cmdlib.py | ||
---|---|---|
11817 | 11817 |
return [(op, idx, params, fn()) for (op, idx, params) in mods] |
11818 | 11818 |
|
11819 | 11819 |
|
11820 |
#: Type description for changes as returned by L{ApplyContainerMods}'s |
|
11821 |
#: callbacks |
|
11822 |
_TApplyContModsCbChanges = \ |
|
11823 |
ht.TMaybeListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([ |
|
11824 |
ht.TNonEmptyString, |
|
11825 |
ht.TAny, |
|
11826 |
]))) |
|
11827 |
|
|
11828 |
|
|
11820 | 11829 |
def ApplyContainerMods(kind, container, chgdesc, mods, |
11821 | 11830 |
create_fn, modify_fn, remove_fn): |
11822 | 11831 |
"""Applies descriptions in C{mods} to C{container}. |
... | ... | |
11831 | 11840 |
@param mods: Modifications as returned by L{PrepareContainerMods} |
11832 | 11841 |
@type create_fn: callable |
11833 | 11842 |
@param create_fn: Callback for creating a new item (L{constants.DDM_ADD}); |
11834 |
receives absolute item index, parameters, list of applied changes and |
|
11835 |
private data object as added by L{PrepareContainerMods}, returns new item |
|
11843 |
receives absolute item index, parameters and private data object as added |
|
11844 |
by L{PrepareContainerMods}, returns tuple containing new item and changes |
|
11845 |
as list |
|
11836 | 11846 |
@type modify_fn: callable |
11837 | 11847 |
@param modify_fn: Callback for modifying an existing item |
11838 |
(L{constants.DDM_MODIFY}); receives absolute item index, item, parameters,
|
|
11839 |
list of applied changes and private data object as added by
|
|
11840 |
L{PrepareContainerMods}
|
|
11848 |
(L{constants.DDM_MODIFY}); receives absolute item index, item, parameters |
|
11849 |
and private data object as added by L{PrepareContainerMods}, returns
|
|
11850 |
changes as list
|
|
11841 | 11851 |
@type remove_fn: callable |
11842 | 11852 |
@param remove_fn: Callback on removing item; receives absolute item index, |
11843 |
item, list of applied changes and private data object as added by |
|
11844 |
L{PrepareContainerMods} |
|
11853 |
item and private data object as added by L{PrepareContainerMods} |
|
11845 | 11854 |
|
11846 | 11855 |
""" |
11847 | 11856 |
for (op, idx, params, private) in mods: |
... | ... | |
11853 | 11862 |
else: |
11854 | 11863 |
absidx = idx |
11855 | 11864 |
|
11865 |
changes = None |
|
11866 |
|
|
11856 | 11867 |
if op == constants.DDM_ADD: |
11857 | 11868 |
if create_fn is None: |
11858 | 11869 |
item = params |
11859 | 11870 |
else: |
11860 |
item = create_fn(absidx + 1, params, chgdesc, private)
|
|
11871 |
(item, changes) = create_fn(absidx + 1, params, private)
|
|
11861 | 11872 |
|
11862 | 11873 |
if idx == -1: |
11863 | 11874 |
container.append(item) |
... | ... | |
11876 | 11887 |
assert not params |
11877 | 11888 |
|
11878 | 11889 |
if remove_fn is not None: |
11879 |
remove_fn(absidx, item, chgdesc, private)
|
|
11890 |
remove_fn(absidx, item, private) |
|
11880 | 11891 |
|
11881 |
if chgdesc is not None: |
|
11882 |
chgdesc.append(("%s/%s" % (kind, absidx), "remove")) |
|
11892 |
changes = [("%s/%s" % (kind, absidx), "remove")] |
|
11883 | 11893 |
|
11884 | 11894 |
assert container[absidx] == item |
11885 | 11895 |
del container[absidx] |
11886 | 11896 |
elif op == constants.DDM_MODIFY: |
11887 | 11897 |
if modify_fn is not None: |
11888 |
modify_fn(absidx, item, params, chgdesc, private)
|
|
11898 |
changes = modify_fn(absidx, item, params, private)
|
|
11889 | 11899 |
else: |
11890 | 11900 |
raise errors.ProgrammerError("Unhandled operation '%s'" % op) |
11891 | 11901 |
|
11902 |
assert _TApplyContModsCbChanges(changes) |
|
11903 |
|
|
11904 |
if not (chgdesc is None or changes is None): |
|
11905 |
chgdesc.extend(changes) |
|
11906 |
|
|
11892 | 11907 |
|
11893 | 11908 |
class _InstNicModPrivate: |
11894 | 11909 |
"""Data structure for network interface modifications. |
... | ... | |
12408 | 12423 |
" diskless instances", |
12409 | 12424 |
errors.ECODE_INVAL) |
12410 | 12425 |
|
12411 |
def _PrepareNicCreate(absidx, params, chgdesc, private): |
|
12412 |
# pylint: disable=W0613 |
|
12426 |
def _PrepareNicCreate(_, params, private): |
|
12413 | 12427 |
return self._PrepareNicModification(params, private, None, {}, |
12414 | 12428 |
cluster, pnode) |
12415 | 12429 |
|
12416 |
def _PrepareNicMod(absidx, nic, params, chgdesc, private): |
|
12417 |
# pylint: disable=W0613 |
|
12430 |
def _PrepareNicMod(_, nic, params, private): |
|
12418 | 12431 |
return self._PrepareNicModification(params, private, nic.ip, |
12419 | 12432 |
nic.nicparams, cluster, pnode) |
12420 | 12433 |
|
... | ... | |
12566 | 12579 |
|
12567 | 12580 |
# Node resource locks will be released by caller |
12568 | 12581 |
|
12569 |
def _CreateNewDisk(self, idx, params, chgdesc, _):
|
|
12582 |
def _CreateNewDisk(self, idx, params, _): |
|
12570 | 12583 |
"""Creates a new disk. |
12571 | 12584 |
|
12572 | 12585 |
""" |
... | ... | |
12599 | 12612 |
self.LogWarning("Failed to create volume %s (%s) on node '%s': %s", |
12600 | 12613 |
disk.iv_name, disk, node, err) |
12601 | 12614 |
|
12602 |
chgdesc.append(("disk/%d" % idx, |
|
12603 |
"add:size=%s,mode=%s" % |
|
12604 |
(disk.size, disk.mode))) |
|
12605 |
|
|
12606 |
return disk |
|
12615 |
return (disk, [ |
|
12616 |
("disk/%d" % idx, "add:size=%s,mode=%s" % (disk.size, disk.mode)), |
|
12617 |
]) |
|
12607 | 12618 |
|
12608 | 12619 |
@staticmethod |
12609 |
def _ModifyDisk(idx, disk, params, chgdesc, _):
|
|
12620 |
def _ModifyDisk(idx, disk, params, _): |
|
12610 | 12621 |
"""Modifies a disk. |
12611 | 12622 |
|
12612 | 12623 |
""" |
12613 | 12624 |
disk.mode = params[constants.IDISK_MODE] |
12614 |
chgdesc.append(("disk.mode/%d" % idx, disk.mode)) |
|
12615 | 12625 |
|
12616 |
def _RemoveDisk(self, idx, root, chgdesc, private): |
|
12626 |
return [ |
|
12627 |
("disk.mode/%d" % idx, disk.mode), |
|
12628 |
] |
|
12629 |
|
|
12630 |
def _RemoveDisk(self, idx, root, _): |
|
12617 | 12631 |
"""Removes a disk. |
12618 | 12632 |
|
12619 | 12633 |
""" |
12620 |
# pylint: disable=W0613 |
|
12621 | 12634 |
for node, disk in root.ComputeNodeTree(self.instance.primary_node): |
12622 | 12635 |
self.cfg.SetDiskID(disk, node) |
12623 | 12636 |
msg = self.rpc.call_blockdev_remove(node, disk).fail_msg |
... | ... | |
12630 | 12643 |
self.cfg.AddTcpUdpPort(root.logical_id[2]) |
12631 | 12644 |
|
12632 | 12645 |
@staticmethod |
12633 |
def _CreateNewNic(idx, params, chgdesc, private):
|
|
12646 |
def _CreateNewNic(idx, params, private): |
|
12634 | 12647 |
"""Creates data structure for a new network interface. |
12635 | 12648 |
|
12636 | 12649 |
""" |
... | ... | |
12638 | 12651 |
ip = params.get(constants.INIC_IP, None) |
12639 | 12652 |
nicparams = private.params |
12640 | 12653 |
|
12641 |
chgdesc.append(("nic.%d" % idx,
|
|
12642 |
"add:mac=%s,ip=%s,mode=%s,link=%s" %
|
|
12643 |
(mac, ip, private.filled[constants.NIC_MODE],
|
|
12644 |
private.filled[constants.NIC_LINK])))
|
|
12645 |
|
|
12646 |
return objects.NIC(mac=mac, ip=ip, nicparams=nicparams)
|
|
12654 |
return (objects.NIC(mac=mac, ip=ip, nicparams=nicparams), [
|
|
12655 |
("nic.%d" % idx,
|
|
12656 |
"add:mac=%s,ip=%s,mode=%s,link=%s" %
|
|
12657 |
(mac, ip, private.filled[constants.NIC_MODE],
|
|
12658 |
private.filled[constants.NIC_LINK])), |
|
12659 |
])
|
|
12647 | 12660 |
|
12648 | 12661 |
@staticmethod |
12649 |
def _ApplyNicMods(idx, nic, params, chgdesc, private):
|
|
12662 |
def _ApplyNicMods(idx, nic, params, private): |
|
12650 | 12663 |
"""Modifies a network interface. |
12651 | 12664 |
|
12652 | 12665 |
""" |
12666 |
changes = [] |
|
12667 |
|
|
12653 | 12668 |
for key in [constants.INIC_MAC, constants.INIC_IP]: |
12654 | 12669 |
if key in params: |
12655 |
chgdesc.append(("nic.%s/%d" % (key, idx), params[key]))
|
|
12670 |
changes.append(("nic.%s/%d" % (key, idx), params[key]))
|
|
12656 | 12671 |
setattr(nic, key, params[key]) |
12657 | 12672 |
|
12658 | 12673 |
if private.params: |
12659 | 12674 |
nic.nicparams = private.params |
12660 | 12675 |
|
12661 | 12676 |
for (key, val) in params.items(): |
12662 |
chgdesc.append(("nic.%s/%d" % (key, idx), val)) |
|
12677 |
changes.append(("nic.%s/%d" % (key, idx), val)) |
|
12678 |
|
|
12679 |
return changes |
|
12663 | 12680 |
|
12664 | 12681 |
def Exec(self, feedback_fn): |
12665 | 12682 |
"""Modifies an instance. |
Also available in: Unified diff