root / lib / cmdlib / common.py @ 178ad717
History | View | Annotate | Download (45 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
|
5 |
#
|
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.
|
10 |
#
|
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.
|
15 |
#
|
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
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Common functions used by multiple logical units."""
|
23 |
|
24 |
import copy |
25 |
import os |
26 |
|
27 |
from ganeti import compat |
28 |
from ganeti import constants |
29 |
from ganeti import errors |
30 |
from ganeti import hypervisor |
31 |
from ganeti import locking |
32 |
from ganeti import objects |
33 |
from ganeti import opcodes |
34 |
from ganeti import pathutils |
35 |
import ganeti.rpc.node as rpc |
36 |
from ganeti import ssconf |
37 |
from ganeti import utils |
38 |
|
39 |
|
40 |
# States of instance
|
41 |
INSTANCE_DOWN = [constants.ADMINST_DOWN] |
42 |
INSTANCE_ONLINE = [constants.ADMINST_DOWN, constants.ADMINST_UP] |
43 |
INSTANCE_NOT_RUNNING = [constants.ADMINST_DOWN, constants.ADMINST_OFFLINE] |
44 |
|
45 |
#: Instance status in which an instance can be marked as offline/online
|
46 |
CAN_CHANGE_INSTANCE_OFFLINE = (frozenset(INSTANCE_DOWN) | frozenset([ |
47 |
constants.ADMINST_OFFLINE, |
48 |
])) |
49 |
|
50 |
|
51 |
def _ExpandItemName(expand_fn, name, kind): |
52 |
"""Expand an item name.
|
53 |
|
54 |
@param expand_fn: the function to use for expansion
|
55 |
@param name: requested item name
|
56 |
@param kind: text description ('Node' or 'Instance')
|
57 |
@return: the result of the expand_fn, if successful
|
58 |
@raise errors.OpPrereqError: if the item is not found
|
59 |
|
60 |
"""
|
61 |
(uuid, full_name) = expand_fn(name) |
62 |
if uuid is None or full_name is None: |
63 |
raise errors.OpPrereqError("%s '%s' not known" % (kind, name), |
64 |
errors.ECODE_NOENT) |
65 |
return (uuid, full_name)
|
66 |
|
67 |
|
68 |
def ExpandInstanceUuidAndName(cfg, expected_uuid, name): |
69 |
"""Wrapper over L{_ExpandItemName} for instance."""
|
70 |
(uuid, full_name) = _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
|
71 |
if expected_uuid is not None and uuid != expected_uuid: |
72 |
raise errors.OpPrereqError(
|
73 |
"The instances UUID '%s' does not match the expected UUID '%s' for"
|
74 |
" instance '%s'. Maybe the instance changed since you submitted this"
|
75 |
" job." % (uuid, expected_uuid, full_name), errors.ECODE_NOTUNIQUE)
|
76 |
return (uuid, full_name)
|
77 |
|
78 |
|
79 |
def ExpandNodeUuidAndName(cfg, expected_uuid, name): |
80 |
"""Expand a short node name into the node UUID and full name.
|
81 |
|
82 |
@type cfg: L{config.ConfigWriter}
|
83 |
@param cfg: The cluster configuration
|
84 |
@type expected_uuid: string
|
85 |
@param expected_uuid: expected UUID for the node (or None if there is no
|
86 |
expectation). If it does not match, a L{errors.OpPrereqError} is
|
87 |
raised.
|
88 |
@type name: string
|
89 |
@param name: the short node name
|
90 |
|
91 |
"""
|
92 |
(uuid, full_name) = _ExpandItemName(cfg.ExpandNodeName, name, "Node")
|
93 |
if expected_uuid is not None and uuid != expected_uuid: |
94 |
raise errors.OpPrereqError(
|
95 |
"The nodes UUID '%s' does not match the expected UUID '%s' for node"
|
96 |
" '%s'. Maybe the node changed since you submitted this job." %
|
97 |
(uuid, expected_uuid, full_name), errors.ECODE_NOTUNIQUE) |
98 |
return (uuid, full_name)
|
99 |
|
100 |
|
101 |
def ShareAll(): |
102 |
"""Returns a dict declaring all lock levels shared.
|
103 |
|
104 |
"""
|
105 |
return dict.fromkeys(locking.LEVELS, 1) |
106 |
|
107 |
|
108 |
def CheckNodeGroupInstances(cfg, group_uuid, owned_instance_names): |
109 |
"""Checks if the instances in a node group are still correct.
|
110 |
|
111 |
@type cfg: L{config.ConfigWriter}
|
112 |
@param cfg: The cluster configuration
|
113 |
@type group_uuid: string
|
114 |
@param group_uuid: Node group UUID
|
115 |
@type owned_instance_names: set or frozenset
|
116 |
@param owned_instance_names: List of currently owned instances
|
117 |
|
118 |
"""
|
119 |
wanted_instances = frozenset(cfg.GetInstanceNames(
|
120 |
cfg.GetNodeGroupInstances(group_uuid))) |
121 |
if owned_instance_names != wanted_instances:
|
122 |
raise errors.OpPrereqError("Instances in node group '%s' changed since" |
123 |
" locks were acquired, wanted '%s', have '%s';"
|
124 |
" retry the operation" %
|
125 |
(group_uuid, |
126 |
utils.CommaJoin(wanted_instances), |
127 |
utils.CommaJoin(owned_instance_names)), |
128 |
errors.ECODE_STATE) |
129 |
|
130 |
return wanted_instances
|
131 |
|
132 |
|
133 |
def GetWantedNodes(lu, short_node_names): |
134 |
"""Returns list of checked and expanded node names.
|
135 |
|
136 |
@type lu: L{LogicalUnit}
|
137 |
@param lu: the logical unit on whose behalf we execute
|
138 |
@type short_node_names: list
|
139 |
@param short_node_names: list of node names or None for all nodes
|
140 |
@rtype: tuple of lists
|
141 |
@return: tupe with (list of node UUIDs, list of node names)
|
142 |
@raise errors.ProgrammerError: if the nodes parameter is wrong type
|
143 |
|
144 |
"""
|
145 |
if short_node_names:
|
146 |
node_uuids = [ExpandNodeUuidAndName(lu.cfg, None, name)[0] |
147 |
for name in short_node_names] |
148 |
else:
|
149 |
node_uuids = lu.cfg.GetNodeList() |
150 |
|
151 |
return (node_uuids, [lu.cfg.GetNodeName(uuid) for uuid in node_uuids]) |
152 |
|
153 |
|
154 |
def GetWantedInstances(lu, short_inst_names): |
155 |
"""Returns list of checked and expanded instance names.
|
156 |
|
157 |
@type lu: L{LogicalUnit}
|
158 |
@param lu: the logical unit on whose behalf we execute
|
159 |
@type short_inst_names: list
|
160 |
@param short_inst_names: list of instance names or None for all instances
|
161 |
@rtype: tuple of lists
|
162 |
@return: tuple of (instance UUIDs, instance names)
|
163 |
@raise errors.OpPrereqError: if the instances parameter is wrong type
|
164 |
@raise errors.OpPrereqError: if any of the passed instances is not found
|
165 |
|
166 |
"""
|
167 |
if short_inst_names:
|
168 |
inst_uuids = [ExpandInstanceUuidAndName(lu.cfg, None, name)[0] |
169 |
for name in short_inst_names] |
170 |
else:
|
171 |
inst_uuids = lu.cfg.GetInstanceList() |
172 |
return (inst_uuids, [lu.cfg.GetInstanceName(uuid) for uuid in inst_uuids]) |
173 |
|
174 |
|
175 |
def RunPostHook(lu, node_name): |
176 |
"""Runs the post-hook for an opcode on a single node.
|
177 |
|
178 |
"""
|
179 |
hm = lu.proc.BuildHooksManager(lu) |
180 |
try:
|
181 |
hm.RunPhase(constants.HOOKS_PHASE_POST, node_names=[node_name]) |
182 |
except Exception, err: # pylint: disable=W0703 |
183 |
lu.LogWarning("Errors occurred running hooks on %s: %s",
|
184 |
node_name, err) |
185 |
|
186 |
|
187 |
def RedistributeAncillaryFiles(lu): |
188 |
"""Distribute additional files which are part of the cluster configuration.
|
189 |
|
190 |
ConfigWriter takes care of distributing the config and ssconf files, but
|
191 |
there are more files which should be distributed to all nodes. This function
|
192 |
makes sure those are copied.
|
193 |
|
194 |
"""
|
195 |
# Gather target nodes
|
196 |
cluster = lu.cfg.GetClusterInfo() |
197 |
master_info = lu.cfg.GetMasterNodeInfo() |
198 |
|
199 |
online_node_uuids = lu.cfg.GetOnlineNodeList() |
200 |
online_node_uuid_set = frozenset(online_node_uuids)
|
201 |
vm_node_uuids = list(online_node_uuid_set.intersection(
|
202 |
lu.cfg.GetVmCapableNodeList())) |
203 |
|
204 |
# Never distribute to master node
|
205 |
for node_uuids in [online_node_uuids, vm_node_uuids]: |
206 |
if master_info.uuid in node_uuids: |
207 |
node_uuids.remove(master_info.uuid) |
208 |
|
209 |
# Gather file lists
|
210 |
(files_all, _, files_mc, files_vm) = \ |
211 |
ComputeAncillaryFiles(cluster, True)
|
212 |
|
213 |
# Never re-distribute configuration file from here
|
214 |
assert not (pathutils.CLUSTER_CONF_FILE in files_all or |
215 |
pathutils.CLUSTER_CONF_FILE in files_vm)
|
216 |
assert not files_mc, "Master candidates not handled in this function" |
217 |
|
218 |
filemap = [ |
219 |
(online_node_uuids, files_all), |
220 |
(vm_node_uuids, files_vm), |
221 |
] |
222 |
|
223 |
# Upload the files
|
224 |
for (node_uuids, files) in filemap: |
225 |
for fname in files: |
226 |
UploadHelper(lu, node_uuids, fname) |
227 |
|
228 |
|
229 |
def ComputeAncillaryFiles(cluster, redist): |
230 |
"""Compute files external to Ganeti which need to be consistent.
|
231 |
|
232 |
@type redist: boolean
|
233 |
@param redist: Whether to include files which need to be redistributed
|
234 |
|
235 |
"""
|
236 |
# Compute files for all nodes
|
237 |
files_all = set([
|
238 |
pathutils.SSH_KNOWN_HOSTS_FILE, |
239 |
pathutils.CONFD_HMAC_KEY, |
240 |
pathutils.CLUSTER_DOMAIN_SECRET_FILE, |
241 |
pathutils.SPICE_CERT_FILE, |
242 |
pathutils.SPICE_CACERT_FILE, |
243 |
pathutils.RAPI_USERS_FILE, |
244 |
]) |
245 |
|
246 |
if redist:
|
247 |
# we need to ship at least the RAPI certificate
|
248 |
files_all.add(pathutils.RAPI_CERT_FILE) |
249 |
else:
|
250 |
files_all.update(pathutils.ALL_CERT_FILES) |
251 |
files_all.update(ssconf.SimpleStore().GetFileList()) |
252 |
|
253 |
if cluster.modify_etc_hosts:
|
254 |
files_all.add(pathutils.ETC_HOSTS) |
255 |
|
256 |
if cluster.use_external_mip_script:
|
257 |
files_all.add(pathutils.EXTERNAL_MASTER_SETUP_SCRIPT) |
258 |
|
259 |
# Files which are optional, these must:
|
260 |
# - be present in one other category as well
|
261 |
# - either exist or not exist on all nodes of that category (mc, vm all)
|
262 |
files_opt = set([
|
263 |
pathutils.RAPI_USERS_FILE, |
264 |
]) |
265 |
|
266 |
# Files which should only be on master candidates
|
267 |
files_mc = set()
|
268 |
|
269 |
if not redist: |
270 |
files_mc.add(pathutils.CLUSTER_CONF_FILE) |
271 |
|
272 |
# File storage
|
273 |
if (not redist and (cluster.IsFileStorageEnabled() or |
274 |
cluster.IsSharedFileStorageEnabled())): |
275 |
files_all.add(pathutils.FILE_STORAGE_PATHS_FILE) |
276 |
files_opt.add(pathutils.FILE_STORAGE_PATHS_FILE) |
277 |
|
278 |
# Files which should only be on VM-capable nodes
|
279 |
files_vm = set(
|
280 |
filename |
281 |
for hv_name in cluster.enabled_hypervisors |
282 |
for filename in |
283 |
hypervisor.GetHypervisorClass(hv_name).GetAncillaryFiles()[0])
|
284 |
|
285 |
files_opt |= set(
|
286 |
filename |
287 |
for hv_name in cluster.enabled_hypervisors |
288 |
for filename in |
289 |
hypervisor.GetHypervisorClass(hv_name).GetAncillaryFiles()[1])
|
290 |
|
291 |
# Filenames in each category must be unique
|
292 |
all_files_set = files_all | files_mc | files_vm |
293 |
assert (len(all_files_set) == |
294 |
sum(map(len, [files_all, files_mc, files_vm]))), \ |
295 |
"Found file listed in more than one file list"
|
296 |
|
297 |
# Optional files must be present in one other category
|
298 |
assert all_files_set.issuperset(files_opt), \
|
299 |
"Optional file not in a different required list"
|
300 |
|
301 |
# This one file should never ever be re-distributed via RPC
|
302 |
assert not (redist and |
303 |
pathutils.FILE_STORAGE_PATHS_FILE in all_files_set)
|
304 |
|
305 |
return (files_all, files_opt, files_mc, files_vm)
|
306 |
|
307 |
|
308 |
def UploadHelper(lu, node_uuids, fname): |
309 |
"""Helper for uploading a file and showing warnings.
|
310 |
|
311 |
"""
|
312 |
if os.path.exists(fname):
|
313 |
result = lu.rpc.call_upload_file(node_uuids, fname) |
314 |
for to_node_uuids, to_result in result.items(): |
315 |
msg = to_result.fail_msg |
316 |
if msg:
|
317 |
msg = ("Copy of file %s to node %s failed: %s" %
|
318 |
(fname, lu.cfg.GetNodeName(to_node_uuids), msg)) |
319 |
lu.LogWarning(msg) |
320 |
|
321 |
|
322 |
def MergeAndVerifyHvState(op_input, obj_input): |
323 |
"""Combines the hv state from an opcode with the one of the object
|
324 |
|
325 |
@param op_input: The input dict from the opcode
|
326 |
@param obj_input: The input dict from the objects
|
327 |
@return: The verified and updated dict
|
328 |
|
329 |
"""
|
330 |
if op_input:
|
331 |
invalid_hvs = set(op_input) - constants.HYPER_TYPES
|
332 |
if invalid_hvs:
|
333 |
raise errors.OpPrereqError("Invalid hypervisor(s) in hypervisor state:" |
334 |
" %s" % utils.CommaJoin(invalid_hvs),
|
335 |
errors.ECODE_INVAL) |
336 |
if obj_input is None: |
337 |
obj_input = {} |
338 |
type_check = constants.HVSTS_PARAMETER_TYPES |
339 |
return _UpdateAndVerifySubDict(obj_input, op_input, type_check)
|
340 |
|
341 |
return None |
342 |
|
343 |
|
344 |
def MergeAndVerifyDiskState(op_input, obj_input): |
345 |
"""Combines the disk state from an opcode with the one of the object
|
346 |
|
347 |
@param op_input: The input dict from the opcode
|
348 |
@param obj_input: The input dict from the objects
|
349 |
@return: The verified and updated dict
|
350 |
"""
|
351 |
if op_input:
|
352 |
invalid_dst = set(op_input) - constants.DS_VALID_TYPES
|
353 |
if invalid_dst:
|
354 |
raise errors.OpPrereqError("Invalid storage type(s) in disk state: %s" % |
355 |
utils.CommaJoin(invalid_dst), |
356 |
errors.ECODE_INVAL) |
357 |
type_check = constants.DSS_PARAMETER_TYPES |
358 |
if obj_input is None: |
359 |
obj_input = {} |
360 |
return dict((key, _UpdateAndVerifySubDict(obj_input.get(key, {}), value, |
361 |
type_check)) |
362 |
for key, value in op_input.items()) |
363 |
|
364 |
return None |
365 |
|
366 |
|
367 |
def CheckOSParams(lu, required, node_uuids, osname, osparams): |
368 |
"""OS parameters validation.
|
369 |
|
370 |
@type lu: L{LogicalUnit}
|
371 |
@param lu: the logical unit for which we check
|
372 |
@type required: boolean
|
373 |
@param required: whether the validation should fail if the OS is not
|
374 |
found
|
375 |
@type node_uuids: list
|
376 |
@param node_uuids: the list of nodes on which we should check
|
377 |
@type osname: string
|
378 |
@param osname: the name of the hypervisor we should use
|
379 |
@type osparams: dict
|
380 |
@param osparams: the parameters which we need to check
|
381 |
@raise errors.OpPrereqError: if the parameters are not valid
|
382 |
|
383 |
"""
|
384 |
node_uuids = _FilterVmNodes(lu, node_uuids) |
385 |
result = lu.rpc.call_os_validate(node_uuids, required, osname, |
386 |
[constants.OS_VALIDATE_PARAMETERS], |
387 |
osparams) |
388 |
for node_uuid, nres in result.items(): |
389 |
# we don't check for offline cases since this should be run only
|
390 |
# against the master node and/or an instance's nodes
|
391 |
nres.Raise("OS Parameters validation failed on node %s" %
|
392 |
lu.cfg.GetNodeName(node_uuid)) |
393 |
if not nres.payload: |
394 |
lu.LogInfo("OS %s not found on node %s, validation skipped",
|
395 |
osname, lu.cfg.GetNodeName(node_uuid)) |
396 |
|
397 |
|
398 |
def CheckHVParams(lu, node_uuids, hvname, hvparams): |
399 |
"""Hypervisor parameter validation.
|
400 |
|
401 |
This function abstract the hypervisor parameter validation to be
|
402 |
used in both instance create and instance modify.
|
403 |
|
404 |
@type lu: L{LogicalUnit}
|
405 |
@param lu: the logical unit for which we check
|
406 |
@type node_uuids: list
|
407 |
@param node_uuids: the list of nodes on which we should check
|
408 |
@type hvname: string
|
409 |
@param hvname: the name of the hypervisor we should use
|
410 |
@type hvparams: dict
|
411 |
@param hvparams: the parameters which we need to check
|
412 |
@raise errors.OpPrereqError: if the parameters are not valid
|
413 |
|
414 |
"""
|
415 |
node_uuids = _FilterVmNodes(lu, node_uuids) |
416 |
|
417 |
cluster = lu.cfg.GetClusterInfo() |
418 |
hvfull = objects.FillDict(cluster.hvparams.get(hvname, {}), hvparams) |
419 |
|
420 |
hvinfo = lu.rpc.call_hypervisor_validate_params(node_uuids, hvname, hvfull) |
421 |
for node_uuid in node_uuids: |
422 |
info = hvinfo[node_uuid] |
423 |
if info.offline:
|
424 |
continue
|
425 |
info.Raise("Hypervisor parameter validation failed on node %s" %
|
426 |
lu.cfg.GetNodeName(node_uuid)) |
427 |
|
428 |
|
429 |
def AdjustCandidatePool(lu, exceptions, feedback_fn): |
430 |
"""Adjust the candidate pool after node operations.
|
431 |
|
432 |
"""
|
433 |
mod_list = lu.cfg.MaintainCandidatePool(exceptions) |
434 |
if mod_list:
|
435 |
lu.LogInfo("Promoted nodes to master candidate role: %s",
|
436 |
utils.CommaJoin(node.name for node in mod_list)) |
437 |
for node in mod_list: |
438 |
lu.context.ReaddNode(node) |
439 |
cluster = lu.cfg.GetClusterInfo() |
440 |
AddNodeCertToCandidateCerts(lu, node.uuid, cluster) |
441 |
lu.cfg.Update(cluster, feedback_fn) |
442 |
mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions) |
443 |
if mc_now > mc_max:
|
444 |
lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
|
445 |
(mc_now, mc_max)) |
446 |
|
447 |
|
448 |
def CheckNodePVs(nresult, exclusive_storage): |
449 |
"""Check node PVs.
|
450 |
|
451 |
"""
|
452 |
pvlist_dict = nresult.get(constants.NV_PVLIST, None)
|
453 |
if pvlist_dict is None: |
454 |
return (["Can't get PV list from node"], None) |
455 |
pvlist = map(objects.LvmPvInfo.FromDict, pvlist_dict)
|
456 |
errlist = [] |
457 |
# check that ':' is not present in PV names, since it's a
|
458 |
# special character for lvcreate (denotes the range of PEs to
|
459 |
# use on the PV)
|
460 |
for pv in pvlist: |
461 |
if ":" in pv.name: |
462 |
errlist.append("Invalid character ':' in PV '%s' of VG '%s'" %
|
463 |
(pv.name, pv.vg_name)) |
464 |
es_pvinfo = None
|
465 |
if exclusive_storage:
|
466 |
(errmsgs, es_pvinfo) = utils.LvmExclusiveCheckNodePvs(pvlist) |
467 |
errlist.extend(errmsgs) |
468 |
shared_pvs = nresult.get(constants.NV_EXCLUSIVEPVS, None)
|
469 |
if shared_pvs:
|
470 |
for (pvname, lvlist) in shared_pvs: |
471 |
# TODO: Check that LVs are really unrelated (snapshots, DRBD meta...)
|
472 |
errlist.append("PV %s is shared among unrelated LVs (%s)" %
|
473 |
(pvname, utils.CommaJoin(lvlist))) |
474 |
return (errlist, es_pvinfo)
|
475 |
|
476 |
|
477 |
def _ComputeMinMaxSpec(name, qualifier, ispecs, value): |
478 |
"""Computes if value is in the desired range.
|
479 |
|
480 |
@param name: name of the parameter for which we perform the check
|
481 |
@param qualifier: a qualifier used in the error message (e.g. 'disk/1',
|
482 |
not just 'disk')
|
483 |
@param ispecs: dictionary containing min and max values
|
484 |
@param value: actual value that we want to use
|
485 |
@return: None or an error string
|
486 |
|
487 |
"""
|
488 |
if value in [None, constants.VALUE_AUTO]: |
489 |
return None |
490 |
max_v = ispecs[constants.ISPECS_MAX].get(name, value) |
491 |
min_v = ispecs[constants.ISPECS_MIN].get(name, value) |
492 |
if value > max_v or min_v > value: |
493 |
if qualifier:
|
494 |
fqn = "%s/%s" % (name, qualifier)
|
495 |
else:
|
496 |
fqn = name |
497 |
return ("%s value %s is not in range [%s, %s]" % |
498 |
(fqn, value, min_v, max_v)) |
499 |
return None |
500 |
|
501 |
|
502 |
def ComputeIPolicySpecViolation(ipolicy, mem_size, cpu_count, disk_count, |
503 |
nic_count, disk_sizes, spindle_use, |
504 |
disk_template, |
505 |
_compute_fn=_ComputeMinMaxSpec): |
506 |
"""Verifies ipolicy against provided specs.
|
507 |
|
508 |
@type ipolicy: dict
|
509 |
@param ipolicy: The ipolicy
|
510 |
@type mem_size: int
|
511 |
@param mem_size: The memory size
|
512 |
@type cpu_count: int
|
513 |
@param cpu_count: Used cpu cores
|
514 |
@type disk_count: int
|
515 |
@param disk_count: Number of disks used
|
516 |
@type nic_count: int
|
517 |
@param nic_count: Number of nics used
|
518 |
@type disk_sizes: list of ints
|
519 |
@param disk_sizes: Disk sizes of used disk (len must match C{disk_count})
|
520 |
@type spindle_use: int
|
521 |
@param spindle_use: The number of spindles this instance uses
|
522 |
@type disk_template: string
|
523 |
@param disk_template: The disk template of the instance
|
524 |
@param _compute_fn: The compute function (unittest only)
|
525 |
@return: A list of violations, or an empty list of no violations are found
|
526 |
|
527 |
"""
|
528 |
assert disk_count == len(disk_sizes) |
529 |
|
530 |
test_settings = [ |
531 |
(constants.ISPEC_MEM_SIZE, "", mem_size),
|
532 |
(constants.ISPEC_CPU_COUNT, "", cpu_count),
|
533 |
(constants.ISPEC_NIC_COUNT, "", nic_count),
|
534 |
(constants.ISPEC_SPINDLE_USE, "", spindle_use),
|
535 |
] + [(constants.ISPEC_DISK_SIZE, str(idx), d)
|
536 |
for idx, d in enumerate(disk_sizes)] |
537 |
if disk_template != constants.DT_DISKLESS:
|
538 |
# This check doesn't make sense for diskless instances
|
539 |
test_settings.append((constants.ISPEC_DISK_COUNT, "", disk_count))
|
540 |
ret = [] |
541 |
allowed_dts = ipolicy[constants.IPOLICY_DTS] |
542 |
if disk_template not in allowed_dts: |
543 |
ret.append("Disk template %s is not allowed (allowed templates: %s)" %
|
544 |
(disk_template, utils.CommaJoin(allowed_dts))) |
545 |
|
546 |
min_errs = None
|
547 |
for minmax in ipolicy[constants.ISPECS_MINMAX]: |
548 |
errs = filter(None, |
549 |
(_compute_fn(name, qualifier, minmax, value) |
550 |
for (name, qualifier, value) in test_settings)) |
551 |
if min_errs is None or len(errs) < len(min_errs): |
552 |
min_errs = errs |
553 |
assert min_errs is not None |
554 |
return ret + min_errs
|
555 |
|
556 |
|
557 |
def ComputeIPolicyInstanceViolation(ipolicy, instance, cfg, |
558 |
_compute_fn=ComputeIPolicySpecViolation): |
559 |
"""Compute if instance meets the specs of ipolicy.
|
560 |
|
561 |
@type ipolicy: dict
|
562 |
@param ipolicy: The ipolicy to verify against
|
563 |
@type instance: L{objects.Instance}
|
564 |
@param instance: The instance to verify
|
565 |
@type cfg: L{config.ConfigWriter}
|
566 |
@param cfg: Cluster configuration
|
567 |
@param _compute_fn: The function to verify ipolicy (unittest only)
|
568 |
@see: L{ComputeIPolicySpecViolation}
|
569 |
|
570 |
"""
|
571 |
ret = [] |
572 |
be_full = cfg.GetClusterInfo().FillBE(instance) |
573 |
mem_size = be_full[constants.BE_MAXMEM] |
574 |
cpu_count = be_full[constants.BE_VCPUS] |
575 |
es_flags = rpc.GetExclusiveStorageForNodes(cfg, instance.all_nodes) |
576 |
if any(es_flags.values()): |
577 |
# With exclusive storage use the actual spindles
|
578 |
try:
|
579 |
spindle_use = sum([disk.spindles for disk in instance.disks]) |
580 |
except TypeError: |
581 |
ret.append("Number of spindles not configured for disks of instance %s"
|
582 |
" while exclusive storage is enabled, try running gnt-cluster"
|
583 |
" repair-disk-sizes" % instance.name)
|
584 |
# _ComputeMinMaxSpec ignores 'None's
|
585 |
spindle_use = None
|
586 |
else:
|
587 |
spindle_use = be_full[constants.BE_SPINDLE_USE] |
588 |
disk_count = len(instance.disks)
|
589 |
disk_sizes = [disk.size for disk in instance.disks] |
590 |
nic_count = len(instance.nics)
|
591 |
disk_template = instance.disk_template |
592 |
|
593 |
return ret + _compute_fn(ipolicy, mem_size, cpu_count, disk_count, nic_count,
|
594 |
disk_sizes, spindle_use, disk_template) |
595 |
|
596 |
|
597 |
def _ComputeViolatingInstances(ipolicy, instances, cfg): |
598 |
"""Computes a set of instances who violates given ipolicy.
|
599 |
|
600 |
@param ipolicy: The ipolicy to verify
|
601 |
@type instances: L{objects.Instance}
|
602 |
@param instances: List of instances to verify
|
603 |
@type cfg: L{config.ConfigWriter}
|
604 |
@param cfg: Cluster configuration
|
605 |
@return: A frozenset of instance names violating the ipolicy
|
606 |
|
607 |
"""
|
608 |
return frozenset([inst.name for inst in instances |
609 |
if ComputeIPolicyInstanceViolation(ipolicy, inst, cfg)])
|
610 |
|
611 |
|
612 |
def ComputeNewInstanceViolations(old_ipolicy, new_ipolicy, instances, cfg): |
613 |
"""Computes a set of any instances that would violate the new ipolicy.
|
614 |
|
615 |
@param old_ipolicy: The current (still in-place) ipolicy
|
616 |
@param new_ipolicy: The new (to become) ipolicy
|
617 |
@param instances: List of instances to verify
|
618 |
@type cfg: L{config.ConfigWriter}
|
619 |
@param cfg: Cluster configuration
|
620 |
@return: A list of instances which violates the new ipolicy but
|
621 |
did not before
|
622 |
|
623 |
"""
|
624 |
return (_ComputeViolatingInstances(new_ipolicy, instances, cfg) -
|
625 |
_ComputeViolatingInstances(old_ipolicy, instances, cfg)) |
626 |
|
627 |
|
628 |
def GetUpdatedParams(old_params, update_dict, |
629 |
use_default=True, use_none=False): |
630 |
"""Return the new version of a parameter dictionary.
|
631 |
|
632 |
@type old_params: dict
|
633 |
@param old_params: old parameters
|
634 |
@type update_dict: dict
|
635 |
@param update_dict: dict containing new parameter values, or
|
636 |
constants.VALUE_DEFAULT to reset the parameter to its default
|
637 |
value
|
638 |
@param use_default: boolean
|
639 |
@type use_default: whether to recognise L{constants.VALUE_DEFAULT}
|
640 |
values as 'to be deleted' values
|
641 |
@param use_none: boolean
|
642 |
@type use_none: whether to recognise C{None} values as 'to be
|
643 |
deleted' values
|
644 |
@rtype: dict
|
645 |
@return: the new parameter dictionary
|
646 |
|
647 |
"""
|
648 |
params_copy = copy.deepcopy(old_params) |
649 |
for key, val in update_dict.iteritems(): |
650 |
if ((use_default and val == constants.VALUE_DEFAULT) or |
651 |
(use_none and val is None)): |
652 |
try:
|
653 |
del params_copy[key]
|
654 |
except KeyError: |
655 |
pass
|
656 |
else:
|
657 |
params_copy[key] = val |
658 |
return params_copy
|
659 |
|
660 |
|
661 |
def GetUpdatedIPolicy(old_ipolicy, new_ipolicy, group_policy=False): |
662 |
"""Return the new version of an instance policy.
|
663 |
|
664 |
@param group_policy: whether this policy applies to a group and thus
|
665 |
we should support removal of policy entries
|
666 |
|
667 |
"""
|
668 |
ipolicy = copy.deepcopy(old_ipolicy) |
669 |
for key, value in new_ipolicy.items(): |
670 |
if key not in constants.IPOLICY_ALL_KEYS: |
671 |
raise errors.OpPrereqError("Invalid key in new ipolicy: %s" % key, |
672 |
errors.ECODE_INVAL) |
673 |
if (not value or value == [constants.VALUE_DEFAULT] or |
674 |
value == constants.VALUE_DEFAULT): |
675 |
if group_policy:
|
676 |
if key in ipolicy: |
677 |
del ipolicy[key]
|
678 |
else:
|
679 |
raise errors.OpPrereqError("Can't unset ipolicy attribute '%s'" |
680 |
" on the cluster'" % key,
|
681 |
errors.ECODE_INVAL) |
682 |
else:
|
683 |
if key in constants.IPOLICY_PARAMETERS: |
684 |
# FIXME: we assume all such values are float
|
685 |
try:
|
686 |
ipolicy[key] = float(value)
|
687 |
except (TypeError, ValueError), err: |
688 |
raise errors.OpPrereqError("Invalid value for attribute" |
689 |
" '%s': '%s', error: %s" %
|
690 |
(key, value, err), errors.ECODE_INVAL) |
691 |
elif key == constants.ISPECS_MINMAX:
|
692 |
for minmax in value: |
693 |
for k in minmax.keys(): |
694 |
utils.ForceDictType(minmax[k], constants.ISPECS_PARAMETER_TYPES) |
695 |
ipolicy[key] = value |
696 |
elif key == constants.ISPECS_STD:
|
697 |
if group_policy:
|
698 |
msg = "%s cannot appear in group instance specs" % key
|
699 |
raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
|
700 |
ipolicy[key] = GetUpdatedParams(old_ipolicy.get(key, {}), value, |
701 |
use_none=False, use_default=False) |
702 |
utils.ForceDictType(ipolicy[key], constants.ISPECS_PARAMETER_TYPES) |
703 |
else:
|
704 |
# FIXME: we assume all others are lists; this should be redone
|
705 |
# in a nicer way
|
706 |
ipolicy[key] = list(value)
|
707 |
try:
|
708 |
objects.InstancePolicy.CheckParameterSyntax(ipolicy, not group_policy)
|
709 |
except errors.ConfigurationError, err:
|
710 |
raise errors.OpPrereqError("Invalid instance policy: %s" % err, |
711 |
errors.ECODE_INVAL) |
712 |
return ipolicy
|
713 |
|
714 |
|
715 |
def AnnotateDiskParams(instance, devs, cfg): |
716 |
"""Little helper wrapper to the rpc annotation method.
|
717 |
|
718 |
@param instance: The instance object
|
719 |
@type devs: List of L{objects.Disk}
|
720 |
@param devs: The root devices (not any of its children!)
|
721 |
@param cfg: The config object
|
722 |
@returns The annotated disk copies
|
723 |
@see L{rpc.node.AnnotateDiskParams}
|
724 |
|
725 |
"""
|
726 |
return rpc.AnnotateDiskParams(devs, cfg.GetInstanceDiskParams(instance))
|
727 |
|
728 |
|
729 |
def SupportsOob(cfg, node): |
730 |
"""Tells if node supports OOB.
|
731 |
|
732 |
@type cfg: L{config.ConfigWriter}
|
733 |
@param cfg: The cluster configuration
|
734 |
@type node: L{objects.Node}
|
735 |
@param node: The node
|
736 |
@return: The OOB script if supported or an empty string otherwise
|
737 |
|
738 |
"""
|
739 |
return cfg.GetNdParams(node)[constants.ND_OOB_PROGRAM]
|
740 |
|
741 |
|
742 |
def _UpdateAndVerifySubDict(base, updates, type_check): |
743 |
"""Updates and verifies a dict with sub dicts of the same type.
|
744 |
|
745 |
@param base: The dict with the old data
|
746 |
@param updates: The dict with the new data
|
747 |
@param type_check: Dict suitable to ForceDictType to verify correct types
|
748 |
@returns: A new dict with updated and verified values
|
749 |
|
750 |
"""
|
751 |
def fn(old, value): |
752 |
new = GetUpdatedParams(old, value) |
753 |
utils.ForceDictType(new, type_check) |
754 |
return new
|
755 |
|
756 |
ret = copy.deepcopy(base) |
757 |
ret.update(dict((key, fn(base.get(key, {}), value))
|
758 |
for key, value in updates.items())) |
759 |
return ret
|
760 |
|
761 |
|
762 |
def _FilterVmNodes(lu, node_uuids): |
763 |
"""Filters out non-vm_capable nodes from a list.
|
764 |
|
765 |
@type lu: L{LogicalUnit}
|
766 |
@param lu: the logical unit for which we check
|
767 |
@type node_uuids: list
|
768 |
@param node_uuids: the list of nodes on which we should check
|
769 |
@rtype: list
|
770 |
@return: the list of vm-capable nodes
|
771 |
|
772 |
"""
|
773 |
vm_nodes = frozenset(lu.cfg.GetNonVmCapableNodeList())
|
774 |
return [uuid for uuid in node_uuids if uuid not in vm_nodes] |
775 |
|
776 |
|
777 |
def GetDefaultIAllocator(cfg, ialloc): |
778 |
"""Decides on which iallocator to use.
|
779 |
|
780 |
@type cfg: L{config.ConfigWriter}
|
781 |
@param cfg: Cluster configuration object
|
782 |
@type ialloc: string or None
|
783 |
@param ialloc: Iallocator specified in opcode
|
784 |
@rtype: string
|
785 |
@return: Iallocator name
|
786 |
|
787 |
"""
|
788 |
if not ialloc: |
789 |
# Use default iallocator
|
790 |
ialloc = cfg.GetDefaultIAllocator() |
791 |
|
792 |
if not ialloc: |
793 |
raise errors.OpPrereqError("No iallocator was specified, neither in the" |
794 |
" opcode nor as a cluster-wide default",
|
795 |
errors.ECODE_INVAL) |
796 |
|
797 |
return ialloc
|
798 |
|
799 |
|
800 |
def CheckInstancesNodeGroups(cfg, instances, owned_groups, owned_node_uuids, |
801 |
cur_group_uuid): |
802 |
"""Checks if node groups for locked instances are still correct.
|
803 |
|
804 |
@type cfg: L{config.ConfigWriter}
|
805 |
@param cfg: Cluster configuration
|
806 |
@type instances: dict; string as key, L{objects.Instance} as value
|
807 |
@param instances: Dictionary, instance UUID as key, instance object as value
|
808 |
@type owned_groups: iterable of string
|
809 |
@param owned_groups: List of owned groups
|
810 |
@type owned_node_uuids: iterable of string
|
811 |
@param owned_node_uuids: List of owned nodes
|
812 |
@type cur_group_uuid: string or None
|
813 |
@param cur_group_uuid: Optional group UUID to check against instance's groups
|
814 |
|
815 |
"""
|
816 |
for (uuid, inst) in instances.items(): |
817 |
assert owned_node_uuids.issuperset(inst.all_nodes), \
|
818 |
"Instance %s's nodes changed while we kept the lock" % inst.name
|
819 |
|
820 |
inst_groups = CheckInstanceNodeGroups(cfg, uuid, owned_groups) |
821 |
|
822 |
assert cur_group_uuid is None or cur_group_uuid in inst_groups, \ |
823 |
"Instance %s has no node in group %s" % (inst.name, cur_group_uuid)
|
824 |
|
825 |
|
826 |
def CheckInstanceNodeGroups(cfg, inst_uuid, owned_groups, primary_only=False): |
827 |
"""Checks if the owned node groups are still correct for an instance.
|
828 |
|
829 |
@type cfg: L{config.ConfigWriter}
|
830 |
@param cfg: The cluster configuration
|
831 |
@type inst_uuid: string
|
832 |
@param inst_uuid: Instance UUID
|
833 |
@type owned_groups: set or frozenset
|
834 |
@param owned_groups: List of currently owned node groups
|
835 |
@type primary_only: boolean
|
836 |
@param primary_only: Whether to check node groups for only the primary node
|
837 |
|
838 |
"""
|
839 |
inst_groups = cfg.GetInstanceNodeGroups(inst_uuid, primary_only) |
840 |
|
841 |
if not owned_groups.issuperset(inst_groups): |
842 |
raise errors.OpPrereqError("Instance %s's node groups changed since" |
843 |
" locks were acquired, current groups are"
|
844 |
" are '%s', owning groups '%s'; retry the"
|
845 |
" operation" %
|
846 |
(cfg.GetInstanceName(inst_uuid), |
847 |
utils.CommaJoin(inst_groups), |
848 |
utils.CommaJoin(owned_groups)), |
849 |
errors.ECODE_STATE) |
850 |
|
851 |
return inst_groups
|
852 |
|
853 |
|
854 |
def LoadNodeEvacResult(lu, alloc_result, early_release, use_nodes): |
855 |
"""Unpacks the result of change-group and node-evacuate iallocator requests.
|
856 |
|
857 |
Iallocator modes L{constants.IALLOCATOR_MODE_NODE_EVAC} and
|
858 |
L{constants.IALLOCATOR_MODE_CHG_GROUP}.
|
859 |
|
860 |
@type lu: L{LogicalUnit}
|
861 |
@param lu: Logical unit instance
|
862 |
@type alloc_result: tuple/list
|
863 |
@param alloc_result: Result from iallocator
|
864 |
@type early_release: bool
|
865 |
@param early_release: Whether to release locks early if possible
|
866 |
@type use_nodes: bool
|
867 |
@param use_nodes: Whether to display node names instead of groups
|
868 |
|
869 |
"""
|
870 |
(moved, failed, jobs) = alloc_result |
871 |
|
872 |
if failed:
|
873 |
failreason = utils.CommaJoin("%s (%s)" % (name, reason)
|
874 |
for (name, reason) in failed) |
875 |
lu.LogWarning("Unable to evacuate instances %s", failreason)
|
876 |
raise errors.OpExecError("Unable to evacuate instances %s" % failreason) |
877 |
|
878 |
if moved:
|
879 |
lu.LogInfo("Instances to be moved: %s",
|
880 |
utils.CommaJoin( |
881 |
"%s (to %s)" %
|
882 |
(name, _NodeEvacDest(use_nodes, group, node_names)) |
883 |
for (name, group, node_names) in moved)) |
884 |
|
885 |
return [map(compat.partial(_SetOpEarlyRelease, early_release), |
886 |
map(opcodes.OpCode.LoadOpCode, ops))
|
887 |
for ops in jobs] |
888 |
|
889 |
|
890 |
def _NodeEvacDest(use_nodes, group, node_names): |
891 |
"""Returns group or nodes depending on caller's choice.
|
892 |
|
893 |
"""
|
894 |
if use_nodes:
|
895 |
return utils.CommaJoin(node_names)
|
896 |
else:
|
897 |
return group
|
898 |
|
899 |
|
900 |
def _SetOpEarlyRelease(early_release, op): |
901 |
"""Sets C{early_release} flag on opcodes if available.
|
902 |
|
903 |
"""
|
904 |
try:
|
905 |
op.early_release = early_release |
906 |
except AttributeError: |
907 |
assert not isinstance(op, opcodes.OpInstanceReplaceDisks) |
908 |
|
909 |
return op
|
910 |
|
911 |
|
912 |
def MapInstanceLvsToNodes(instances): |
913 |
"""Creates a map from (node, volume) to instance name.
|
914 |
|
915 |
@type instances: list of L{objects.Instance}
|
916 |
@rtype: dict; tuple of (node uuid, volume name) as key, L{objects.Instance}
|
917 |
object as value
|
918 |
|
919 |
"""
|
920 |
return dict(((node_uuid, vol), inst) |
921 |
for inst in instances |
922 |
for (node_uuid, vols) in inst.MapLVsByNode().items() |
923 |
for vol in vols) |
924 |
|
925 |
|
926 |
def CheckParamsNotGlobal(params, glob_pars, kind, bad_levels, good_levels): |
927 |
"""Make sure that none of the given paramters is global.
|
928 |
|
929 |
If a global parameter is found, an L{errors.OpPrereqError} exception is
|
930 |
raised. This is used to avoid setting global parameters for individual nodes.
|
931 |
|
932 |
@type params: dictionary
|
933 |
@param params: Parameters to check
|
934 |
@type glob_pars: dictionary
|
935 |
@param glob_pars: Forbidden parameters
|
936 |
@type kind: string
|
937 |
@param kind: Kind of parameters (e.g. "node")
|
938 |
@type bad_levels: string
|
939 |
@param bad_levels: Level(s) at which the parameters are forbidden (e.g.
|
940 |
"instance")
|
941 |
@type good_levels: strings
|
942 |
@param good_levels: Level(s) at which the parameters are allowed (e.g.
|
943 |
"cluster or group")
|
944 |
|
945 |
"""
|
946 |
used_globals = glob_pars.intersection(params) |
947 |
if used_globals:
|
948 |
msg = ("The following %s parameters are global and cannot"
|
949 |
" be customized at %s level, please modify them at"
|
950 |
" %s level: %s" %
|
951 |
(kind, bad_levels, good_levels, utils.CommaJoin(used_globals))) |
952 |
raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
|
953 |
|
954 |
|
955 |
def IsExclusiveStorageEnabledNode(cfg, node): |
956 |
"""Whether exclusive_storage is in effect for the given node.
|
957 |
|
958 |
@type cfg: L{config.ConfigWriter}
|
959 |
@param cfg: The cluster configuration
|
960 |
@type node: L{objects.Node}
|
961 |
@param node: The node
|
962 |
@rtype: bool
|
963 |
@return: The effective value of exclusive_storage
|
964 |
|
965 |
"""
|
966 |
return cfg.GetNdParams(node)[constants.ND_EXCLUSIVE_STORAGE]
|
967 |
|
968 |
|
969 |
def CheckInstanceState(lu, instance, req_states, msg=None): |
970 |
"""Ensure that an instance is in one of the required states.
|
971 |
|
972 |
@param lu: the LU on behalf of which we make the check
|
973 |
@param instance: the instance to check
|
974 |
@param msg: if passed, should be a message to replace the default one
|
975 |
@raise errors.OpPrereqError: if the instance is not in the required state
|
976 |
|
977 |
"""
|
978 |
if msg is None: |
979 |
msg = ("can't use instance from outside %s states" %
|
980 |
utils.CommaJoin(req_states)) |
981 |
if instance.admin_state not in req_states: |
982 |
raise errors.OpPrereqError("Instance '%s' is marked to be %s, %s" % |
983 |
(instance.name, instance.admin_state, msg), |
984 |
errors.ECODE_STATE) |
985 |
|
986 |
if constants.ADMINST_UP not in req_states: |
987 |
pnode_uuid = instance.primary_node |
988 |
if not lu.cfg.GetNodeInfo(pnode_uuid).offline: |
989 |
all_hvparams = lu.cfg.GetClusterInfo().hvparams |
990 |
ins_l = lu.rpc.call_instance_list( |
991 |
[pnode_uuid], [instance.hypervisor], all_hvparams)[pnode_uuid] |
992 |
ins_l.Raise("Can't contact node %s for instance information" %
|
993 |
lu.cfg.GetNodeName(pnode_uuid), |
994 |
prereq=True, ecode=errors.ECODE_ENVIRON)
|
995 |
if instance.name in ins_l.payload: |
996 |
raise errors.OpPrereqError("Instance %s is running, %s" % |
997 |
(instance.name, msg), errors.ECODE_STATE) |
998 |
else:
|
999 |
lu.LogWarning("Primary node offline, ignoring check that instance"
|
1000 |
" is down")
|
1001 |
|
1002 |
|
1003 |
def CheckIAllocatorOrNode(lu, iallocator_slot, node_slot): |
1004 |
"""Check the sanity of iallocator and node arguments and use the
|
1005 |
cluster-wide iallocator if appropriate.
|
1006 |
|
1007 |
Check that at most one of (iallocator, node) is specified. If none is
|
1008 |
specified, or the iallocator is L{constants.DEFAULT_IALLOCATOR_SHORTCUT},
|
1009 |
then the LU's opcode's iallocator slot is filled with the cluster-wide
|
1010 |
default iallocator.
|
1011 |
|
1012 |
@type iallocator_slot: string
|
1013 |
@param iallocator_slot: the name of the opcode iallocator slot
|
1014 |
@type node_slot: string
|
1015 |
@param node_slot: the name of the opcode target node slot
|
1016 |
|
1017 |
"""
|
1018 |
node = getattr(lu.op, node_slot, None) |
1019 |
ialloc = getattr(lu.op, iallocator_slot, None) |
1020 |
if node == []:
|
1021 |
node = None
|
1022 |
|
1023 |
if node is not None and ialloc is not None: |
1024 |
raise errors.OpPrereqError("Do not specify both, iallocator and node", |
1025 |
errors.ECODE_INVAL) |
1026 |
elif ((node is None and ialloc is None) or |
1027 |
ialloc == constants.DEFAULT_IALLOCATOR_SHORTCUT): |
1028 |
default_iallocator = lu.cfg.GetDefaultIAllocator() |
1029 |
if default_iallocator:
|
1030 |
setattr(lu.op, iallocator_slot, default_iallocator)
|
1031 |
else:
|
1032 |
raise errors.OpPrereqError("No iallocator or node given and no" |
1033 |
" cluster-wide default iallocator found;"
|
1034 |
" please specify either an iallocator or a"
|
1035 |
" node, or set a cluster-wide default"
|
1036 |
" iallocator", errors.ECODE_INVAL)
|
1037 |
|
1038 |
|
1039 |
def FindFaultyInstanceDisks(cfg, rpc_runner, instance, node_uuid, prereq): |
1040 |
faulty = [] |
1041 |
|
1042 |
result = rpc_runner.call_blockdev_getmirrorstatus( |
1043 |
node_uuid, (instance.disks, instance)) |
1044 |
result.Raise("Failed to get disk status from node %s" %
|
1045 |
cfg.GetNodeName(node_uuid), |
1046 |
prereq=prereq, ecode=errors.ECODE_ENVIRON) |
1047 |
|
1048 |
for idx, bdev_status in enumerate(result.payload): |
1049 |
if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY: |
1050 |
faulty.append(idx) |
1051 |
|
1052 |
return faulty
|
1053 |
|
1054 |
|
1055 |
def CheckNodeOnline(lu, node_uuid, msg=None): |
1056 |
"""Ensure that a given node is online.
|
1057 |
|
1058 |
@param lu: the LU on behalf of which we make the check
|
1059 |
@param node_uuid: the node to check
|
1060 |
@param msg: if passed, should be a message to replace the default one
|
1061 |
@raise errors.OpPrereqError: if the node is offline
|
1062 |
|
1063 |
"""
|
1064 |
if msg is None: |
1065 |
msg = "Can't use offline node"
|
1066 |
if lu.cfg.GetNodeInfo(node_uuid).offline:
|
1067 |
raise errors.OpPrereqError("%s: %s" % (msg, lu.cfg.GetNodeName(node_uuid)), |
1068 |
errors.ECODE_STATE) |
1069 |
|
1070 |
|
1071 |
def CheckDiskTemplateEnabled(cluster, disk_template): |
1072 |
"""Helper function to check if a disk template is enabled.
|
1073 |
|
1074 |
@type cluster: C{objects.Cluster}
|
1075 |
@param cluster: the cluster's configuration
|
1076 |
@type disk_template: str
|
1077 |
@param disk_template: the disk template to be checked
|
1078 |
|
1079 |
"""
|
1080 |
assert disk_template is not None |
1081 |
if disk_template not in constants.DISK_TEMPLATES: |
1082 |
raise errors.OpPrereqError("'%s' is not a valid disk template." |
1083 |
" Valid disk templates are: %s" %
|
1084 |
(disk_template, |
1085 |
",".join(constants.DISK_TEMPLATES)))
|
1086 |
if not disk_template in cluster.enabled_disk_templates: |
1087 |
raise errors.OpPrereqError("Disk template '%s' is not enabled in cluster." |
1088 |
" Enabled disk templates are: %s" %
|
1089 |
(disk_template, |
1090 |
",".join(cluster.enabled_disk_templates)))
|
1091 |
|
1092 |
|
1093 |
def CheckStorageTypeEnabled(cluster, storage_type): |
1094 |
"""Helper function to check if a storage type is enabled.
|
1095 |
|
1096 |
@type cluster: C{objects.Cluster}
|
1097 |
@param cluster: the cluster's configuration
|
1098 |
@type storage_type: str
|
1099 |
@param storage_type: the storage type to be checked
|
1100 |
|
1101 |
"""
|
1102 |
assert storage_type is not None |
1103 |
assert storage_type in constants.STORAGE_TYPES |
1104 |
# special case for lvm-pv, because it cannot be enabled
|
1105 |
# via disk templates
|
1106 |
if storage_type == constants.ST_LVM_PV:
|
1107 |
CheckStorageTypeEnabled(cluster, constants.ST_LVM_VG) |
1108 |
else:
|
1109 |
possible_disk_templates = \ |
1110 |
utils.storage.GetDiskTemplatesOfStorageTypes(storage_type) |
1111 |
for disk_template in possible_disk_templates: |
1112 |
if disk_template in cluster.enabled_disk_templates: |
1113 |
return
|
1114 |
raise errors.OpPrereqError("No disk template of storage type '%s' is" |
1115 |
" enabled in this cluster. Enabled disk"
|
1116 |
" templates are: %s" % (storage_type,
|
1117 |
",".join(cluster.enabled_disk_templates)))
|
1118 |
|
1119 |
|
1120 |
def CheckIpolicyVsDiskTemplates(ipolicy, enabled_disk_templates): |
1121 |
"""Checks ipolicy disk templates against enabled disk tempaltes.
|
1122 |
|
1123 |
@type ipolicy: dict
|
1124 |
@param ipolicy: the new ipolicy
|
1125 |
@type enabled_disk_templates: list of string
|
1126 |
@param enabled_disk_templates: list of enabled disk templates on the
|
1127 |
cluster
|
1128 |
@raises errors.OpPrereqError: if there is at least one allowed disk
|
1129 |
template that is not also enabled.
|
1130 |
|
1131 |
"""
|
1132 |
assert constants.IPOLICY_DTS in ipolicy |
1133 |
allowed_disk_templates = ipolicy[constants.IPOLICY_DTS] |
1134 |
not_enabled = set(allowed_disk_templates) - set(enabled_disk_templates) |
1135 |
if not_enabled:
|
1136 |
raise errors.OpPrereqError("The following disk template are allowed" |
1137 |
" by the ipolicy, but not enabled on the"
|
1138 |
" cluster: %s" % utils.CommaJoin(not_enabled))
|
1139 |
|
1140 |
|
1141 |
def CheckDiskAccessModeValidity(parameters): |
1142 |
"""Checks if the access parameter is legal.
|
1143 |
|
1144 |
@see: L{CheckDiskAccessModeConsistency} for cluster consistency checks.
|
1145 |
@raise errors.OpPrereqError: if the check fails.
|
1146 |
|
1147 |
"""
|
1148 |
for disk_template in parameters: |
1149 |
access = parameters[disk_template].get(constants.LDP_ACCESS, |
1150 |
constants.DISK_KERNELSPACE) |
1151 |
if access not in constants.DISK_VALID_ACCESS_MODES: |
1152 |
valid_vals_str = utils.CommaJoin(constants.DISK_VALID_ACCESS_MODES) |
1153 |
raise errors.OpPrereqError("Invalid value of '{d}:{a}': '{v}' (expected" |
1154 |
" one of {o})".format(d=disk_template,
|
1155 |
a=constants.LDP_ACCESS, |
1156 |
v=access, |
1157 |
o=valid_vals_str)) |
1158 |
|
1159 |
|
1160 |
def CheckDiskAccessModeConsistency(parameters, cfg, group=None): |
1161 |
"""Checks if the access param is consistent with the cluster configuration.
|
1162 |
|
1163 |
@note: requires a configuration lock to run.
|
1164 |
@param parameters: the parameters to validate
|
1165 |
@param cfg: the cfg object of the cluster
|
1166 |
@param group: if set, only check for consistency within this group.
|
1167 |
@raise errors.OpPrereqError: if the LU attempts to change the access parameter
|
1168 |
to an invalid value, such as "pink bunny".
|
1169 |
@raise errors.OpPrereqError: if the LU attempts to change the access parameter
|
1170 |
to an inconsistent value, such as asking for RBD
|
1171 |
userspace access to the chroot hypervisor.
|
1172 |
|
1173 |
"""
|
1174 |
CheckDiskAccessModeValidity(parameters) |
1175 |
|
1176 |
for disk_template in parameters: |
1177 |
access = parameters[disk_template].get(constants.LDP_ACCESS, |
1178 |
constants.DISK_KERNELSPACE) |
1179 |
|
1180 |
if disk_template not in constants.DTS_HAVE_ACCESS: |
1181 |
continue
|
1182 |
|
1183 |
#Check the combination of instance hypervisor, disk template and access
|
1184 |
#protocol is sane.
|
1185 |
inst_uuids = cfg.GetNodeGroupInstances(group) if group else \ |
1186 |
cfg.GetInstanceList() |
1187 |
|
1188 |
for entry in inst_uuids: |
1189 |
inst = cfg.GetInstanceInfo(entry) |
1190 |
hv = inst.hypervisor |
1191 |
dt = inst.disk_template |
1192 |
|
1193 |
if not IsValidDiskAccessModeCombination(hv, dt, access): |
1194 |
raise errors.OpPrereqError("Instance {i}: cannot use '{a}' access" |
1195 |
" setting with {h} hypervisor and {d} disk"
|
1196 |
" type.".format(i=inst.name,
|
1197 |
a=access, |
1198 |
h=hv, |
1199 |
d=dt)) |
1200 |
|
1201 |
|
1202 |
def IsValidDiskAccessModeCombination(hv, disk_template, mode): |
1203 |
"""Checks if an hypervisor can read a disk template with given mode.
|
1204 |
|
1205 |
@param hv: the hypervisor that will access the data
|
1206 |
@param disk_template: the disk template the data is stored as
|
1207 |
@param mode: how the hypervisor should access the data
|
1208 |
@return: True if the hypervisor can read a given read disk_template
|
1209 |
in the specified mode.
|
1210 |
|
1211 |
"""
|
1212 |
if mode == constants.DISK_KERNELSPACE:
|
1213 |
return True |
1214 |
|
1215 |
if (hv == constants.HT_KVM and |
1216 |
disk_template in (constants.DT_RBD, constants.DT_GLUSTER) and |
1217 |
mode == constants.DISK_USERSPACE): |
1218 |
return True |
1219 |
|
1220 |
# Everything else:
|
1221 |
return False |
1222 |
|
1223 |
|
1224 |
def AddNodeCertToCandidateCerts(lu, node_uuid, cluster): |
1225 |
"""Add the node's client SSL certificate digest to the candidate certs.
|
1226 |
|
1227 |
@type node_uuid: string
|
1228 |
@param node_uuid: the node's UUID
|
1229 |
@type cluster: C{object.Cluster}
|
1230 |
@param cluster: the cluster's configuration
|
1231 |
|
1232 |
"""
|
1233 |
result = lu.rpc.call_node_crypto_tokens( |
1234 |
node_uuid, |
1235 |
[(constants.CRYPTO_TYPE_SSL_DIGEST, constants.CRYPTO_ACTION_GET, |
1236 |
None)])
|
1237 |
result.Raise("Could not retrieve the node's (uuid %s) SSL digest."
|
1238 |
% node_uuid) |
1239 |
((crypto_type, digest), ) = result.payload |
1240 |
assert crypto_type == constants.CRYPTO_TYPE_SSL_DIGEST
|
1241 |
|
1242 |
utils.AddNodeToCandidateCerts(node_uuid, digest, cluster.candidate_certs) |
1243 |
|
1244 |
|
1245 |
def RemoveNodeCertFromCandidateCerts(node_uuid, cluster): |
1246 |
"""Removes the node's certificate from the candidate certificates list.
|
1247 |
|
1248 |
@type node_uuid: string
|
1249 |
@param node_uuid: the node's UUID
|
1250 |
@type cluster: C{objects.Cluster}
|
1251 |
@param cluster: the cluster's configuration
|
1252 |
|
1253 |
"""
|
1254 |
utils.RemoveNodeFromCandidateCerts(node_uuid, cluster.candidate_certs) |
1255 |
|
1256 |
|
1257 |
def CreateNewClientCert(lu, node_uuid, filename=None): |
1258 |
"""Creates a new client SSL certificate for the node.
|
1259 |
|
1260 |
@type node_uuid: string
|
1261 |
@param node_uuid: the node's UUID
|
1262 |
@type filename: string
|
1263 |
@param filename: the certificate's filename
|
1264 |
@rtype: string
|
1265 |
@return: the digest of the newly created certificate
|
1266 |
|
1267 |
"""
|
1268 |
options = {} |
1269 |
if filename:
|
1270 |
options[constants.CRYPTO_OPTION_CERT_FILE] = filename |
1271 |
result = lu.rpc.call_node_crypto_tokens( |
1272 |
node_uuid, |
1273 |
[(constants.CRYPTO_TYPE_SSL_DIGEST, |
1274 |
constants.CRYPTO_ACTION_CREATE, |
1275 |
options)]) |
1276 |
result.Raise("Could not create the node's (uuid %s) SSL client"
|
1277 |
" certificate." % node_uuid)
|
1278 |
((crypto_type, new_digest), ) = result.payload |
1279 |
assert crypto_type == constants.CRYPTO_TYPE_SSL_DIGEST
|
1280 |
return new_digest
|