Revision 0fb81174 lib/cmdlib.py
b/lib/cmdlib.py | ||
---|---|---|
1036 | 1036 |
return None |
1037 | 1037 |
|
1038 | 1038 |
|
1039 |
def _ComputeIPolicySpecViolation(ipolicy, mem_size, cpu_count, disk_count, |
|
1040 |
nic_count, disk_sizes, |
|
1041 |
_check_spec_fn=_CheckMinMaxSpecs): |
|
1042 |
"""Verifies ipolicy against provided specs. |
|
1043 |
|
|
1044 |
@type ipolicy: dict |
|
1045 |
@param ipolicy: The ipolicy |
|
1046 |
@type mem_size: int |
|
1047 |
@param mem_size: The memory size |
|
1048 |
@type cpu_count: int |
|
1049 |
@param cpu_count: Used cpu cores |
|
1050 |
@type disk_count: int |
|
1051 |
@param disk_count: Number of disks used |
|
1052 |
@type nic_count: int |
|
1053 |
@param nic_count: Number of nics used |
|
1054 |
@type disk_sizes: list of ints |
|
1055 |
@param disk_sizes: Disk sizes of used disk (len must match C{disk_count}) |
|
1056 |
@param _check_spec_fn: The checking function (unittest only) |
|
1057 |
@return: A list of violations, or an empty list of no violations are found |
|
1058 |
|
|
1059 |
""" |
|
1060 |
assert disk_count == len(disk_sizes) |
|
1061 |
|
|
1062 |
test_settings = [ |
|
1063 |
(constants.ISPEC_MEM_SIZE, mem_size), |
|
1064 |
(constants.ISPEC_CPU_COUNT, cpu_count), |
|
1065 |
(constants.ISPEC_DISK_COUNT, disk_count), |
|
1066 |
(constants.ISPEC_NIC_COUNT, nic_count), |
|
1067 |
] + map((lambda d: (constants.ISPEC_DISK_SIZE, d)), disk_sizes) |
|
1068 |
|
|
1069 |
return filter(None, |
|
1070 |
(_check_spec_fn(name, ipolicy, value) |
|
1071 |
for (name, value) in test_settings)) |
|
1072 |
|
|
1073 |
|
|
1074 |
def _ComputeIPolicyInstanceViolation(ipolicy, instance, |
|
1075 |
_compute_fn=_ComputeIPolicySpecViolation): |
|
1076 |
"""Compute if instance meets the specs of ipolicy. |
|
1077 |
|
|
1078 |
@type ipolicy: dict |
|
1079 |
@param ipolicy: The ipolicy to verify against |
|
1080 |
@type instance: L{objects.Instance} |
|
1081 |
@param instance: The instance to verify |
|
1082 |
@param _compute_fn: The function to verify ipolicy (unittest only) |
|
1083 |
@see: L{_ComputeIPolicySpecViolation} |
|
1084 |
|
|
1085 |
""" |
|
1086 |
mem_size = instance.beparams.get(constants.BE_MAXMEM, None) |
|
1087 |
cpu_count = instance.beparams.get(constants.BE_VCPUS, None) |
|
1088 |
disk_count = len(instance.disks) |
|
1089 |
disk_sizes = [disk.size for disk in instance.disks] |
|
1090 |
nic_count = len(instance.nics) |
|
1091 |
|
|
1092 |
return _compute_fn(ipolicy, mem_size, cpu_count, disk_count, nic_count, |
|
1093 |
disk_sizes) |
|
1094 |
|
|
1095 |
|
|
1096 |
def _ComputeIPolicyInstanceSpecViolation(ipolicy, instance_spec, |
|
1097 |
_compute_fn=_ComputeIPolicySpecViolation): |
|
1098 |
"""Compute if instance specs meets the specs of ipolicy. |
|
1099 |
|
|
1100 |
@type ipolicy: dict |
|
1101 |
@param ipolicy: The ipolicy to verify against |
|
1102 |
@param instance_spec: dict |
|
1103 |
@param instance_spec: The instance spec to verify |
|
1104 |
@param _compute_fn: The function to verify ipolicy (unittest only) |
|
1105 |
@see: L{_ComputeIPolicySpecViolation} |
|
1106 |
|
|
1107 |
""" |
|
1108 |
mem_size = instance_spec.get(constants.ISPEC_MEM_SIZE, None) |
|
1109 |
cpu_count = instance_spec.get(constants.ISPEC_CPU_COUNT, None) |
|
1110 |
disk_count = instance_spec.get(constants.ISPEC_DISK_COUNT, 0) |
|
1111 |
disk_sizes = instance_spec.get(constants.ISPEC_DISK_SIZE, []) |
|
1112 |
nic_count = instance_spec.get(constants.ISPEC_NIC_COUNT, 0) |
|
1113 |
|
|
1114 |
return _compute_fn(ipolicy, mem_size, cpu_count, disk_count, nic_count, |
|
1115 |
disk_sizes) |
|
1116 |
|
|
1117 |
|
|
1118 |
def _ComputeIPolicyNodeViolation(ipolicy, instance, current_group, |
|
1119 |
target_group, |
|
1120 |
_compute_fn=_ComputeIPolicyInstanceViolation): |
|
1121 |
"""Compute if instance meets the specs of the new target group. |
|
1122 |
|
|
1123 |
@param ipolicy: The ipolicy to verify |
|
1124 |
@param instance: The instance object to verify |
|
1125 |
@param current_group: The current group of the instance |
|
1126 |
@param target_group: The new group of the instance |
|
1127 |
@param _compute_fn: The function to verify ipolicy (unittest only) |
|
1128 |
@see: L{_ComputeIPolicySpecViolation} |
|
1129 |
|
|
1130 |
""" |
|
1131 |
if current_group == target_group: |
|
1132 |
return [] |
|
1133 |
else: |
|
1134 |
return _compute_fn(ipolicy, instance) |
|
1135 |
|
|
1136 |
|
|
1137 |
def _CheckTargetNodeIPolicy(lu, ipolicy, instance, node, ignore=False, |
|
1138 |
_compute_fn=_ComputeIPolicyNodeViolation): |
|
1139 |
"""Checks that the target node is correct in terms of instance policy. |
|
1140 |
|
|
1141 |
@param ipolicy: The ipolicy to verify |
|
1142 |
@param instance: The instance object to verify |
|
1143 |
@param node: The new node to relocate |
|
1144 |
@param ignore: Ignore violations of the ipolicy |
|
1145 |
@param _compute_fn: The function to verify ipolicy (unittest only) |
|
1146 |
@see: L{_ComputeIPolicySpecViolation} |
|
1147 |
|
|
1148 |
""" |
|
1149 |
res = _compute_fn(ipolicy, instance, instance.primary_node.group, node.group) |
|
1150 |
|
|
1151 |
if res: |
|
1152 |
msg = ("Instance does not meet target node group's (%s) instance" |
|
1153 |
" policy: %s") % (node.group, utils.CommaJoin(res)) |
|
1154 |
if ignore: |
|
1155 |
lu.LogWarning(msg) |
|
1156 |
else: |
|
1157 |
raise errors.OpPrereqError(msg, errors.ECODE_INVAL) |
|
1158 |
|
|
1159 |
|
|
1039 | 1160 |
def _ExpandItemName(fn, name, kind): |
1040 | 1161 |
"""Expand an item name. |
1041 | 1162 |
|
... | ... | |
1249 | 1370 |
return mc_now < mc_should |
1250 | 1371 |
|
1251 | 1372 |
|
1252 |
def _CalculateGroupIPolicy(cfg, group):
|
|
1373 |
def _CalculateGroupIPolicy(cluster, group):
|
|
1253 | 1374 |
"""Calculate instance policy for group. |
1254 | 1375 |
|
1255 | 1376 |
""" |
1256 |
cluster = cfg.GetClusterInfo() |
|
1257 | 1377 |
return cluster.SimpleFillIPolicy(group.ipolicy) |
1258 | 1378 |
|
1259 | 1379 |
|
Also available in: Unified diff