4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
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.
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.
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
22 """Module implementing the master-side code."""
24 # pylint: disable=W0201,C0302
26 # W0201 since most LU attributes are defined in CheckPrereq or similar
29 # C0302: since we have waaaay too many lines in this module
45 from ganeti import ssh
46 from ganeti import utils
47 from ganeti import errors
48 from ganeti import hypervisor
49 from ganeti import locking
50 from ganeti import constants
51 from ganeti import objects
52 from ganeti import serializer
53 from ganeti import ssconf
54 from ganeti import uidpool
55 from ganeti import compat
56 from ganeti import masterd
57 from ganeti import netutils
58 from ganeti import query
59 from ganeti import qlang
60 from ganeti import opcodes
63 import ganeti.masterd.instance # pylint: disable=W0611
67 """Data container for LU results with jobs.
69 Instances of this class returned from L{LogicalUnit.Exec} will be recognized
70 by L{mcpu.Processor._ProcessResult}. The latter will then submit the jobs
71 contained in the C{jobs} attribute and include the job IDs in the opcode
75 def __init__(self, jobs, **kwargs):
76 """Initializes this class.
78 Additional return values can be specified as keyword arguments.
80 @type jobs: list of lists of L{opcode.OpCode}
81 @param jobs: A list of lists of opcode objects
88 class LogicalUnit(object):
89 """Logical Unit base class.
91 Subclasses must follow these rules:
92 - implement ExpandNames
93 - implement CheckPrereq (except when tasklets are used)
94 - implement Exec (except when tasklets are used)
95 - implement BuildHooksEnv
96 - implement BuildHooksNodes
97 - redefine HPATH and HTYPE
98 - optionally redefine their run requirements:
99 REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
101 Note that all commands require root permissions.
103 @ivar dry_run_result: the value (if any) that will be returned to the caller
104 in dry-run mode (signalled by opcode dry_run parameter)
111 def __init__(self, processor, op, context, rpc):
112 """Constructor for LogicalUnit.
114 This needs to be overridden in derived classes in order to check op
118 self.proc = processor
120 self.cfg = context.cfg
121 self.glm = context.glm
123 self.owned_locks = context.glm.list_owned
124 self.context = context
126 # Dicts used to declare locking needs to mcpu
127 self.needed_locks = None
128 self.share_locks = dict.fromkeys(locking.LEVELS, 0)
130 self.remove_locks = {}
131 # Used to force good behavior when calling helper functions
132 self.recalculate_locks = {}
134 self.Log = processor.Log # pylint: disable=C0103
135 self.LogWarning = processor.LogWarning # pylint: disable=C0103
136 self.LogInfo = processor.LogInfo # pylint: disable=C0103
137 self.LogStep = processor.LogStep # pylint: disable=C0103
138 # support for dry-run
139 self.dry_run_result = None
140 # support for generic debug attribute
141 if (not hasattr(self.op, "debug_level") or
142 not isinstance(self.op.debug_level, int)):
143 self.op.debug_level = 0
148 # Validate opcode parameters and set defaults
149 self.op.Validate(True)
151 self.CheckArguments()
153 def CheckArguments(self):
154 """Check syntactic validity for the opcode arguments.
156 This method is for doing a simple syntactic check and ensure
157 validity of opcode parameters, without any cluster-related
158 checks. While the same can be accomplished in ExpandNames and/or
159 CheckPrereq, doing these separate is better because:
161 - ExpandNames is left as as purely a lock-related function
162 - CheckPrereq is run after we have acquired locks (and possible
165 The function is allowed to change the self.op attribute so that
166 later methods can no longer worry about missing parameters.
171 def ExpandNames(self):
172 """Expand names for this LU.
174 This method is called before starting to execute the opcode, and it should
175 update all the parameters of the opcode to their canonical form (e.g. a
176 short node name must be fully expanded after this method has successfully
177 completed). This way locking, hooks, logging, etc. can work correctly.
179 LUs which implement this method must also populate the self.needed_locks
180 member, as a dict with lock levels as keys, and a list of needed lock names
183 - use an empty dict if you don't need any lock
184 - if you don't need any lock at a particular level omit that level
185 - don't put anything for the BGL level
186 - if you want all locks at a level use locking.ALL_SET as a value
188 If you need to share locks (rather than acquire them exclusively) at one
189 level you can modify self.share_locks, setting a true value (usually 1) for
190 that level. By default locks are not shared.
192 This function can also define a list of tasklets, which then will be
193 executed in order instead of the usual LU-level CheckPrereq and Exec
194 functions, if those are not defined by the LU.
198 # Acquire all nodes and one instance
199 self.needed_locks = {
200 locking.LEVEL_NODE: locking.ALL_SET,
201 locking.LEVEL_INSTANCE: ['instance1.example.com'],
203 # Acquire just two nodes
204 self.needed_locks = {
205 locking.LEVEL_NODE: ['node1.example.com', 'node2.example.com'],
208 self.needed_locks = {} # No, you can't leave it to the default value None
211 # The implementation of this method is mandatory only if the new LU is
212 # concurrent, so that old LUs don't need to be changed all at the same
215 self.needed_locks = {} # Exclusive LUs don't need locks.
217 raise NotImplementedError
219 def DeclareLocks(self, level):
220 """Declare LU locking needs for a level
222 While most LUs can just declare their locking needs at ExpandNames time,
223 sometimes there's the need to calculate some locks after having acquired
224 the ones before. This function is called just before acquiring locks at a
225 particular level, but after acquiring the ones at lower levels, and permits
226 such calculations. It can be used to modify self.needed_locks, and by
227 default it does nothing.
229 This function is only called if you have something already set in
230 self.needed_locks for the level.
232 @param level: Locking level which is going to be locked
233 @type level: member of ganeti.locking.LEVELS
237 def CheckPrereq(self):
238 """Check prerequisites for this LU.
240 This method should check that the prerequisites for the execution
241 of this LU are fulfilled. It can do internode communication, but
242 it should be idempotent - no cluster or system changes are
245 The method should raise errors.OpPrereqError in case something is
246 not fulfilled. Its return value is ignored.
248 This method should also update all the parameters of the opcode to
249 their canonical form if it hasn't been done by ExpandNames before.
252 if self.tasklets is not None:
253 for (idx, tl) in enumerate(self.tasklets):
254 logging.debug("Checking prerequisites for tasklet %s/%s",
255 idx + 1, len(self.tasklets))
260 def Exec(self, feedback_fn):
263 This method should implement the actual work. It should raise
264 errors.OpExecError for failures that are somewhat dealt with in
268 if self.tasklets is not None:
269 for (idx, tl) in enumerate(self.tasklets):
270 logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets))
273 raise NotImplementedError
275 def BuildHooksEnv(self):
276 """Build hooks environment for this LU.
279 @return: Dictionary containing the environment that will be used for
280 running the hooks for this LU. The keys of the dict must not be prefixed
281 with "GANETI_"--that'll be added by the hooks runner. The hooks runner
282 will extend the environment with additional variables. If no environment
283 should be defined, an empty dictionary should be returned (not C{None}).
284 @note: If the C{HPATH} attribute of the LU class is C{None}, this function
288 raise NotImplementedError
290 def BuildHooksNodes(self):
291 """Build list of nodes to run LU's hooks.
293 @rtype: tuple; (list, list)
294 @return: Tuple containing a list of node names on which the hook
295 should run before the execution and a list of node names on which the
296 hook should run after the execution. No nodes should be returned as an
297 empty list (and not None).
298 @note: If the C{HPATH} attribute of the LU class is C{None}, this function
302 raise NotImplementedError
304 def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
305 """Notify the LU about the results of its hooks.
307 This method is called every time a hooks phase is executed, and notifies
308 the Logical Unit about the hooks' result. The LU can then use it to alter
309 its result based on the hooks. By default the method does nothing and the
310 previous result is passed back unchanged but any LU can define it if it
311 wants to use the local cluster hook-scripts somehow.
313 @param phase: one of L{constants.HOOKS_PHASE_POST} or
314 L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
315 @param hook_results: the results of the multi-node hooks rpc call
316 @param feedback_fn: function used send feedback back to the caller
317 @param lu_result: the previous Exec result this LU had, or None
319 @return: the new Exec result, based on the previous result
323 # API must be kept, thus we ignore the unused argument and could
324 # be a function warnings
325 # pylint: disable=W0613,R0201
328 def _ExpandAndLockInstance(self):
329 """Helper function to expand and lock an instance.
331 Many LUs that work on an instance take its name in self.op.instance_name
332 and need to expand it and then declare the expanded name for locking. This
333 function does it, and then updates self.op.instance_name to the expanded
334 name. It also initializes needed_locks as a dict, if this hasn't been done
338 if self.needed_locks is None:
339 self.needed_locks = {}
341 assert locking.LEVEL_INSTANCE not in self.needed_locks, \
342 "_ExpandAndLockInstance called with instance-level locks set"
343 self.op.instance_name = _ExpandInstanceName(self.cfg,
344 self.op.instance_name)
345 self.needed_locks[locking.LEVEL_INSTANCE] = self.op.instance_name
347 def _LockInstancesNodes(self, primary_only=False):
348 """Helper function to declare instances' nodes for locking.
350 This function should be called after locking one or more instances to lock
351 their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
352 with all primary or secondary nodes for instances already locked and
353 present in self.needed_locks[locking.LEVEL_INSTANCE].
355 It should be called from DeclareLocks, and for safety only works if
356 self.recalculate_locks[locking.LEVEL_NODE] is set.
358 In the future it may grow parameters to just lock some instance's nodes, or
359 to just lock primaries or secondary nodes, if needed.
361 If should be called in DeclareLocks in a way similar to::
363 if level == locking.LEVEL_NODE:
364 self._LockInstancesNodes()
366 @type primary_only: boolean
367 @param primary_only: only lock primary nodes of locked instances
370 assert locking.LEVEL_NODE in self.recalculate_locks, \
371 "_LockInstancesNodes helper function called with no nodes to recalculate"
373 # TODO: check if we're really been called with the instance locks held
375 # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
376 # future we might want to have different behaviors depending on the value
377 # of self.recalculate_locks[locking.LEVEL_NODE]
379 locked_i = self.owned_locks(locking.LEVEL_INSTANCE)
380 for _, instance in self.cfg.GetMultiInstanceInfo(locked_i):
381 wanted_nodes.append(instance.primary_node)
383 wanted_nodes.extend(instance.secondary_nodes)
385 if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
386 self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
387 elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
388 self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
390 del self.recalculate_locks[locking.LEVEL_NODE]
393 class NoHooksLU(LogicalUnit): # pylint: disable=W0223
394 """Simple LU which runs no hooks.
396 This LU is intended as a parent for other LogicalUnits which will
397 run no hooks, in order to reduce duplicate code.
403 def BuildHooksEnv(self):
404 """Empty BuildHooksEnv for NoHooksLu.
406 This just raises an error.
409 raise AssertionError("BuildHooksEnv called for NoHooksLUs")
411 def BuildHooksNodes(self):
412 """Empty BuildHooksNodes for NoHooksLU.
415 raise AssertionError("BuildHooksNodes called for NoHooksLU")
419 """Tasklet base class.
421 Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
422 they can mix legacy code with tasklets. Locking needs to be done in the LU,
423 tasklets know nothing about locks.
425 Subclasses must follow these rules:
426 - Implement CheckPrereq
430 def __init__(self, lu):
437 def CheckPrereq(self):
438 """Check prerequisites for this tasklets.
440 This method should check whether the prerequisites for the execution of
441 this tasklet are fulfilled. It can do internode communication, but it
442 should be idempotent - no cluster or system changes are allowed.
444 The method should raise errors.OpPrereqError in case something is not
445 fulfilled. Its return value is ignored.
447 This method should also update all parameters to their canonical form if it
448 hasn't been done before.
453 def Exec(self, feedback_fn):
454 """Execute the tasklet.
456 This method should implement the actual work. It should raise
457 errors.OpExecError for failures that are somewhat dealt with in code, or
461 raise NotImplementedError
465 """Base for query utility classes.
468 #: Attribute holding field definitions
471 def __init__(self, filter_, fields, use_locking):
472 """Initializes this class.
475 self.use_locking = use_locking
477 self.query = query.Query(self.FIELDS, fields, filter_=filter_,
479 self.requested_data = self.query.RequestedData()
480 self.names = self.query.RequestedNames()
482 # Sort only if no names were requested
483 self.sort_by_name = not self.names
485 self.do_locking = None
488 def _GetNames(self, lu, all_names, lock_level):
489 """Helper function to determine names asked for in the query.
493 names = lu.owned_locks(lock_level)
497 if self.wanted == locking.ALL_SET:
498 assert not self.names
499 # caller didn't specify names, so ordering is not important
500 return utils.NiceSort(names)
502 # caller specified names and we must keep the same order
504 assert not self.do_locking or lu.glm.is_owned(lock_level)
506 missing = set(self.wanted).difference(names)
508 raise errors.OpExecError("Some items were removed before retrieving"
509 " their data: %s" % missing)
511 # Return expanded names
514 def ExpandNames(self, lu):
515 """Expand names for this query.
517 See L{LogicalUnit.ExpandNames}.
520 raise NotImplementedError()
522 def DeclareLocks(self, lu, level):
523 """Declare locks for this query.
525 See L{LogicalUnit.DeclareLocks}.
528 raise NotImplementedError()
530 def _GetQueryData(self, lu):
531 """Collects all data for this query.
533 @return: Query data object
536 raise NotImplementedError()
538 def NewStyleQuery(self, lu):
539 """Collect data and execute query.
542 return query.GetQueryResponse(self.query, self._GetQueryData(lu),
543 sort_by_name=self.sort_by_name)
545 def OldStyleQuery(self, lu):
546 """Collect data and execute query.
549 return self.query.OldStyleQuery(self._GetQueryData(lu),
550 sort_by_name=self.sort_by_name)
554 """Returns a dict declaring all lock levels shared.
557 return dict.fromkeys(locking.LEVELS, 1)
560 def _CheckInstanceNodeGroups(cfg, instance_name, owned_groups):
561 """Checks if the owned node groups are still correct for an instance.
563 @type cfg: L{config.ConfigWriter}
564 @param cfg: The cluster configuration
565 @type instance_name: string
566 @param instance_name: Instance name
567 @type owned_groups: set or frozenset
568 @param owned_groups: List of currently owned node groups
571 inst_groups = cfg.GetInstanceNodeGroups(instance_name)
573 if not owned_groups.issuperset(inst_groups):
574 raise errors.OpPrereqError("Instance %s's node groups changed since"
575 " locks were acquired, current groups are"
576 " are '%s', owning groups '%s'; retry the"
579 utils.CommaJoin(inst_groups),
580 utils.CommaJoin(owned_groups)),
586 def _CheckNodeGroupInstances(cfg, group_uuid, owned_instances):
587 """Checks if the instances in a node group are still correct.
589 @type cfg: L{config.ConfigWriter}
590 @param cfg: The cluster configuration
591 @type group_uuid: string
592 @param group_uuid: Node group UUID
593 @type owned_instances: set or frozenset
594 @param owned_instances: List of currently owned instances
597 wanted_instances = cfg.GetNodeGroupInstances(group_uuid)
598 if owned_instances != wanted_instances:
599 raise errors.OpPrereqError("Instances in node group '%s' changed since"
600 " locks were acquired, wanted '%s', have '%s';"
601 " retry the operation" %
603 utils.CommaJoin(wanted_instances),
604 utils.CommaJoin(owned_instances)),
607 return wanted_instances
610 def _SupportsOob(cfg, node):
611 """Tells if node supports OOB.
613 @type cfg: L{config.ConfigWriter}
614 @param cfg: The cluster configuration
615 @type node: L{objects.Node}
616 @param node: The node
617 @return: The OOB script if supported or an empty string otherwise
620 return cfg.GetNdParams(node)[constants.ND_OOB_PROGRAM]
623 def _GetWantedNodes(lu, nodes):
624 """Returns list of checked and expanded node names.
626 @type lu: L{LogicalUnit}
627 @param lu: the logical unit on whose behalf we execute
629 @param nodes: list of node names or None for all nodes
631 @return: the list of nodes, sorted
632 @raise errors.ProgrammerError: if the nodes parameter is wrong type
636 return [_ExpandNodeName(lu.cfg, name) for name in nodes]
638 return utils.NiceSort(lu.cfg.GetNodeList())
641 def _GetWantedInstances(lu, instances):
642 """Returns list of checked and expanded instance names.
644 @type lu: L{LogicalUnit}
645 @param lu: the logical unit on whose behalf we execute
646 @type instances: list
647 @param instances: list of instance names or None for all instances
649 @return: the list of instances, sorted
650 @raise errors.OpPrereqError: if the instances parameter is wrong type
651 @raise errors.OpPrereqError: if any of the passed instances is not found
655 wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances]
657 wanted = utils.NiceSort(lu.cfg.GetInstanceList())
661 def _GetUpdatedParams(old_params, update_dict,
662 use_default=True, use_none=False):
663 """Return the new version of a parameter dictionary.
665 @type old_params: dict
666 @param old_params: old parameters
667 @type update_dict: dict
668 @param update_dict: dict containing new parameter values, or
669 constants.VALUE_DEFAULT to reset the parameter to its default
671 @param use_default: boolean
672 @type use_default: whether to recognise L{constants.VALUE_DEFAULT}
673 values as 'to be deleted' values
674 @param use_none: boolean
675 @type use_none: whether to recognise C{None} values as 'to be
678 @return: the new parameter dictionary
681 params_copy = copy.deepcopy(old_params)
682 for key, val in update_dict.iteritems():
683 if ((use_default and val == constants.VALUE_DEFAULT) or
684 (use_none and val is None)):
690 params_copy[key] = val
694 def _ReleaseLocks(lu, level, names=None, keep=None):
695 """Releases locks owned by an LU.
697 @type lu: L{LogicalUnit}
698 @param level: Lock level
699 @type names: list or None
700 @param names: Names of locks to release
701 @type keep: list or None
702 @param keep: Names of locks to retain
705 assert not (keep is not None and names is not None), \
706 "Only one of the 'names' and the 'keep' parameters can be given"
708 if names is not None:
709 should_release = names.__contains__
711 should_release = lambda name: name not in keep
713 should_release = None
719 # Determine which locks to release
720 for name in lu.owned_locks(level):
721 if should_release(name):
726 assert len(lu.owned_locks(level)) == (len(retain) + len(release))
728 # Release just some locks
729 lu.glm.release(level, names=release)
731 assert frozenset(lu.owned_locks(level)) == frozenset(retain)
734 lu.glm.release(level)
736 assert not lu.glm.is_owned(level), "No locks should be owned"
739 def _MapInstanceDisksToNodes(instances):
740 """Creates a map from (node, volume) to instance name.
742 @type instances: list of L{objects.Instance}
743 @rtype: dict; tuple of (node name, volume name) as key, instance name as value
746 return dict(((node, vol), inst.name)
747 for inst in instances
748 for (node, vols) in inst.MapLVsByNode().items()
752 def _RunPostHook(lu, node_name):
753 """Runs the post-hook for an opcode on a single node.
756 hm = lu.proc.hmclass(lu.rpc.call_hooks_runner, lu)
758 hm.RunPhase(constants.HOOKS_PHASE_POST, nodes=[node_name])
760 # pylint: disable=W0702
761 lu.LogWarning("Errors occurred running hooks on %s" % node_name)
764 def _CheckOutputFields(static, dynamic, selected):
765 """Checks whether all selected fields are valid.
767 @type static: L{utils.FieldSet}
768 @param static: static fields set
769 @type dynamic: L{utils.FieldSet}
770 @param dynamic: dynamic fields set
777 delta = f.NonMatching(selected)
779 raise errors.OpPrereqError("Unknown output fields selected: %s"
780 % ",".join(delta), errors.ECODE_INVAL)
783 def _CheckGlobalHvParams(params):
784 """Validates that given hypervisor params are not global ones.
786 This will ensure that instances don't get customised versions of
790 used_globals = constants.HVC_GLOBALS.intersection(params)
792 msg = ("The following hypervisor parameters are global and cannot"
793 " be customized at instance level, please modify them at"
794 " cluster level: %s" % utils.CommaJoin(used_globals))
795 raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
798 def _CheckNodeOnline(lu, node, msg=None):
799 """Ensure that a given node is online.
801 @param lu: the LU on behalf of which we make the check
802 @param node: the node to check
803 @param msg: if passed, should be a message to replace the default one
804 @raise errors.OpPrereqError: if the node is offline
808 msg = "Can't use offline node"
809 if lu.cfg.GetNodeInfo(node).offline:
810 raise errors.OpPrereqError("%s: %s" % (msg, node), errors.ECODE_STATE)
813 def _CheckNodeNotDrained(lu, node):
814 """Ensure that a given node is not drained.
816 @param lu: the LU on behalf of which we make the check
817 @param node: the node to check
818 @raise errors.OpPrereqError: if the node is drained
821 if lu.cfg.GetNodeInfo(node).drained:
822 raise errors.OpPrereqError("Can't use drained node %s" % node,
826 def _CheckNodeVmCapable(lu, node):
827 """Ensure that a given node is vm capable.
829 @param lu: the LU on behalf of which we make the check
830 @param node: the node to check
831 @raise errors.OpPrereqError: if the node is not vm capable
834 if not lu.cfg.GetNodeInfo(node).vm_capable:
835 raise errors.OpPrereqError("Can't use non-vm_capable node %s" % node,
839 def _CheckNodeHasOS(lu, node, os_name, force_variant):
840 """Ensure that a node supports a given OS.
842 @param lu: the LU on behalf of which we make the check
843 @param node: the node to check
844 @param os_name: the OS to query about
845 @param force_variant: whether to ignore variant errors
846 @raise errors.OpPrereqError: if the node is not supporting the OS
849 result = lu.rpc.call_os_get(node, os_name)
850 result.Raise("OS '%s' not in supported OS list for node %s" %
852 prereq=True, ecode=errors.ECODE_INVAL)
853 if not force_variant:
854 _CheckOSVariant(result.payload, os_name)
857 def _CheckNodeHasSecondaryIP(lu, node, secondary_ip, prereq):
858 """Ensure that a node has the given secondary ip.
860 @type lu: L{LogicalUnit}
861 @param lu: the LU on behalf of which we make the check
863 @param node: the node to check
864 @type secondary_ip: string
865 @param secondary_ip: the ip to check
866 @type prereq: boolean
867 @param prereq: whether to throw a prerequisite or an execute error
868 @raise errors.OpPrereqError: if the node doesn't have the ip, and prereq=True
869 @raise errors.OpExecError: if the node doesn't have the ip, and prereq=False
872 result = lu.rpc.call_node_has_ip_address(node, secondary_ip)
873 result.Raise("Failure checking secondary ip on node %s" % node,
874 prereq=prereq, ecode=errors.ECODE_ENVIRON)
875 if not result.payload:
876 msg = ("Node claims it doesn't have the secondary ip you gave (%s),"
877 " please fix and re-run this command" % secondary_ip)
879 raise errors.OpPrereqError(msg, errors.ECODE_ENVIRON)
881 raise errors.OpExecError(msg)
884 def _GetClusterDomainSecret():
885 """Reads the cluster domain secret.
888 return utils.ReadOneLineFile(constants.CLUSTER_DOMAIN_SECRET_FILE,
892 def _CheckInstanceDown(lu, instance, reason):
893 """Ensure that an instance is not running."""
894 if instance.admin_up:
895 raise errors.OpPrereqError("Instance %s is marked to be up, %s" %
896 (instance.name, reason), errors.ECODE_STATE)
898 pnode = instance.primary_node
899 ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
900 ins_l.Raise("Can't contact node %s for instance information" % pnode,
901 prereq=True, ecode=errors.ECODE_ENVIRON)
903 if instance.name in ins_l.payload:
904 raise errors.OpPrereqError("Instance %s is running, %s" %
905 (instance.name, reason), errors.ECODE_STATE)
908 def _ExpandItemName(fn, name, kind):
909 """Expand an item name.
911 @param fn: the function to use for expansion
912 @param name: requested item name
913 @param kind: text description ('Node' or 'Instance')
914 @return: the resolved (full) name
915 @raise errors.OpPrereqError: if the item is not found
919 if full_name is None:
920 raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
925 def _ExpandNodeName(cfg, name):
926 """Wrapper over L{_ExpandItemName} for nodes."""
927 return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
930 def _ExpandInstanceName(cfg, name):
931 """Wrapper over L{_ExpandItemName} for instance."""
932 return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
935 def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
936 memory, vcpus, nics, disk_template, disks,
937 bep, hvp, hypervisor_name, tags):
938 """Builds instance related env variables for hooks
940 This builds the hook environment from individual variables.
943 @param name: the name of the instance
944 @type primary_node: string
945 @param primary_node: the name of the instance's primary node
946 @type secondary_nodes: list
947 @param secondary_nodes: list of secondary nodes as strings
948 @type os_type: string
949 @param os_type: the name of the instance's OS
950 @type status: boolean
951 @param status: the should_run status of the instance
953 @param memory: the memory size of the instance
955 @param vcpus: the count of VCPUs the instance has
957 @param nics: list of tuples (ip, mac, mode, link) representing
958 the NICs the instance has
959 @type disk_template: string
960 @param disk_template: the disk template of the instance
962 @param disks: the list of (size, mode) pairs
964 @param bep: the backend parameters for the instance
966 @param hvp: the hypervisor parameters for the instance
967 @type hypervisor_name: string
968 @param hypervisor_name: the hypervisor for the instance
970 @param tags: list of instance tags as strings
972 @return: the hook environment for this instance
981 "INSTANCE_NAME": name,
982 "INSTANCE_PRIMARY": primary_node,
983 "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
984 "INSTANCE_OS_TYPE": os_type,
985 "INSTANCE_STATUS": str_status,
986 "INSTANCE_MEMORY": memory,
987 "INSTANCE_VCPUS": vcpus,
988 "INSTANCE_DISK_TEMPLATE": disk_template,
989 "INSTANCE_HYPERVISOR": hypervisor_name,
993 nic_count = len(nics)
994 for idx, (ip, mac, mode, link) in enumerate(nics):
997 env["INSTANCE_NIC%d_IP" % idx] = ip
998 env["INSTANCE_NIC%d_MAC" % idx] = mac
999 env["INSTANCE_NIC%d_MODE" % idx] = mode
1000 env["INSTANCE_NIC%d_LINK" % idx] = link
1001 if mode == constants.NIC_MODE_BRIDGED:
1002 env["INSTANCE_NIC%d_BRIDGE" % idx] = link
1006 env["INSTANCE_NIC_COUNT"] = nic_count
1009 disk_count = len(disks)
1010 for idx, (size, mode) in enumerate(disks):
1011 env["INSTANCE_DISK%d_SIZE" % idx] = size
1012 env["INSTANCE_DISK%d_MODE" % idx] = mode
1016 env["INSTANCE_DISK_COUNT"] = disk_count
1021 env["INSTANCE_TAGS"] = " ".join(tags)
1023 for source, kind in [(bep, "BE"), (hvp, "HV")]:
1024 for key, value in source.items():
1025 env["INSTANCE_%s_%s" % (kind, key)] = value
1030 def _NICListToTuple(lu, nics):
1031 """Build a list of nic information tuples.
1033 This list is suitable to be passed to _BuildInstanceHookEnv or as a return
1034 value in LUInstanceQueryData.
1036 @type lu: L{LogicalUnit}
1037 @param lu: the logical unit on whose behalf we execute
1038 @type nics: list of L{objects.NIC}
1039 @param nics: list of nics to convert to hooks tuples
1043 cluster = lu.cfg.GetClusterInfo()
1047 filled_params = cluster.SimpleFillNIC(nic.nicparams)
1048 mode = filled_params[constants.NIC_MODE]
1049 link = filled_params[constants.NIC_LINK]
1050 hooks_nics.append((ip, mac, mode, link))
1054 def _BuildInstanceHookEnvByObject(lu, instance, override=None):
1055 """Builds instance related env variables for hooks from an object.
1057 @type lu: L{LogicalUnit}
1058 @param lu: the logical unit on whose behalf we execute
1059 @type instance: L{objects.Instance}
1060 @param instance: the instance for which we should build the
1062 @type override: dict
1063 @param override: dictionary with key/values that will override
1066 @return: the hook environment dictionary
1069 cluster = lu.cfg.GetClusterInfo()
1070 bep = cluster.FillBE(instance)
1071 hvp = cluster.FillHV(instance)
1073 "name": instance.name,
1074 "primary_node": instance.primary_node,
1075 "secondary_nodes": instance.secondary_nodes,
1076 "os_type": instance.os,
1077 "status": instance.admin_up,
1078 "memory": bep[constants.BE_MEMORY],
1079 "vcpus": bep[constants.BE_VCPUS],
1080 "nics": _NICListToTuple(lu, instance.nics),
1081 "disk_template": instance.disk_template,
1082 "disks": [(disk.size, disk.mode) for disk in instance.disks],
1085 "hypervisor_name": instance.hypervisor,
1086 "tags": instance.tags,
1089 args.update(override)
1090 return _BuildInstanceHookEnv(**args) # pylint: disable=W0142
1093 def _AdjustCandidatePool(lu, exceptions):
1094 """Adjust the candidate pool after node operations.
1097 mod_list = lu.cfg.MaintainCandidatePool(exceptions)
1099 lu.LogInfo("Promoted nodes to master candidate role: %s",
1100 utils.CommaJoin(node.name for node in mod_list))
1101 for name in mod_list:
1102 lu.context.ReaddNode(name)
1103 mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
1105 lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
1109 def _DecideSelfPromotion(lu, exceptions=None):
1110 """Decide whether I should promote myself as a master candidate.
1113 cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
1114 mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
1115 # the new node will increase mc_max with one, so:
1116 mc_should = min(mc_should + 1, cp_size)
1117 return mc_now < mc_should
1120 def _CheckNicsBridgesExist(lu, target_nics, target_node):
1121 """Check that the brigdes needed by a list of nics exist.
1124 cluster = lu.cfg.GetClusterInfo()
1125 paramslist = [cluster.SimpleFillNIC(nic.nicparams) for nic in target_nics]
1126 brlist = [params[constants.NIC_LINK] for params in paramslist
1127 if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
1129 result = lu.rpc.call_bridges_exist(target_node, brlist)
1130 result.Raise("Error checking bridges on destination node '%s'" %
1131 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
1134 def _CheckInstanceBridgesExist(lu, instance, node=None):
1135 """Check that the brigdes needed by an instance exist.
1139 node = instance.primary_node
1140 _CheckNicsBridgesExist(lu, instance.nics, node)
1143 def _CheckOSVariant(os_obj, name):
1144 """Check whether an OS name conforms to the os variants specification.
1146 @type os_obj: L{objects.OS}
1147 @param os_obj: OS object to check
1149 @param name: OS name passed by the user, to check for validity
1152 variant = objects.OS.GetVariant(name)
1153 if not os_obj.supported_variants:
1155 raise errors.OpPrereqError("OS '%s' doesn't support variants ('%s'"
1156 " passed)" % (os_obj.name, variant),
1160 raise errors.OpPrereqError("OS name must include a variant",
1163 if variant not in os_obj.supported_variants:
1164 raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
1167 def _GetNodeInstancesInner(cfg, fn):
1168 return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
1171 def _GetNodeInstances(cfg, node_name):
1172 """Returns a list of all primary and secondary instances on a node.
1176 return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
1179 def _GetNodePrimaryInstances(cfg, node_name):
1180 """Returns primary instances on a node.
1183 return _GetNodeInstancesInner(cfg,
1184 lambda inst: node_name == inst.primary_node)
1187 def _GetNodeSecondaryInstances(cfg, node_name):
1188 """Returns secondary instances on a node.
1191 return _GetNodeInstancesInner(cfg,
1192 lambda inst: node_name in inst.secondary_nodes)
1195 def _GetStorageTypeArgs(cfg, storage_type):
1196 """Returns the arguments for a storage type.
1199 # Special case for file storage
1200 if storage_type == constants.ST_FILE:
1201 # storage.FileStorage wants a list of storage directories
1202 return [[cfg.GetFileStorageDir(), cfg.GetSharedFileStorageDir()]]
1207 def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
1210 for dev in instance.disks:
1211 cfg.SetDiskID(dev, node_name)
1213 result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
1214 result.Raise("Failed to get disk status from node %s" % node_name,
1215 prereq=prereq, ecode=errors.ECODE_ENVIRON)
1217 for idx, bdev_status in enumerate(result.payload):
1218 if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
1224 def _CheckIAllocatorOrNode(lu, iallocator_slot, node_slot):
1225 """Check the sanity of iallocator and node arguments and use the
1226 cluster-wide iallocator if appropriate.
1228 Check that at most one of (iallocator, node) is specified. If none is
1229 specified, then the LU's opcode's iallocator slot is filled with the
1230 cluster-wide default iallocator.
1232 @type iallocator_slot: string
1233 @param iallocator_slot: the name of the opcode iallocator slot
1234 @type node_slot: string
1235 @param node_slot: the name of the opcode target node slot
1238 node = getattr(lu.op, node_slot, None)
1239 iallocator = getattr(lu.op, iallocator_slot, None)
1241 if node is not None and iallocator is not None:
1242 raise errors.OpPrereqError("Do not specify both, iallocator and node",
1244 elif node is None and iallocator is None:
1245 default_iallocator = lu.cfg.GetDefaultIAllocator()
1246 if default_iallocator:
1247 setattr(lu.op, iallocator_slot, default_iallocator)
1249 raise errors.OpPrereqError("No iallocator or node given and no"
1250 " cluster-wide default iallocator found;"
1251 " please specify either an iallocator or a"
1252 " node, or set a cluster-wide default"
1256 def _GetDefaultIAllocator(cfg, iallocator):
1257 """Decides on which iallocator to use.
1259 @type cfg: L{config.ConfigWriter}
1260 @param cfg: Cluster configuration object
1261 @type iallocator: string or None
1262 @param iallocator: Iallocator specified in opcode
1264 @return: Iallocator name
1268 # Use default iallocator
1269 iallocator = cfg.GetDefaultIAllocator()
1272 raise errors.OpPrereqError("No iallocator was specified, neither in the"
1273 " opcode nor as a cluster-wide default",
1279 class LUClusterPostInit(LogicalUnit):
1280 """Logical unit for running hooks after cluster initialization.
1283 HPATH = "cluster-init"
1284 HTYPE = constants.HTYPE_CLUSTER
1286 def BuildHooksEnv(self):
1291 "OP_TARGET": self.cfg.GetClusterName(),
1294 def BuildHooksNodes(self):
1295 """Build hooks nodes.
1298 return ([], [self.cfg.GetMasterNode()])
1300 def Exec(self, feedback_fn):
1307 class LUClusterDestroy(LogicalUnit):
1308 """Logical unit for destroying the cluster.
1311 HPATH = "cluster-destroy"
1312 HTYPE = constants.HTYPE_CLUSTER
1314 def BuildHooksEnv(self):
1319 "OP_TARGET": self.cfg.GetClusterName(),
1322 def BuildHooksNodes(self):
1323 """Build hooks nodes.
1328 def CheckPrereq(self):
1329 """Check prerequisites.
1331 This checks whether the cluster is empty.
1333 Any errors are signaled by raising errors.OpPrereqError.
1336 master = self.cfg.GetMasterNode()
1338 nodelist = self.cfg.GetNodeList()
1339 if len(nodelist) != 1 or nodelist[0] != master:
1340 raise errors.OpPrereqError("There are still %d node(s) in"
1341 " this cluster." % (len(nodelist) - 1),
1343 instancelist = self.cfg.GetInstanceList()
1345 raise errors.OpPrereqError("There are still %d instance(s) in"
1346 " this cluster." % len(instancelist),
1349 def Exec(self, feedback_fn):
1350 """Destroys the cluster.
1353 master = self.cfg.GetMasterNode()
1355 # Run post hooks on master node before it's removed
1356 _RunPostHook(self, master)
1358 result = self.rpc.call_node_stop_master(master, False)
1359 result.Raise("Could not disable the master role")
1364 def _VerifyCertificate(filename):
1365 """Verifies a certificate for L{LUClusterVerifyConfig}.
1367 @type filename: string
1368 @param filename: Path to PEM file
1372 cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
1373 utils.ReadFile(filename))
1374 except Exception, err: # pylint: disable=W0703
1375 return (LUClusterVerifyConfig.ETYPE_ERROR,
1376 "Failed to load X509 certificate %s: %s" % (filename, err))
1379 utils.VerifyX509Certificate(cert, constants.SSL_CERT_EXPIRATION_WARN,
1380 constants.SSL_CERT_EXPIRATION_ERROR)
1383 fnamemsg = "While verifying %s: %s" % (filename, msg)
1388 return (None, fnamemsg)
1389 elif errcode == utils.CERT_WARNING:
1390 return (LUClusterVerifyConfig.ETYPE_WARNING, fnamemsg)
1391 elif errcode == utils.CERT_ERROR:
1392 return (LUClusterVerifyConfig.ETYPE_ERROR, fnamemsg)
1394 raise errors.ProgrammerError("Unhandled certificate error code %r" % errcode)
1397 def _GetAllHypervisorParameters(cluster, instances):
1398 """Compute the set of all hypervisor parameters.
1400 @type cluster: L{objects.Cluster}
1401 @param cluster: the cluster object
1402 @param instances: list of L{objects.Instance}
1403 @param instances: additional instances from which to obtain parameters
1404 @rtype: list of (origin, hypervisor, parameters)
1405 @return: a list with all parameters found, indicating the hypervisor they
1406 apply to, and the origin (can be "cluster", "os X", or "instance Y")
1411 for hv_name in cluster.enabled_hypervisors:
1412 hvp_data.append(("cluster", hv_name, cluster.GetHVDefaults(hv_name)))
1414 for os_name, os_hvp in cluster.os_hvp.items():
1415 for hv_name, hv_params in os_hvp.items():
1417 full_params = cluster.GetHVDefaults(hv_name, os_name=os_name)
1418 hvp_data.append(("os %s" % os_name, hv_name, full_params))
1420 # TODO: collapse identical parameter values in a single one
1421 for instance in instances:
1422 if instance.hvparams:
1423 hvp_data.append(("instance %s" % instance.name, instance.hypervisor,
1424 cluster.FillHV(instance)))
1429 class _VerifyErrors(object):
1430 """Mix-in for cluster/group verify LUs.
1432 It provides _Error and _ErrorIf, and updates the self.bad boolean. (Expects
1433 self.op and self._feedback_fn to be available.)
1436 TCLUSTER = "cluster"
1438 TINSTANCE = "instance"
1440 ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
1441 ECLUSTERCERT = (TCLUSTER, "ECLUSTERCERT")
1442 ECLUSTERFILECHECK = (TCLUSTER, "ECLUSTERFILECHECK")
1443 ECLUSTERDANGLINGNODES = (TNODE, "ECLUSTERDANGLINGNODES")
1444 ECLUSTERDANGLINGINST = (TNODE, "ECLUSTERDANGLINGINST")
1445 EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
1446 EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
1447 EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
1448 EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1449 EINSTANCEFAULTYDISK = (TINSTANCE, "EINSTANCEFAULTYDISK")
1450 EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
1451 EINSTANCESPLITGROUPS = (TINSTANCE, "EINSTANCESPLITGROUPS")
1452 ENODEDRBD = (TNODE, "ENODEDRBD")
1453 ENODEDRBDHELPER = (TNODE, "ENODEDRBDHELPER")
1454 ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
1455 ENODEHOOKS = (TNODE, "ENODEHOOKS")
1456 ENODEHV = (TNODE, "ENODEHV")
1457 ENODELVM = (TNODE, "ENODELVM")
1458 ENODEN1 = (TNODE, "ENODEN1")
1459 ENODENET = (TNODE, "ENODENET")
1460 ENODEOS = (TNODE, "ENODEOS")
1461 ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
1462 ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
1463 ENODERPC = (TNODE, "ENODERPC")
1464 ENODESSH = (TNODE, "ENODESSH")
1465 ENODEVERSION = (TNODE, "ENODEVERSION")
1466 ENODESETUP = (TNODE, "ENODESETUP")
1467 ENODETIME = (TNODE, "ENODETIME")
1468 ENODEOOBPATH = (TNODE, "ENODEOOBPATH")
1470 ETYPE_FIELD = "code"
1471 ETYPE_ERROR = "ERROR"
1472 ETYPE_WARNING = "WARNING"
1474 def _Error(self, ecode, item, msg, *args, **kwargs):
1475 """Format an error message.
1477 Based on the opcode's error_codes parameter, either format a
1478 parseable error code, or a simpler error string.
1480 This must be called only from Exec and functions called from Exec.
1483 ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
1485 # first complete the msg
1488 # then format the whole message
1489 if self.op.error_codes: # This is a mix-in. pylint: disable=E1101
1490 msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
1496 msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
1497 # and finally report it via the feedback_fn
1498 self._feedback_fn(" - %s" % msg) # Mix-in. pylint: disable=E1101
1500 def _ErrorIf(self, cond, *args, **kwargs):
1501 """Log an error message if the passed condition is True.
1505 or self.op.debug_simulate_errors) # pylint: disable=E1101
1507 self._Error(*args, **kwargs)
1508 # do not mark the operation as failed for WARN cases only
1509 if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
1510 self.bad = self.bad or cond
1513 class LUClusterVerify(NoHooksLU):
1514 """Submits all jobs necessary to verify the cluster.
1519 def ExpandNames(self):
1520 self.needed_locks = {}
1522 def Exec(self, feedback_fn):
1525 if self.op.group_name:
1526 groups = [self.op.group_name]
1527 depends_fn = lambda: None
1529 groups = self.cfg.GetNodeGroupList()
1531 # Verify global configuration
1532 jobs.append([opcodes.OpClusterVerifyConfig()])
1534 # Always depend on global verification
1535 depends_fn = lambda: [(-len(jobs), [])]
1537 jobs.extend([opcodes.OpClusterVerifyGroup(group_name=group,
1538 depends=depends_fn())]
1539 for group in groups)
1541 # Fix up all parameters
1542 for op in itertools.chain(*jobs): # pylint: disable=W0142
1543 op.debug_simulate_errors = self.op.debug_simulate_errors
1544 op.verbose = self.op.verbose
1545 op.error_codes = self.op.error_codes
1547 op.skip_checks = self.op.skip_checks
1548 except AttributeError:
1549 assert not isinstance(op, opcodes.OpClusterVerifyGroup)
1551 return ResultWithJobs(jobs)
1554 class LUClusterVerifyConfig(NoHooksLU, _VerifyErrors):
1555 """Verifies the cluster config.
1560 def _VerifyHVP(self, hvp_data):
1561 """Verifies locally the syntax of the hypervisor parameters.
1564 for item, hv_name, hv_params in hvp_data:
1565 msg = ("hypervisor %s parameters syntax check (source %s): %%s" %
1568 hv_class = hypervisor.GetHypervisor(hv_name)
1569 utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
1570 hv_class.CheckParameterSyntax(hv_params)
1571 except errors.GenericError, err:
1572 self._ErrorIf(True, self.ECLUSTERCFG, None, msg % str(err))
1574 def ExpandNames(self):
1575 # Information can be safely retrieved as the BGL is acquired in exclusive
1577 assert locking.BGL in self.owned_locks(locking.LEVEL_CLUSTER)
1578 self.all_group_info = self.cfg.GetAllNodeGroupsInfo()
1579 self.all_node_info = self.cfg.GetAllNodesInfo()
1580 self.all_inst_info = self.cfg.GetAllInstancesInfo()
1581 self.needed_locks = {}
1583 def Exec(self, feedback_fn):
1584 """Verify integrity of cluster, performing various test on nodes.
1588 self._feedback_fn = feedback_fn
1590 feedback_fn("* Verifying cluster config")
1592 for msg in self.cfg.VerifyConfig():
1593 self._ErrorIf(True, self.ECLUSTERCFG, None, msg)
1595 feedback_fn("* Verifying cluster certificate files")
1597 for cert_filename in constants.ALL_CERT_FILES:
1598 (errcode, msg) = _VerifyCertificate(cert_filename)
1599 self._ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
1601 feedback_fn("* Verifying hypervisor parameters")
1603 self._VerifyHVP(_GetAllHypervisorParameters(self.cfg.GetClusterInfo(),
1604 self.all_inst_info.values()))
1606 feedback_fn("* Verifying all nodes belong to an existing group")
1608 # We do this verification here because, should this bogus circumstance
1609 # occur, it would never be caught by VerifyGroup, which only acts on
1610 # nodes/instances reachable from existing node groups.
1612 dangling_nodes = set(node.name for node in self.all_node_info.values()
1613 if node.group not in self.all_group_info)
1615 dangling_instances = {}
1616 no_node_instances = []
1618 for inst in self.all_inst_info.values():
1619 if inst.primary_node in dangling_nodes:
1620 dangling_instances.setdefault(inst.primary_node, []).append(inst.name)
1621 elif inst.primary_node not in self.all_node_info:
1622 no_node_instances.append(inst.name)
1627 utils.CommaJoin(dangling_instances.get(node.name,
1629 for node in dangling_nodes]
1631 self._ErrorIf(bool(dangling_nodes), self.ECLUSTERDANGLINGNODES, None,
1632 "the following nodes (and their instances) belong to a non"
1633 " existing group: %s", utils.CommaJoin(pretty_dangling))
1635 self._ErrorIf(bool(no_node_instances), self.ECLUSTERDANGLINGINST, None,
1636 "the following instances have a non-existing primary-node:"
1637 " %s", utils.CommaJoin(no_node_instances))
1642 class LUClusterVerifyGroup(LogicalUnit, _VerifyErrors):
1643 """Verifies the status of a node group.
1646 HPATH = "cluster-verify"
1647 HTYPE = constants.HTYPE_CLUSTER
1650 _HOOKS_INDENT_RE = re.compile("^", re.M)
1652 class NodeImage(object):
1653 """A class representing the logical and physical status of a node.
1656 @ivar name: the node name to which this object refers
1657 @ivar volumes: a structure as returned from
1658 L{ganeti.backend.GetVolumeList} (runtime)
1659 @ivar instances: a list of running instances (runtime)
1660 @ivar pinst: list of configured primary instances (config)
1661 @ivar sinst: list of configured secondary instances (config)
1662 @ivar sbp: dictionary of {primary-node: list of instances} for all
1663 instances for which this node is secondary (config)
1664 @ivar mfree: free memory, as reported by hypervisor (runtime)
1665 @ivar dfree: free disk, as reported by the node (runtime)
1666 @ivar offline: the offline status (config)
1667 @type rpc_fail: boolean
1668 @ivar rpc_fail: whether the RPC verify call was successfull (overall,
1669 not whether the individual keys were correct) (runtime)
1670 @type lvm_fail: boolean
1671 @ivar lvm_fail: whether the RPC call didn't return valid LVM data
1672 @type hyp_fail: boolean
1673 @ivar hyp_fail: whether the RPC call didn't return the instance list
1674 @type ghost: boolean
1675 @ivar ghost: whether this is a known node or not (config)
1676 @type os_fail: boolean
1677 @ivar os_fail: whether the RPC call didn't return valid OS data
1679 @ivar oslist: list of OSes as diagnosed by DiagnoseOS
1680 @type vm_capable: boolean
1681 @ivar vm_capable: whether the node can host instances
1684 def __init__(self, offline=False, name=None, vm_capable=True):
1693 self.offline = offline
1694 self.vm_capable = vm_capable
1695 self.rpc_fail = False
1696 self.lvm_fail = False
1697 self.hyp_fail = False
1699 self.os_fail = False
1702 def ExpandNames(self):
1703 # This raises errors.OpPrereqError on its own:
1704 self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
1706 # Get instances in node group; this is unsafe and needs verification later
1707 inst_names = self.cfg.GetNodeGroupInstances(self.group_uuid)
1709 self.needed_locks = {
1710 locking.LEVEL_INSTANCE: inst_names,
1711 locking.LEVEL_NODEGROUP: [self.group_uuid],
1712 locking.LEVEL_NODE: [],
1715 self.share_locks = _ShareAll()
1717 def DeclareLocks(self, level):
1718 if level == locking.LEVEL_NODE:
1719 # Get members of node group; this is unsafe and needs verification later
1720 nodes = set(self.cfg.GetNodeGroup(self.group_uuid).members)
1722 all_inst_info = self.cfg.GetAllInstancesInfo()
1724 # In Exec(), we warn about mirrored instances that have primary and
1725 # secondary living in separate node groups. To fully verify that
1726 # volumes for these instances are healthy, we will need to do an
1727 # extra call to their secondaries. We ensure here those nodes will
1729 for inst in self.owned_locks(locking.LEVEL_INSTANCE):
1730 # Important: access only the instances whose lock is owned
1731 if all_inst_info[inst].disk_template in constants.DTS_INT_MIRROR:
1732 nodes.update(all_inst_info[inst].secondary_nodes)
1734 self.needed_locks[locking.LEVEL_NODE] = nodes
1736 def CheckPrereq(self):
1737 assert self.group_uuid in self.owned_locks(locking.LEVEL_NODEGROUP)
1738 self.group_info = self.cfg.GetNodeGroup(self.group_uuid)
1740 group_nodes = set(self.group_info.members)
1741 group_instances = self.cfg.GetNodeGroupInstances(self.group_uuid)
1744 group_nodes.difference(self.owned_locks(locking.LEVEL_NODE))
1746 unlocked_instances = \
1747 group_instances.difference(self.owned_locks(locking.LEVEL_INSTANCE))
1750 raise errors.OpPrereqError("Missing lock for nodes: %s" %
1751 utils.CommaJoin(unlocked_nodes))
1753 if unlocked_instances:
1754 raise errors.OpPrereqError("Missing lock for instances: %s" %
1755 utils.CommaJoin(unlocked_instances))
1757 self.all_node_info = self.cfg.GetAllNodesInfo()
1758 self.all_inst_info = self.cfg.GetAllInstancesInfo()
1760 self.my_node_names = utils.NiceSort(group_nodes)
1761 self.my_inst_names = utils.NiceSort(group_instances)
1763 self.my_node_info = dict((name, self.all_node_info[name])
1764 for name in self.my_node_names)
1766 self.my_inst_info = dict((name, self.all_inst_info[name])
1767 for name in self.my_inst_names)
1769 # We detect here the nodes that will need the extra RPC calls for verifying
1770 # split LV volumes; they should be locked.
1771 extra_lv_nodes = set()
1773 for inst in self.my_inst_info.values():
1774 if inst.disk_template in constants.DTS_INT_MIRROR:
1775 group = self.my_node_info[inst.primary_node].group
1776 for nname in inst.secondary_nodes:
1777 if self.all_node_info[nname].group != group:
1778 extra_lv_nodes.add(nname)
1780 unlocked_lv_nodes = \
1781 extra_lv_nodes.difference(self.owned_locks(locking.LEVEL_NODE))
1783 if unlocked_lv_nodes:
1784 raise errors.OpPrereqError("these nodes could be locked: %s" %
1785 utils.CommaJoin(unlocked_lv_nodes))
1786 self.extra_lv_nodes = list(extra_lv_nodes)
1788 def _VerifyNode(self, ninfo, nresult):
1789 """Perform some basic validation on data returned from a node.
1791 - check the result data structure is well formed and has all the
1793 - check ganeti version
1795 @type ninfo: L{objects.Node}
1796 @param ninfo: the node to check
1797 @param nresult: the results from the node
1799 @return: whether overall this call was successful (and we can expect
1800 reasonable values in the respose)
1804 _ErrorIf = self._ErrorIf # pylint: disable=C0103
1806 # main result, nresult should be a non-empty dict
1807 test = not nresult or not isinstance(nresult, dict)
1808 _ErrorIf(test, self.ENODERPC, node,
1809 "unable to verify node: no data returned")
1813 # compares ganeti version
1814 local_version = constants.PROTOCOL_VERSION
1815 remote_version = nresult.get("version", None)
1816 test = not (remote_version and
1817 isinstance(remote_version, (list, tuple)) and
1818 len(remote_version) == 2)
1819 _ErrorIf(test, self.ENODERPC, node,
1820 "connection to node returned invalid data")
1824 test = local_version != remote_version[0]
1825 _ErrorIf(test, self.ENODEVERSION, node,
1826 "incompatible protocol versions: master %s,"
1827 " node %s", local_version, remote_version[0])
1831 # node seems compatible, we can actually try to look into its results
1833 # full package version
1834 self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1835 self.ENODEVERSION, node,
1836 "software version mismatch: master %s, node %s",
1837 constants.RELEASE_VERSION, remote_version[1],
1838 code=self.ETYPE_WARNING)
1840 hyp_result = nresult.get(constants.NV_HYPERVISOR, None)
1841 if ninfo.vm_capable and isinstance(hyp_result, dict):
1842 for hv_name, hv_result in hyp_result.iteritems():
1843 test = hv_result is not None
1844 _ErrorIf(test, self.ENODEHV, node,
1845 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1847 hvp_result = nresult.get(constants.NV_HVPARAMS, None)
1848 if ninfo.vm_capable and isinstance(hvp_result, list):
1849 for item, hv_name, hv_result in hvp_result:
1850 _ErrorIf(True, self.ENODEHV, node,
1851 "hypervisor %s parameter verify failure (source %s): %s",
1852 hv_name, item, hv_result)
1854 test = nresult.get(constants.NV_NODESETUP,
1855 ["Missing NODESETUP results"])
1856 _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1861 def _VerifyNodeTime(self, ninfo, nresult,
1862 nvinfo_starttime, nvinfo_endtime):
1863 """Check the node time.
1865 @type ninfo: L{objects.Node}
1866 @param ninfo: the node to check
1867 @param nresult: the remote results for the node
1868 @param nvinfo_starttime: the start time of the RPC call
1869 @param nvinfo_endtime: the end time of the RPC call
1873 _ErrorIf = self._ErrorIf # pylint: disable=C0103
1875 ntime = nresult.get(constants.NV_TIME, None)
1877 ntime_merged = utils.MergeTime(ntime)
1878 except (ValueError, TypeError):
1879 _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time")
1882 if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
1883 ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged)
1884 elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
1885 ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime)
1889 _ErrorIf(ntime_diff is not None, self.ENODETIME, node,
1890 "Node time diverges by at least %s from master node time",
1893 def _VerifyNodeLVM(self, ninfo, nresult, vg_name):
1894 """Check the node LVM results.
1896 @type ninfo: L{objects.Node}
1897 @param ninfo: the node to check
1898 @param nresult: the remote results for the node
1899 @param vg_name: the configured VG name
1906 _ErrorIf = self._ErrorIf # pylint: disable=C0103
1908 # checks vg existence and size > 20G
1909 vglist = nresult.get(constants.NV_VGLIST, None)
1911 _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1913 vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1914 constants.MIN_VG_SIZE)
1915 _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1918 pvlist = nresult.get(constants.NV_PVLIST, None)
1919 test = pvlist is None
1920 _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1922 # check that ':' is not present in PV names, since it's a
1923 # special character for lvcreate (denotes the range of PEs to
1925 for _, pvname, owner_vg in pvlist:
1926 test = ":" in pvname
1927 _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1928 " '%s' of VG '%s'", pvname, owner_vg)
1930 def _VerifyNodeBridges(self, ninfo, nresult, bridges):
1931 """Check the node bridges.
1933 @type ninfo: L{objects.Node}
1934 @param ninfo: the node to check
1935 @param nresult: the remote results for the node
1936 @param bridges: the expected list of bridges
1943 _ErrorIf = self._ErrorIf # pylint: disable=C0103
1945 missing = nresult.get(constants.NV_BRIDGES, None)
1946 test = not isinstance(missing, list)
1947 _ErrorIf(test, self.ENODENET, node,
1948 "did not return valid bridge information")
1950 _ErrorIf(bool(missing), self.ENODENET, node, "missing bridges: %s" %
1951 utils.CommaJoin(sorted(missing)))
1953 def _VerifyNodeNetwork(self, ninfo, nresult):
1954 """Check the node network connectivity results.
1956 @type ninfo: L{objects.Node}
1957 @param ninfo: the node to check
1958 @param nresult: the remote results for the node
1962 _ErrorIf = self._ErrorIf # pylint: disable=C0103
1964 test = constants.NV_NODELIST not in nresult
1965 _ErrorIf(test, self.ENODESSH, node,
1966 "node hasn't returned node ssh connectivity data")
1968 if nresult[constants.NV_NODELIST]:
1969 for a_node, a_msg in nresult[constants.NV_NODELIST].items():
1970 _ErrorIf(True, self.ENODESSH, node,
1971 "ssh communication with node '%s': %s", a_node, a_msg)
1973 test = constants.NV_NODENETTEST not in nresult
1974 _ErrorIf(test, self.ENODENET, node,
1975 "node hasn't returned node tcp connectivity data")
1977 if nresult[constants.NV_NODENETTEST]:
1978 nlist = utils.NiceSort(nresult[constants.NV_NODENETTEST].keys())
1980 _ErrorIf(True, self.ENODENET, node,
1981 "tcp communication with node '%s': %s",
1982 anode, nresult[constants.NV_NODENETTEST][anode])
1984 test = constants.NV_MASTERIP not in nresult
1985 _ErrorIf(test, self.ENODENET, node,
1986 "node hasn't returned node master IP reachability data")
1988 if not nresult[constants.NV_MASTERIP]:
1989 if node == self.master_node:
1990 msg = "the master node cannot reach the master IP (not configured?)"
1992 msg = "cannot reach the master IP"
1993 _ErrorIf(True, self.ENODENET, node, msg)
1995 def _VerifyInstance(self, instance, instanceconfig, node_image,
1997 """Verify an instance.
1999 This function checks to see if the required block devices are
2000 available on the instance's node.
2003 _ErrorIf = self._ErrorIf # pylint: disable=C0103
2004 node_current = instanceconfig.primary_node
2006 node_vol_should = {}
2007 instanceconfig.MapLVsByNode(node_vol_should)
2009 for node in node_vol_should:
2010 n_img = node_image[node]
2011 if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
2012 # ignore missing volumes on offline or broken nodes
2014 for volume in node_vol_should[node]:
2015 test = volume not in n_img.volumes
2016 _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
2017 "volume %s missing on node %s", volume, node)
2019 if instanceconfig.admin_up:
2020 pri_img = node_image[node_current]
2021 test = instance not in pri_img.instances and not pri_img.offline
2022 _ErrorIf(test, self.EINSTANCEDOWN, instance,
2023 "instance not running on its primary node %s",
2026 diskdata = [(nname, success, status, idx)
2027 for (nname, disks) in diskstatus.items()
2028 for idx, (success, status) in enumerate(disks)]
2030 for nname, success, bdev_status, idx in diskdata:
2031 # the 'ghost node' construction in Exec() ensures that we have a
2033 snode = node_image[nname]
2034 bad_snode = snode.ghost or snode.offline
2035 _ErrorIf(instanceconfig.admin_up and not success and not bad_snode,
2036 self.EINSTANCEFAULTYDISK, instance,
2037 "couldn't retrieve status for disk/%s on %s: %s",
2038 idx, nname, bdev_status)
2039 _ErrorIf((instanceconfig.admin_up and success and
2040 bdev_status.ldisk_status == constants.LDS_FAULTY),
2041 self.EINSTANCEFAULTYDISK, instance,
2042 "disk/%s on %s is faulty", idx, nname)
2044 def _VerifyOrphanVolumes(self, node_vol_should, node_image, reserved):
2045 """Verify if there are any unknown volumes in the cluster.
2047 The .os, .swap and backup volumes are ignored. All other volumes are
2048 reported as unknown.
2050 @type reserved: L{ganeti.utils.FieldSet}
2051 @param reserved: a FieldSet of reserved volume names
2054 for node, n_img in node_image.items():
2055 if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
2056 # skip non-healthy nodes
2058 for volume in n_img.volumes:
2059 test = ((node not in node_vol_should or
2060 volume not in node_vol_should[node]) and
2061 not reserved.Matches(volume))
2062 self._ErrorIf(test, self.ENODEORPHANLV, node,
2063 "volume %s is unknown", volume)
2065 def _VerifyNPlusOneMemory(self, node_image, instance_cfg):
2066 """Verify N+1 Memory Resilience.
2068 Check that if one single node dies we can still start all the
2069 instances it was primary for.
2072 cluster_info = self.cfg.GetClusterInfo()
2073 for node, n_img in node_image.items():
2074 # This code checks that every node which is now listed as
2075 # secondary has enough memory to host all instances it is
2076 # supposed to should a single other node in the cluster fail.
2077 # FIXME: not ready for failover to an arbitrary node
2078 # FIXME: does not support file-backed instances
2079 # WARNING: we currently take into account down instances as well
2080 # as up ones, considering that even if they're down someone
2081 # might want to start them even in the event of a node failure.
2083 # we're skipping offline nodes from the N+1 warning, since
2084 # most likely we don't have good memory infromation from them;
2085 # we already list instances living on such nodes, and that's
2088 for prinode, instances in n_img.sbp.items():
2090 for instance in instances:
2091 bep = cluster_info.FillBE(instance_cfg[instance])
2092 if bep[constants.BE_AUTO_BALANCE]:
2093 needed_mem += bep[constants.BE_MEMORY]
2094 test = n_img.mfree < needed_mem
2095 self._ErrorIf(test, self.ENODEN1, node,
2096 "not enough memory to accomodate instance failovers"
2097 " should node %s fail (%dMiB needed, %dMiB available)",
2098 prinode, needed_mem, n_img.mfree)
2101 def _VerifyFiles(cls, errorif, nodeinfo, master_node, all_nvinfo,
2102 (files_all, files_all_opt, files_mc, files_vm)):
2103 """Verifies file checksums collected from all nodes.
2105 @param errorif: Callback for reporting errors
2106 @param nodeinfo: List of L{objects.Node} objects
2107 @param master_node: Name of master node
2108 @param all_nvinfo: RPC results
2111 node_names = frozenset(node.name for node in nodeinfo if not node.offline)
2113 assert master_node in node_names
2114 assert (len(files_all | files_all_opt | files_mc | files_vm) ==
2115 sum(map(len, [files_all, files_all_opt, files_mc, files_vm]))), \
2116 "Found file listed in more than one file list"
2118 # Define functions determining which nodes to consider for a file
2119 file2nodefn = dict([(filename, fn)
2120 for (files, fn) in [(files_all, None),
2121 (files_all_opt, None),
2122 (files_mc, lambda node: (node.master_candidate or
2123 node.name == master_node)),
2124 (files_vm, lambda node: node.vm_capable)]
2125 for filename in files])
2127 fileinfo = dict((filename, {}) for filename in file2nodefn.keys())
2129 for node in nodeinfo:
2133 nresult = all_nvinfo[node.name]
2135 if nresult.fail_msg or not nresult.payload:
2138 node_files = nresult.payload.get(constants.NV_FILELIST, None)
2140 test = not (node_files and isinstance(node_files, dict))
2141 errorif(test, cls.ENODEFILECHECK, node.name,
2142 "Node did not return file checksum data")
2146 for (filename, checksum) in node_files.items():
2147 # Check if the file should be considered for a node
2148 fn = file2nodefn[filename]
2149 if fn is None or fn(node):
2150 fileinfo[filename].setdefault(checksum, set()).add(node.name)
2152 for (filename, checksums) in fileinfo.items():
2153 assert compat.all(len(i) > 10 for i in checksums), "Invalid checksum"
2155 # Nodes having the file
2156 with_file = frozenset(node_name
2157 for nodes in fileinfo[filename].values()
2158 for node_name in nodes)
2160 # Nodes missing file
2161 missing_file = node_names - with_file
2163 if filename in files_all_opt:
2165 errorif(missing_file and missing_file != node_names,
2166 cls.ECLUSTERFILECHECK, None,
2167 "File %s is optional, but it must exist on all or no"
2168 " nodes (not found on %s)",
2169 filename, utils.CommaJoin(utils.NiceSort(missing_file)))
2171 errorif(missing_file, cls.ECLUSTERFILECHECK, None,
2172 "File %s is missing from node(s) %s", filename,
2173 utils.CommaJoin(utils.NiceSort(missing_file)))
2175 # See if there are multiple versions of the file
2176 test = len(checksums) > 1
2178 variants = ["variant %s on %s" %
2179 (idx + 1, utils.CommaJoin(utils.NiceSort(nodes)))
2180 for (idx, (checksum, nodes)) in
2181 enumerate(sorted(checksums.items()))]
2185 errorif(test, cls.ECLUSTERFILECHECK, None,
2186 "File %s found with %s different checksums (%s)",
2187 filename, len(checksums), "; ".join(variants))
2189 def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_helper,
2191 """Verifies and the node DRBD status.
2193 @type ninfo: L{objects.Node}
2194 @param ninfo: the node to check
2195 @param nresult: the remote results for the node
2196 @param instanceinfo: the dict of instances
2197 @param drbd_helper: the configured DRBD usermode helper
2198 @param drbd_map: the DRBD map as returned by
2199 L{ganeti.config.ConfigWriter.ComputeDRBDMap}
2203 _ErrorIf = self._ErrorIf # pylint: disable=C0103
2206 helper_result = nresult.get(constants.NV_DRBDHELPER, None)
2207 test = (helper_result == None)
2208 _ErrorIf(test, self.ENODEDRBDHELPER, node,
2209 "no drbd usermode helper returned")
2211 status, payload = helper_result
2213 _ErrorIf(test, self.ENODEDRBDHELPER, node,
2214 "drbd usermode helper check unsuccessful: %s", payload)
2215 test = status and (payload != drbd_helper)
2216 _ErrorIf(test, self.ENODEDRBDHELPER, node,
2217 "wrong drbd usermode helper: %s", payload)
2219 # compute the DRBD minors
2221 for minor, instance in drbd_map[node].items():
2222 test = instance not in instanceinfo
2223 _ErrorIf(test, self.ECLUSTERCFG, None,
2224 "ghost instance '%s' in temporary DRBD map", instance)
2225 # ghost instance should not be running, but otherwise we
2226 # don't give double warnings (both ghost instance and
2227 # unallocated minor in use)
2229 node_drbd[minor] = (instance, False)
2231 instance = instanceinfo[instance]
2232 node_drbd[minor] = (instance.name, instance.admin_up)
2234 # and now check them
2235 used_minors = nresult.get(constants.NV_DRBDLIST, [])
2236 test = not isinstance(used_minors, (tuple, list))
2237 _ErrorIf(test, self.ENODEDRBD, node,
2238 "cannot parse drbd status file: %s", str(used_minors))
2240 # we cannot check drbd status
2243 for minor, (iname, must_exist) in node_drbd.items():
2244 test = minor not in used_minors and must_exist
2245 _ErrorIf(test, self.ENODEDRBD, node,
2246 "drbd minor %d of instance %s is not active", minor, iname)
2247 for minor in used_minors:
2248 test = minor not in node_drbd
2249 _ErrorIf(test, self.ENODEDRBD, node,
2250 "unallocated drbd minor %d is in use", minor)
2252 def _UpdateNodeOS(self, ninfo, nresult, nimg):
2253 """Builds the node OS structures.
2255 @type ninfo: L{objects.Node}
2256 @param ninfo: the node to check
2257 @param nresult: the remote results for the node
2258 @param nimg: the node image object
2262 _ErrorIf = self._ErrorIf # pylint: disable=C0103
2264 remote_os = nresult.get(constants.NV_OSLIST, None)
2265 test = (not isinstance(remote_os, list) or
2266 not compat.all(isinstance(v, list) and len(v) == 7
2267 for v in remote_os))
2269 _ErrorIf(test, self.ENODEOS, node,
2270 "node hasn't returned valid OS data")
2279 for (name, os_path, status, diagnose,
2280 variants, parameters, api_ver) in nresult[constants.NV_OSLIST]:
2282 if name not in os_dict:
2285 # parameters is a list of lists instead of list of tuples due to
2286 # JSON lacking a real tuple type, fix it:
2287 parameters = [tuple(v) for v in parameters]
2288 os_dict[name].append((os_path, status, diagnose,
2289 set(variants), set(parameters), set(api_ver)))
2291 nimg.oslist = os_dict
2293 def _VerifyNodeOS(self, ninfo, nimg, base):
2294 """Verifies the node OS list.
2296 @type ninfo: L{objects.Node}
2297 @param ninfo: the node to check
2298 @param nimg: the node image object
2299 @param base: the 'template' node we match against (e.g. from the master)
2303 _ErrorIf = self._ErrorIf # pylint: disable=C0103
2305 assert not nimg.os_fail, "Entered _VerifyNodeOS with failed OS rpc?"
2307 beautify_params = lambda l: ["%s: %s" % (k, v) for (k, v) in l]
2308 for os_name, os_data in nimg.oslist.items():
2309 assert os_data, "Empty OS status for OS %s?!" % os_name
2310 f_path, f_status, f_diag, f_var, f_param, f_api = os_data[0]
2311 _ErrorIf(not f_status, self.ENODEOS, node,
2312 "Invalid OS %s (located at %s): %s", os_name, f_path, f_diag)
2313 _ErrorIf(len(os_data) > 1, self.ENODEOS, node,
2314 "OS '%s' has multiple entries (first one shadows the rest): %s",
2315 os_name, utils.CommaJoin([v[0] for v in os_data]))
2316 # comparisons with the 'base' image
2317 test = os_name not in base.oslist
2318 _ErrorIf(test, self.ENODEOS, node,
2319 "Extra OS %s not present on reference node (%s)",
2323 assert base.oslist[os_name], "Base node has empty OS status?"
2324 _, b_status, _, b_var, b_param, b_api = base.oslist[os_name][0]
2326 # base OS is invalid, skipping
2328 for kind, a, b in [("API version", f_api, b_api),
2329 ("variants list", f_var, b_var),
2330 ("parameters", beautify_params(f_param),
2331 beautify_params(b_param))]:
2332 _ErrorIf(a != b, self.ENODEOS, node,
2333 "OS %s for %s differs from reference node %s: [%s] vs. [%s]",
2334 kind, os_name, base.name,
2335 utils.CommaJoin(sorted(a)), utils.CommaJoin(sorted(b)))
2337 # check any missing OSes
2338 missing = set(base.oslist.keys()).difference(nimg.oslist.keys())
2339 _ErrorIf(missing, self.ENODEOS, node,
2340 "OSes present on reference node %s but missing on this node: %s",
2341 base.name, utils.CommaJoin(missing))
2343 def _VerifyOob(self, ninfo, nresult):
2344 """Verifies out of band functionality of a node.
2346 @type ninfo: L{objects.Node}
2347 @param ninfo: the node to check
2348 @param nresult: the remote results for the node
2352 # We just have to verify the paths on master and/or master candidates
2353 # as the oob helper is invoked on the master
2354 if ((ninfo.master_candidate or ninfo.master_capable) and
2355 constants.NV_OOB_PATHS in nresult):
2356 for path_result in nresult[constants.NV_OOB_PATHS]:
2357 self._ErrorIf(path_result, self.ENODEOOBPATH, node, path_result)
2359 def _UpdateNodeVolumes(self, ninfo, nresult, nimg, vg_name):
2360 """Verifies and updates the node volume data.
2362 This function will update a L{NodeImage}'s internal structures
2363 with data from the remote call.
2365 @type ninfo: L{objects.Node}
2366 @param ninfo: the node to check
2367 @param nresult: the remote results for the node
2368 @param nimg: the node image object
2369 @param vg_name: the configured VG name
2373 _ErrorIf = self._ErrorIf # pylint: disable=C0103
2375 nimg.lvm_fail = True
2376 lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
2379 elif isinstance(lvdata, basestring):
2380 _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
2381 utils.SafeEncode(lvdata))
2382 elif not isinstance(lvdata, dict):
2383 _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
2385 nimg.volumes = lvdata
2386 nimg.lvm_fail = False
2388 def _UpdateNodeInstances(self, ninfo, nresult, nimg):
2389 """Verifies and updates the node instance list.
2391 If the listing was successful, then updates this node's instance
2392 list. Otherwise, it marks the RPC call as failed for the instance
2395 @type ninfo: L{objects.Node}
2396 @param ninfo: the node to check
2397 @param nresult: the remote results for the node
2398 @param nimg: the node image object
2401 idata = nresult.get(constants.NV_INSTANCELIST, None)
2402 test = not isinstance(idata, list)
2403 self._ErrorIf(test, self.ENODEHV, ninfo.name, "rpc call to node failed"
2404 " (instancelist): %s", utils.SafeEncode(str(idata)))
2406 nimg.hyp_fail = True
2408 nimg.instances = idata
2410 def _UpdateNodeInfo(self, ninfo, nresult, nimg, vg_name):
2411 """Verifies and computes a node information map
2413 @type ninfo: L{objects.Node}
2414 @param ninfo: the node to check
2415 @param nresult: the remote results for the node
2416 @param nimg: the node image object
2417 @param vg_name: the configured VG name
2421 _ErrorIf = self._ErrorIf # pylint: disable=C0103
2423 # try to read free memory (from the hypervisor)
2424 hv_info = nresult.get(constants.NV_HVINFO, None)
2425 test = not isinstance(hv_info, dict) or "memory_free" not in hv_info
2426 _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
2429 nimg.mfree = int(hv_info["memory_free"])
2430 except (ValueError, TypeError):
2431 _ErrorIf(True, self.ENODERPC, node,
2432 "node returned invalid nodeinfo, check hypervisor")
2434 # FIXME: devise a free space model for file based instances as well
2435 if vg_name is not None:
2436 test = (constants.NV_VGLIST not in nresult or
2437 vg_name not in nresult[constants.NV_VGLIST])
2438 _ErrorIf(test, self.ENODELVM, node,
2439 "node didn't return data for the volume group '%s'"
2440 " - it is either missing or broken", vg_name)
2443 nimg.dfree = int(nresult[constants.NV_VGLIST][vg_name])
2444 except (ValueError, TypeError):
2445 _ErrorIf(True, self.ENODERPC, node,
2446 "node returned invalid LVM info, check LVM status")
2448 def _CollectDiskInfo(self, nodelist, node_image, instanceinfo):
2449 """Gets per-disk status information for all instances.
2451 @type nodelist: list of strings
2452 @param nodelist: Node names
2453 @type node_image: dict of (name, L{objects.Node})
2454 @param node_image: Node objects
2455 @type instanceinfo: dict of (name, L{objects.Instance})
2456 @param instanceinfo: Instance objects
2457 @rtype: {instance: {node: [(succes, payload)]}}
2458 @return: a dictionary of per-instance dictionaries with nodes as
2459 keys and disk information as values; the disk information is a
2460 list of tuples (success, payload)
2463 _ErrorIf = self._ErrorIf # pylint: disable=C0103
2466 node_disks_devonly = {}
2467 diskless_instances = set()
2468 diskless = constants.DT_DISKLESS
2470 for nname in nodelist:
2471 node_instances = list(itertools.chain(node_image[nname].pinst,
2472 node_image[nname].sinst))
2473 diskless_instances.update(inst for inst in node_instances
2474 if instanceinfo[inst].disk_template == diskless)
2475 disks = [(inst, disk)
2476 for inst in node_instances
2477 for disk in instanceinfo[inst].disks]
2480 # No need to collect data
2483 node_disks[nname] = disks
2485 # Creating copies as SetDiskID below will modify the objects and that can
2486 # lead to incorrect data returned from nodes
2487 devonly = [dev.Copy() for (_, dev) in disks]
2490 self.cfg.SetDiskID(dev, nname)
2492 node_disks_devonly[nname] = devonly
2494 assert len(node_disks) == len(node_disks_devonly)
2496 # Collect data from all nodes with disks
2497 result = self.rpc.call_blockdev_getmirrorstatus_multi(node_disks.keys(),
2500 assert len(result) == len(node_disks)
2504 for (nname, nres) in result.items():
2505 disks = node_disks[nname]
2508 # No data from this node
2509 data = len(disks) * [(False, "node offline")]
2512 _ErrorIf(msg, self.ENODERPC, nname,
2513 "while getting disk information: %s", msg)
2515 # No data from this node
2516 data = len(disks) * [(False, msg)]
2519 for idx, i in enumerate(nres.payload):
2520 if isinstance(i, (tuple, list)) and len(i) == 2:
2523 logging.warning("Invalid result from node %s, entry %d: %s",
2525 data.append((False, "Invalid result from the remote node"))
2527 for ((inst, _), status) in zip(disks, data):
2528 instdisk.setdefault(inst, {}).setdefault(nname, []).append(status)
2530 # Add empty entries for diskless instances.
2531 for inst in diskless_instances:
2532 assert inst not in instdisk
2535 assert compat.all(len(statuses) == len(instanceinfo[inst].disks) and
2536 len(nnames) <= len(instanceinfo[inst].all_nodes) and
2537 compat.all(isinstance(s, (tuple, list)) and
2538 len(s) == 2 for s in statuses)
2539 for inst, nnames in instdisk.items()
2540 for nname, statuses in nnames.items())
2541 assert set(instdisk) == set(instanceinfo), "instdisk consistency failure"
2545 def BuildHooksEnv(self):
2548 Cluster-Verify hooks just ran in the post phase and their failure makes
2549 the output be logged in the verify output and the verification to fail.
2553 "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
2556 env.update(("NODE_TAGS_%s" % node.name, " ".join(node.GetTags()))
2557 for node in self.my_node_info.values())
2561 def BuildHooksNodes(self):
2562 """Build hooks nodes.
2565 return ([], self.my_node_names)
2567 def Exec(self, feedback_fn):
2568 """Verify integrity of the node group, performing various test on nodes.
2571 # This method has too many local variables. pylint: disable=R0914
2572 feedback_fn("* Verifying group '%s'" % self.group_info.name)
2574 if not self.my_node_names:
2576 feedback_fn("* Empty node group, skipping verification")
2580 _ErrorIf = self._ErrorIf # pylint: disable=C0103
2581 verbose = self.op.verbose
2582 self._feedback_fn = feedback_fn
2584 vg_name = self.cfg.GetVGName()
2585 drbd_helper = self.cfg.GetDRBDHelper()
2586 cluster = self.cfg.GetClusterInfo()
2587 groupinfo = self.cfg.GetAllNodeGroupsInfo()
2588 hypervisors = cluster.enabled_hypervisors
2589 node_data_list = [self.my_node_info[name] for name in self.my_node_names]
2591 i_non_redundant = [] # Non redundant instances
2592 i_non_a_balanced = [] # Non auto-balanced instances
2593 n_offline = 0 # Count of offline nodes
2594 n_drained = 0 # Count of nodes being drained
2595 node_vol_should = {}
2597 # FIXME: verify OS list
2600 filemap = _ComputeAncillaryFiles(cluster, False)
2602 # do local checksums
2603 master_node = self.master_node = self.cfg.GetMasterNode()
2604 master_ip = self.cfg.GetMasterIP()
2606 feedback_fn("* Gathering data (%d nodes)" % len(self.my_node_names))
2608 # We will make nodes contact all nodes in their group, and one node from
2609 # every other group.
2610 # TODO: should it be a *random* node, different every time?
2611 online_nodes = [node.name for node in node_data_list if not node.offline]
2612 other_group_nodes = {}
2614 for name in sorted(self.all_node_info):
2615 node = self.all_node_info[name]
2616 if (node.group not in other_group_nodes
2617 and node.group != self.group_uuid
2618 and not node.offline):
2619 other_group_nodes[node.group] = node.name
2621 node_verify_param = {
2622 constants.NV_FILELIST:
2623 utils.UniqueSequence(filename
2624 for files in filemap
2625 for filename in files),
2626 constants.NV_NODELIST: online_nodes + other_group_nodes.values(),
2627 constants.NV_HYPERVISOR: hypervisors,
2628 constants.NV_HVPARAMS:
2629 _GetAllHypervisorParameters(cluster, self.all_inst_info.values()),
2630 constants.NV_NODENETTEST: [(node.name, node.primary_ip, node.secondary_ip)
2631 for node in node_data_list
2632 if not node.offline],
2633 constants.NV_INSTANCELIST: hypervisors,
2634 constants.NV_VERSION: None,
2635 constants.NV_HVINFO: self.cfg.GetHypervisorType(),
2636 constants.NV_NODESETUP: None,
2637 constants.NV_TIME: None,
2638 constants.NV_MASTERIP: (master_node, master_ip),
2639 constants.NV_OSLIST: None,
2640 constants.NV_VMNODES: self.cfg.GetNonVmCapableNodeList(),
2643 if vg_name is not None:
2644 node_verify_param[constants.NV_VGLIST] = None
2645 node_verify_param[constants.NV_LVLIST] = vg_name
2646 node_verify_param[constants.NV_PVLIST] = [vg_name]
2647 node_verify_param[constants.NV_DRBDLIST] = None
2650 node_verify_param[constants.NV_DRBDHELPER] = drbd_helper
2653 # FIXME: this needs to be changed per node-group, not cluster-wide
2655 default_nicpp = cluster.nicparams[constants.PP_DEFAULT]
2656 if default_nicpp[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
2657 bridges.add(default_nicpp[constants.NIC_LINK])
2658 for instance in self.my_inst_info.values():
2659 for nic in instance.nics:
2660 full_nic = cluster.SimpleFillNIC(nic.nicparams)
2661 if full_nic[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
2662 bridges.add(full_nic[constants.NIC_LINK])
2665 node_verify_param[constants.NV_BRIDGES] = list(bridges)
2667 # Build our expected cluster state
2668 node_image = dict((node.name, self.NodeImage(offline=node.offline,
2670 vm_capable=node.vm_capable))
2671 for node in node_data_list)
2675 for node in self.all_node_info.values():
2676 path = _SupportsOob(self.cfg, node)
2677 if path and path not in oob_paths:
2678 oob_paths.append(path)
2681 node_verify_param[constants.NV_OOB_PATHS] = oob_paths
2683 for instance in self.my_inst_names:
2684 inst_config = self.my_inst_info[instance]
2686 for nname in inst_config.all_nodes:
2687 if nname not in node_image:
2688 gnode = self.NodeImage(name=nname)
2689 gnode.ghost = (nname not in self.all_node_info)
2690 node_image[nname] = gnode
2692 inst_config.MapLVsByNode(node_vol_should)
2694 pnode = inst_config.primary_node
2695 node_image[pnode].pinst.append(instance)
2697 for snode in inst_config.secondary_nodes:
2698 nimg = node_image[snode]
2699 nimg.sinst.append(instance)
2700 if pnode not in nimg.sbp:
2701 nimg.sbp[pnode] = []
2702 nimg.sbp[pnode].append(instance)
2704 # At this point, we have the in-memory data structures complete,
2705 # except for the runtime information, which we'll gather next
2707 # Due to the way our RPC system works, exact response times cannot be
2708 # guaranteed (e.g. a broken node could run into a timeout). By keeping the
2709 # time before and after executing the request, we can at least have a time
2711 nvinfo_starttime = time.time()
2712 all_nvinfo = self.rpc.call_node_verify(self.my_node_names,
2714 self.cfg.GetClusterName())
2715 nvinfo_endtime = time.time()
2717 if self.extra_lv_nodes and vg_name is not None:
2719 self.rpc.call_node_verify(self.extra_lv_nodes,
2720 {constants.NV_LVLIST: vg_name},
2721 self.cfg.GetClusterName())
2723 extra_lv_nvinfo = {}
2725 all_drbd_map = self.cfg.ComputeDRBDMap()
2727 feedback_fn("* Gathering disk information (%s nodes)" %
2728 len(self.my_node_names))
2729 instdisk = self._CollectDiskInfo(self.my_node_names, node_image,
2732 feedback_fn("* Verifying configuration file consistency")
2734 # If not all nodes are being checked, we need to make sure the master node
2735 # and a non-checked vm_capable node are in the list.
2736 absent_nodes = set(self.all_node_info).difference(self.my_node_info)
2738 vf_nvinfo = all_nvinfo.copy()
2739 vf_node_info = list(self.my_node_info.values())
2740 additional_nodes = []
2741 if master_node not in self.my_node_info:
2742 additional_nodes.append(master_node)
2743 vf_node_info.append(self.all_node_info[master_node])
2744 # Add the first vm_capable node we find which is not included
2745 for node in absent_nodes:
2746 nodeinfo = self.all_node_info[node]
2747 if nodeinfo.vm_capable and not nodeinfo.offline:
2748 additional_nodes.append(node)
2749 vf_node_info.append(self.all_node_info[node])
2751 key = constants.NV_FILELIST
2752 vf_nvinfo.update(self.rpc.call_node_verify(additional_nodes,
2753 {key: node_verify_param[key]},
2754 self.cfg.GetClusterName()))
2756 vf_nvinfo = all_nvinfo
2757 vf_node_info = self.my_node_info.values()
2759 self._VerifyFiles(_ErrorIf, vf_node_info, master_node, vf_nvinfo, filemap)
2761 feedback_fn("* Verifying node status")
2765 for node_i in node_data_list:
2767 nimg = node_image[node]
2771 feedback_fn("* Skipping offline node %s" % (node,))
2775 if node == master_node:
2777 elif node_i.master_candidate:
2778 ntype = "master candidate"
2779 elif node_i.drained:
2785 feedback_fn("* Verifying node %s (%s)" % (node, ntype))
2787 msg = all_nvinfo[node].fail_msg
2788 _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
2790 nimg.rpc_fail = True
2793 nresult = all_nvinfo[node].payload
2795 nimg.call_ok = self._VerifyNode(node_i, nresult)
2796 self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
2797 self._VerifyNodeNetwork(node_i, nresult)
2798 self._VerifyOob(node_i, nresult)
2801 self._VerifyNodeLVM(node_i, nresult, vg_name)
2802 self._VerifyNodeDrbd(node_i, nresult, self.all_inst_info, drbd_helper,
2805 self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
2806 self._UpdateNodeInstances(node_i, nresult, nimg)
2807 self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
2808 self._UpdateNodeOS(node_i, nresult, nimg)
2810 if not nimg.os_fail:
2811 if refos_img is None:
2813 self._VerifyNodeOS(node_i, nimg, refos_img)
2814 self._VerifyNodeBridges(node_i, nresult, bridges)
2816 # Check whether all running instancies are primary for the node. (This
2817 # can no longer be done from _VerifyInstance below, since some of the
2818 # wrong instances could be from other node groups.)
2819 non_primary_inst = set(nimg.instances).difference(nimg.pinst)
2821 for inst in non_primary_inst:
2822 test = inst in self.all_inst_info
2823 _ErrorIf(test, self.EINSTANCEWRONGNODE, inst,
2824 "instance should not run on node %s", node_i.name)
2825 _ErrorIf(not test, self.ENODEORPHANINSTANCE, node_i.name,
2826 "node is running unknown instance %s", inst)
2828 for node, result in extra_lv_nvinfo.items():
2829 self._UpdateNodeVolumes(self.all_node_info[node], result.payload,
2830 node_image[node], vg_name)
2832 feedback_fn("* Verifying instance status")
2833 for instance in self.my_inst_names:
2835 feedback_fn("* Verifying instance %s" % instance)
2836 inst_config = self.my_inst_info[instance]
2837 self._VerifyInstance(instance, inst_config, node_image,
2839 inst_nodes_offline = []
2841 pnode = inst_config.primary_node
2842 pnode_img = node_image[pnode]
2843 _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline,
2844 self.ENODERPC, pnode, "instance %s, connection to"
2845 " primary node failed", instance)
2847 _ErrorIf(inst_config.admin_up and pnode_img.offline,
2848 self.EINSTANCEBADNODE, instance,
2849 "instance is marked as running and lives on offline node %s",
2850 inst_config.primary_node)
2852 # If the instance is non-redundant we cannot survive losing its primary
2853 # node, so we are not N+1 compliant. On the other hand we have no disk
2854 # templates with more than one secondary so that situation is not well
2856 # FIXME: does not support file-backed instances
2857 if not inst_config.secondary_nodes:
2858 i_non_redundant.append(instance)
2860 _ErrorIf(len(inst_config.secondary_nodes) > 1, self.EINSTANCELAYOUT,
2861 instance, "instance has multiple secondary nodes: %s",
2862 utils.CommaJoin(inst_config.secondary_nodes),
2863 code=self.ETYPE_WARNING)
2865 if inst_config.disk_template in constants.DTS_INT_MIRROR:
2866 pnode = inst_config.primary_node
2867 instance_nodes = utils.NiceSort(inst_config.all_nodes)
2868 instance_groups = {}
2870 for node in instance_nodes:
2871 instance_groups.setdefault(self.all_node_info[node].group,
2875 "%s (group %s)" % (utils.CommaJoin(nodes), groupinfo[group].name)
2876 # Sort so that we always list the primary node first.
2877 for group, nodes in sorted(instance_groups.items(),
2878 key=lambda (_, nodes): pnode in nodes,
2881 self._ErrorIf(len(instance_groups) > 1, self.EINSTANCESPLITGROUPS,
2882 instance, "instance has primary and secondary nodes in"
2883 " different groups: %s", utils.CommaJoin(pretty_list),
2884 code=self.ETYPE_WARNING)
2886 if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
2887 i_non_a_balanced.append(instance)
2889 for snode in inst_config.secondary_nodes:
2890 s_img = node_image[snode]
2891 _ErrorIf(s_img.rpc_fail and not s_img.offline, self.ENODERPC, snode,
2892 "instance %s, connection to secondary node failed", instance)
2895 inst_nodes_offline.append(snode)
2897 # warn that the instance lives on offline nodes
2898 _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
2899 "instance has offline secondary node(s) %s",
2900 utils.CommaJoin(inst_nodes_offline))
2901 # ... or ghost/non-vm_capable nodes
2902 for node in inst_config.all_nodes:
2903 _ErrorIf(node_image[node].ghost, self.EINSTANCEBADNODE, instance,
2904 "instance lives on ghost node %s", node)
2905 _ErrorIf(not node_image[node].vm_capable, self.EINSTANCEBADNODE,
2906 instance, "instance lives on non-vm_capable node %s", node)
2908 feedback_fn("* Verifying orphan volumes")
2909 reserved = utils.FieldSet(*cluster.reserved_lvs)
2911 # We will get spurious "unknown volume" warnings if any node of this group
2912 # is secondary for an instance whose primary is in another group. To avoid
2913 # them, we find these instances and add their volumes to node_vol_should.
2914 for inst in self.all_inst_info.values():
2915 for secondary in inst.secondary_nodes:
2916 if (secondary in self.my_node_info
2917 and inst.name not in self.my_inst_info):
2918 inst.MapLVsByNode(node_vol_should)
2921 self._VerifyOrphanVolumes(node_vol_should, node_image, reserved)
2923 if constants.VERIFY_NPLUSONE_MEM not in self.op.skip_checks:
2924 feedback_fn("* Verifying N+1 Memory redundancy")
2925 self._VerifyNPlusOneMemory(node_image, self.my_inst_info)
2927 feedback_fn("* Other Notes")
2929 feedback_fn(" - NOTICE: %d non-redundant instance(s) found."
2930 % len(i_non_redundant))
2932 if i_non_a_balanced:
2933 feedback_fn(" - NOTICE: %d non-auto-balanced instance(s) found."
2934 % len(i_non_a_balanced))
2937 feedback_fn(" - NOTICE: %d offline node(s) found." % n_offline)
2940 feedback_fn(" - NOTICE: %d drained node(s) found." % n_drained)
2944 def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
2945 """Analyze the post-hooks' result
2947 This method analyses the hook result, handles it, and sends some
2948 nicely-formatted feedback back to the user.
2950 @param phase: one of L{constants.HOOKS_PHASE_POST} or
2951 L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
2952 @param hooks_results: the results of the multi-node hooks rpc call
2953 @param feedback_fn: function used send feedback back to the caller
2954 @param lu_result: previous Exec result
2955 @return: the new Exec result, based on the previous result
2959 # We only really run POST phase hooks, only for non-empty groups,
2960 # and are only interested in their results
2961 if not self.my_node_names:
2964 elif phase == constants.HOOKS_PHASE_POST:
2965 # Used to change hooks' output to proper indentation
2966 feedback_fn("* Hooks Results")
2967 assert hooks_results, "invalid result from hooks"
2969 for node_name in hooks_results:
2970 res = hooks_results[node_name]
2972 test = msg and not res.offline
2973 self._ErrorIf(test, self.ENODEHOOKS, node_name,
2974 "Communication failure in hooks execution: %s", msg)
2975 if res.offline or msg:
2976 # No need to investigate payload if node is offline or gave an error.
2977 # override manually lu_result here as _ErrorIf only
2978 # overrides self.bad
2981 for script, hkr, output in res.payload:
2982 test = hkr == constants.HKR_FAIL
2983 self._ErrorIf(test, self.ENODEHOOKS, node_name,
2984 "Script %s failed, output:", script)
2986 output = self._HOOKS_INDENT_RE.sub(" ", output)
2987 feedback_fn("%s" % output)
2993 class LUClusterVerifyDisks(NoHooksLU):
2994 """Verifies the cluster disks status.
2999 def ExpandNames(self):
3000 self.share_locks = _ShareAll()
3001 self.needed_locks = {
3002 locking.LEVEL_NODEGROUP: locking.ALL_SET,
3005 def Exec(self, feedback_fn):
3006 group_names = self.owned_locks(locking.LEVEL_NODEGROUP)
3008 # Submit one instance of L{opcodes.OpGroupVerifyDisks} per node group
3009 return ResultWithJobs([[opcodes.OpGroupVerifyDisks(group_name=group)]
3010 for group in group_names])
3013 class LUGroupVerifyDisks(NoHooksLU):
3014 """Verifies the status of all disks in a node group.
3019 def ExpandNames(self):
3020 # Raises errors.OpPrereqError on its own if group can't be found
3021 self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
3023 self.share_locks = _ShareAll()
3024 self.needed_locks = {
3025 locking.LEVEL_INSTANCE: [],
3026 locking.LEVEL_NODEGROUP: [],
3027 locking.LEVEL_NODE: [],
3030 def DeclareLocks(self, level):
3031 if level == locking.LEVEL_INSTANCE:
3032 assert not self.needed_locks[locking.LEVEL_INSTANCE]
3034 # Lock instances optimistically, needs verification once node and group
3035 # locks have been acquired
3036 self.needed_locks[locking.LEVEL_INSTANCE] = \
3037 self.cfg.GetNodeGroupInstances(self.group_uuid)
3039 elif level == locking.LEVEL_NODEGROUP:
3040 assert not self.needed_locks[locking.LEVEL_NODEGROUP]
3042 self.needed_locks[locking.LEVEL_NODEGROUP] = \
3043 set([self.group_uuid] +
3044 # Lock all groups used by instances optimistically; this requires
3045 # going via the node before it's locked, requiring verification
3048 for instance_name in self.owned_locks(locking.LEVEL_INSTANCE)
3049 for group_uuid in self.cfg.GetInstanceNodeGroups(instance_name)])
3051 elif level == locking.LEVEL_NODE:
3052 # This will only lock the nodes in the group to be verified which contain
3054 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
3055 self._LockInstancesNodes()
3057 # Lock all nodes in group to be verified
3058 assert self.group_uuid in self.owned_locks(locking.LEVEL_NODEGROUP)
3059 member_nodes = self.cfg.GetNodeGroup(self.group_uuid).members
3060 self.needed_locks[locking.LEVEL_NODE].extend(member_nodes)
3062 def CheckPrereq(self):
3063 owned_instances = frozenset(self.owned_locks(locking.LEVEL_INSTANCE))
3064 owned_groups = frozenset(self.owned_locks(locking.LEVEL_NODEGROUP))
3065 owned_nodes = frozenset(self.owned_locks(locking.LEVEL_NODE))
3067 assert self.group_uuid in owned_groups
3069 # Check if locked instances are still correct
3070 _CheckNodeGroupInstances(self.cfg, self.group_uuid, owned_instances)
3072 # Get instance information
3073 self.instances = dict(self.cfg.GetMultiInstanceInfo(owned_instances))
3075 # Check if node groups for locked instances are still correct
3076 for (instance_name, inst) in self.instances.items():
3077 assert owned_nodes.issuperset(inst.all_nodes), \
3078 "Instance %s's nodes changed while we kept the lock" % instance_name
3080 inst_groups = _CheckInstanceNodeGroups(self.cfg, instance_name,
3083 assert self.group_uuid in inst_groups, \
3084 "Instance %s has no node in group %s" % (instance_name, self.group_uuid)
3086 def Exec(self, feedback_fn):
3087 """Verify integrity of cluster disks.
3089 @rtype: tuple of three items
3090 @return: a tuple of (dict of node-to-node_error, list of instances
3091 which need activate-disks, dict of instance: (node, volume) for
3096 res_instances = set()
3099 nv_dict = _MapInstanceDisksToNodes([inst
3100 for inst in self.instances.values()
3104 nodes = utils.NiceSort(set(self.owned_locks(locking.LEVEL_NODE)) &
3105 set(self.cfg.GetVmCapableNodeList()))
3107 node_lvs = self.rpc.call_lv_list(nodes, [])
3109 for (node, node_res) in node_lvs.items():
3110 if node_res.offline:
3113 msg = node_res.fail_msg
3115 logging.warning("Error enumerating LVs on node %s: %s", node, msg)
3116 res_nodes[node] = msg
3119 for lv_name, (_, _, lv_online) in node_res.payload.items():
3120 inst = nv_dict.pop((node, lv_name), None)
3121 if not (lv_online or inst is None):
3122 res_instances.add(inst)
3124 # any leftover items in nv_dict are missing LVs, let's arrange the data
3126 for key, inst in nv_dict.iteritems():
3127 res_missing.setdefault(inst, []).append(key)
3129 return (res_nodes, list(res_instances), res_missing)
3132 class LUClusterRepairDiskSizes(NoHooksLU):
3133 """Verifies the cluster disks sizes.
3138 def ExpandNames(self):
3139 if self.op.instances:
3140 self.wanted_names = _GetWantedInstances(self, self.op.instances)
3141 self.needed_locks = {
3142 locking.LEVEL_NODE: [],
3143 locking.LEVEL_INSTANCE: self.wanted_names,
3145 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3147 self.wanted_names = None
3148 self.needed_locks = {
3149 locking.LEVEL_NODE: locking.ALL_SET,
3150 locking.LEVEL_INSTANCE: locking.ALL_SET,
3152 self.share_locks = _ShareAll()
3154 def DeclareLocks(self, level):
3155 if level == locking.LEVEL_NODE and self.wanted_names is not None:
3156 self._LockInstancesNodes(primary_only=True)
3158 def CheckPrereq(self):
3159 """Check prerequisites.
3161 This only checks the optional instance list against the existing names.
3164 if self.wanted_names is None:
3165 self.wanted_names = self.owned_locks(locking.LEVEL_INSTANCE)
3167 self.wanted_instances = \
3168 map(compat.snd, self.cfg.GetMultiInstanceInfo(self.wanted_names))
3170 def _EnsureChildSizes(self, disk):
3171 """Ensure children of the disk have the needed disk size.
3173 This is valid mainly for DRBD8 and fixes an issue where the
3174 children have smaller disk size.
3176 @param disk: an L{ganeti.objects.Disk} object
3179 if disk.dev_type == constants.LD_DRBD8:
3180 assert disk.children, "Empty children for DRBD8?"
3181 fchild = disk.children[0]
3182 mismatch = fchild.size < disk.size
3184 self.LogInfo("Child disk has size %d, parent %d, fixing",
3185 fchild.size, disk.size)
3186 fchild.size = disk.size
3188 # and we recurse on this child only, not on the metadev
3189 return self._EnsureChildSizes(fchild) or mismatch
3193 def Exec(self, feedback_fn):
3194 """Verify the size of cluster disks.
3197 # TODO: check child disks too
3198 # TODO: check differences in size between primary/secondary nodes
3200 for instance in self.wanted_instances:
3201 pnode = instance.primary_node
3202 if pnode not in per_node_disks:
3203 per_node_disks[pnode] = []
3204 for idx, disk in enumerate(instance.disks):
3205 per_node_disks[pnode].append((instance, idx, disk))
3208 for node, dskl in per_node_disks.items():
3209 newl = [v[2].Copy() for v in dskl]
3211 self.cfg.SetDiskID(dsk, node)
3212 result = self.rpc.call_blockdev_getsize(node, newl)
3214 self.LogWarning("Failure in blockdev_getsize call to node"
3215 " %s, ignoring", node)
3217 if len(result.payload) != len(dskl):
3218 logging.warning("Invalid result from node %s: len(dksl)=%d,"
3219 " result.payload=%s", node, len(dskl), result.payload)
3220 self.LogWarning("Invalid result from node %s, ignoring node results",
3223 for ((instance, idx, disk), size) in zip(dskl, result.payload):
3225 self.LogWarning("Disk %d of instance %s did not return size"
3226 " information, ignoring", idx, instance.name)
3228 if not isinstance(size, (int, long)):
3229 self.LogWarning("Disk %d of instance %s did not return valid"
3230 " size information, ignoring", idx, instance.name)
3233 if size != disk.size:
3234 self.LogInfo("Disk %d of instance %s has mismatched size,"
3235 " correcting: recorded %d, actual %d", idx,
3236 instance.name, disk.size, size)
3238 self.cfg.Update(instance, feedback_fn)
3239 changed.append((instance.name, idx, size))
3240 if self._EnsureChildSizes(disk):
3241 self.cfg.Update(instance, feedback_fn)
3242 changed.append((instance.name, idx, disk.size))
3246 class LUClusterRename(LogicalUnit):
3247 """Rename the cluster.
3250 HPATH = "cluster-rename"
3251 HTYPE = constants.HTYPE_CLUSTER
3253 def BuildHooksEnv(self):
3258 "OP_TARGET": self.cfg.GetClusterName(),
3259 "NEW_NAME": self.op.name,
3262 def BuildHooksNodes(self):
3263 """Build hooks nodes.
3266 return ([self.cfg.GetMasterNode()], self.cfg.GetNodeList())
3268 def CheckPrereq(self):
3269 """Verify that the passed name is a valid one.
3272 hostname = netutils.GetHostname(name=self.op.name,
3273 family=self.cfg.GetPrimaryIPFamily())
3275 new_name = hostname.name
3276 self.ip = new_ip = hostname.ip
3277 old_name = self.cfg.GetClusterName()
3278 old_ip = self.cfg.GetMasterIP()
3279 if new_name == old_name and new_ip == old_ip:
3280 raise errors.OpPrereqError("Neither the name nor the IP address of the"
3281 " cluster has changed",
3283 if new_ip != old_ip:
3284 if netutils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
3285 raise errors.OpPrereqError("The given cluster IP address (%s) is"
3286 " reachable on the network" %
3287 new_ip, errors.ECODE_NOTUNIQUE)
3289 self.op.name = new_name
3291 def Exec(self, feedback_fn):
3292 """Rename the cluster.
3295 clustername = self.op.name
3298 # shutdown the master IP
3299 master = self.cfg.GetMasterNode()
3300 result = self.rpc.call_node_stop_master(master, False)
3301 result.Raise("Could not disable the master role")
3304 cluster = self.cfg.GetClusterInfo()
3305 cluster.cluster_name = clustername
3306 cluster.master_ip = ip
3307 self.cfg.Update(cluster, feedback_fn)
3309 # update the known hosts file
3310 ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
3311 node_list = self.cfg.GetOnlineNodeList()
3313 node_list.remove(master)
3316 _UploadHelper(self, node_list, constants.SSH_KNOWN_HOSTS_FILE)
3318 result = self.rpc.call_node_start_master(master, False, False)
3319 msg = result.fail_msg
3321 self.LogWarning("Could not re-enable the master role on"
3322 " the master, please restart manually: %s", msg)
3327 class LUClusterSetParams(LogicalUnit):
3328 """Change the parameters of the cluster.
3331 HPATH = "cluster-modify"
3332 HTYPE = constants.HTYPE_CLUSTER
3335 def CheckArguments(self):
3339 if self.op.uid_pool:
3340 uidpool.CheckUidPool(self.op.uid_pool)
3342 if self.op.add_uids:
3343 uidpool.CheckUidPool(self.op.add_uids)
3345 if self.op.remove_uids:
3346 uidpool.CheckUidPool(self.op.remove_uids)
3348 def ExpandNames(self):
3349 # FIXME: in the future maybe other cluster params won't require checking on
3350 # all nodes to be modified.
3351 self.needed_locks = {
3352 locking.LEVEL_NODE: locking.ALL_SET,
3354 self.share_locks[locking.LEVEL_NODE] = 1
3356 def BuildHooksEnv(self):
3361 "OP_TARGET": self.cfg.GetClusterName(),
3362 "NEW_VG_NAME": self.op.vg_name,
3365 def BuildHooksNodes(self):
3366 """Build hooks nodes.
3369 mn = self.cfg.GetMasterNode()
3372 def CheckPrereq(self):
3373 """Check prerequisites.
3375 This checks whether the given params don't conflict and
3376 if the given volume group is valid.
3379 if self.op.vg_name is not None and not self.op.vg_name:
3380 if self.cfg.HasAnyDiskOfType(constants.LD_LV):
3381 raise errors.OpPrereqError("Cannot disable lvm storage while lvm-based"
3382 " instances exist", errors.ECODE_INVAL)
3384 if self.op.drbd_helper is not None and not self.op.drbd_helper:
3385 if self.cfg.HasAnyDiskOfType(constants.LD_DRBD8):
3386 raise errors.OpPrereqError("Cannot disable drbd helper while"
3387 " drbd-based instances exist",
3390 node_list = self.owned_locks(locking.LEVEL_NODE)
3392 # if vg_name not None, checks given volume group on all nodes
3394 vglist = self.rpc.call_vg_list(node_list)
3395 for node in node_list:
3396 msg = vglist[node].fail_msg
3398 # ignoring down node
3399 self.LogWarning("Error while gathering data on node %s"
3400 " (ignoring node): %s", node, msg)
3402 vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
3404 constants.MIN_VG_SIZE)
3406 raise errors.OpPrereqError("Error on node '%s': %s" %
3407 (node, vgstatus), errors.ECODE_ENVIRON)
3409 if self.op.drbd_helper:
3410 # checks given drbd helper on all nodes
3411 helpers = self.rpc.call_drbd_helper(node_list)
3412 for (node, ninfo) in self.cfg.GetMultiNodeInfo(node_list):
3414 self.LogInfo("Not checking drbd helper on offline node %s", node)
3416 msg = helpers[node].fail_msg
3418 raise errors.OpPrereqError("Error checking drbd helper on node"
3419 " '%s': %s" % (node, msg),
3420 errors.ECODE_ENVIRON)
3421 node_helper = helpers[node].payload
3422 if node_helper != self.op.drbd_helper:
3423 raise errors.OpPrereqError("Error on node '%s': drbd helper is %s" %
3424 (node, node_helper), errors.ECODE_ENVIRON)
3426 self.cluster = cluster = self.cfg.GetClusterInfo()
3427 # validate params changes
3428 if self.op.beparams:
3429 utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
3430 self.new_beparams = cluster.SimpleFillBE(self.op.beparams)
3432 if self.op.ndparams:
3433 utils.ForceDictType(self.op.ndparams, constants.NDS_PARAMETER_TYPES)
3434 self.new_ndparams = cluster.SimpleFillND(self.op.ndparams)
3436 # TODO: we need a more general way to handle resetting
3437 # cluster-level parameters to default values
3438 if self.new_ndparams["oob_program"] == "":
3439 self.new_ndparams["oob_program"] = \
3440 constants.NDC_DEFAULTS[constants.ND_OOB_PROGRAM]
3442 if self.op.nicparams:
3443 utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
3444 self.new_nicparams = cluster.SimpleFillNIC(self.op.nicparams)
3445 objects.NIC.CheckParameterSyntax(self.new_nicparams)
3448 # check all instances for consistency
3449 for instance in self.cfg.GetAllInstancesInfo().values():
3450 for nic_idx, nic in enumerate(instance.nics):
3451 params_copy = copy.deepcopy(nic.nicparams)
3452 params_filled = objects.FillDict(self.new_nicparams, params_copy)
3454 # check parameter syntax
3456 objects.NIC.CheckParameterSyntax(params_filled)
3457 except errors.ConfigurationError, err:
3458 nic_errors.append("Instance %s, nic/%d: %s" %
3459 (instance.name, nic_idx, err))
3461 # if we're moving instances to routed, check that they have an ip
3462 target_mode = params_filled[constants.NIC_MODE]
3463 if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
3464 nic_errors.append("Instance %s, nic/%d: routed NIC with no ip"
3465 " address" % (instance.name, nic_idx))
3467 raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
3468 "\n".join(nic_errors))
3470 # hypervisor list/parameters
3471 self.new_hvparams = new_hvp = objects.FillDict(cluster.hvparams, {})
3472 if self.op.hvparams:
3473 for hv_name, hv_dict in self.op.hvparams.items():
3474 if hv_name not in self.new_hvparams:
3475 self.new_hvparams[hv_name] = hv_dict
3477 self.new_hvparams[hv_name].update(hv_dict)
3479 # os hypervisor parameters
3480 self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
3482 for os_name, hvs in self.op.os_hvp.items():
3483 if os_name not in self.new_os_hvp:
3484 self.new_os_hvp[os_name] = hvs
3486 for hv_name, hv_dict in hvs.items():
3487 if hv_name not in self.new_os_hvp[os_name]:
3488 self.new_os_hvp[os_name][hv_name] = hv_dict
3490 self.new_os_hvp[os_name][hv_name].update(hv_dict)
3493 self.new_osp = objects.FillDict(cluster.osparams, {})
3494 if self.op.osparams:
3495 for os_name, osp in self.op.osparams.items():
3496 if os_name not in self.new_osp:
3497 self.new_osp[os_name] = {}
3499 self.new_osp[os_name] = _GetUpdatedParams(self.new_osp[os_name], osp,
3502 if not self.new_osp[os_name]:
3503 # we removed all parameters
3504 del self.new_osp[os_name]
3506 # check the parameter validity (remote check)
3507 _CheckOSParams(self, False, [self.cfg.GetMasterNode()],
3508 os_name, self.new_osp[os_name])
3510 # changes to the hypervisor list
3511 if self.op.enabled_hypervisors is not None:
3512 self.hv_list = self.op.enabled_hypervisors
3513 for hv in self.hv_list:
3514 # if the hypervisor doesn't already exist in the cluster
3515 # hvparams, we initialize it to empty, and then (in both
3516 # cases) we make sure to fill the defaults, as we might not
3517 # have a complete defaults list if the hypervisor wasn't
3519 if hv not in new_hvp:
3521 new_hvp[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], new_hvp[hv])
3522 utils.ForceDictType(new_hvp[hv], constants.HVS_PARAMETER_TYPES)
3524 self.hv_list = cluster.enabled_hypervisors
3526 if self.op.hvparams or self.op.enabled_hypervisors is not None:
3527 # either the enabled list has changed, or the parameters have, validate
3528 for hv_name, hv_params in self.new_hvparams.items():
3529 if ((self.op.hvparams and hv_name in self.op.hvparams) or
3530 (self.op.enabled_hypervisors and
3531 hv_name in self.op.enabled_hypervisors)):
3532 # either this is a new hypervisor, or its parameters have changed
3533 hv_class = hypervisor.GetHypervisor(hv_name)
3534 utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
3535 hv_class.CheckParameterSyntax(hv_params)
3536 _CheckHVParams(self, node_list, hv_name, hv_params)
3539 # no need to check any newly-enabled hypervisors, since the
3540 # defaults have already been checked in the above code-block
3541 for os_name, os_hvp in self.new_os_hvp.items():
3542 for hv_name, hv_params in os_hvp.items():
3543 utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
3544 # we need to fill in the new os_hvp on top of the actual hv_p
3545 cluster_defaults = self.new_hvparams.get(hv_name, {})
3546 new_osp = objects.FillDict(cluster_defaults, hv_params)
3547 hv_class = hypervisor.GetHypervisor(hv_name)
3548 hv_class.CheckParameterSyntax(new_osp)
3549 _CheckHVParams(self, node_list, hv_name, new_osp)
3551 if self.op.default_iallocator:
3552 alloc_script = utils.FindFile(self.op.default_iallocator,
3553 constants.IALLOCATOR_SEARCH_PATH,
3555 if alloc_script is None:
3556 raise errors.OpPrereqError("Invalid default iallocator script '%s'"
3557 " specified" % self.op.default_iallocator,
3560 def Exec(self, feedback_fn):
3561 """Change the parameters of the cluster.
3564 if self.op.vg_name is not None:
3565 new_volume = self.op.vg_name
3568 if new_volume != self.cfg.GetVGName():
3569 self.cfg.SetVGName(new_volume)
3571 feedback_fn("Cluster LVM configuration already in desired"
3572 " state, not changing")
3573 if self.op.drbd_helper is not None:
3574 new_helper = self.op.drbd_helper
3577 if new_helper != self.cfg.GetDRBDHelper():
3578 self.cfg.SetDRBDHelper(new_helper)
3580 feedback_fn("Cluster DRBD helper already in desired state,"
3582 if self.op.hvparams:
3583 self.cluster.hvparams = self.new_hvparams
3585 self.cluster.os_hvp = self.new_os_hvp
3586 if self.op.enabled_hypervisors is not None:
3587 self.cluster.hvparams = self.new_hvparams
3588 self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
3589 if self.op.beparams:
3590 self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
3591 if self.op.nicparams:
3592 self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
3593 if self.op.osparams:
3594 self.cluster.osparams = self.new_osp
3595 if self.op.ndparams:
3596 self.cluster.ndparams = self.new_ndparams
3598 if self.op.candidate_pool_size is not None:
3599 self.cluster.candidate_pool_size = self.op.candidate_pool_size
3600 # we need to update the pool size here, otherwise the save will fail
3601 _AdjustCandidatePool(self, [])
3603 if self.op.maintain_node_health is not None:
3604 self.cluster.maintain_node_health = self.op.maintain_node_health
3606 if self.op.prealloc_wipe_disks is not None:
3607 self.cluster.prealloc_wipe_disks = self.op.prealloc_wipe_disks
3609 if self.op.add_uids is not None:
3610 uidpool.AddToUidPool(self.cluster.uid_pool, self.op.add_uids)
3612 if self.op.remove_uids is not None:
3613 uidpool.RemoveFromUidPool(self.cluster.uid_pool, self.op.remove_uids)
3615 if self.op.uid_pool is not None:
3616 self.cluster.uid_pool = self.op.uid_pool
3618 if self.op.default_iallocator is not None:
3619 self.cluster.default_iallocator = self.op.default_iallocator
3621 if self.op.reserved_lvs is not None:
3622 self.cluster.reserved_lvs = self.op.reserved_lvs
3624 def helper_os(aname, mods, desc):
3626 lst = getattr(self.cluster, aname)
3627 for key, val in mods:
3628 if key == constants.DDM_ADD:
3630 feedback_fn("OS %s already in %s, ignoring" % (val, desc))
3633 elif key == constants.DDM_REMOVE:
3637 feedback_fn("OS %s not found in %s, ignoring" % (val, desc))
3639 raise errors.ProgrammerError("Invalid modification '%s'" % key)
3641 if self.op.hidden_os:
3642 helper_os("hidden_os", self.op.hidden_os, "hidden")
3644 if self.op.blacklisted_os:
3645 helper_os("blacklisted_os", self.op.blacklisted_os, "blacklisted")
3647 if self.op.master_netdev:
3648 master = self.cfg.GetMasterNode()
3649 feedback_fn("Shutting down master ip on the current netdev (%s)" %
3650 self.cluster.master_netdev)
3651 result = self.rpc.call_node_stop_master(master, False)
3652 result.Raise("Could not disable the master ip")
3653 feedback_fn("Changing master_netdev from %s to %s" %
3654 (self.cluster.master_netdev, self.op.master_netdev))
3655 self.cluster.master_netdev = self.op.master_netdev
3657 self.cfg.Update(self.cluster, feedback_fn)
3659 if self.op.master_netdev:
3660 feedback_fn("Starting the master ip on the new master netdev (%s)" %
3661 self.op.master_netdev)
3662 result = self.rpc.call_node_start_master(master, False, False)
3664 self.LogWarning("Could not re-enable the master ip on"
3665 " the master, please restart manually: %s",
3669 def _UploadHelper(lu, nodes, fname):
3670 """Helper for uploading a file and showing warnings.
3673 if os.path.exists(fname):
3674 result = lu.rpc.call_upload_file(nodes, fname)
3675 for to_node, to_result in result.items():
3676 msg = to_result.fail_msg
3678 msg = ("Copy of file %s to node %s failed: %s" %
3679 (fname, to_node, msg))
3680 lu.proc.LogWarning(msg)
3683 def _ComputeAncillaryFiles(cluster, redist):
3684 """Compute files external to Ganeti which need to be consistent.
3686 @type redist: boolean
3687 @param redist: Whether to include files which need to be redistributed
3690 # Compute files for all nodes
3692 constants.SSH_KNOWN_HOSTS_FILE,
3693 constants.CONFD_HMAC_KEY,
3694 constants.CLUSTER_DOMAIN_SECRET_FILE,
3698 files_all.update(constants.ALL_CERT_FILES)
3699 files_all.update(ssconf.SimpleStore().GetFileList())
3701 if cluster.modify_etc_hosts:
3702 files_all.add(constants.ETC_HOSTS)
3704 # Files which must either exist on all nodes or on none
3705 files_all_opt = set([
3706 constants.RAPI_USERS_FILE,
3709 # Files which should only be on master candidates
3712 files_mc.add(constants.CLUSTER_CONF_FILE)
3714 # Files which should only be on VM-capable nodes
3715 files_vm = set(filename
3716 for hv_name in cluster.enabled_hypervisors
3717 for filename in hypervisor.GetHypervisor(hv_name).GetAncillaryFiles())
3719 # Filenames must be unique
3720 assert (len(files_all | files_all_opt | files_mc | files_vm) ==
3721 sum(map(len, [files_all, files_all_opt, files_mc, files_vm]))), \
3722 "Found file listed in more than one file list"
3724 return (files_all, files_all_opt, files_mc, files_vm)
3727 def _RedistributeAncillaryFiles(lu, additional_nodes=None, additional_vm=True):
3728 """Distribute additional files which are part of the cluster configuration.
3730 ConfigWriter takes care of distributing the config and ssconf files, but
3731 there are more files which should be distributed to all nodes. This function
3732 makes sure those are copied.
3734 @param lu: calling logical unit
3735 @param additional_nodes: list of nodes not in the config to distribute to
3736 @type additional_vm: boolean
3737 @param additional_vm: whether the additional nodes are vm-capable or not
3740 # Gather target nodes
3741 cluster = lu.cfg.GetClusterInfo()
3742 master_info = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
3744 online_nodes = lu.cfg.GetOnlineNodeList()
3745 vm_nodes = lu.cfg.GetVmCapableNodeList()
3747 if additional_nodes is not None:
3748 online_nodes.extend(additional_nodes)
3750 vm_nodes.extend(additional_nodes)
3752 # Never distribute to master node
3753 for nodelist in [online_nodes, vm_nodes]:
3754 if master_info.name in nodelist:
3755 nodelist.remove(master_info.name)
3758 (files_all, files_all_opt, files_mc, files_vm) = \
3759 _ComputeAncillaryFiles(cluster, True)
3761 # Never re-distribute configuration file from here
3762 assert not (constants.CLUSTER_CONF_FILE in files_all or
3763 constants.CLUSTER_CONF_FILE in files_vm)
3764 assert not files_mc, "Master candidates not handled in this function"
3767 (online_nodes, files_all),
3768 (online_nodes, files_all_opt),
3769 (vm_nodes, files_vm),
3773 for (node_list, files) in filemap:
3775 _UploadHelper(lu, node_list, fname)
3778 class LUClusterRedistConf(NoHooksLU):
3779 """Force the redistribution of cluster configuration.
3781 This is a very simple LU.
3786 def ExpandNames(self):
3787 self.needed_locks = {
3788 locking.LEVEL_NODE: locking.ALL_SET,
3790 self.share_locks[locking.LEVEL_NODE] = 1
3792 def Exec(self, feedback_fn):
3793 """Redistribute the configuration.
3796 self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
3797 _RedistributeAncillaryFiles(self)
3800 def _WaitForSync(lu, instance, disks=None, oneshot=False):
3801 """Sleep and poll for an instance's disk to sync.
3804 if not instance.disks or disks is not None and not disks:
3807 disks = _ExpandCheckDisks(instance, disks)
3810 lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
3812 node = instance.primary_node
3815 lu.cfg.SetDiskID(dev, node)
3817 # TODO: Convert to utils.Retry
3820 degr_retries = 10 # in seconds, as we sleep 1 second each time
3824 cumul_degraded = False
3825 rstats = lu.rpc.call_blockdev_getmirrorstatus(node, disks)
3826 msg = rstats.fail_msg
3828 lu.LogWarning("Can't get any data from node %s: %s", node, msg)
3831 raise errors.RemoteError("Can't contact node %s for mirror data,"
3832 " aborting." % node)
3835 rstats = rstats.payload
3837 for i, mstat in enumerate(rstats):
3839 lu.LogWarning("Can't compute data for node %s/%s",
3840 node, disks[i].iv_name)
3843 cumul_degraded = (cumul_degraded or
3844 (mstat.is_degraded and mstat.sync_percent is None))
3845 if mstat.sync_percent is not None:
3847 if mstat.estimated_time is not None:
3848 rem_time = ("%s remaining (estimated)" %
3849 utils.FormatSeconds(mstat.estimated_time))
3850 max_time = mstat.estimated_time
3852 rem_time = "no time estimate"
3853 lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
3854 (disks[i].iv_name, mstat.sync_percent, rem_time))
3856 # if we're done but degraded, let's do a few small retries, to
3857 # make sure we see a stable and not transient situation; therefore
3858 # we force restart of the loop
3859 if (done or oneshot) and cumul_degraded and degr_retries > 0:
3860 logging.info("Degraded disks found, %d retries left", degr_retries)
3868 time.sleep(min(60, max_time))
3871 lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
3872 return not cumul_degraded
3875 def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
3876 """Check that mirrors are not degraded.
3878 The ldisk parameter, if True, will change the test from the
3879 is_degraded attribute (which represents overall non-ok status for
3880 the device(s)) to the ldisk (representing the local storage status).
3883 lu.cfg.SetDiskID(dev, node)
3887 if on_primary or dev.AssembleOnSecondary():
3888 rstats = lu.rpc.call_blockdev_find(node, dev)
3889 msg = rstats.fail_msg
3891 lu.LogWarning("Can't find disk on node %s: %s", node, msg)
3893 elif not rstats.payload:
3894 lu.LogWarning("Can't find disk on node %s", node)
3898 result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
3900 result = result and not rstats.payload.is_degraded
3903 for child in dev.children:
3904 result = result and _CheckDiskConsistency(lu, child, node, on_primary)
3909 class LUOobCommand(NoHooksLU):
3910 """Logical unit for OOB handling.
3914 _SKIP_MASTER = (constants.OOB_POWER_OFF, constants.OOB_POWER_CYCLE)
3916 def ExpandNames(self):
3917 """Gather locks we need.
3920 if self.op.node_names:
3921 self.op.node_names = _GetWantedNodes(self, self.op.node_names)
3922 lock_names = self.op.node_names
3924 lock_names = locking.ALL_SET
3926 self.needed_locks = {
3927 locking.LEVEL_NODE: lock_names,
3930 def CheckPrereq(self):
3931 """Check prerequisites.
3934 - the node exists in the configuration
3937 Any errors are signaled by raising errors.OpPrereqError.
3941 self.master_node = self.cfg.GetMasterNode()
3943 assert self.op.power_delay >= 0.0
3945 if self.op.node_names:
3946 if (self.op.command in self._SKIP_MASTER and
3947 self.master_node in self.op.node_names):
3948 master_node_obj = self.cfg.GetNodeInfo(self.master_node)
3949 master_oob_handler = _SupportsOob(self.cfg, master_node_obj)
3951 if master_oob_handler:
3952 additional_text = ("run '%s %s %s' if you want to operate on the"
3953 " master regardless") % (master_oob_handler,
3957 additional_text = "it does not support out-of-band operations"
3959 raise errors.OpPrereqError(("Operating on the master node %s is not"
3960 " allowed for %s; %s") %
3961 (self.master_node, self.op.command,
3962 additional_text), errors.ECODE_INVAL)
3964 self.op.node_names = self.cfg.GetNodeList()
3965 if self.op.command in self._SKIP_MASTER:
3966 self.op.node_names.remove(self.master_node)
3968 if self.op.command in self._SKIP_MASTER:
3969 assert self.master_node not in self.op.node_names
3971 for (node_name, node) in self.cfg.GetMultiNodeInfo(self.op.node_names):
3973 raise errors.OpPrereqError("Node %s not found" % node_name,
3976 self.nodes.append(node)
3978 if (not self.op.ignore_status and
3979 (self.op.command == constants.OOB_POWER_OFF and not node.offline)):
3980 raise errors.OpPrereqError(("Cannot power off node %s because it is"
3981 " not marked offline") % node_name,
3984 def Exec(self, feedback_fn):
3985 """Execute OOB and return result if we expect any.
3988 master_node = self.master_node
3991 for idx, node in enumerate(utils.NiceSort(self.nodes,
3992 key=lambda node: node.name)):
3993 node_entry = [(constants.RS_NORMAL, node.name)]
3994 ret.append(node_entry)
3996 oob_program = _SupportsOob(self.cfg, node)
3999 node_entry.append((constants.RS_UNAVAIL, None))
4002 logging.info("Executing out-of-band command '%s' using '%s' on %s",
4003 self.op.command, oob_program, node.name)
4004 result = self.rpc.call_run_oob(master_node, oob_program,
4005 self.op.command, node.name,
4009 self.LogWarning("Out-of-band RPC failed on node '%s': %s",
4010 node.name, result.fail_msg)
4011 node_entry.append((constants.RS_NODATA, None))
4014 self._CheckPayload(result)
4015 except errors.OpExecError, err:
4016 self.LogWarning("Payload returned by node '%s' is not valid: %s",
4018 node_entry.append((constants.RS_NODATA, None))
4020 if self.op.command == constants.OOB_HEALTH:
4021 # For health we should log important events
4022 for item, status in result.payload:
4023 if status in [constants.OOB_STATUS_WARNING,
4024 constants.OOB_STATUS_CRITICAL]:
4025 self.LogWarning("Item '%s' on node '%s' has status '%s'",
4026 item, node.name, status)
4028 if self.op.command == constants.OOB_POWER_ON:
4030 elif self.op.command == constants.OOB_POWER_OFF:
4031 node.powered = False
4032 elif self.op.command == constants.OOB_POWER_STATUS:
4033 powered = result.payload[constants.OOB_POWER_STATUS_POWERED]
4034 if powered != node.powered:
4035 logging.warning(("Recorded power state (%s) of node '%s' does not"
4036 " match actual power state (%s)"), node.powered,
4039 # For configuration changing commands we should update the node
4040 if self.op.command in (constants.OOB_POWER_ON,
4041 constants.OOB_POWER_OFF):
4042 self.cfg.Update(node, feedback_fn)
4044 node_entry.append((constants.RS_NORMAL, result.payload))
4046 if (self.op.command == constants.OOB_POWER_ON and
4047 idx < len(self.nodes) - 1):
4048 time.sleep(self.op.power_delay)
4052 def _CheckPayload(self, result):
4053 """Checks if the payload is valid.
4055 @param result: RPC result
4056 @raises errors.OpExecError: If payload is not valid
4060 if self.op.command == constants.OOB_HEALTH:
4061 if not isinstance(result.payload, list):
4062 errs.append("command 'health' is expected to return a list but got %s" %
4063 type(result.payload))
4065 for item, status in result.payload:
4066 if status not in constants.OOB_STATUSES:
4067 errs.append("health item '%s' has invalid status '%s'" %
4070 if self.op.command == constants.OOB_POWER_STATUS:
4071 if not isinstance(result.payload, dict):
4072 errs.append("power-status is expected to return a dict but got %s" %
4073 type(result.payload))
4075 if self.op.command in [
4076 constants.OOB_POWER_ON,
4077 constants.OOB_POWER_OFF,
4078 constants.OOB_POWER_CYCLE,
4080 if result.payload is not None:
4081 errs.append("%s is expected to not return payload but got '%s'" %
4082 (self.op.command, result.payload))
4085 raise errors.OpExecError("Check of out-of-band payload failed due to %s" %
4086 utils.CommaJoin(errs))
4089 class _OsQuery(_QueryBase):
4090 FIELDS = query.OS_FIELDS
4092 def ExpandNames(self, lu):
4093 # Lock all nodes in shared mode
4094 # Temporary removal of locks, should be reverted later
4095 # TODO: reintroduce locks when they are lighter-weight
4096 lu.needed_locks = {}
4097 #self.share_locks[locking.LEVEL_NODE] = 1
4098 #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
4100 # The following variables interact with _QueryBase._GetNames
4102 self.wanted = self.names
4104 self.wanted = locking.ALL_SET
4106 self.do_locking = self.use_locking
4108 def DeclareLocks(self, lu, level):
4112 def _DiagnoseByOS(rlist):
4113 """Remaps a per-node return list into an a per-os per-node dictionary
4115 @param rlist: a map with node names as keys and OS objects as values
4118 @return: a dictionary with osnames as keys and as value another
4119 map, with nodes as keys and tuples of (path, status, diagnose,
4120 variants, parameters, api_versions) as values, eg::
4122 {"debian-etch": {"node1": [(/usr/lib/..., True, "", [], []),
4123 (/srv/..., False, "invalid api")],
4124 "node2": [(/srv/..., True, "", [], [])]}
4129 # we build here the list of nodes that didn't fail the RPC (at RPC
4130 # level), so that nodes with a non-responding node daemon don't
4131 # make all OSes invalid
4132 good_nodes = [node_name for node_name in rlist
4133 if not rlist[node_name].fail_msg]
4134 for node_name, nr in rlist.items():
4135 if nr.fail_msg or not nr.payload:
4137 for (name, path, status, diagnose, variants,
4138 params, api_versions) in nr.payload:
4139 if name not in all_os:
4140 # build a list of nodes for this os containing empty lists
4141 # for each node in node_list
4143 for nname in good_nodes:
4144 all_os[name][nname] = []
4145 # convert params from [name, help] to (name, help)
4146 params = [tuple(v) for v in params]
4147 all_os[name][node_name].append((path, status, diagnose,
4148 variants, params, api_versions))
4151 def _GetQueryData(self, lu):
4152 """Computes the list of nodes and their attributes.
4155 # Locking is not used
4156 assert not (compat.any(lu.glm.is_owned(level)
4157 for level in locking.LEVELS
4158 if level != locking.LEVEL_CLUSTER) or
4159 self.do_locking or self.use_locking)
4161 valid_nodes = [node.name
4162 for node in lu.cfg.GetAllNodesInfo().values()
4163 if not node.offline and node.vm_capable]
4164 pol = self._DiagnoseByOS(lu.rpc.call_os_diagnose(valid_nodes))
4165 cluster = lu.cfg.GetClusterInfo()
4169 for (os_name, os_data) in pol.items():
4170 info = query.OsInfo(name=os_name, valid=True, node_status=os_data,
4171 hidden=(os_name in cluster.hidden_os),
4172 blacklisted=(os_name in cluster.blacklisted_os))
4176 api_versions = set()
4178 for idx, osl in enumerate(os_data.values()):
4179 info.valid = bool(info.valid and osl and osl[0][1])
4183 (node_variants, node_params, node_api) = osl[0][3:6]
4186 variants.update(node_variants)
4187 parameters.update(node_params)
4188 api_versions.update(node_api)
4190 # Filter out inconsistent values
4191 variants.intersection_update(node_variants)
4192 parameters.intersection_update(node_params)
4193 api_versions.intersection_update(node_api)
4195 info.variants = list(variants)
4196 info.parameters = list(parameters)
4197 info.api_versions = list(api_versions)
4199 data[os_name] = info
4201 # Prepare data in requested order
4202 return [data[name] for name in self._GetNames(lu, pol.keys(), None)
4206 class LUOsDiagnose(NoHooksLU):
4207 """Logical unit for OS diagnose/query.
4213 def _BuildFilter(fields, names):
4214 """Builds a filter for querying OSes.
4217 name_filter = qlang.MakeSimpleFilter("name", names)
4219 # Legacy behaviour: Hide hidden, blacklisted or invalid OSes if the
4220 # respective field is not requested
4221 status_filter = [[qlang.OP_NOT, [qlang.OP_TRUE, fname]]
4222 for fname in ["hidden", "blacklisted"]
4223 if fname not in fields]
4224 if "valid" not in fields:
4225 status_filter.append([qlang.OP_TRUE, "valid"])
4228 status_filter.insert(0, qlang.OP_AND)
4230 status_filter = None
4232 if name_filter and status_filter:
4233 return [qlang.OP_AND, name_filter, status_filter]
4237 return status_filter
4239 def CheckArguments(self):
4240 self.oq = _OsQuery(self._BuildFilter(self.op.output_fields, self.op.names),
4241 self.op.output_fields, False)
4243 def ExpandNames(self):
4244 self.oq.ExpandNames(self)
4246 def Exec(self, feedback_fn):
4247 return self.oq.OldStyleQuery(self)
4250 class LUNodeRemove(LogicalUnit):
4251 """Logical unit for removing a node.
4254 HPATH = "node-remove"
4255 HTYPE = constants.HTYPE_NODE
4257 def BuildHooksEnv(self):
4260 This doesn't run on the target node in the pre phase as a failed
4261 node would then be impossible to remove.
4265 "OP_TARGET": self.op.node_name,
4266 "NODE_NAME": self.op.node_name,
4269 def BuildHooksNodes(self):
4270 """Build hooks nodes.
4273 all_nodes = self.cfg.GetNodeList()
4275 all_nodes.remove(self.op.node_name)
4277 logging.warning("Node '%s', which is about to be removed, was not found"
4278 " in the list of all nodes", self.op.node_name)
4279 return (all_nodes, all_nodes)
4281 def CheckPrereq(self):
4282 """Check prerequisites.
4285 - the node exists in the configuration
4286 - it does not have primary or secondary instances
4287 - it's not the master
4289 Any errors are signaled by raising errors.OpPrereqError.
4292 self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
4293 node = self.cfg.GetNodeInfo(self.op.node_name)
4294 assert node is not None
4296 masternode = self.cfg.GetMasterNode()
4297 if node.name == masternode:
4298 raise errors.OpPrereqError("Node is the master node, failover to another"
4299 " node is required", errors.ECODE_INVAL)
4301 for instance_name, instance in self.cfg.GetAllInstancesInfo():
4302 if node.name in instance.all_nodes:
4303 raise errors.OpPrereqError("Instance %s is still running on the node,"
4304 " please remove first" % instance_name,
4306 self.op.node_name = node.name
4309 def Exec(self, feedback_fn):
4310 """Removes the node from the cluster.
4314 logging.info("Stopping the node daemon and removing configs from node %s",
4317 modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
4319 # Promote nodes to master candidate as needed
4320 _AdjustCandidatePool(self, exceptions=[node.name])
4321 self.context.RemoveNode(node.name)
4323 # Run post hooks on the node before it's removed
4324 _RunPostHook(self, node.name)
4326 result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
4327 msg = result.fail_msg
4329 self.LogWarning("Errors encountered on the remote node while leaving"
4330 " the cluster: %s", msg)
4332 # Remove node from our /etc/hosts
4333 if self.cfg.GetClusterInfo().modify_etc_hosts:
4334 master_node = self.cfg.GetMasterNode()
4335 result = self.rpc.call_etc_hosts_modify(master_node,
4336 constants.ETC_HOSTS_REMOVE,
4338 result.Raise("Can't update hosts file with new host data")
4339 _RedistributeAncillaryFiles(self)
4342 class _NodeQuery(_QueryBase):
4343 FIELDS = query.NODE_FIELDS
4345 def ExpandNames(self, lu):
4346 lu.needed_locks = {}
4347 lu.share_locks = _ShareAll()
4350 self.wanted = _GetWantedNodes(lu, self.names)
4352 self.wanted = locking.ALL_SET
4354 self.do_locking = (self.use_locking and
4355 query.NQ_LIVE in self.requested_data)
4358 # If any non-static field is requested we need to lock the nodes
4359 lu.needed_locks[locking.LEVEL_NODE] = self.wanted
4361 def DeclareLocks(self, lu, level):
4364 def _GetQueryData(self, lu):
4365 """Computes the list of nodes and their attributes.
4368 all_info = lu.cfg.GetAllNodesInfo()
4370 nodenames = self._GetNames(lu, all_info.keys(), locking.LEVEL_NODE)
4372 # Gather data as requested
4373 if query.NQ_LIVE in self.requested_data:
4374 # filter out non-vm_capable nodes
4375 toquery_nodes = [name for name in nodenames if all_info[name].vm_capable]
4377 node_data = lu.rpc.call_node_info(toquery_nodes, lu.cfg.GetVGName(),
4378 lu.cfg.GetHypervisorType())
4379 live_data = dict((name, nresult.payload)
4380 for (name, nresult) in node_data.items()
4381 if not nresult.fail_msg and nresult.payload)
4385 if query.NQ_INST in self.requested_data:
4386 node_to_primary = dict([(name, set()) for name in nodenames])
4387 node_to_secondary = dict([(name, set()) for name in nodenames])
4389 inst_data = lu.cfg.GetAllInstancesInfo()
4391 for inst in inst_data.values():
4392 if inst.primary_node in node_to_primary:
4393 node_to_primary[inst.primary_node].add(inst.name)
4394 for secnode in inst.secondary_nodes:
4395 if secnode in node_to_secondary:
4396 node_to_secondary[secnode].add(inst.name)
4398 node_to_primary = None
4399 node_to_secondary = None
4401 if query.NQ_OOB in self.requested_data:
4402 oob_support = dict((name, bool(_SupportsOob(lu.cfg, node)))
4403 for name, node in all_info.iteritems())
4407 if query.NQ_GROUP in self.requested_data:
4408 groups = lu.cfg.GetAllNodeGroupsInfo()
4412 return query.NodeQueryData([all_info[name] for name in nodenames],
4413 live_data, lu.cfg.GetMasterNode(),
4414 node_to_primary, node_to_secondary, groups,
4415 oob_support, lu.cfg.GetClusterInfo())
4418 class LUNodeQuery(NoHooksLU):
4419 """Logical unit for querying nodes.
4422 # pylint: disable=W0142
4425 def CheckArguments(self):
4426 self.nq = _NodeQuery(qlang.MakeSimpleFilter("name", self.op.names),
4427 self.op.output_fields, self.op.use_locking)
4429 def ExpandNames(self):
4430 self.nq.ExpandNames(self)
4432 def Exec(self, feedback_fn):
4433 return self.nq.OldStyleQuery(self)
4436 class LUNodeQueryvols(NoHooksLU):
4437 """Logical unit for getting volumes on node(s).
4441 _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
4442 _FIELDS_STATIC = utils.FieldSet("node")
4444 def CheckArguments(self):
4445 _CheckOutputFields(static=self._FIELDS_STATIC,
4446 dynamic=self._FIELDS_DYNAMIC,
4447 selected=self.op.output_fields)
4449 def ExpandNames(self):
4450 self.needed_locks = {}
4451 self.share_locks[locking.LEVEL_NODE] = 1
4452 if not self.op.nodes:
4453 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
4455 self.needed_locks[locking.LEVEL_NODE] = \
4456 _GetWantedNodes(self, self.op.nodes)
4458 def Exec(self, feedback_fn):
4459 """Computes the list of nodes and their attributes.
4462 nodenames = self.owned_locks(locking.LEVEL_NODE)
4463 volumes = self.rpc.call_node_volumes(nodenames)
4465 ilist = self.cfg.GetAllInstancesInfo()
4466 vol2inst = _MapInstanceDisksToNodes(ilist.values())
4469 for node in nodenames:
4470 nresult = volumes[node]
4473 msg = nresult.fail_msg
4475 self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
4478 node_vols = sorted(nresult.payload,
4479 key=operator.itemgetter("dev"))
4481 for vol in node_vols:
4483 for field in self.op.output_fields:
4486 elif field == "phys":
4490 elif field == "name":
4492 elif field == "size":
4493 val = int(float(vol["size"]))
4494 elif field == "instance":
4495 val = vol2inst.get((node, vol["vg"] + "/" + vol["name"]), "-")
4497 raise errors.ParameterError(field)
4498 node_output.append(str(val))
4500 output.append(node_output)
4505 class LUNodeQueryStorage(NoHooksLU):
4506 """Logical unit for getting information on storage units on node(s).
4509 _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
4512 def CheckArguments(self):
4513 _CheckOutputFields(static=self._FIELDS_STATIC,
4514 dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
4515 selected=self.op.output_fields)
4517 def ExpandNames(self):
4518 self.needed_locks = {}
4519 self.share_locks[locking.LEVEL_NODE] = 1
4522 self.needed_locks[locking.LEVEL_NODE] = \
4523 _GetWantedNodes(self, self.op.nodes)
4525 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
4527 def Exec(self, feedback_fn):
4528 """Computes the list of nodes and their attributes.
4531 self.nodes = self.owned_locks(locking.LEVEL_NODE)
4533 # Always get name to sort by
4534 if constants.SF_NAME in self.op.output_fields:
4535 fields = self.op.output_fields[:]
4537 fields = [constants.SF_NAME] + self.op.output_fields
4539 # Never ask for node or type as it's only known to the LU
4540 for extra in [constants.SF_NODE, constants.SF_TYPE]:
4541 while extra in fields:
4542 fields.remove(extra)
4544 field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
4545 name_idx = field_idx[constants.SF_NAME]
4547 st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
4548 data = self.rpc.call_storage_list(self.nodes,
4549 self.op.storage_type, st_args,
4550 self.op.name, fields)
4554 for node in utils.NiceSort(self.nodes):
4555 nresult = data[node]
4559 msg = nresult.fail_msg
4561 self.LogWarning("Can't get storage data from node %s: %s", node, msg)
4564 rows = dict([(row[name_idx], row) for row in nresult.payload])
4566 for name in utils.NiceSort(rows.keys()):
4571 for field in self.op.output_fields:
4572 if field == constants.SF_NODE:
4574 elif field == constants.SF_TYPE:
4575 val = self.op.storage_type
4576 elif field in field_idx:
4577 val = row[field_idx[field]]
4579 raise errors.ParameterError(field)
4588 class _InstanceQuery(_QueryBase):
4589 FIELDS = query.INSTANCE_FIELDS
4591 def ExpandNames(self, lu):
4592 lu.needed_locks = {}
4593 lu.share_locks = _ShareAll()
4596 self.wanted = _GetWantedInstances(lu, self.names)
4598 self.wanted = locking.ALL_SET
4600 self.do_locking = (self.use_locking and
4601 query.IQ_LIVE in self.requested_data)
4603 lu.needed_locks[locking.LEVEL_INSTANCE] = self.wanted
4604 lu.needed_locks[locking.LEVEL_NODEGROUP] = []
4605 lu.needed_locks[locking.LEVEL_NODE] = []
4606 lu.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
4608 self.do_grouplocks = (self.do_locking and
4609 query.IQ_NODES in self.requested_data)
4611 def DeclareLocks(self, lu, level):
4613 if level == locking.LEVEL_NODEGROUP and self.do_grouplocks:
4614 assert not lu.needed_locks[locking.LEVEL_NODEGROUP]
4616 # Lock all groups used by instances optimistically; this requires going
4617 # via the node before it's locked, requiring verification later on
4618 lu.needed_locks[locking.LEVEL_NODEGROUP] = \
4620 for instance_name in lu.owned_locks(locking.LEVEL_INSTANCE)
4621 for group_uuid in lu.cfg.GetInstanceNodeGroups(instance_name))
4622 elif level == locking.LEVEL_NODE:
4623 lu._LockInstancesNodes() # pylint: disable=W0212
4626 def _CheckGroupLocks(lu):
4627 owned_instances = frozenset(lu.owned_locks(locking.LEVEL_INSTANCE))
4628 owned_groups = frozenset(lu.owned_locks(locking.LEVEL_NODEGROUP))
4630 # Check if node groups for locked instances are still correct
4631 for instance_name in owned_instances:
4632 _CheckInstanceNodeGroups(lu.cfg, instance_name, owned_groups)
4634 def _GetQueryData(self, lu):
4635 """Computes the list of instances and their attributes.
4638 if self.do_grouplocks:
4639 self._CheckGroupLocks(lu)
4641 cluster = lu.cfg.GetClusterInfo()
4642 all_info = lu.cfg.GetAllInstancesInfo()
4644 instance_names = self._GetNames(lu, all_info.keys(), locking.LEVEL_INSTANCE)
4646 instance_list = [all_info[name] for name in instance_names]
4647 nodes = frozenset(itertools.chain(*(inst.all_nodes
4648 for inst in instance_list)))
4649 hv_list = list(set([inst.hypervisor for inst in instance_list]))
4652 wrongnode_inst = set()
4654 # Gather data as requested
4655 if self.requested_data & set([query.IQ_LIVE, query.IQ_CONSOLE]):
4657 node_data = lu.rpc.call_all_instances_info(nodes, hv_list)
4659 result = node_data[name]
4661 # offline nodes will be in both lists
4662 assert result.fail_msg
4663 offline_nodes.append(name)
4665 bad_nodes.append(name)
4666 elif result.payload:
4667 for inst in result.payload:
4668 if inst in all_info:
4669 if all_info[inst].primary_node == name:
4670 live_data.update(result.payload)
4672 wrongnode_inst.add(inst)
4674 # orphan instance; we don't list it here as we don't
4675 # handle this case yet in the output of instance listing
4676 logging.warning("Orphan instance '%s' found on node %s",
4678 # else no instance is alive
4682 if query.IQ_DISKUSAGE in self.requested_data:
4683 disk_usage = dict((inst.name,
4684 _ComputeDiskSize(inst.disk_template,
4685 [{constants.IDISK_SIZE: disk.size}
4686 for disk in inst.disks]))
4687 for inst in instance_list)
4691 if query.IQ_CONSOLE in self.requested_data:
4693 for inst in instance_list:
4694 if inst.name in live_data:
4695 # Instance is running
4696 consinfo[inst.name] = _GetInstanceConsole(cluster, inst)
4698 consinfo[inst.name] = None
4699 assert set(consinfo.keys()) == set(instance_names)
4703 if query.IQ_NODES in self.requested_data:
4704 node_names = set(itertools.chain(*map(operator.attrgetter("all_nodes"),
4706 nodes = dict(lu.cfg.GetMultiNodeInfo(node_names))
4707 groups = dict((uuid, lu.cfg.GetNodeGroup(uuid))
4708 for uuid in set(map(operator.attrgetter("group"),
4714 return query.InstanceQueryData(instance_list, lu.cfg.GetClusterInfo(),
4715 disk_usage, offline_nodes, bad_nodes,
4716 live_data, wrongnode_inst, consinfo,
4720 class LUQuery(NoHooksLU):
4721 """Query for resources/items of a certain kind.
4724 # pylint: disable=W0142
4727 def CheckArguments(self):
4728 qcls = _GetQueryImplementation(self.op.what)
4730 self.impl = qcls(self.op.filter, self.op.fields, self.op.use_locking)
4732 def ExpandNames(self):
4733 self.impl.ExpandNames(self)
4735 def DeclareLocks(self, level):
4736 self.impl.DeclareLocks(self, level)
4738 def Exec(self, feedback_fn):
4739 return self.impl.NewStyleQuery(self)
4742 class LUQueryFields(NoHooksLU):
4743 """Query for resources/items of a certain kind.
4746 # pylint: disable=W0142
4749 def CheckArguments(self):
4750 self.qcls = _GetQueryImplementation(self.op.what)
4752 def ExpandNames(self):
4753 self.needed_locks = {}
4755 def Exec(self, feedback_fn):
4756 return query.QueryFields(self.qcls.FIELDS, self.op.fields)
4759 class LUNodeModifyStorage(NoHooksLU):
4760 """Logical unit for modifying a storage volume on a node.
4765 def CheckArguments(self):
4766 self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
4768 storage_type = self.op.storage_type
4771 modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
4773 raise errors.OpPrereqError("Storage units of type '%s' can not be"
4774 " modified" % storage_type,
4777 diff = set(self.op.changes.keys()) - modifiable
4779 raise errors.OpPrereqError("The following fields can not be modified for"
4780 " storage units of type '%s': %r" %
4781 (storage_type, list(diff)),
4784 def ExpandNames(self):
4785 self.needed_locks = {
4786 locking.LEVEL_NODE: self.op.node_name,
4789 def Exec(self, feedback_fn):
4790 """Computes the list of nodes and their attributes.
4793 st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
4794 result = self.rpc.call_storage_modify(self.op.node_name,
4795 self.op.storage_type, st_args,
4796 self.op.name, self.op.changes)
4797 result.Raise("Failed to modify storage unit '%s' on %s" %
4798 (self.op.name, self.op.node_name))
4801 class LUNodeAdd(LogicalUnit):
4802 """Logical unit for adding node to the cluster.
4806 HTYPE = constants.HTYPE_NODE
4807 _NFLAGS = ["master_capable", "vm_capable"]
4809 def CheckArguments(self):
4810 self.primary_ip_family = self.cfg.GetPrimaryIPFamily()
4811 # validate/normalize the node name
4812 self.hostname = netutils.GetHostname(name=self.op.node_name,
4813 family=self.primary_ip_family)
4814 self.op.node_name = self.hostname.name
4816 if self.op.readd and self.op.node_name == self.cfg.GetMasterNode():
4817 raise errors.OpPrereqError("Cannot readd the master node",
4820 if self.op.readd and self.op.group:
4821 raise errors.OpPrereqError("Cannot pass a node group when a node is"
4822 " being readded", errors.ECODE_INVAL)
4824 def BuildHooksEnv(self):
4827 This will run on all nodes before, and on all nodes + the new node after.
4831 "OP_TARGET": self.op.node_name,
4832 "NODE_NAME": self.op.node_name,
4833 "NODE_PIP": self.op.primary_ip,
4834 "NODE_SIP": self.op.secondary_ip,
4835 "MASTER_CAPABLE": str(self.op.master_capable),
4836 "VM_CAPABLE": str(self.op.vm_capable),
4839 def BuildHooksNodes(self):
4840 """Build hooks nodes.
4843 # Exclude added node
4844 pre_nodes = list(set(self.cfg.GetNodeList()) - set([self.op.node_name]))
4845 post_nodes = pre_nodes + [self.op.node_name, ]
4847 return (pre_nodes, post_nodes)
4849 def CheckPrereq(self):
4850 """Check prerequisites.
4853 - the new node is not already in the config
4855 - its parameters (single/dual homed) matches the cluster
4857 Any errors are signaled by raising errors.OpPrereqError.
4861 hostname = self.hostname
4862 node = hostname.name
4863 primary_ip = self.op.primary_ip = hostname.ip
4864 if self.op.secondary_ip is None:
4865 if self.primary_ip_family == netutils.IP6Address.family:
4866 raise errors.OpPrereqError("When using a IPv6 primary address, a valid"
4867 " IPv4 address must be given as secondary",
4869 self.op.secondary_ip = primary_ip
4871 secondary_ip = self.op.secondary_ip
4872 if not netutils.IP4Address.IsValid(secondary_ip):
4873 raise errors.OpPrereqError("Secondary IP (%s) needs to be a valid IPv4"
4874 " address" % secondary_ip, errors.ECODE_INVAL)
4876 node_list = cfg.GetNodeList()
4877 if not self.op.readd and node in node_list:
4878 raise errors.OpPrereqError("Node %s is already in the configuration" %
4879 node, errors.ECODE_EXISTS)
4880 elif self.op.readd and node not in node_list:
4881 raise errors.OpPrereqError("Node %s is not in the configuration" % node,
4884 self.changed_primary_ip = False
4886 for existing_node_name, existing_node in cfg.GetMultiNodeInfo(node_list):
4887 if self.op.readd and node == existing_node_name:
4888 if existing_node.secondary_ip != secondary_ip:
4889 raise errors.OpPrereqError("Readded node doesn't have the same IP"
4890 " address configuration as before",
4892 if existing_node.primary_ip != primary_ip:
4893 self.changed_primary_ip = True
4897 if (existing_node.primary_ip == primary_ip or
4898 existing_node.secondary_ip == primary_ip or
4899 existing_node.primary_ip == secondary_ip or
4900 existing_node.secondary_ip == secondary_ip):
4901 raise errors.OpPrereqError("New node ip address(es) conflict with"
4902 " existing node %s" % existing_node.name,
4903 errors.ECODE_NOTUNIQUE)
4905 # After this 'if' block, None is no longer a valid value for the
4906 # _capable op attributes
4908 old_node = self.cfg.GetNodeInfo(node)
4909 assert old_node is not None, "Can't retrieve locked node %s" % node
4910 for attr in self._NFLAGS:
4911 if getattr(self.op, attr) is None:
4912 setattr(self.op, attr, getattr(old_node, attr))
4914 for attr in self._NFLAGS:
4915 if getattr(self.op, attr) is None:
4916 setattr(self.op, attr, True)
4918 if self.op.readd and not self.op.vm_capable:
4919 pri, sec = cfg.GetNodeInstances(node)
4921 raise errors.OpPrereqError("Node %s being re-added with vm_capable"
4922 " flag set to false, but it already holds"
4923 " instances" % node,
4926 # check that the type of the node (single versus dual homed) is the
4927 # same as for the master
4928 myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
4929 master_singlehomed = myself.secondary_ip == myself.primary_ip
4930 newbie_singlehomed = secondary_ip == primary_ip
4931 if master_singlehomed != newbie_singlehomed:
4932 if master_singlehomed:
4933 raise errors.OpPrereqError("The master has no secondary ip but the"
4934 " new node has one",
4937 raise errors.OpPrereqError("The master has a secondary ip but the"
4938 " new node doesn't have one",
4941 # checks reachability
4942 if not netutils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
4943 raise errors.OpPrereqError("Node not reachable by ping",
4944 errors.ECODE_ENVIRON)
4946 if not newbie_singlehomed:
4947 # check reachability from my secondary ip to newbie's secondary ip
4948 if not netutils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
4949 source=myself.secondary_ip):
4950 raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
4951 " based ping to node daemon port",
4952 errors.ECODE_ENVIRON)
4959 if self.op.master_capable:
4960 self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
4962 self.master_candidate = False
4965 self.new_node = old_node
4967 node_group = cfg.LookupNodeGroup(self.op.group)
4968 self.new_node = objects.Node(name=node,
4969 primary_ip=primary_ip,
4970 secondary_ip=secondary_ip,
4971 master_candidate=self.master_candidate,
4972 offline=False, drained=False,
4975 if self.op.ndparams:
4976 utils.ForceDictType(self.op.ndparams, constants.NDS_PARAMETER_TYPES)
4978 def Exec(self, feedback_fn):
4979 """Adds the new node to the cluster.
4982 new_node = self.new_node
4983 node = new_node.name
4985 # We adding a new node so we assume it's powered
4986 new_node.powered = True
4988 # for re-adds, reset the offline/drained/master-candidate flags;
4989 # we need to reset here, otherwise offline would prevent RPC calls
4990 # later in the procedure; this also means that if the re-add
4991 # fails, we are left with a non-offlined, broken node
4993 new_node.drained = new_node.offline = False # pylint: disable=W0201
4994 self.LogInfo("Readding a node, the offline/drained flags were reset")
4995 # if we demote the node, we do cleanup later in the procedure
4996 new_node.master_candidate = self.master_candidate
4997 if self.changed_primary_ip:
4998 new_node.primary_ip = self.op.primary_ip
5000 # copy the master/vm_capable flags
5001 for attr in self._NFLAGS:
5002 setattr(new_node, attr, getattr(self.op, attr))
5004 # notify the user about any possible mc promotion
5005 if new_node.master_candidate:
5006 self.LogInfo("Node will be a master candidate")
5008 if self.op.ndparams:
5009 new_node.ndparams = self.op.ndparams
5011 new_node.ndparams = {}
5013 # check connectivity
5014 result = self.rpc.call_version([node])[node]
5015 result.Raise("Can't get version information from node %s" % node)
5016 if constants.PROTOCOL_VERSION == result.payload:
5017 logging.info("Communication to node %s fine, sw version %s match",
5018 node, result.payload)
5020 raise errors.OpExecError("Version mismatch master version %s,"
5021 " node version %s" %
5022 (constants.PROTOCOL_VERSION, result.payload))
5024 # Add node to our /etc/hosts, and add key to known_hosts
5025 if self.cfg.GetClusterInfo().modify_etc_hosts:
5026 master_node = self.cfg.GetMasterNode()
5027 result = self.rpc.call_etc_hosts_modify(master_node,
5028 constants.ETC_HOSTS_ADD,
5031 result.Raise("Can't update hosts file with new host data")
5033 if new_node.secondary_ip != new_node.primary_ip:
5034 _CheckNodeHasSecondaryIP(self, new_node.name, new_node.secondary_ip,
5037 node_verify_list = [self.cfg.GetMasterNode()]
5038 node_verify_param = {
5039 constants.NV_NODELIST: [node],
5040 # TODO: do a node-net-test as well?
5043 result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
5044 self.cfg.GetClusterName())
5045 for verifier in node_verify_list:
5046 result[verifier].Raise("Cannot communicate with node %s" % verifier)
5047 nl_payload = result[verifier].payload[constants.NV_NODELIST]
5049 for failed in nl_payload:
5050 feedback_fn("ssh/hostname verification failed"
5051 " (checking from %s): %s" %
5052 (verifier, nl_payload[failed]))
5053 raise errors.OpExecError("ssh/hostname verification failed")
5056 _RedistributeAncillaryFiles(self)
5057 self.context.ReaddNode(new_node)
5058 # make sure we redistribute the config
5059 self.cfg.Update(new_node, feedback_fn)
5060 # and make sure the new node will not have old files around
5061 if not new_node.master_candidate:
5062 result = self.rpc.call_node_demote_from_mc(new_node.name)
5063 msg = result.fail_msg
5065 self.LogWarning("Node failed to demote itself from master"
5066 " candidate status: %s" % msg)
5068 _RedistributeAncillaryFiles(self, additional_nodes=[node],
5069 additional_vm=self.op.vm_capable)
5070 self.context.AddNode(new_node, self.proc.GetECId())
5073 class LUNodeSetParams(LogicalUnit):
5074 """Modifies the parameters of a node.
5076 @cvar _F2R: a dictionary from tuples of flags (mc, drained, offline)
5077 to the node role (as _ROLE_*)
5078 @cvar _R2F: a dictionary from node role to tuples of flags
5079 @cvar _FLAGS: a list of attribute names corresponding to the flags
5082 HPATH = "node-modify"
5083 HTYPE = constants.HTYPE_NODE
5085 (_ROLE_CANDIDATE, _ROLE_DRAINED, _ROLE_OFFLINE, _ROLE_REGULAR) = range(4)
5087 (True, False, False): _ROLE_CANDIDATE,
5088 (False, True, False): _ROLE_DRAINED,
5089 (False, False, True): _ROLE_OFFLINE,
5090 (False, False, False): _ROLE_REGULAR,
5092 _R2F = dict((v, k) for k, v in _F2R.items())
5093 _FLAGS = ["master_candidate", "drained", "offline"]
5095 def CheckArguments(self):
5096 self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
5097 all_mods = [self.op.offline, self.op.master_candidate, self.op.drained,
5098 self.op.master_capable, self.op.vm_capable,
5099 self.op.secondary_ip, self.op.ndparams]
5100 if all_mods.count(None) == len(all_mods):
5101 raise errors.OpPrereqError("Please pass at least one modification",
5103 if all_mods.count(True) > 1:
5104 raise errors.OpPrereqError("Can't set the node into more than one"
5105 " state at the same time",
5108 # Boolean value that tells us whether we might be demoting from MC
5109 self.might_demote = (self.op.master_candidate == False or
5110 self.op.offline == True or
5111 self.op.drained == True or
5112 self.op.master_capable == False)
5114 if self.op.secondary_ip:
5115 if not netutils.IP4Address.IsValid(self.op.secondary_ip):
5116 raise errors.OpPrereqError("Secondary IP (%s) needs to be a valid IPv4"
5117 " address" % self.op.secondary_ip,
5120 self.lock_all = self.op.auto_promote and self.might_demote
5121 self.lock_instances = self.op.secondary_ip is not None
5123 def ExpandNames(self):
5125 self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
5127 self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
5129 if self.lock_instances:
5130 self.needed_locks[locking.LEVEL_INSTANCE] = locking.ALL_SET
5132 def DeclareLocks(self, level):
5133 # If we have locked all instances, before waiting to lock nodes, release
5134 # all the ones living on nodes unrelated to the current operation.
5135 if level == locking.LEVEL_NODE and self.lock_instances:
5136 self.affected_instances = []
5137 if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET:
5140 # Build list of instances to release
5141 locked_i = self.owned_locks(locking.LEVEL_INSTANCE)
5142 for instance_name, instance in self.cfg.GetMultiInstanceInfo(locked_i):
5143 if (instance.disk_template in constants.DTS_INT_MIRROR and
5144 self.op.node_name in instance.all_nodes):
5145 instances_keep.append(instance_name)
5146 self.affected_instances.append(instance)
5148 _ReleaseLocks(self, locking.LEVEL_INSTANCE, keep=instances_keep)
5150 assert (set(self.owned_locks(locking.LEVEL_INSTANCE)) ==
5151 set(instances_keep))
5153 def BuildHooksEnv(self):
5156 This runs on the master node.
5160 "OP_TARGET": self.op.node_name,
5161 "MASTER_CANDIDATE": str(self.op.master_candidate),
5162 "OFFLINE": str(self.op.offline),
5163 "DRAINED": str(self.op.drained),
5164 "MASTER_CAPABLE": str(self.op.master_capable),
5165 "VM_CAPABLE": str(self.op.vm_capable),
5168 def BuildHooksNodes(self):
5169 """Build hooks nodes.
5172 nl = [self.cfg.GetMasterNode(), self.op.node_name]
5175 def CheckPrereq(self):
5176 """Check prerequisites.
5178 This only checks the instance list against the existing names.
5181 node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
5183 if (self.op.master_candidate is not None or
5184 self.op.drained is not None or
5185 self.op.offline is not None):
5186 # we can't change the master's node flags
5187 if self.op.node_name == self.cfg.GetMasterNode():
5188 raise errors.OpPrereqError("The master role can be changed"
5189 " only via master-failover",
5192 if self.op.master_candidate and not node.master_capable:
5193 raise errors.OpPrereqError("Node %s is not master capable, cannot make"
5194 " it a master candidate" % node.name,
5197 if self.op.vm_capable == False:
5198 (ipri, isec) = self.cfg.GetNodeInstances(self.op.node_name)
5200 raise errors.OpPrereqError("Node %s hosts instances, cannot unset"
5201 " the vm_capable flag" % node.name,
5204 if node.master_candidate and self.might_demote and not self.lock_all:
5205 assert not self.op.auto_promote, "auto_promote set but lock_all not"
5206 # check if after removing the current node, we're missing master
5208 (mc_remaining, mc_should, _) = \
5209 self.cfg.GetMasterCandidateStats(exceptions=[node.name])
5210 if mc_remaining < mc_should:
5211 raise errors.OpPrereqError("Not enough master candidates, please"
5212 " pass auto promote option to allow"
5213 " promotion", errors.ECODE_STATE)
5215 self.old_flags = old_flags = (node.master_candidate,
5216 node.drained, node.offline)
5217 assert old_flags in self._F2R, "Un-handled old flags %s" % str(old_flags)
5218 self.old_role = old_role = self._F2R[old_flags]
5220 # Check for ineffective changes
5221 for attr in self._FLAGS:
5222 if (getattr(self.op, attr) == False and getattr(node, attr) == False):
5223 self.LogInfo("Ignoring request to unset flag %s, already unset", attr)
5224 setattr(self.op, attr, None)
5226 # Past this point, any flag change to False means a transition
5227 # away from the respective state, as only real changes are kept
5229 # TODO: We might query the real power state if it supports OOB
5230 if _SupportsOob(self.cfg, node):
5231 if self.op.offline is False and not (node.powered or
5232 self.op.powered == True):
5233 raise errors.OpPrereqError(("Node %s needs to be turned on before its"
5234 " offline status can be reset") %
5236 elif self.op.powered is not None:
5237 raise errors.OpPrereqError(("Unable to change powered state for node %s"
5238 " as it does not support out-of-band"
5239 " handling") % self.op.node_name)
5241 # If we're being deofflined/drained, we'll MC ourself if needed
5242 if (self.op.drained == False or self.op.offline == False or
5243 (self.op.master_capable and not node.master_capable)):
5244 if _DecideSelfPromotion(self):
5245 self.op.master_candidate = True
5246 self.LogInfo("Auto-promoting node to master candidate")
5248 # If we're no longer master capable, we'll demote ourselves from MC
5249 if self.op.master_capable == False and node.master_candidate:
5250 self.LogInfo("Demoting from master candidate")
5251 self.op.master_candidate = False
5254 assert [getattr(self.op, attr) for attr in self._FLAGS].count(True) <= 1
5255 if self.op.master_candidate:
5256 new_role = self._ROLE_CANDIDATE
5257 elif self.op.drained:
5258 new_role = self._ROLE_DRAINED
5259 elif self.op.offline:
5260 new_role = self._ROLE_OFFLINE
5261 elif False in [self.op.master_candidate, self.op.drained, self.op.offline]:
5262 # False is still in new flags, which means we're un-setting (the
5264 new_role = self._ROLE_REGULAR
5265 else: # no new flags, nothing, keep old role
5268 self.new_role = new_role
5270 if old_role == self._ROLE_OFFLINE and new_role != old_role:
5271 # Trying to transition out of offline status
5272 result = self.rpc.call_version([node.name])[node.name]
5274 raise errors.OpPrereqError("Node %s is being de-offlined but fails"
5275 " to report its version: %s" %
5276 (node.name, result.fail_msg),
5279 self.LogWarning("Transitioning node from offline to online state"
5280 " without using re-add. Please make sure the node"
5283 if self.op.secondary_ip:
5284 # Ok even without locking, because this can't be changed by any LU
5285 master = self.cfg.GetNodeInfo(self.cfg.GetMasterNode())
5286 master_singlehomed = master.secondary_ip == master.primary_ip
5287 if master_singlehomed and self.op.secondary_ip:
5288 raise errors.OpPrereqError("Cannot change the secondary ip on a single"
5289 " homed cluster", errors.ECODE_INVAL)
5292 if self.affected_instances:
5293 raise errors.OpPrereqError("Cannot change secondary ip: offline"
5294 " node has instances (%s) configured"
5295 " to use it" % self.affected_instances)
5297 # On online nodes, check that no instances are running, and that
5298 # the node has the new ip and we can reach it.
5299 for instance in self.affected_instances:
5300 _CheckInstanceDown(self, instance, "cannot change secondary ip")
5302 _CheckNodeHasSecondaryIP(self, node.name, self.op.secondary_ip, True)
5303 if master.name != node.name:
5304 # check reachability from master secondary ip to new secondary ip
5305 if not netutils.TcpPing(self.op.secondary_ip,
5306 constants.DEFAULT_NODED_PORT,
5307 source=master.secondary_ip):
5308 raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
5309 " based ping to node daemon port",
5310 errors.ECODE_ENVIRON)
5312 if self.op.ndparams:
5313 new_ndparams = _GetUpdatedParams(self.node.ndparams, self.op.ndparams)
5314 utils.ForceDictType(new_ndparams, constants.NDS_PARAMETER_TYPES)
5315 self.new_ndparams = new_ndparams
5317 def Exec(self, feedback_fn):
5322 old_role = self.old_role
5323 new_role = self.new_role
5327 if self.op.ndparams:
5328 node.ndparams = self.new_ndparams
5330 if self.op.powered is not None:
5331 node.powered = self.op.powered
5333 for attr in ["master_capable", "vm_capable"]:
5334 val = getattr(self.op, attr)
5336 setattr(node, attr, val)
5337 result.append((attr, str(val)))
5339 if new_role != old_role:
5340 # Tell the node to demote itself, if no longer MC and not offline
5341 if old_role == self._ROLE_CANDIDATE and new_role != self._ROLE_OFFLINE:
5342 msg = self.rpc.call_node_demote_from_mc(node.name).fail_msg
5344 self.LogWarning("Node failed to demote itself: %s", msg)
5346 new_flags = self._R2F[new_role]
5347 for of, nf, desc in zip(self.old_flags, new_flags, self._FLAGS):
5349 result.append((desc, str(nf)))
5350 (node.master_candidate, node.drained, node.offline) = new_flags
5352 # we locked all nodes, we adjust the CP before updating this node
5354 _AdjustCandidatePool(self, [node.name])
5356 if self.op.secondary_ip:
5357 node.secondary_ip = self.op.secondary_ip
5358 result.append(("secondary_ip", self.op.secondary_ip))
5360 # this will trigger configuration file update, if needed
5361 self.cfg.Update(node, feedback_fn)
5363 # this will trigger job queue propagation or cleanup if the mc
5365 if [old_role, new_role].count(self._ROLE_CANDIDATE) == 1:
5366 self.context.ReaddNode(node)
5371 class LUNodePowercycle(NoHooksLU):
5372 """Powercycles a node.
5377 def CheckArguments(self):
5378 self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
5379 if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force:
5380 raise errors.OpPrereqError("The node is the master and the force"
5381 " parameter was not set",
5384 def ExpandNames(self):
5385 """Locking for PowercycleNode.
5387 This is a last-resort option and shouldn't block on other
5388 jobs. Therefore, we grab no locks.
5391 self.needed_locks = {}
5393 def Exec(self, feedback_fn):
5397 result = self.rpc.call_node_powercycle(self.op.node_name,
5398 self.cfg.GetHypervisorType())
5399 result.Raise("Failed to schedule the reboot")
5400 return result.payload
5403 class LUClusterQuery(NoHooksLU):
5404 """Query cluster configuration.
5409 def ExpandNames(self):
5410 self.needed_locks = {}
5412 def Exec(self, feedback_fn):
5413 """Return cluster config.
5416 cluster = self.cfg.GetClusterInfo()
5419 # Filter just for enabled hypervisors
5420 for os_name, hv_dict in cluster.os_hvp.items():
5421 os_hvp[os_name] = {}
5422 for hv_name, hv_params in hv_dict.items():
5423 if hv_name in cluster.enabled_hypervisors:
5424 os_hvp[os_name][hv_name] = hv_params
5426 # Convert ip_family to ip_version
5427 primary_ip_version = constants.IP4_VERSION
5428 if cluster.primary_ip_family == netutils.IP6Address.family:
5429 primary_ip_version = constants.IP6_VERSION
5432 "software_version": constants.RELEASE_VERSION,
5433 "protocol_version": constants.PROTOCOL_VERSION,
5434 "config_version": constants.CONFIG_VERSION,
5435 "os_api_version": max(constants.OS_API_VERSIONS),
5436 "export_version": constants.EXPORT_VERSION,
5437 "architecture": (platform.architecture()[0], platform.machine()),
5438 "name": cluster.cluster_name,
5439 "master": cluster.master_node,
5440 "default_hypervisor": cluster.enabled_hypervisors[0],
5441 "enabled_hypervisors": cluster.enabled_hypervisors,
5442 "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
5443 for hypervisor_name in cluster.enabled_hypervisors]),
5445 "beparams": cluster.beparams,
5446 "osparams": cluster.osparams,
5447 "nicparams": cluster.nicparams,
5448 "ndparams": cluster.ndparams,
5449 "candidate_pool_size": cluster.candidate_pool_size,
5450 "master_netdev": cluster.master_netdev,
5451 "volume_group_name": cluster.volume_group_name,
5452 "drbd_usermode_helper": cluster.drbd_usermode_helper,
5453 "file_storage_dir": cluster.file_storage_dir,
5454 "shared_file_storage_dir": cluster.shared_file_storage_dir,
5455 "maintain_node_health": cluster.maintain_node_health,
5456 "ctime": cluster.ctime,
5457 "mtime": cluster.mtime,
5458 "uuid": cluster.uuid,
5459 "tags": list(cluster.GetTags()),
5460 "uid_pool": cluster.uid_pool,
5461 "default_iallocator": cluster.default_iallocator,
5462 "reserved_lvs": cluster.reserved_lvs,
5463 "primary_ip_version": primary_ip_version,
5464 "prealloc_wipe_disks": cluster.prealloc_wipe_disks,
5465 "hidden_os": cluster.hidden_os,
5466 "blacklisted_os": cluster.blacklisted_os,
5472 class LUClusterConfigQuery(NoHooksLU):
5473 """Return configuration values.
5477 _FIELDS_DYNAMIC = utils.FieldSet()
5478 _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag",
5479 "watcher_pause", "volume_group_name")
5481 def CheckArguments(self):
5482 _CheckOutputFields(static=self._FIELDS_STATIC,
5483 dynamic=self._FIELDS_DYNAMIC,
5484 selected=self.op.output_fields)
5486 def ExpandNames(self):
5487 self.needed_locks = {}
5489 def Exec(self, feedback_fn):
5490 """Dump a representation of the cluster config to the standard output.
5494 for field in self.op.output_fields:
5495 if field == "cluster_name":
5496 entry = self.cfg.GetClusterName()
5497 elif field == "master_node":
5498 entry = self.cfg.GetMasterNode()
5499 elif field == "drain_flag":
5500 entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
5501 elif field == "watcher_pause":
5502 entry = utils.ReadWatcherPauseFile(constants.WATCHER_PAUSEFILE)
5503 elif field == "volume_group_name":
5504 entry = self.cfg.GetVGName()
5506 raise errors.ParameterError(field)
5507 values.append(entry)
5511 class LUInstanceActivateDisks(NoHooksLU):
5512 """Bring up an instance's disks.
5517 def ExpandNames(self):
5518 self._ExpandAndLockInstance()
5519 self.needed_locks[locking.LEVEL_NODE] = []
5520 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
5522 def DeclareLocks(self, level):
5523 if level == locking.LEVEL_NODE:
5524 self._LockInstancesNodes()
5526 def CheckPrereq(self):
5527 """Check prerequisites.
5529 This checks that the instance is in the cluster.
5532 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5533 assert self.instance is not None, \
5534 "Cannot retrieve locked instance %s" % self.op.instance_name
5535 _CheckNodeOnline(self, self.instance.primary_node)
5537 def Exec(self, feedback_fn):
5538 """Activate the disks.
5541 disks_ok, disks_info = \
5542 _AssembleInstanceDisks(self, self.instance,
5543 ignore_size=self.op.ignore_size)
5545 raise errors.OpExecError("Cannot activate block devices")
5550 def _AssembleInstanceDisks(lu, instance, disks=None, ignore_secondaries=False,
5552 """Prepare the block devices for an instance.
5554 This sets up the block devices on all nodes.
5556 @type lu: L{LogicalUnit}
5557 @param lu: the logical unit on whose behalf we execute
5558 @type instance: L{objects.Instance}
5559 @param instance: the instance for whose disks we assemble
5560 @type disks: list of L{objects.Disk} or None
5561 @param disks: which disks to assemble (or all, if None)
5562 @type ignore_secondaries: boolean
5563 @param ignore_secondaries: if true, errors on secondary nodes
5564 won't result in an error return from the function
5565 @type ignore_size: boolean
5566 @param ignore_size: if true, the current known size of the disk
5567 will not be used during the disk activation, useful for cases
5568 when the size is wrong
5569 @return: False if the operation failed, otherwise a list of
5570 (host, instance_visible_name, node_visible_name)
5571 with the mapping from node devices to instance devices
5576 iname = instance.name
5577 disks = _ExpandCheckDisks(instance, disks)
5579 # With the two passes mechanism we try to reduce the window of
5580 # opportunity for the race condition of switching DRBD to primary
5581 # before handshaking occured, but we do not eliminate it
5583 # The proper fix would be to wait (with some limits) until the
5584 # connection has been made and drbd transitions from WFConnection
5585 # into any other network-connected state (Connected, SyncTarget,
5588 # 1st pass, assemble on all nodes in secondary mode
5589 for idx, inst_disk in enumerate(disks):
5590 for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
5592 node_disk = node_disk.Copy()
5593 node_disk.UnsetSize()
5594 lu.cfg.SetDiskID(node_disk, node)
5595 result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False, idx)
5596 msg = result.fail_msg
5598 lu.proc.LogWarning("Could not prepare block device %s on node %s"
5599 " (is_primary=False, pass=1): %s",
5600 inst_disk.iv_name, node, msg)
5601 if not ignore_secondaries:
5604 # FIXME: race condition on drbd migration to primary
5606 # 2nd pass, do only the primary node
5607 for idx, inst_disk in enumerate(disks):
5610 for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
5611 if node != instance.primary_node:
5614 node_disk = node_disk.Copy()
5615 node_disk.UnsetSize()
5616 lu.cfg.SetDiskID(node_disk, node)
5617 result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True, idx)
5618 msg = result.fail_msg
5620 lu.proc.LogWarning("Could not prepare block device %s on node %s"
5621 " (is_primary=True, pass=2): %s",
5622 inst_disk.iv_name, node, msg)
5625 dev_path = result.payload
5627 device_info.append((instance.primary_node, inst_disk.iv_name, dev_path))
5629 # leave the disks configured for the primary node
5630 # this is a workaround that would be fixed better by
5631 # improving the logical/physical id handling
5633 lu.cfg.SetDiskID(disk, instance.primary_node)
5635 return disks_ok, device_info
5638 def _StartInstanceDisks(lu, instance, force):
5639 """Start the disks of an instance.
5642 disks_ok, _ = _AssembleInstanceDisks(lu, instance,
5643 ignore_secondaries=force)
5645 _ShutdownInstanceDisks(lu, instance)
5646 if force is not None and not force:
5647 lu.proc.LogWarning("", hint="If the message above refers to a"
5649 " you can retry the operation using '--force'.")
5650 raise errors.OpExecError("Disk consistency error")
5653 class LUInstanceDeactivateDisks(NoHooksLU):
5654 """Shutdown an instance's disks.
5659 def ExpandNames(self):
5660 self._ExpandAndLockInstance()
5661 self.needed_locks[locking.LEVEL_NODE] = []
5662 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
5664 def DeclareLocks(self, level):
5665 if level == locking.LEVEL_NODE:
5666 self._LockInstancesNodes()
5668 def CheckPrereq(self):
5669 """Check prerequisites.
5671 This checks that the instance is in the cluster.
5674 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5675 assert self.instance is not None, \
5676 "Cannot retrieve locked instance %s" % self.op.instance_name
5678 def Exec(self, feedback_fn):
5679 """Deactivate the disks
5682 instance = self.instance
5684 _ShutdownInstanceDisks(self, instance)
5686 _SafeShutdownInstanceDisks(self, instance)
5689 def _SafeShutdownInstanceDisks(lu, instance, disks=None):
5690 """Shutdown block devices of an instance.
5692 This function checks if an instance is running, before calling
5693 _ShutdownInstanceDisks.
5696 _CheckInstanceDown(lu, instance, "cannot shutdown disks")
5697 _ShutdownInstanceDisks(lu, instance, disks=disks)
5700 def _ExpandCheckDisks(instance, disks):
5701 """Return the instance disks selected by the disks list
5703 @type disks: list of L{objects.Disk} or None
5704 @param disks: selected disks
5705 @rtype: list of L{objects.Disk}
5706 @return: selected instance disks to act on
5710 return instance.disks
5712 if not set(disks).issubset(instance.disks):
5713 raise errors.ProgrammerError("Can only act on disks belonging to the"
5718 def _ShutdownInstanceDisks(lu, instance, disks=None, ignore_primary=False):
5719 """Shutdown block devices of an instance.
5721 This does the shutdown on all nodes of the instance.
5723 If the ignore_primary is false, errors on the primary node are
5728 disks = _ExpandCheckDisks(instance, disks)
5731 for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
5732 lu.cfg.SetDiskID(top_disk, node)
5733 result = lu.rpc.call_blockdev_shutdown(node, top_disk)
5734 msg = result.fail_msg
5736 lu.LogWarning("Could not shutdown block device %s on node %s: %s",
5737 disk.iv_name, node, msg)
5738 if ((node == instance.primary_node and not ignore_primary) or
5739 (node != instance.primary_node and not result.offline)):
5744 def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
5745 """Checks if a node has enough free memory.
5747 This function check if a given node has the needed amount of free
5748 memory. In case the node has less memory or we cannot get the
5749 information from the node, this function raise an OpPrereqError
5752 @type lu: C{LogicalUnit}
5753 @param lu: a logical unit from which we get configuration data
5755 @param node: the node to check
5756 @type reason: C{str}
5757 @param reason: string to use in the error message
5758 @type requested: C{int}
5759 @param requested: the amount of memory in MiB to check for
5760 @type hypervisor_name: C{str}
5761 @param hypervisor_name: the hypervisor to ask for memory stats
5762 @raise errors.OpPrereqError: if the node doesn't have enough memory, or
5763 we cannot check the node
5766 nodeinfo = lu.rpc.call_node_info([node], None, hypervisor_name)
5767 nodeinfo[node].Raise("Can't get data from node %s" % node,
5768 prereq=True, ecode=errors.ECODE_ENVIRON)
5769 free_mem = nodeinfo[node].payload.get("memory_free", None)
5770 if not isinstance(free_mem, int):
5771 raise errors.OpPrereqError("Can't compute free memory on node %s, result"
5772 " was '%s'" % (node, free_mem),
5773 errors.ECODE_ENVIRON)
5774 if requested > free_mem:
5775 raise errors.OpPrereqError("Not enough memory on node %s for %s:"
5776 " needed %s MiB, available %s MiB" %
5777 (node, reason, requested, free_mem),
5781 def _CheckNodesFreeDiskPerVG(lu, nodenames, req_sizes):
5782 """Checks if nodes have enough free disk space in the all VGs.
5784 This function check if all given nodes have the needed amount of
5785 free disk. In case any node has less disk or we cannot get the
5786 information from the node, this function raise an OpPrereqError
5789 @type lu: C{LogicalUnit}
5790 @param lu: a logical unit from which we get configuration data
5791 @type nodenames: C{list}
5792 @param nodenames: the list of node names to check
5793 @type req_sizes: C{dict}
5794 @param req_sizes: the hash of vg and corresponding amount of disk in
5796 @raise errors.OpPrereqError: if the node doesn't have enough disk,
5797 or we cannot check the node
5800 for vg, req_size in req_sizes.items():
5801 _CheckNodesFreeDiskOnVG(lu, nodenames, vg, req_size)
5804 def _CheckNodesFreeDiskOnVG(lu, nodenames, vg, requested):
5805 """Checks if nodes have enough free disk space in the specified VG.
5807 This function check if all given nodes have the needed amount of
5808 free disk. In case any node has less disk or we cannot get the
5809 information from the node, this function raise an OpPrereqError
5812 @type lu: C{LogicalUnit}
5813 @param lu: a logical unit from which we get configuration data
5814 @type nodenames: C{list}
5815 @param nodenames: the list of node names to check
5817 @param vg: the volume group to check
5818 @type requested: C{int}
5819 @param requested: the amount of disk in MiB to check for
5820 @raise errors.OpPrereqError: if the node doesn't have enough disk,
5821 or we cannot check the node
5824 nodeinfo = lu.rpc.call_node_info(nodenames, vg, None)
5825 for node in nodenames:
5826 info = nodeinfo[node]
5827 info.Raise("Cannot get current information from node %s" % node,
5828 prereq=True, ecode=errors.ECODE_ENVIRON)
5829 vg_free = info.payload.get("vg_free", None)
5830 if not isinstance(vg_free, int):
5831 raise errors.OpPrereqError("Can't compute free disk space on node"
5832 " %s for vg %s, result was '%s'" %
5833 (node, vg, vg_free), errors.ECODE_ENVIRON)
5834 if requested > vg_free:
5835 raise errors.OpPrereqError("Not enough disk space on target node %s"
5836 " vg %s: required %d MiB, available %d MiB" %
5837 (node, vg, requested, vg_free),
5841 def _CheckNodesPhysicalCPUs(lu, nodenames, requested, hypervisor_name):
5842 """Checks if nodes have enough physical CPUs
5844 This function checks if all given nodes have the needed number of
5845 physical CPUs. In case any node has less CPUs or we cannot get the
5846 information from the node, this function raises an OpPrereqError
5849 @type lu: C{LogicalUnit}
5850 @param lu: a logical unit from which we get configuration data
5851 @type nodenames: C{list}
5852 @param nodenames: the list of node names to check
5853 @type requested: C{int}
5854 @param requested: the minimum acceptable number of physical CPUs
5855 @raise errors.OpPrereqError: if the node doesn't have enough CPUs,
5856 or we cannot check the node
5859 nodeinfo = lu.rpc.call_node_info(nodenames, None, hypervisor_name)
5860 for node in nodenames:
5861 info = nodeinfo[node]
5862 info.Raise("Cannot get current information from node %s" % node,
5863 prereq=True, ecode=errors.ECODE_ENVIRON)
5864 num_cpus = info.payload.get("cpu_total", None)
5865 if not isinstance(num_cpus, int):
5866 raise errors.OpPrereqError("Can't compute the number of physical CPUs"
5867 " on node %s, result was '%s'" %
5868 (node, num_cpus), errors.ECODE_ENVIRON)
5869 if requested > num_cpus:
5870 raise errors.OpPrereqError("Node %s has %s physical CPUs, but %s are "
5871 "required" % (node, num_cpus, requested),
5875 class LUInstanceStartup(LogicalUnit):
5876 """Starts an instance.
5879 HPATH = "instance-start"
5880 HTYPE = constants.HTYPE_INSTANCE
5883 def CheckArguments(self):
5885 if self.op.beparams:
5886 # fill the beparams dict
5887 utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
5889 def ExpandNames(self):
5890 self._ExpandAndLockInstance()
5892 def BuildHooksEnv(self):
5895 This runs on master, primary and secondary nodes of the instance.
5899 "FORCE": self.op.force,
5902 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
5906 def BuildHooksNodes(self):
5907 """Build hooks nodes.
5910 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
5913 def CheckPrereq(self):
5914 """Check prerequisites.
5916 This checks that the instance is in the cluster.
5919 self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5920 assert self.instance is not None, \
5921 "Cannot retrieve locked instance %s" % self.op.instance_name
5924 if self.op.hvparams:
5925 # check hypervisor parameter syntax (locally)
5926 cluster = self.cfg.GetClusterInfo()
5927 utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
5928 filled_hvp = cluster.FillHV(instance)
5929 filled_hvp.update(self.op.hvparams)
5930 hv_type = hypervisor.GetHypervisor(instance.hypervisor)
5931 hv_type.CheckParameterSyntax(filled_hvp)
5932 _CheckHVParams(self, instance.all_nodes, instance.hypervisor, filled_hvp)
5934 self.primary_offline = self.cfg.GetNodeInfo(instance.primary_node).offline
5936 if self.primary_offline and self.op.ignore_offline_nodes:
5937 self.proc.LogWarning("Ignoring offline primary node")
5939 if self.op.hvparams or self.op.beparams:
5940 self.proc.LogWarning("Overridden parameters are ignored")
5942 _CheckNodeOnline(self, instance.primary_node)
5944 bep = self.cfg.GetClusterInfo().FillBE(instance)
5946 # check bridges existence
5947 _CheckInstanceBridgesExist(self, instance)
5949 remote_info = self.rpc.call_instance_info(instance.primary_node,
5951 instance.hypervisor)
5952 remote_info.Raise("Error checking node %s" % instance.primary_node,
5953 prereq=True, ecode=errors.ECODE_ENVIRON)
5954 if not remote_info.payload: # not running already
5955 _CheckNodeFreeMemory(self, instance.primary_node,
5956 "starting instance %s" % instance.name,
5957 bep[constants.BE_MEMORY], instance.hypervisor)
5959 def Exec(self, feedback_fn):
5960 """Start the instance.
5963 instance = self.instance
5964 force = self.op.force
5966 if not self.op.no_remember:
5967 self.cfg.MarkInstanceUp(instance.name)
5969 if self.primary_offline:
5970 assert self.op.ignore_offline_nodes
5971 self.proc.LogInfo("Primary node offline, marked instance as started")
5973 node_current = instance.primary_node
5975 _StartInstanceDisks(self, instance, force)
5977 result = self.rpc.call_instance_start(node_current, instance,
5978 self.op.hvparams, self.op.beparams,
5979 self.op.startup_paused)
5980 msg = result.fail_msg
5982 _ShutdownInstanceDisks(self, instance)
5983 raise errors.OpExecError("Could not start instance: %s" % msg)
5986 class LUInstanceReboot(LogicalUnit):
5987 """Reboot an instance.
5990 HPATH = "instance-reboot"
5991 HTYPE = constants.HTYPE_INSTANCE
5994 def ExpandNames(self):
5995 self._ExpandAndLockInstance()
5997 def BuildHooksEnv(self):
6000 This runs on master, primary and secondary nodes of the instance.
6004 "IGNORE_SECONDARIES": self.op.ignore_secondaries,
6005 "REBOOT_TYPE": self.op.reboot_type,
6006 "SHUTDOWN_TIMEOUT": self.op.shutdown_timeout,
6009 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
6013 def BuildHooksNodes(self):
6014 """Build hooks nodes.
6017 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
6020 def CheckPrereq(self):
6021 """Check prerequisites.
6023 This checks that the instance is in the cluster.
6026 self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
6027 assert self.instance is not None, \
6028 "Cannot retrieve locked instance %s" % self.op.instance_name
6030 _CheckNodeOnline(self, instance.primary_node)
6032 # check bridges existence
6033 _CheckInstanceBridgesExist(self, instance)
6035 def Exec(self, feedback_fn):
6036 """Reboot the instance.
6039 instance = self.instance
6040 ignore_secondaries = self.op.ignore_secondaries
6041 reboot_type = self.op.reboot_type
6043 remote_info = self.rpc.call_instance_info(instance.primary_node,
6045 instance.hypervisor)
6046 remote_info.Raise("Error checking node %s" % instance.primary_node)
6047 instance_running = bool(remote_info.payload)
6049 node_current = instance.primary_node
6051 if instance_running and reboot_type in [constants.INSTANCE_REBOOT_SOFT,
6052 constants.INSTANCE_REBOOT_HARD]:
6053 for disk in instance.disks:
6054 self.cfg.SetDiskID(disk, node_current)
6055 result = self.rpc.call_instance_reboot(node_current, instance,
6057 self.op.shutdown_timeout)
6058 result.Raise("Could not reboot instance")
6060 if instance_running:
6061 result = self.rpc.call_instance_shutdown(node_current, instance,
6062 self.op.shutdown_timeout)
6063 result.Raise("Could not shutdown instance for full reboot")
6064 _ShutdownInstanceDisks(self, instance)
6066 self.LogInfo("Instance %s was already stopped, starting now",
6068 _StartInstanceDisks(self, instance, ignore_secondaries)
6069 result = self.rpc.call_instance_start(node_current, instance,
6071 msg = result.fail_msg
6073 _ShutdownInstanceDisks(self, instance)
6074 raise errors.OpExecError("Could not start instance for"
6075 " full reboot: %s" % msg)
6077 self.cfg.MarkInstanceUp(instance.name)
6080 class LUInstanceShutdown(LogicalUnit):
6081 """Shutdown an instance.
6084 HPATH = "instance-stop"
6085 HTYPE = constants.HTYPE_INSTANCE
6088 def ExpandNames(self):
6089 self._ExpandAndLockInstance()
6091 def BuildHooksEnv(self):
6094 This runs on master, primary and secondary nodes of the instance.
6097 env = _BuildInstanceHookEnvByObject(self, self.instance)
6098 env["TIMEOUT"] = self.op.timeout
6101 def BuildHooksNodes(self):
6102 """Build hooks nodes.
6105 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
6108 def CheckPrereq(self):
6109 """Check prerequisites.
6111 This checks that the instance is in the cluster.
6114 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
6115 assert self.instance is not None, \
6116 "Cannot retrieve locked instance %s" % self.op.instance_name
6118 self.primary_offline = \
6119 self.cfg.GetNodeInfo(self.instance.primary_node).offline
6121 if self.primary_offline and self.op.ignore_offline_nodes:
6122 self.proc.LogWarning("Ignoring offline primary node")
6124 _CheckNodeOnline(self, self.instance.primary_node)
6126 def Exec(self, feedback_fn):
6127 """Shutdown the instance.
6130 instance = self.instance
6131 node_current = instance.primary_node
6132 timeout = self.op.timeout
6134 if not self.op.no_remember:
6135 self.cfg.MarkInstanceDown(instance.name)
6137 if self.primary_offline:
6138 assert self.op.ignore_offline_nodes
6139 self.proc.LogInfo("Primary node offline, marked instance as stopped")
6141 result = self.rpc.call_instance_shutdown(node_current, instance, timeout)
6142 msg = result.fail_msg
6144 self.proc.LogWarning("Could not shutdown instance: %s" % msg)
6146 _ShutdownInstanceDisks(self, instance)
6149 class LUInstanceReinstall(LogicalUnit):
6150 """Reinstall an instance.
6153 HPATH = "instance-reinstall"
6154 HTYPE = constants.HTYPE_INSTANCE
6157 def ExpandNames(self):
6158 self._ExpandAndLockInstance()
6160 def BuildHooksEnv(self):
6163 This runs on master, primary and secondary nodes of the instance.
6166 return _BuildInstanceHookEnvByObject(self, self.instance)
6168 def BuildHooksNodes(self):
6169 """Build hooks nodes.
6172 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
6175 def CheckPrereq(self):
6176 """Check prerequisites.
6178 This checks that the instance is in the cluster and is not running.
6181 instance = self.cfg.GetInstanceInfo(self.op.instance_name)
6182 assert instance is not None, \
6183 "Cannot retrieve locked instance %s" % self.op.instance_name
6184 _CheckNodeOnline(self, instance.primary_node, "Instance primary node"
6185 " offline, cannot reinstall")
6186 for node in instance.secondary_nodes:
6187 _CheckNodeOnline(self, node, "Instance secondary node offline,"
6188 " cannot reinstall")
6190 if instance.disk_template == constants.DT_DISKLESS:
6191 raise errors.OpPrereqError("Instance '%s' has no disks" %
6192 self.op.instance_name,
6194 _CheckInstanceDown(self, instance, "cannot reinstall")
6196 if self.op.os_type is not None:
6198 pnode = _ExpandNodeName(self.cfg, instance.primary_node)
6199 _CheckNodeHasOS(self, pnode, self.op.os_type, self.op.force_variant)
6200 instance_os = self.op.os_type
6202 instance_os = instance.os
6204 nodelist = list(instance.all_nodes)
6206 if self.op.osparams:
6207 i_osdict = _GetUpdatedParams(instance.osparams, self.op.osparams)
6208 _CheckOSParams(self, True, nodelist, instance_os, i_osdict)
6209 self.os_inst = i_osdict # the new dict (without defaults)
6213 self.instance = instance
6215 def Exec(self, feedback_fn):
6216 """Reinstall the instance.
6219 inst = self.instance
6221 if self.op.os_type is not None:
6222 feedback_fn("Changing OS to '%s'..." % self.op.os_type)
6223 inst.os = self.op.os_type
6224 # Write to configuration
6225 self.cfg.Update(inst, feedback_fn)
6227 _StartInstanceDisks(self, inst, None)
6229 feedback_fn("Running the instance OS create scripts...")
6230 # FIXME: pass debug option from opcode to backend
6231 result = self.rpc.call_instance_os_add(inst.primary_node, inst, True,
6232 self.op.debug_level,
6233 osparams=self.os_inst)
6234 result.Raise("Could not install OS for instance %s on node %s" %
6235 (inst.name, inst.primary_node))
6237 _ShutdownInstanceDisks(self, inst)
6240 class LUInstanceRecreateDisks(LogicalUnit):
6241 """Recreate an instance's missing disks.
6244 HPATH = "instance-recreate-disks"
6245 HTYPE = constants.HTYPE_INSTANCE
6248 def CheckArguments(self):
6249 # normalise the disk list
6250 self.op.disks = sorted(frozenset(self.op.disks))
6252 def ExpandNames(self):
6253 self._ExpandAndLockInstance()
6254 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
6256 self.op.nodes = [_ExpandNodeName(self.cfg, n) for n in self.op.nodes]
6257 self.needed_locks[locking.LEVEL_NODE] = list(self.op.nodes)
6259 self.needed_locks[locking.LEVEL_NODE] = []
6261 def DeclareLocks(self, level):
6262 if level == locking.LEVEL_NODE:
6263 # if we replace the nodes, we only need to lock the old primary,
6264 # otherwise we need to lock all nodes for disk re-creation
6265 primary_only = bool(self.op.nodes)
6266 self._LockInstancesNodes(primary_only=primary_only)
6268 def BuildHooksEnv(self):
6271 This runs on master, primary and secondary nodes of the instance.
6274 return _BuildInstanceHookEnvByObject(self, self.instance)
6276 def BuildHooksNodes(self):
6277 """Build hooks nodes.
6280 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
6283 def CheckPrereq(self):
6284 """Check prerequisites.
6286 This checks that the instance is in the cluster and is not running.
6289 instance = self.cfg.GetInstanceInfo(self.op.instance_name)
6290 assert instance is not None, \
6291 "Cannot retrieve locked instance %s" % self.op.instance_name
6293 if len(self.op.nodes) != len(instance.all_nodes):
6294 raise errors.OpPrereqError("Instance %s currently has %d nodes, but"
6295 " %d replacement nodes were specified" %
6296 (instance.name, len(instance.all_nodes),
6297 len(self.op.nodes)),
6299 assert instance.disk_template != constants.DT_DRBD8 or \
6300 len(self.op.nodes) == 2
6301 assert instance.disk_template != constants.DT_PLAIN or \
6302 len(self.op.nodes) == 1
6303 primary_node = self.op.nodes[0]
6305 primary_node = instance.primary_node
6306 _CheckNodeOnline(self, primary_node)
6308 if instance.disk_template == constants.DT_DISKLESS:
6309 raise errors.OpPrereqError("Instance '%s' has no disks" %
6310 self.op.instance_name, errors.ECODE_INVAL)
6311 # if we replace nodes *and* the old primary is offline, we don't
6313 assert instance.primary_node in self.needed_locks[locking.LEVEL_NODE]
6314 old_pnode = self.cfg.GetNodeInfo(instance.primary_node)
6315 if not (self.op.nodes and old_pnode.offline):
6316 _CheckInstanceDown(self, instance, "cannot recreate disks")
6318 if not self.op.disks:
6319 self.op.disks = range(len(instance.disks))
6321 for idx in self.op.disks:
6322 if idx >= len(instance.disks):
6323 raise errors.OpPrereqError("Invalid disk index '%s'" % idx,
6325 if self.op.disks != range(len(instance.disks)) and self.op.nodes:
6326 raise errors.OpPrereqError("Can't recreate disks partially and"
6327 " change the nodes at the same time",
6329 self.instance = instance
6331 def Exec(self, feedback_fn):
6332 """Recreate the disks.
6335 instance = self.instance
6338 mods = [] # keeps track of needed logical_id changes
6340 for idx, disk in enumerate(instance.disks):
6341 if idx not in self.op.disks: # disk idx has not been passed in
6344 # update secondaries for disks, if needed
6346 if disk.dev_type == constants.LD_DRBD8:
6347 # need to update the nodes and minors
6348 assert len(self.op.nodes) == 2
6349 assert len(disk.logical_id) == 6 # otherwise disk internals
6351 (_, _, old_port, _, _, old_secret) = disk.logical_id
6352 new_minors = self.cfg.AllocateDRBDMinor(self.op.nodes, instance.name)
6353 new_id = (self.op.nodes[0], self.op.nodes[1], old_port,
6354 new_minors[0], new_minors[1], old_secret)
6355 assert len(disk.logical_id) == len(new_id)
6356 mods.append((idx, new_id))
6358 # now that we have passed all asserts above, we can apply the mods
6359 # in a single run (to avoid partial changes)
6360 for idx, new_id in mods:
6361 instance.disks[idx].logical_id = new_id
6363 # change primary node, if needed
6365 instance.primary_node = self.op.nodes[0]
6366 self.LogWarning("Changing the instance's nodes, you will have to"
6367 " remove any disks left on the older nodes manually")
6370 self.cfg.Update(instance, feedback_fn)
6372 _CreateDisks(self, instance, to_skip=to_skip)
6375 class LUInstanceRename(LogicalUnit):
6376 """Rename an instance.
6379 HPATH = "instance-rename"
6380 HTYPE = constants.HTYPE_INSTANCE
6382 def CheckArguments(self):
6386 if self.op.ip_check and not self.op.name_check:
6387 # TODO: make the ip check more flexible and not depend on the name check
6388 raise errors.OpPrereqError("IP address check requires a name check",
6391 def BuildHooksEnv(self):
6394 This runs on master, primary and secondary nodes of the instance.
6397 env = _BuildInstanceHookEnvByObject(self, self.instance)
6398 env["INSTANCE_NEW_NAME"] = self.op.new_name
6401 def BuildHooksNodes(self):
6402 """Build hooks nodes.
6405 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
6408 def CheckPrereq(self):
6409 """Check prerequisites.
6411 This checks that the instance is in the cluster and is not running.
6414 self.op.instance_name = _ExpandInstanceName(self.cfg,
6415 self.op.instance_name)
6416 instance = self.cfg.GetInstanceInfo(self.op.instance_name)
6417 assert instance is not None
6418 _CheckNodeOnline(self, instance.primary_node)
6419 _CheckInstanceDown(self, instance, "cannot rename")
6420 self.instance = instance
6422 new_name = self.op.new_name
6423 if self.op.name_check:
6424 hostname = netutils.GetHostname(name=new_name)
6425 if hostname != new_name:
6426 self.LogInfo("Resolved given name '%s' to '%s'", new_name,
6428 if not utils.MatchNameComponent(self.op.new_name, [hostname.name]):
6429 raise errors.OpPrereqError(("Resolved hostname '%s' does not look the"
6430 " same as given hostname '%s'") %
6431 (hostname.name, self.op.new_name),
6433 new_name = self.op.new_name = hostname.name
6434 if (self.op.ip_check and
6435 netutils.TcpPing(hostname.ip, constants.DEFAULT_NODED_PORT)):
6436 raise errors.OpPrereqError("IP %s of instance %s already in use" %
6437 (hostname.ip, new_name),
6438 errors.ECODE_NOTUNIQUE)
6440 instance_list = self.cfg.GetInstanceList()
6441 if new_name in instance_list and new_name != instance.name:
6442 raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
6443 new_name, errors.ECODE_EXISTS)
6445 def Exec(self, feedback_fn):
6446 """Rename the instance.
6449 inst = self.instance
6450 old_name = inst.name
6452 rename_file_storage = False
6453 if (inst.disk_template in constants.DTS_FILEBASED and
6454 self.op.new_name != inst.name):
6455 old_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
6456 rename_file_storage = True
6458 self.cfg.RenameInstance(inst.name, self.op.new_name)
6459 # Change the instance lock. This is definitely safe while we hold the BGL.
6460 # Otherwise the new lock would have to be added in acquired mode.
6462 self.glm.remove(locking.LEVEL_INSTANCE, old_name)
6463 self.glm.add(locking.LEVEL_INSTANCE, self.op.new_name)
6465 # re-read the instance from the configuration after rename
6466 inst = self.cfg.GetInstanceInfo(self.op.new_name)
6468 if rename_file_storage:
6469 new_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
6470 result = self.rpc.call_file_storage_dir_rename(inst.primary_node,
6471 old_file_storage_dir,
6472 new_file_storage_dir)
6473 result.Raise("Could not rename on node %s directory '%s' to '%s'"
6474 " (but the instance has been renamed in Ganeti)" %
6475 (inst.primary_node, old_file_storage_dir,
6476 new_file_storage_dir))
6478 _StartInstanceDisks(self, inst, None)
6480 result = self.rpc.call_instance_run_rename(inst.primary_node, inst,
6481 old_name, self.op.debug_level)
6482 msg = result.fail_msg
6484 msg = ("Could not run OS rename script for instance %s on node %s"
6485 " (but the instance has been renamed in Ganeti): %s" %
6486 (inst.name, inst.primary_node, msg))
6487 self.proc.LogWarning(msg)
6489 _ShutdownInstanceDisks(self, inst)
6494 class LUInstanceRemove(LogicalUnit):
6495 """Remove an instance.
6498 HPATH = "instance-remove"
6499 HTYPE = constants.HTYPE_INSTANCE
6502 def ExpandNames(self):
6503 self._ExpandAndLockInstance()
6504 self.needed_locks[locking.LEVEL_NODE] = []
6505 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
6507 def DeclareLocks(self, level):
6508 if level == locking.LEVEL_NODE:
6509 self._LockInstancesNodes()
6511 def BuildHooksEnv(self):
6514 This runs on master, primary and secondary nodes of the instance.
6517 env = _BuildInstanceHookEnvByObject(self, self.instance)
6518 env["SHUTDOWN_TIMEOUT"] = self.op.shutdown_timeout
6521 def BuildHooksNodes(self):
6522 """Build hooks nodes.
6525 nl = [self.cfg.GetMasterNode()]
6526 nl_post = list(self.instance.all_nodes) + nl
6527 return (nl, nl_post)
6529 def CheckPrereq(self):
6530 """Check prerequisites.
6532 This checks that the instance is in the cluster.
6535 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
6536 assert self.instance is not None, \
6537 "Cannot retrieve locked instance %s" % self.op.instance_name
6539 def Exec(self, feedback_fn):
6540 """Remove the instance.
6543 instance = self.instance
6544 logging.info("Shutting down instance %s on node %s",
6545 instance.name, instance.primary_node)
6547 result = self.rpc.call_instance_shutdown(instance.primary_node, instance,
6548 self.op.shutdown_timeout)
6549 msg = result.fail_msg
6551 if self.op.ignore_failures:
6552 feedback_fn("Warning: can't shutdown instance: %s" % msg)
6554 raise errors.OpExecError("Could not shutdown instance %s on"
6556 (instance.name, instance.primary_node, msg))
6558 _RemoveInstance(self, feedback_fn, instance, self.op.ignore_failures)
6561 def _RemoveInstance(lu, feedback_fn, instance, ignore_failures):
6562 """Utility function to remove an instance.
6565 logging.info("Removing block devices for instance %s", instance.name)
6567 if not _RemoveDisks(lu, instance):
6568 if not ignore_failures:
6569 raise errors.OpExecError("Can't remove instance's disks")
6570 feedback_fn("Warning: can't remove instance's disks")
6572 logging.info("Removing instance %s out of cluster config", instance.name)
6574 lu.cfg.RemoveInstance(instance.name)
6576 assert not lu.remove_locks.get(locking.LEVEL_INSTANCE), \
6577 "Instance lock removal conflict"
6579 # Remove lock for the instance
6580 lu.remove_locks[locking.LEVEL_INSTANCE] = instance.name
6583 class LUInstanceQuery(NoHooksLU):
6584 """Logical unit for querying instances.
6587 # pylint: disable=W0142
6590 def CheckArguments(self):
6591 self.iq = _InstanceQuery(qlang.MakeSimpleFilter("name", self.op.names),
6592 self.op.output_fields, self.op.use_locking)
6594 def ExpandNames(self):
6595 self.iq.ExpandNames(self)
6597 def DeclareLocks(self, level):
6598 self.iq.DeclareLocks(self, level)
6600 def Exec(self, feedback_fn):
6601 return self.iq.OldStyleQuery(self)
6604 class LUInstanceFailover(LogicalUnit):
6605 """Failover an instance.
6608 HPATH = "instance-failover"
6609 HTYPE = constants.HTYPE_INSTANCE
6612 def CheckArguments(self):
6613 """Check the arguments.
6616 self.iallocator = getattr(self.op, "iallocator", None)
6617 self.target_node = getattr(self.op, "target_node", None)
6619 def ExpandNames(self):
6620 self._ExpandAndLockInstance()
6622 if self.op.target_node is not None:
6623 self.op.target_node = _ExpandNodeName(self.cfg, self.op.target_node)
6625 self.needed_locks[locking.LEVEL_NODE] = []
6626 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
6628 ignore_consistency = self.op.ignore_consistency
6629 shutdown_timeout = self.op.shutdown_timeout
6630 self._migrater = TLMigrateInstance(self, self.op.instance_name,
6633 ignore_consistency=ignore_consistency,
6634 shutdown_timeout=shutdown_timeout)
6635 self.tasklets = [self._migrater]
6637 def DeclareLocks(self, level):
6638 if level == locking.LEVEL_NODE:
6639 instance = self.context.cfg.GetInstanceInfo(self.op.instance_name)
6640 if instance.disk_template in constants.DTS_EXT_MIRROR:
6641 if self.op.target_node is None:
6642 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
6644 self.needed_locks[locking.LEVEL_NODE] = [instance.primary_node,
6645 self.op.target_node]
6646 del self.recalculate_locks[locking.LEVEL_NODE]
6648 self._LockInstancesNodes()
6650 def BuildHooksEnv(self):
6653 This runs on master, primary and secondary nodes of the instance.
6656 instance = self._migrater.instance
6657 source_node = instance.primary_node
6658 target_node = self.op.target_node
6660 "IGNORE_CONSISTENCY": self.op.ignore_consistency,
6661 "SHUTDOWN_TIMEOUT": self.op.shutdown_timeout,
6662 "OLD_PRIMARY": source_node,
6663 "NEW_PRIMARY": target_node,
6666 if instance.disk_template in constants.DTS_INT_MIRROR:
6667 env["OLD_SECONDARY"] = instance.secondary_nodes[0]
6668 env["NEW_SECONDARY"] = source_node
6670 env["OLD_SECONDARY"] = env["NEW_SECONDARY"] = ""
6672 env.update(_BuildInstanceHookEnvByObject(self, instance))
6676 def BuildHooksNodes(self):
6677 """Build hooks nodes.
6680 instance = self._migrater.instance
6681 nl = [self.cfg.GetMasterNode()] + list(instance.secondary_nodes)
6682 return (nl, nl + [instance.primary_node])
6685 class LUInstanceMigrate(LogicalUnit):
6686 """Migrate an instance.
6688 This is migration without shutting down, compared to the failover,
6689 which is done with shutdown.
6692 HPATH = "instance-migrate"
6693 HTYPE = constants.HTYPE_INSTANCE
6696 def ExpandNames(self):
6697 self._ExpandAndLockInstance()
6699 if self.op.target_node is not None:
6700 self.op.target_node = _ExpandNodeName(self.cfg, self.op.target_node)
6702 self.needed_locks[locking.LEVEL_NODE] = []
6703 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
6705 self._migrater = TLMigrateInstance(self, self.op.instance_name,
6706 cleanup=self.op.cleanup,
6708 fallback=self.op.allow_failover)
6709 self.tasklets = [self._migrater]
6711 def DeclareLocks(self, level):
6712 if level == locking.LEVEL_NODE:
6713 instance = self.context.cfg.GetInstanceInfo(self.op.instance_name)
6714 if instance.disk_template in constants.DTS_EXT_MIRROR:
6715 if self.op.target_node is None:
6716 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
6718 self.needed_locks[locking.LEVEL_NODE] = [instance.primary_node,
6719 self.op.target_node]
6720 del self.recalculate_locks[locking.LEVEL_NODE]
6722 self._LockInstancesNodes()
6724 def BuildHooksEnv(self):
6727 This runs on master, primary and secondary nodes of the instance.
6730 instance = self._migrater.instance
6731 source_node = instance.primary_node
6732 target_node = self.op.target_node
6733 env = _BuildInstanceHookEnvByObject(self, instance)
6735 "MIGRATE_LIVE": self._migrater.live,
6736 "MIGRATE_CLEANUP": self.op.cleanup,
6737 "OLD_PRIMARY": source_node,
6738 "NEW_PRIMARY": target_node,
6741 if instance.disk_template in constants.DTS_INT_MIRROR:
6742 env["OLD_SECONDARY"] = target_node
6743 env["NEW_SECONDARY"] = source_node
6745 env["OLD_SECONDARY"] = env["NEW_SECONDARY"] = None
6749 def BuildHooksNodes(self):
6750 """Build hooks nodes.
6753 instance = self._migrater.instance
6754 nl = [self.cfg.GetMasterNode()] + list(instance.secondary_nodes)
6755 return (nl, nl + [instance.primary_node])
6758 class LUInstanceMove(LogicalUnit):
6759 """Move an instance by data-copying.
6762 HPATH = "instance-move"
6763 HTYPE = constants.HTYPE_INSTANCE
6766 def ExpandNames(self):
6767 self._ExpandAndLockInstance()
6768 target_node = _ExpandNodeName(self.cfg, self.op.target_node)
6769 self.op.target_node = target_node
6770 self.needed_locks[locking.LEVEL_NODE] = [target_node]
6771 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
6773 def DeclareLocks(self, level):
6774 if level == locking.LEVEL_NODE:
6775 self._LockInstancesNodes(primary_only=True)
6777 def BuildHooksEnv(self):
6780 This runs on master, primary and secondary nodes of the instance.
6784 "TARGET_NODE": self.op.target_node,
6785 "SHUTDOWN_TIMEOUT": self.op.shutdown_timeout,
6787 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
6790 def BuildHooksNodes(self):
6791 """Build hooks nodes.
6795 self.cfg.GetMasterNode(),
6796 self.instance.primary_node,
6797 self.op.target_node,
6801 def CheckPrereq(self):
6802 """Check prerequisites.
6804 This checks that the instance is in the cluster.
6807 self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
6808 assert self.instance is not None, \
6809 "Cannot retrieve locked instance %s" % self.op.instance_name
6811 node = self.cfg.GetNodeInfo(self.op.target_node)
6812 assert node is not None, \
6813 "Cannot retrieve locked node %s" % self.op.target_node
6815 self.target_node = target_node = node.name
6817 if target_node == instance.primary_node:
6818 raise errors.OpPrereqError("Instance %s is already on the node %s" %
6819 (instance.name, target_node),
6822 bep = self.cfg.GetClusterInfo().FillBE(instance)
6824 for idx, dsk in enumerate(instance.disks):
6825 if dsk.dev_type not in (constants.LD_LV, constants.LD_FILE):
6826 raise errors.OpPrereqError("Instance disk %d has a complex layout,"
6827 " cannot copy" % idx, errors.ECODE_STATE)
6829 _CheckNodeOnline(self, target_node)
6830 _CheckNodeNotDrained(self, target_node)
6831 _CheckNodeVmCapable(self, target_node)
6833 if instance.admin_up:
6834 # check memory requirements on the secondary node
6835 _CheckNodeFreeMemory(self, target_node, "failing over instance %s" %
6836 instance.name, bep[constants.BE_MEMORY],
6837 instance.hypervisor)
6839 self.LogInfo("Not checking memory on the secondary node as"
6840 " instance will not be started")
6842 # check bridge existance
6843 _CheckInstanceBridgesExist(self, instance, node=target_node)
6845 def Exec(self, feedback_fn):
6846 """Move an instance.
6848 The move is done by shutting it down on its present node, copying
6849 the data over (slow) and starting it on the new node.
6852 instance = self.instance
6854 source_node = instance.primary_node
6855 target_node = self.target_node
6857 self.LogInfo("Shutting down instance %s on source node %s",
6858 instance.name, source_node)
6860 result = self.rpc.call_instance_shutdown(source_node, instance,
6861 self.op.shutdown_timeout)
6862 msg = result.fail_msg
6864 if self.op.ignore_consistency:
6865 self.proc.LogWarning("Could not shutdown instance %s on node %s."
6866 " Proceeding anyway. Please make sure node"
6867 " %s is down. Error details: %s",
6868 instance.name, source_node, source_node, msg)
6870 raise errors.OpExecError("Could not shutdown instance %s on"
6872 (instance.name, source_node, msg))
6874 # create the target disks
6876 _CreateDisks(self, instance, target_node=target_node)
6877 except errors.OpExecError:
6878 self.LogWarning("Device creation failed, reverting...")
6880 _RemoveDisks(self, instance, target_node=target_node)
6882 self.cfg.ReleaseDRBDMinors(instance.name)
6885 cluster_name = self.cfg.GetClusterInfo().cluster_name
6888 # activate, get path, copy the data over
6889 for idx, disk in enumerate(instance.disks):
6890 self.LogInfo("Copying data for disk %d", idx)
6891 result = self.rpc.call_blockdev_assemble(target_node, disk,
6892 instance.name, True, idx)
6894 self.LogWarning("Can't assemble newly created disk %d: %s",
6895 idx, result.fail_msg)
6896 errs.append(result.fail_msg)
6898 dev_path = result.payload
6899 result = self.rpc.call_blockdev_export(source_node, disk,
6900 target_node, dev_path,
6903 self.LogWarning("Can't copy data over for disk %d: %s",
6904 idx, result.fail_msg)
6905 errs.append(result.fail_msg)
6909 self.LogWarning("Some disks failed to copy, aborting")
6911 _RemoveDisks(self, instance, target_node=target_node)
6913 self.cfg.ReleaseDRBDMinors(instance.name)
6914 raise errors.OpExecError("Errors during disk copy: %s" %
6917 instance.primary_node = target_node
6918 self.cfg.Update(instance, feedback_fn)
6920 self.LogInfo("Removing the disks on the original node")
6921 _RemoveDisks(self, instance, target_node=source_node)
6923 # Only start the instance if it's marked as up
6924 if instance.admin_up:
6925 self.LogInfo("Starting instance %s on node %s",
6926 instance.name, target_node)
6928 disks_ok, _ = _AssembleInstanceDisks(self, instance,
6929 ignore_secondaries=True)
6931 _ShutdownInstanceDisks(self, instance)
6932 raise errors.OpExecError("Can't activate the instance's disks")
6934 result = self.rpc.call_instance_start(target_node, instance,
6936 msg = result.fail_msg
6938 _ShutdownInstanceDisks(self, instance)
6939 raise errors.OpExecError("Could not start instance %s on node %s: %s" %
6940 (instance.name, target_node, msg))
6943 class LUNodeMigrate(LogicalUnit):
6944 """Migrate all instances from a node.
6947 HPATH = "node-migrate"
6948 HTYPE = constants.HTYPE_NODE
6951 def CheckArguments(self):
6954 def ExpandNames(self):
6955 self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
6957 self.share_locks = _ShareAll()
6958 self.needed_locks = {
6959 locking.LEVEL_NODE: [self.op.node_name],
6962 def BuildHooksEnv(self):
6965 This runs on the master, the primary and all the secondaries.
6969 "NODE_NAME": self.op.node_name,
6972 def BuildHooksNodes(self):
6973 """Build hooks nodes.
6976 nl = [self.cfg.GetMasterNode()]
6979 def CheckPrereq(self):
6982 def Exec(self, feedback_fn):
6983 # Prepare jobs for migration instances
6985 [opcodes.OpInstanceMigrate(instance_name=inst.name,
6988 iallocator=self.op.iallocator,
6989 target_node=self.op.target_node)]
6990 for inst in _GetNodePrimaryInstances(self.cfg, self.op.node_name)
6993 # TODO: Run iallocator in this opcode and pass correct placement options to
6994 # OpInstanceMigrate. Since other jobs can modify the cluster between
6995 # running the iallocator and the actual migration, a good consistency model
6996 # will have to be found.
6998 assert (frozenset(self.owned_locks(locking.LEVEL_NODE)) ==
6999 frozenset([self.op.node_name]))
7001 return ResultWithJobs(jobs)
7004 class TLMigrateInstance(Tasklet):
7005 """Tasklet class for instance migration.
7008 @ivar live: whether the migration will be done live or non-live;
7009 this variable is initalized only after CheckPrereq has run
7010 @type cleanup: boolean
7011 @ivar cleanup: Wheater we cleanup from a failed migration
7012 @type iallocator: string
7013 @ivar iallocator: The iallocator used to determine target_node
7014 @type target_node: string
7015 @ivar target_node: If given, the target_node to reallocate the instance to
7016 @type failover: boolean
7017 @ivar failover: Whether operation results in failover or migration
7018 @type fallback: boolean
7019 @ivar fallback: Whether fallback to failover is allowed if migration not
7021 @type ignore_consistency: boolean
7022 @ivar ignore_consistency: Wheter we should ignore consistency between source
7024 @type shutdown_timeout: int
7025 @ivar shutdown_timeout: In case of failover timeout of the shutdown
7028 def __init__(self, lu, instance_name, cleanup=False,
7029 failover=False, fallback=False,
7030 ignore_consistency=False,
7031 shutdown_timeout=constants.DEFAULT_SHUTDOWN_TIMEOUT):
7032 """Initializes this class.
7035 Tasklet.__init__(self, lu)
7038 self.instance_name = instance_name
7039 self.cleanup = cleanup
7040 self.live = False # will be overridden later
7041 self.failover = failover
7042 self.fallback = fallback
7043 self.ignore_consistency = ignore_consistency
7044 self.shutdown_timeout = shutdown_timeout
7046 def CheckPrereq(self):
7047 """Check prerequisites.
7049 This checks that the instance is in the cluster.
7052 instance_name = _ExpandInstanceName(self.lu.cfg, self.instance_name)
7053 instance = self.cfg.GetInstanceInfo(instance_name)
7054 assert instance is not None
7055 self.instance = instance
7057 if (not self.cleanup and not instance.admin_up and not self.failover and
7059 self.lu.LogInfo("Instance is marked down, fallback allowed, switching"
7061 self.failover = True
7063 if instance.disk_template not in constants.DTS_MIRRORED:
7068 raise errors.OpPrereqError("Instance's disk layout '%s' does not allow"
7069 " %s" % (instance.disk_template, text),
7072 if instance.disk_template in constants.DTS_EXT_MIRROR:
7073 _CheckIAllocatorOrNode(self.lu, "iallocator", "target_node")
7075 if self.lu.op.iallocator:
7076 self._RunAllocator()
7078 # We set set self.target_node as it is required by
7080 self.target_node = self.lu.op.target_node
7082 # self.target_node is already populated, either directly or by the
7084 target_node = self.target_node
7085 if self.target_node == instance.primary_node:
7086 raise errors.OpPrereqError("Cannot migrate instance %s"
7087 " to its primary (%s)" %
7088 (instance.name, instance.primary_node))
7090 if len(self.lu.tasklets) == 1:
7091 # It is safe to release locks only when we're the only tasklet
7093 _ReleaseLocks(self.lu, locking.LEVEL_NODE,
7094 keep=[instance.primary_node, self.target_node])
7097 secondary_nodes = instance.secondary_nodes
7098 if not secondary_nodes:
7099 raise errors.ConfigurationError("No secondary node but using"
7100 " %s disk template" %
7101 instance.disk_template)
7102 target_node = secondary_nodes[0]
7103 if self.lu.op.iallocator or (self.lu.op.target_node and
7104 self.lu.op.target_node != target_node):
7106 text = "failed over"
7109 raise errors.OpPrereqError("Instances with disk template %s cannot"
7110 " be %s to arbitrary nodes"
7111 " (neither an iallocator nor a target"
7112 " node can be passed)" %
7113 (instance.disk_template, text),
7116 i_be = self.cfg.GetClusterInfo().FillBE(instance)
7118 # check memory requirements on the secondary node
7119 if not self.failover or instance.admin_up:
7120 _CheckNodeFreeMemory(self.lu, target_node, "migrating instance %s" %
7121 instance.name, i_be[constants.BE_MEMORY],
7122 instance.hypervisor)
7124 self.lu.LogInfo("Not checking memory on the secondary node as"
7125 " instance will not be started")
7127 # check bridge existance
7128 _CheckInstanceBridgesExist(self.lu, instance, node=target_node)
7130 if not self.cleanup:
7131 _CheckNodeNotDrained(self.lu, target_node)
7132 if not self.failover:
7133 result = self.rpc.call_instance_migratable(instance.primary_node,
7135 if result.fail_msg and self.fallback:
7136 self.lu.LogInfo("Can't migrate, instance offline, fallback to"
7138 self.failover = True
7140 result.Raise("Can't migrate, please use failover",
7141 prereq=True, ecode=errors.ECODE_STATE)
7143 assert not (self.failover and self.cleanup)
7145 if not self.failover:
7146 if self.lu.op.live is not None and self.lu.op.mode is not None:
7147 raise errors.OpPrereqError("Only one of the 'live' and 'mode'"
7148 " parameters are accepted",
7150 if self.lu.op.live is not None:
7152 self.lu.op.mode = constants.HT_MIGRATION_LIVE
7154 self.lu.op.mode = constants.HT_MIGRATION_NONLIVE
7155 # reset the 'live' parameter to None so that repeated
7156 # invocations of CheckPrereq do not raise an exception
7157 self.lu.op.live = None
7158 elif self.lu.op.mode is None:
7159 # read the default value from the hypervisor
7160 i_hv = self.cfg.GetClusterInfo().FillHV(self.instance,
7162 self.lu.op.mode = i_hv[constants.HV_MIGRATION_MODE]
7164 self.live = self.lu.op.mode == constants.HT_MIGRATION_LIVE
7166 # Failover is never live
7169 def _RunAllocator(self):
7170 """Run the allocator based on input opcode.
7173 ial = IAllocator(self.cfg, self.rpc,
7174 mode=constants.IALLOCATOR_MODE_RELOC,
7175 name=self.instance_name,
7176 # TODO See why hail breaks with a single node below
7177 relocate_from=[self.instance.primary_node,
7178 self.instance.primary_node],
7181 ial.Run(self.lu.op.iallocator)
7184 raise errors.OpPrereqError("Can't compute nodes using"
7185 " iallocator '%s': %s" %
7186 (self.lu.op.iallocator, ial.info),
7188 if len(ial.result) != ial.required_nodes:
7189 raise errors.OpPrereqError("iallocator '%s' returned invalid number"
7190 " of nodes (%s), required %s" %
7191 (self.lu.op.iallocator, len(ial.result),
7192 ial.required_nodes), errors.ECODE_FAULT)
7193 self.target_node = ial.result[0]
7194 self.lu.LogInfo("Selected nodes for instance %s via iallocator %s: %s",
7195 self.instance_name, self.lu.op.iallocator,
7196 utils.CommaJoin(ial.result))
7198 def _WaitUntilSync(self):
7199 """Poll with custom rpc for disk sync.
7201 This uses our own step-based rpc call.
7204 self.feedback_fn("* wait until resync is done")
7208 result = self.rpc.call_drbd_wait_sync(self.all_nodes,
7210 self.instance.disks)
7212 for node, nres in result.items():
7213 nres.Raise("Cannot resync disks on node %s" % node)
7214 node_done, node_percent = nres.payload
7215 all_done = all_done and node_done
7216 if node_percent is not None:
7217 min_percent = min(min_percent, node_percent)
7219 if min_percent < 100:
7220 self.feedback_fn(" - progress: %.1f%%" % min_percent)
7223 def _EnsureSecondary(self, node):
7224 """Demote a node to secondary.
7227 self.feedback_fn("* switching node %s to secondary mode" % node)
7229 for dev in self.instance.disks:
7230 self.cfg.SetDiskID(dev, node)
7232 result = self.rpc.call_blockdev_close(node, self.instance.name,
7233 self.instance.disks)
7234 result.Raise("Cannot change disk to secondary on node %s" % node)
7236 def _GoStandalone(self):
7237 """Disconnect from the network.
7240 self.feedback_fn("* changing into standalone mode")
7241 result = self.rpc.call_drbd_disconnect_net(self.all_nodes, self.nodes_ip,
7242 self.instance.disks)
7243 for node, nres in result.items():
7244 nres.Raise("Cannot disconnect disks node %s" % node)
7246 def _GoReconnect(self, multimaster):
7247 """Reconnect to the network.
7253 msg = "single-master"
7254 self.feedback_fn("* changing disks into %s mode" % msg)
7255 result = self.rpc.call_drbd_attach_net(self.all_nodes, self.nodes_ip,
7256 self.instance.disks,
7257 self.instance.name, multimaster)
7258 for node, nres in result.items():
7259 nres.Raise("Cannot change disks config on node %s" % node)
7261 def _ExecCleanup(self):
7262 """Try to cleanup after a failed migration.
7264 The cleanup is done by:
7265 - check that the instance is running only on one node
7266 (and update the config if needed)
7267 - change disks on its secondary node to secondary
7268 - wait until disks are fully synchronized
7269 - disconnect from the network
7270 - change disks into single-master mode
7271 - wait again until disks are fully synchronized
7274 instance = self.instance
7275 target_node = self.target_node
7276 source_node = self.source_node
7278 # check running on only one node
7279 self.feedback_fn("* checking where the instance actually runs"
7280 " (if this hangs, the hypervisor might be in"
7282 ins_l = self.rpc.call_instance_list(self.all_nodes, [instance.hypervisor])
7283 for node, result in ins_l.items():
7284 result.Raise("Can't contact node %s" % node)
7286 runningon_source = instance.name in ins_l[source_node].payload
7287 runningon_target = instance.name in ins_l[target_node].payload
7289 if runningon_source and runningon_target:
7290 raise errors.OpExecError("Instance seems to be running on two nodes,"
7291 " or the hypervisor is confused; you will have"
7292 " to ensure manually that it runs only on one"
7293 " and restart this operation")
7295 if not (runningon_source or runningon_target):
7296 raise errors.OpExecError("Instance does not seem to be running at all;"
7297 " in this case it's safer to repair by"
7298 " running 'gnt-instance stop' to ensure disk"
7299 " shutdown, and then restarting it")
7301 if runningon_target:
7302 # the migration has actually succeeded, we need to update the config
7303 self.feedback_fn("* instance running on secondary node (%s),"
7304 " updating config" % target_node)
7305 instance.primary_node = target_node
7306 self.cfg.Update(instance, self.feedback_fn)
7307 demoted_node = source_node
7309 self.feedback_fn("* instance confirmed to be running on its"
7310 " primary node (%s)" % source_node)
7311 demoted_node = target_node
7313 if instance.disk_template in constants.DTS_INT_MIRROR:
7314 self._EnsureSecondary(demoted_node)
7316 self._WaitUntilSync()
7317 except errors.OpExecError:
7318 # we ignore here errors, since if the device is standalone, it
7319 # won't be able to sync
7321 self._GoStandalone()
7322 self._GoReconnect(False)
7323 self._WaitUntilSync()
7325 self.feedback_fn("* done")
7327 def _RevertDiskStatus(self):
7328 """Try to revert the disk status after a failed migration.
7331 target_node = self.target_node
7332 if self.instance.disk_template in constants.DTS_EXT_MIRROR:
7336 self._EnsureSecondary(target_node)
7337 self._GoStandalone()
7338 self._GoReconnect(False)
7339 self._WaitUntilSync()
7340 except errors.OpExecError, err:
7341 self.lu.LogWarning("Migration failed and I can't reconnect the drives,"
7342 " please try to recover the instance manually;"
7343 " error '%s'" % str(err))
7345 def _AbortMigration(self):
7346 """Call the hypervisor code to abort a started migration.
7349 instance = self.instance
7350 target_node = self.target_node
7351 migration_info = self.migration_info
7353 abort_result = self.rpc.call_finalize_migration(target_node,
7357 abort_msg = abort_result.fail_msg
7359 logging.error("Aborting migration failed on target node %s: %s",
7360 target_node, abort_msg)
7361 # Don't raise an exception here, as we stil have to try to revert the
7362 # disk status, even if this step failed.
7364 def _ExecMigration(self):
7365 """Migrate an instance.
7367 The migrate is done by:
7368 - change the disks into dual-master mode
7369 - wait until disks are fully synchronized again
7370 - migrate the instance
7371 - change disks on the new secondary node (the old primary) to secondary
7372 - wait until disks are fully synchronized
7373 - change disks into single-master mode
7376 instance = self.instance
7377 target_node = self.target_node
7378 source_node = self.source_node
7380 self.feedback_fn("* checking disk consistency between source and target")
7381 for dev in instance.disks:
7382 if not _CheckDiskConsistency(self.lu, dev, target_node, False):
7383 raise errors.OpExecError("Disk %s is degraded or not fully"
7384 " synchronized on target node,"
7385 " aborting migration" % dev.iv_name)
7387 # First get the migration information from the remote node
7388 result = self.rpc.call_migration_info(source_node, instance)
7389 msg = result.fail_msg
7391 log_err = ("Failed fetching source migration information from %s: %s" %
7393 logging.error(log_err)
7394 raise errors.OpExecError(log_err)
7396 self.migration_info = migration_info = result.payload
7398 if self.instance.disk_template not in constants.DTS_EXT_MIRROR:
7399 # Then switch the disks to master/master mode
7400 self._EnsureSecondary(target_node)
7401 self._GoStandalone()
7402 self._GoReconnect(True)
7403 self._WaitUntilSync()
7405 self.feedback_fn("* preparing %s to accept the instance" % target_node)
7406 result = self.rpc.call_accept_instance(target_node,
7409 self.nodes_ip[target_node])
7411 msg = result.fail_msg
7413 logging.error("Instance pre-migration failed, trying to revert"
7414 " disk status: %s", msg)
7415 self.feedback_fn("Pre-migration failed, aborting")
7416 self._AbortMigration()
7417 self._RevertDiskStatus()
7418 raise errors.OpExecError("Could not pre-migrate instance %s: %s" %
7419 (instance.name, msg))
7421 self.feedback_fn("* migrating instance to %s" % target_node)
7422 result = self.rpc.call_instance_migrate(source_node, instance,
7423 self.nodes_ip[target_node],
7425 msg = result.fail_msg
7427 logging.error("Instance migration failed, trying to revert"
7428 " disk status: %s", msg)
7429 self.feedback_fn("Migration failed, aborting")
7430 self._AbortMigration()
7431 self._RevertDiskStatus()
7432 raise errors.OpExecError("Could not migrate instance %s: %s" %
7433 (instance.name, msg))
7435 instance.primary_node = target_node
7436 # distribute new instance config to the other nodes
7437 self.cfg.Update(instance, self.feedback_fn)
7439 result = self.rpc.call_finalize_migration(target_node,
7443 msg = result.fail_msg
7445 logging.error("Instance migration succeeded, but finalization failed:"
7447 raise errors.OpExecError("Could not finalize instance migration: %s" %
7450 if self.instance.disk_template not in constants.DTS_EXT_MIRROR:
7451 self._EnsureSecondary(source_node)
7452 self._WaitUntilSync()
7453 self._GoStandalone()
7454 self._GoReconnect(False)
7455 self._WaitUntilSync()
7457 self.feedback_fn("* done")
7459 def _ExecFailover(self):
7460 """Failover an instance.
7462 The failover is done by shutting it down on its present node and
7463 starting it on the secondary.
7466 instance = self.instance
7467 primary_node = self.cfg.GetNodeInfo(instance.primary_node)
7469 source_node = instance.primary_node
7470 target_node = self.target_node
7472 if instance.admin_up:
7473 self.feedback_fn("* checking disk consistency between source and target")
7474 for dev in instance.disks:
7475 # for drbd, these are drbd over lvm
7476 if not _CheckDiskConsistency(self.lu, dev, target_node, False):
7477 if primary_node.offline:
7478 self.feedback_fn("Node %s is offline, ignoring degraded disk %s on"
7480 (primary_node.name, dev.iv_name, target_node))
7481 elif not self.ignore_consistency:
7482 raise errors.OpExecError("Disk %s is degraded on target node,"
7483 " aborting failover" % dev.iv_name)
7485 self.feedback_fn("* not checking disk consistency as instance is not"
7488 self.feedback_fn("* shutting down instance on source node")
7489 logging.info("Shutting down instance %s on node %s",
7490 instance.name, source_node)
7492 result = self.rpc.call_instance_shutdown(source_node, instance,
7493 self.shutdown_timeout)
7494 msg = result.fail_msg
7496 if self.ignore_consistency or primary_node.offline:
7497 self.lu.LogWarning("Could not shutdown instance %s on node %s,"
7498 " proceeding anyway; please make sure node"
7499 " %s is down; error details: %s",
7500 instance.name, source_node, source_node, msg)
7502 raise errors.OpExecError("Could not shutdown instance %s on"
7504 (instance.name, source_node, msg))
7506 self.feedback_fn("* deactivating the instance's disks on source node")
7507 if not _ShutdownInstanceDisks(self.lu, instance, ignore_primary=True):
7508 raise errors.OpExecError("Can't shut down the instance's disks")
7510 instance.primary_node = target_node
7511 # distribute new instance config to the other nodes
7512 self.cfg.Update(instance, self.feedback_fn)
7514 # Only start the instance if it's marked as up
7515 if instance.admin_up:
7516 self.feedback_fn("* activating the instance's disks on target node %s" %
7518 logging.info("Starting instance %s on node %s",
7519 instance.name, target_node)
7521 disks_ok, _ = _AssembleInstanceDisks(self.lu, instance,
7522 ignore_secondaries=True)
7524 _ShutdownInstanceDisks(self.lu, instance)
7525 raise errors.OpExecError("Can't activate the instance's disks")
7527 self.feedback_fn("* starting the instance on the target node %s" %
7529 result = self.rpc.call_instance_start(target_node, instance, None, None,
7531 msg = result.fail_msg
7533 _ShutdownInstanceDisks(self.lu, instance)
7534 raise errors.OpExecError("Could not start instance %s on node %s: %s" %
7535 (instance.name, target_node, msg))
7537 def Exec(self, feedback_fn):
7538 """Perform the migration.
7541 self.feedback_fn = feedback_fn
7542 self.source_node = self.instance.primary_node
7544 # FIXME: if we implement migrate-to-any in DRBD, this needs fixing
7545 if self.instance.disk_template in constants.DTS_INT_MIRROR:
7546 self.target_node = self.instance.secondary_nodes[0]
7547 # Otherwise self.target_node has been populated either
7548 # directly, or through an iallocator.
7550 self.all_nodes = [self.source_node, self.target_node]
7551 self.nodes_ip = dict((name, node.secondary_ip) for (name, node)
7552 in self.cfg.GetMultiNodeInfo(self.all_nodes))
7555 feedback_fn("Failover instance %s" % self.instance.name)
7556 self._ExecFailover()
7558 feedback_fn("Migrating instance %s" % self.instance.name)
7561 return self._ExecCleanup()
7563 return self._ExecMigration()
7566 def _CreateBlockDev(lu, node, instance, device, force_create,
7568 """Create a tree of block devices on a given node.
7570 If this device type has to be created on secondaries, create it and
7573 If not, just recurse to children keeping the same 'force' value.
7575 @param lu: the lu on whose behalf we execute
7576 @param node: the node on which to create the device
7577 @type instance: L{objects.Instance}
7578 @param instance: the instance which owns the device
7579 @type device: L{objects.Disk}
7580 @param device: the device to create
7581 @type force_create: boolean
7582 @param force_create: whether to force creation of this device; this
7583 will be change to True whenever we find a device which has
7584 CreateOnSecondary() attribute
7585 @param info: the extra 'metadata' we should attach to the device
7586 (this will be represented as a LVM tag)
7587 @type force_open: boolean
7588 @param force_open: this parameter will be passes to the
7589 L{backend.BlockdevCreate} function where it specifies
7590 whether we run on primary or not, and it affects both
7591 the child assembly and the device own Open() execution
7594 if device.CreateOnSecondary():
7598 for child in device.children:
7599 _CreateBlockDev(lu, node, instance, child, force_create,
7602 if not force_create:
7605 _CreateSingleBlockDev(lu, node, instance, device, info, force_open)
7608 def _CreateSingleBlockDev(lu, node, instance, device, info, force_open):
7609 """Create a single block device on a given node.
7611 This will not recurse over children of the device, so they must be
7614 @param lu: the lu on whose behalf we execute
7615 @param node: the node on which to create the device
7616 @type instance: L{objects.Instance}
7617 @param instance: the instance which owns the device
7618 @type device: L{objects.Disk}
7619 @param device: the device to create
7620 @param info: the extra 'metadata' we should attach to the device
7621 (this will be represented as a LVM tag)
7622 @type force_open: boolean
7623 @param force_open: this parameter will be passes to the
7624 L{backend.BlockdevCreate} function where it specifies
7625 whether we run on primary or not, and it affects both
7626 the child assembly and the device own Open() execution
7629 lu.cfg.SetDiskID(device, node)
7630 result = lu.rpc.call_blockdev_create(node, device, device.size,
7631 instance.name, force_open, info)
7632 result.Raise("Can't create block device %s on"
7633 " node %s for instance %s" % (device, node, instance.name))
7634 if device.physical_id is None:
7635 device.physical_id = result.payload
7638 def _GenerateUniqueNames(lu, exts):
7639 """Generate a suitable LV name.
7641 This will generate a logical volume name for the given instance.
7646 new_id = lu.cfg.GenerateUniqueID(lu.proc.GetECId())
7647 results.append("%s%s" % (new_id, val))
7651 def _GenerateDRBD8Branch(lu, primary, secondary, size, vgnames, names,
7652 iv_name, p_minor, s_minor):
7653 """Generate a drbd8 device complete with its children.
7656 assert len(vgnames) == len(names) == 2
7657 port = lu.cfg.AllocatePort()
7658 shared_secret = lu.cfg.GenerateDRBDSecret(lu.proc.GetECId())
7659 dev_data = objects.Disk(dev_type=constants.LD_LV, size=size,
7660 logical_id=(vgnames[0], names[0]))
7661 dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
7662 logical_id=(vgnames[1], names[1]))
7663 drbd_dev = objects.Disk(dev_type=constants.LD_DRBD8, size=size,
7664 logical_id=(primary, secondary, port,
7667 children=[dev_data, dev_meta],
7672 def _GenerateDiskTemplate(lu, template_name,
7673 instance_name, primary_node,
7674 secondary_nodes, disk_info,
7675 file_storage_dir, file_driver,
7676 base_index, feedback_fn):
7677 """Generate the entire disk layout for a given template type.
7680 #TODO: compute space requirements
7682 vgname = lu.cfg.GetVGName()
7683 disk_count = len(disk_info)
7685 if template_name == constants.DT_DISKLESS:
7687 elif template_name == constants.DT_PLAIN:
7688 if len(secondary_nodes) != 0:
7689 raise errors.ProgrammerError("Wrong template configuration")
7691 names = _GenerateUniqueNames(lu, [".disk%d" % (base_index + i)
7692 for i in range(disk_count)])
7693 for idx, disk in enumerate(disk_info):
7694 disk_index = idx + base_index
7695 vg = disk.get(constants.IDISK_VG, vgname)
7696 feedback_fn("* disk %i, vg %s, name %s" % (idx, vg, names[idx]))
7697 disk_dev = objects.Disk(dev_type=constants.LD_LV,
7698 size=disk[constants.IDISK_SIZE],
7699 logical_id=(vg, names[idx]),
7700 iv_name="disk/%d" % disk_index,
7701 mode=disk[constants.IDISK_MODE])
7702 disks.append(disk_dev)
7703 elif template_name == constants.DT_DRBD8:
7704 if len(secondary_nodes) != 1:
7705 raise errors.ProgrammerError("Wrong template configuration")
7706 remote_node = secondary_nodes[0]
7707 minors = lu.cfg.AllocateDRBDMinor(
7708 [primary_node, remote_node] * len(disk_info), instance_name)
7711 for lv_prefix in _GenerateUniqueNames(lu, [".disk%d" % (base_index + i)
7712 for i in range(disk_count)]):
7713 names.append(lv_prefix + "_data")
7714 names.append(lv_prefix + "_meta")
7715 for idx, disk in enumerate(disk_info):
7716 disk_index = idx + base_index
7717 data_vg = disk.get(constants.IDISK_VG, vgname)
7718 meta_vg = disk.get(constants.IDISK_METAVG, data_vg)
7719 disk_dev = _GenerateDRBD8Branch(lu, primary_node, remote_node,
7720 disk[constants.IDISK_SIZE],
7722 names[idx * 2:idx * 2 + 2],
7723 "disk/%d" % disk_index,
7724 minors[idx * 2], minors[idx * 2 + 1])
7725 disk_dev.mode = disk[constants.IDISK_MODE]
7726 disks.append(disk_dev)
7727 elif template_name == constants.DT_FILE:
7728 if len(secondary_nodes) != 0:
7729 raise errors.ProgrammerError("Wrong template configuration")
7731 opcodes.RequireFileStorage()
7733 for idx, disk in enumerate(disk_info):
7734 disk_index = idx + base_index
7735 disk_dev = objects.Disk(dev_type=constants.LD_FILE,
7736 size=disk[constants.IDISK_SIZE],
7737 iv_name="disk/%d" % disk_index,
7738 logical_id=(file_driver,
7739 "%s/disk%d" % (file_storage_dir,
7741 mode=disk[constants.IDISK_MODE])
7742 disks.append(disk_dev)
7743 elif template_name == constants.DT_SHARED_FILE:
7744 if len(secondary_nodes) != 0:
7745 raise errors.ProgrammerError("Wrong template configuration")
7747 opcodes.RequireSharedFileStorage()
7749 for idx, disk in enumerate(disk_info):
7750 disk_index = idx + base_index
7751 disk_dev = objects.Disk(dev_type=constants.LD_FILE,
7752 size=disk[constants.IDISK_SIZE],
7753 iv_name="disk/%d" % disk_index,
7754 logical_id=(file_driver,
7755 "%s/disk%d" % (file_storage_dir,
7757 mode=disk[constants.IDISK_MODE])
7758 disks.append(disk_dev)
7759 elif template_name == constants.DT_BLOCK:
7760 if len(secondary_nodes) != 0:
7761 raise errors.ProgrammerError("Wrong template configuration")
7763 for idx, disk in enumerate(disk_info):
7764 disk_index = idx + base_index
7765 disk_dev = objects.Disk(dev_type=constants.LD_BLOCKDEV,
7766 size=disk[constants.IDISK_SIZE],
7767 logical_id=(constants.BLOCKDEV_DRIVER_MANUAL,
7768 disk[constants.IDISK_ADOPT]),
7769 iv_name="disk/%d" % disk_index,
7770 mode=disk[constants.IDISK_MODE])
7771 disks.append(disk_dev)
7774 raise errors.ProgrammerError("Invalid disk template '%s'" % template_name)
7778 def _GetInstanceInfoText(instance):
7779 """Compute that text that should be added to the disk's metadata.
7782 return "originstname+%s" % instance.name
7785 def _CalcEta(time_taken, written, total_size):
7786 """Calculates the ETA based on size written and total size.
7788 @param time_taken: The time taken so far
7789 @param written: amount written so far
7790 @param total_size: The total size of data to be written
7791 @return: The remaining time in seconds
7794 avg_time = time_taken / float(written)
7795 return (total_size - written) * avg_time
7798 def _WipeDisks(lu, instance):
7799 """Wipes instance disks.
7801 @type lu: L{LogicalUnit}
7802 @param lu: the logical unit on whose behalf we execute
7803 @type instance: L{objects.Instance}
7804 @param instance: the instance whose disks we should create
7805 @return: the success of the wipe
7808 node = instance.primary_node
7810 for device in instance.disks:
7811 lu.cfg.SetDiskID(device, node)
7813 logging.info("Pause sync of instance %s disks", instance.name)
7814 result = lu.rpc.call_blockdev_pause_resume_sync(node, instance.disks, True)
7816 for idx, success in enumerate(result.payload):
7818 logging.warn("pause-sync of instance %s for disks %d failed",
7822 for idx, device in enumerate(instance.disks):
7823 # The wipe size is MIN_WIPE_CHUNK_PERCENT % of the instance disk but
7824 # MAX_WIPE_CHUNK at max
7825 wipe_chunk_size = min(constants.MAX_WIPE_CHUNK, device.size / 100.0 *
7826 constants.MIN_WIPE_CHUNK_PERCENT)
7827 # we _must_ make this an int, otherwise rounding errors will
7829 wipe_chunk_size = int(wipe_chunk_size)
7831 lu.LogInfo("* Wiping disk %d", idx)
7832 logging.info("Wiping disk %d for instance %s, node %s using"
7833 " chunk size %s", idx, instance.name, node, wipe_chunk_size)
7838 start_time = time.time()
7840 while offset < size:
7841 wipe_size = min(wipe_chunk_size, size - offset)
7842 logging.debug("Wiping disk %d, offset %s, chunk %s",
7843 idx, offset, wipe_size)
7844 result = lu.rpc.call_blockdev_wipe(node, device, offset, wipe_size)
7845 result.Raise("Could not wipe disk %d at offset %d for size %d" %
7846 (idx, offset, wipe_size))
7849 if now - last_output >= 60:
7850 eta = _CalcEta(now - start_time, offset, size)
7851 lu.LogInfo(" - done: %.1f%% ETA: %s" %
7852 (offset / float(size) * 100, utils.FormatSeconds(eta)))
7855 logging.info("Resume sync of instance %s disks", instance.name)
7857 result = lu.rpc.call_blockdev_pause_resume_sync(node, instance.disks, False)
7859 for idx, success in enumerate(result.payload):
7861 lu.LogWarning("Resume sync of disk %d failed, please have a"
7862 " look at the status and troubleshoot the issue", idx)
7863 logging.warn("resume-sync of instance %s for disks %d failed",
7867 def _CreateDisks(lu, instance, to_skip=None, target_node=None):
7868 """Create all disks for an instance.
7870 This abstracts away some work from AddInstance.
7872 @type lu: L{LogicalUnit}
7873 @param lu: the logical unit on whose behalf we execute
7874 @type instance: L{objects.Instance}
7875 @param instance: the instance whose disks we should create
7877 @param to_skip: list of indices to skip
7878 @type target_node: string
7879 @param target_node: if passed, overrides the target node for creation
7881 @return: the success of the creation
7884 info = _GetInstanceInfoText(instance)
7885 if target_node is None:
7886 pnode = instance.primary_node
7887 all_nodes = instance.all_nodes
7892 if instance.disk_template in constants.DTS_FILEBASED:
7893 file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1])
7894 result = lu.rpc.call_file_storage_dir_create(pnode, file_storage_dir)
7896 result.Raise("Failed to create directory '%s' on"
7897 " node %s" % (file_storage_dir, pnode))
7899 # Note: this needs to be kept in sync with adding of disks in
7900 # LUInstanceSetParams
7901 for idx, device in enumerate(instance.disks):
7902 if to_skip and idx in to_skip:
7904 logging.info("Creating volume %s for instance %s",
7905 device.iv_name, instance.name)
7907 for node in all_nodes:
7908 f_create = node == pnode
7909 _CreateBlockDev(lu, node, instance, device, f_create, info, f_create)
7912 def _RemoveDisks(lu, instance, target_node=None):
7913 """Remove all disks for an instance.
7915 This abstracts away some work from `AddInstance()` and
7916 `RemoveInstance()`. Note that in case some of the devices couldn't
7917 be removed, the removal will continue with the other ones (compare
7918 with `_CreateDisks()`).
7920 @type lu: L{LogicalUnit}
7921 @param lu: the logical unit on whose behalf we execute
7922 @type instance: L{objects.Instance}
7923 @param instance: the instance whose disks we should remove
7924 @type target_node: string
7925 @param target_node: used to override the node on which to remove the disks
7927 @return: the success of the removal
7930 logging.info("Removing block devices for instance %s", instance.name)
7933 for device in instance.disks:
7935 edata = [(target_node, device)]
7937 edata = device.ComputeNodeTree(instance.primary_node)
7938 for node, disk in edata:
7939 lu.cfg.SetDiskID(disk, node)
7940 msg = lu.rpc.call_blockdev_remove(node, disk).fail_msg
7942 lu.LogWarning("Could not remove block device %s on node %s,"
7943 " continuing anyway: %s", device.iv_name, node, msg)
7946 if instance.disk_template == constants.DT_FILE:
7947 file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1])
7951 tgt = instance.primary_node
7952 result = lu.rpc.call_file_storage_dir_remove(tgt, file_storage_dir)
7954 lu.LogWarning("Could not remove directory '%s' on node %s: %s",
7955 file_storage_dir, instance.primary_node, result.fail_msg)
7961 def _ComputeDiskSizePerVG(disk_template, disks):
7962 """Compute disk size requirements in the volume group
7965 def _compute(disks, payload):
7966 """Universal algorithm.
7971 vgs[disk[constants.IDISK_VG]] = \
7972 vgs.get(constants.IDISK_VG, 0) + disk[constants.IDISK_SIZE] + payload
7976 # Required free disk space as a function of disk and swap space
7978 constants.DT_DISKLESS: {},
7979 constants.DT_PLAIN: _compute(disks, 0),
7980 # 128 MB are added for drbd metadata for each disk
7981 constants.DT_DRBD8: _compute(disks, 128),
7982 constants.DT_FILE: {},
7983 constants.DT_SHARED_FILE: {},
7986 if disk_template not in req_size_dict:
7987 raise errors.ProgrammerError("Disk template '%s' size requirement"
7988 " is unknown" % disk_template)
7990 return req_size_dict[disk_template]
7993 def _ComputeDiskSize(disk_template, disks):
7994 """Compute disk size requirements in the volume group
7997 # Required free disk space as a function of disk and swap space
7999 constants.DT_DISKLESS: None,
8000 constants.DT_PLAIN: sum(d[constants.IDISK_SIZE] for d in disks),
8001 # 128 MB are added for drbd metadata for each disk
8002 constants.DT_DRBD8: sum(d[constants.IDISK_SIZE] + 128 for d in disks),
8003 constants.DT_FILE: None,
8004 constants.DT_SHARED_FILE: 0,
8005 constants.DT_BLOCK: 0,
8008 if disk_template not in req_size_dict:
8009 raise errors.ProgrammerError("Disk template '%s' size requirement"
8010 " is unknown" % disk_template)
8012 return req_size_dict[disk_template]
8015 def _FilterVmNodes(lu, nodenames):
8016 """Filters out non-vm_capable nodes from a list.
8018 @type lu: L{LogicalUnit}
8019 @param lu: the logical unit for which we check
8020 @type nodenames: list
8021 @param nodenames: the list of nodes on which we should check
8023 @return: the list of vm-capable nodes
8026 vm_nodes = frozenset(lu.cfg.GetNonVmCapableNodeList())
8027 return [name for name in nodenames if name not in vm_nodes]
8030 def _CheckHVParams(lu, nodenames, hvname, hvparams):
8031 """Hypervisor parameter validation.
8033 This function abstract the hypervisor parameter validation to be
8034 used in both instance create and instance modify.
8036 @type lu: L{LogicalUnit}
8037 @param lu: the logical unit for which we check
8038 @type nodenames: list
8039 @param nodenames: the list of nodes on which we should check
8040 @type hvname: string
8041 @param hvname: the name of the hypervisor we should use
8042 @type hvparams: dict
8043 @param hvparams: the parameters which we need to check
8044 @raise errors.OpPrereqError: if the parameters are not valid
8047 nodenames = _FilterVmNodes(lu, nodenames)
8048 hvinfo = lu.rpc.call_hypervisor_validate_params(nodenames,
8051 for node in nodenames:
8055 info.Raise("Hypervisor parameter validation failed on node %s" % node)
8058 def _CheckOSParams(lu, required, nodenames, osname, osparams):
8059 """OS parameters validation.
8061 @type lu: L{LogicalUnit}
8062 @param lu: the logical unit for which we check
8063 @type required: boolean
8064 @param required: whether the validation should fail if the OS is not
8066 @type nodenames: list
8067 @param nodenames: the list of nodes on which we should check
8068 @type osname: string
8069 @param osname: the name of the hypervisor we should use
8070 @type osparams: dict
8071 @param osparams: the parameters which we need to check
8072 @raise errors.OpPrereqError: if the parameters are not valid
8075 nodenames = _FilterVmNodes(lu, nodenames)
8076 result = lu.rpc.call_os_validate(required, nodenames, osname,
8077 [constants.OS_VALIDATE_PARAMETERS],
8079 for node, nres in result.items():
8080 # we don't check for offline cases since this should be run only
8081 # against the master node and/or an instance's nodes
8082 nres.Raise("OS Parameters validation failed on node %s" % node)
8083 if not nres.payload:
8084 lu.LogInfo("OS %s not found on node %s, validation skipped",
8088 class LUInstanceCreate(LogicalUnit):
8089 """Create an instance.
8092 HPATH = "instance-add"
8093 HTYPE = constants.HTYPE_INSTANCE
8096 def CheckArguments(self):
8100 # do not require name_check to ease forward/backward compatibility
8102 if self.op.no_install and self.op.start:
8103 self.LogInfo("No-installation mode selected, disabling startup")
8104 self.op.start = False
8105 # validate/normalize the instance name
8106 self.op.instance_name = \
8107 netutils.Hostname.GetNormalizedName(self.op.instance_name)
8109 if self.op.ip_check and not self.op.name_check:
8110 # TODO: make the ip check more flexible and not depend on the name check
8111 raise errors.OpPrereqError("Cannot do IP address check without a name"
8112 " check", errors.ECODE_INVAL)
8114 # check nics' parameter names
8115 for nic in self.op.nics:
8116 utils.ForceDictType(nic, constants.INIC_PARAMS_TYPES)
8118 # check disks. parameter names and consistent adopt/no-adopt strategy
8119 has_adopt = has_no_adopt = False
8120 for disk in self.op.disks:
8121 utils.ForceDictType(disk, constants.IDISK_PARAMS_TYPES)
8122 if constants.IDISK_ADOPT in disk:
8126 if has_adopt and has_no_adopt:
8127 raise errors.OpPrereqError("Either all disks are adopted or none is",
8130 if self.op.disk_template not in constants.DTS_MAY_ADOPT:
8131 raise errors.OpPrereqError("Disk adoption is not supported for the"
8132 " '%s' disk template" %
8133 self.op.disk_template,
8135 if self.op.iallocator is not None:
8136 raise errors.OpPrereqError("Disk adoption not allowed with an"
8137 " iallocator script", errors.ECODE_INVAL)
8138 if self.op.mode == constants.INSTANCE_IMPORT:
8139 raise errors.OpPrereqError("Disk adoption not allowed for"
8140 " instance import", errors.ECODE_INVAL)
8142 if self.op.disk_template in constants.DTS_MUST_ADOPT:
8143 raise errors.OpPrereqError("Disk template %s requires disk adoption,"
8144 " but no 'adopt' parameter given" %
8145 self.op.disk_template,
8148 self.adopt_disks = has_adopt
8150 # instance name verification
8151 if self.op.name_check:
8152 self.hostname1 = netutils.GetHostname(name=self.op.instance_name)
8153 self.op.instance_name = self.hostname1.name
8154 # used in CheckPrereq for ip ping check
8155 self.check_ip = self.hostname1.ip
8157 self.check_ip = None
8159 # file storage checks
8160 if (self.op.file_driver and
8161 not self.op.file_driver in constants.FILE_DRIVER):
8162 raise errors.OpPrereqError("Invalid file driver name '%s'" %
8163 self.op.file_driver, errors.ECODE_INVAL)
8165 if self.op.disk_template == constants.DT_FILE:
8166 opcodes.RequireFileStorage()
8167 elif self.op.disk_template == constants.DT_SHARED_FILE:
8168 opcodes.RequireSharedFileStorage()
8170 ### Node/iallocator related checks
8171 _CheckIAllocatorOrNode(self, "iallocator", "pnode")
8173 if self.op.pnode is not None:
8174 if self.op.disk_template in constants.DTS_INT_MIRROR:
8175 if self.op.snode is None:
8176 raise errors.OpPrereqError("The networked disk templates need"
8177 " a mirror node", errors.ECODE_INVAL)
8179 self.LogWarning("Secondary node will be ignored on non-mirrored disk"
8181 self.op.snode = None
8183 self._cds = _GetClusterDomainSecret()
8185 if self.op.mode == constants.INSTANCE_IMPORT:
8186 # On import force_variant must be True, because if we forced it at
8187 # initial install, our only chance when importing it back is that it
8189 self.op.force_variant = True
8191 if self.op.no_install:
8192 self.LogInfo("No-installation mode has no effect during import")
8194 elif self.op.mode == constants.INSTANCE_CREATE:
8195 if self.op.os_type is None:
8196 raise errors.OpPrereqError("No guest OS specified",
8198 if self.op.os_type in self.cfg.GetClusterInfo().blacklisted_os:
8199 raise errors.OpPrereqError("Guest OS '%s' is not allowed for"
8200 " installation" % self.op.os_type,
8202 if self.op.disk_template is None:
8203 raise errors.OpPrereqError("No disk template specified",
8206 elif self.op.mode == constants.INSTANCE_REMOTE_IMPORT:
8207 # Check handshake to ensure both clusters have the same domain secret
8208 src_handshake = self.op.source_handshake
8209 if not src_handshake:
8210 raise errors.OpPrereqError("Missing source handshake",
8213 errmsg = masterd.instance.CheckRemoteExportHandshake(self._cds,
8216 raise errors.OpPrereqError("Invalid handshake: %s" % errmsg,
8219 # Load and check source CA
8220 self.source_x509_ca_pem = self.op.source_x509_ca
8221 if not self.source_x509_ca_pem:
8222 raise errors.OpPrereqError("Missing source X509 CA",
8226 (cert, _) = utils.LoadSignedX509Certificate(self.source_x509_ca_pem,
8228 except OpenSSL.crypto.Error, err:
8229 raise errors.OpPrereqError("Unable to load source X509 CA (%s)" %
8230 (err, ), errors.ECODE_INVAL)
8232 (errcode, msg) = utils.VerifyX509Certificate(cert, None, None)
8233 if errcode is not None:
8234 raise errors.OpPrereqError("Invalid source X509 CA (%s)" % (msg, ),
8237 self.source_x509_ca = cert
8239 src_instance_name = self.op.source_instance_name
8240 if not src_instance_name:
8241 raise errors.OpPrereqError("Missing source instance name",
8244 self.source_instance_name = \
8245 netutils.GetHostname(name=src_instance_name).name
8248 raise errors.OpPrereqError("Invalid instance creation mode %r" %
8249 self.op.mode, errors.ECODE_INVAL)
8251 def ExpandNames(self):
8252 """ExpandNames for CreateInstance.
8254 Figure out the right locks for instance creation.
8257 self.needed_locks = {}
8259 instance_name = self.op.instance_name
8260 # this is just a preventive check, but someone might still add this
8261 # instance in the meantime, and creation will fail at lock-add time
8262 if instance_name in self.cfg.GetInstanceList():
8263 raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
8264 instance_name, errors.ECODE_EXISTS)
8266 self.add_locks[locking.LEVEL_INSTANCE] = instance_name
8268 if self.op.iallocator:
8269 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
8271 self.op.pnode = _ExpandNodeName(self.cfg, self.op.pnode)
8272 nodelist = [self.op.pnode]
8273 if self.op.snode is not None:
8274 self.op.snode = _ExpandNodeName(self.cfg, self.op.snode)
8275 nodelist.append(self.op.snode)
8276 self.needed_locks[locking.LEVEL_NODE] = nodelist
8278 # in case of import lock the source node too
8279 if self.op.mode == constants.INSTANCE_IMPORT:
8280 src_node = self.op.src_node
8281 src_path = self.op.src_path
8283 if src_path is None:
8284 self.op.src_path = src_path = self.op.instance_name
8286 if src_node is None:
8287 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
8288 self.op.src_node = None
8289 if os.path.isabs(src_path):
8290 raise errors.OpPrereqError("Importing an instance from a path"
8291 " requires a source node option",
8294 self.op.src_node = src_node = _ExpandNodeName(self.cfg, src_node)
8295 if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET:
8296 self.needed_locks[locking.LEVEL_NODE].append(src_node)
8297 if not os.path.isabs(src_path):
8298 self.op.src_path = src_path = \
8299 utils.PathJoin(constants.EXPORT_DIR, src_path)
8301 def _RunAllocator(self):
8302 """Run the allocator based on input opcode.
8305 nics = [n.ToDict() for n in self.nics]
8306 ial = IAllocator(self.cfg, self.rpc,
8307 mode=constants.IALLOCATOR_MODE_ALLOC,
8308 name=self.op.instance_name,
8309 disk_template=self.op.disk_template,
8312 vcpus=self.be_full[constants.BE_VCPUS],
8313 memory=self.be_full[constants.BE_MEMORY],
8316 hypervisor=self.op.hypervisor,
8319 ial.Run(self.op.iallocator)
8322 raise errors.OpPrereqError("Can't compute nodes using"
8323 " iallocator '%s': %s" %
8324 (self.op.iallocator, ial.info),
8326 if len(ial.result) != ial.required_nodes:
8327 raise errors.OpPrereqError("iallocator '%s' returned invalid number"
8328 " of nodes (%s), required %s" %
8329 (self.op.iallocator, len(ial.result),
8330 ial.required_nodes), errors.ECODE_FAULT)
8331 self.op.pnode = ial.result[0]
8332 self.LogInfo("Selected nodes for instance %s via iallocator %s: %s",
8333 self.op.instance_name, self.op.iallocator,
8334 utils.CommaJoin(ial.result))
8335 if ial.required_nodes == 2:
8336 self.op.snode = ial.result[1]
8338 def BuildHooksEnv(self):
8341 This runs on master, primary and secondary nodes of the instance.
8345 "ADD_MODE": self.op.mode,
8347 if self.op.mode == constants.INSTANCE_IMPORT:
8348 env["SRC_NODE"] = self.op.src_node
8349 env["SRC_PATH"] = self.op.src_path
8350 env["SRC_IMAGES"] = self.src_images
8352 env.update(_BuildInstanceHookEnv(
8353 name=self.op.instance_name,
8354 primary_node=self.op.pnode,
8355 secondary_nodes=self.secondaries,
8356 status=self.op.start,
8357 os_type=self.op.os_type,
8358 memory=self.be_full[constants.BE_MEMORY],
8359 vcpus=self.be_full[constants.BE_VCPUS],
8360 nics=_NICListToTuple(self, self.nics),
8361 disk_template=self.op.disk_template,
8362 disks=[(d[constants.IDISK_SIZE], d[constants.IDISK_MODE])
8363 for d in self.disks],
8366 hypervisor_name=self.op.hypervisor,
8372 def BuildHooksNodes(self):
8373 """Build hooks nodes.
8376 nl = [self.cfg.GetMasterNode(), self.op.pnode] + self.secondaries
8379 def _ReadExportInfo(self):
8380 """Reads the export information from disk.
8382 It will override the opcode source node and path with the actual
8383 information, if these two were not specified before.
8385 @return: the export information
8388 assert self.op.mode == constants.INSTANCE_IMPORT
8390 src_node = self.op.src_node
8391 src_path = self.op.src_path
8393 if src_node is None:
8394 locked_nodes = self.owned_locks(locking.LEVEL_NODE)
8395 exp_list = self.rpc.call_export_list(locked_nodes)
8397 for node in exp_list:
8398 if exp_list[node].fail_msg:
8400 if src_path in exp_list[node].payload:
8402 self.op.src_node = src_node = node
8403 self.op.src_path = src_path = utils.PathJoin(constants.EXPORT_DIR,
8407 raise errors.OpPrereqError("No export found for relative path %s" %
8408 src_path, errors.ECODE_INVAL)
8410 _CheckNodeOnline(self, src_node)
8411 result = self.rpc.call_export_info(src_node, src_path)
8412 result.Raise("No export or invalid export found in dir %s" % src_path)
8414 export_info = objects.SerializableConfigParser.Loads(str(result.payload))
8415 if not export_info.has_section(constants.INISECT_EXP):
8416 raise errors.ProgrammerError("Corrupted export config",
8417 errors.ECODE_ENVIRON)
8419 ei_version = export_info.get(constants.INISECT_EXP, "version")
8420 if (int(ei_version) != constants.EXPORT_VERSION):
8421 raise errors.OpPrereqError("Wrong export version %s (wanted %d)" %
8422 (ei_version, constants.EXPORT_VERSION),
8423 errors.ECODE_ENVIRON)
8426 def _ReadExportParams(self, einfo):
8427 """Use export parameters as defaults.
8429 In case the opcode doesn't specify (as in override) some instance
8430 parameters, then try to use them from the export information, if
8434 self.op.os_type = einfo.get(constants.INISECT_EXP, "os")
8436 if self.op.disk_template is None:
8437 if einfo.has_option(constants.INISECT_INS, "disk_template"):
8438 self.op.disk_template = einfo.get(constants.INISECT_INS,
8440 if self.op.disk_template not in constants.DISK_TEMPLATES:
8441 raise errors.OpPrereqError("Disk template specified in configuration"
8442 " file is not one of the allowed values:"
8443 " %s" % " ".join(constants.DISK_TEMPLATES))
8445 raise errors.OpPrereqError("No disk template specified and the export"
8446 " is missing the disk_template information",
8449 if not self.op.disks:
8451 # TODO: import the disk iv_name too
8452 for idx in range(constants.MAX_DISKS):
8453 if einfo.has_option(constants.INISECT_INS, "disk%d_size" % idx):
8454 disk_sz = einfo.getint(constants.INISECT_INS, "disk%d_size" % idx)
8455 disks.append({constants.IDISK_SIZE: disk_sz})
8456 self.op.disks = disks
8457 if not disks and self.op.disk_template != constants.DT_DISKLESS:
8458 raise errors.OpPrereqError("No disk info specified and the export"
8459 " is missing the disk information",
8462 if not self.op.nics:
8464 for idx in range(constants.MAX_NICS):
8465 if einfo.has_option(constants.INISECT_INS, "nic%d_mac" % idx):
8467 for name in list(constants.NICS_PARAMETERS) + ["ip", "mac"]:
8468 v = einfo.get(constants.INISECT_INS, "nic%d_%s" % (idx, name))
8475 if not self.op.tags and einfo.has_option(constants.INISECT_INS, "tags"):
8476 self.op.tags = einfo.get(constants.INISECT_INS, "tags").split()
8478 if (self.op.hypervisor is None and
8479 einfo.has_option(constants.INISECT_INS, "hypervisor")):
8480 self.op.hypervisor = einfo.get(constants.INISECT_INS, "hypervisor")
8482 if einfo.has_section(constants.INISECT_HYP):
8483 # use the export parameters but do not override the ones
8484 # specified by the user
8485 for name, value in einfo.items(constants.INISECT_HYP):
8486 if name not in self.op.hvparams:
8487 self.op.hvparams[name] = value
8489 if einfo.has_section(constants.INISECT_BEP):
8490 # use the parameters, without overriding
8491 for name, value in einfo.items(constants.INISECT_BEP):
8492 if name not in self.op.beparams:
8493 self.op.beparams[name] = value
8495 # try to read the parameters old style, from the main section
8496 for name in constants.BES_PARAMETERS:
8497 if (name not in self.op.beparams and
8498 einfo.has_option(constants.INISECT_INS, name)):
8499 self.op.beparams[name] = einfo.get(constants.INISECT_INS, name)
8501 if einfo.has_section(constants.INISECT_OSP):
8502 # use the parameters, without overriding
8503 for name, value in einfo.items(constants.INISECT_OSP):
8504 if name not in self.op.osparams:
8505 self.op.osparams[name] = value
8507 def _RevertToDefaults(self, cluster):
8508 """Revert the instance parameters to the default values.
8512 hv_defs = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type, {})
8513 for name in self.op.hvparams.keys():
8514 if name in hv_defs and hv_defs[name] == self.op.hvparams[name]:
8515 del self.op.hvparams[name]
8517 be_defs = cluster.SimpleFillBE({})
8518 for name in self.op.beparams.keys():
8519 if name in be_defs and be_defs[name] == self.op.beparams[name]:
8520 del self.op.beparams[name]
8522 nic_defs = cluster.SimpleFillNIC({})
8523 for nic in self.op.nics:
8524 for name in constants.NICS_PARAMETERS:
8525 if name in nic and name in nic_defs and nic[name] == nic_defs[name]:
8528 os_defs = cluster.SimpleFillOS(self.op.os_type, {})
8529 for name in self.op.osparams.keys():
8530 if name in os_defs and os_defs[name] == self.op.osparams[name]:
8531 del self.op.osparams[name]
8533 def _CalculateFileStorageDir(self):
8534 """Calculate final instance file storage dir.
8537 # file storage dir calculation/check
8538 self.instance_file_storage_dir = None
8539 if self.op.disk_template in constants.DTS_FILEBASED:
8540 # build the full file storage dir path
8543 if self.op.disk_template == constants.DT_SHARED_FILE:
8544 get_fsd_fn = self.cfg.GetSharedFileStorageDir
8546 get_fsd_fn = self.cfg.GetFileStorageDir
8548 cfg_storagedir = get_fsd_fn()
8549 if not cfg_storagedir:
8550 raise errors.OpPrereqError("Cluster file storage dir not defined")
8551 joinargs.append(cfg_storagedir)
8553 if self.op.file_storage_dir is not None:
8554 joinargs.append(self.op.file_storage_dir)
8556 joinargs.append(self.op.instance_name)
8558 # pylint: disable=W0142
8559 self.instance_file_storage_dir = utils.PathJoin(*joinargs)
8561 def CheckPrereq(self):
8562 """Check prerequisites.
8565 self._CalculateFileStorageDir()
8567 if self.op.mode == constants.INSTANCE_IMPORT:
8568 export_info = self._ReadExportInfo()
8569 self._ReadExportParams(export_info)
8571 if (not self.cfg.GetVGName() and
8572 self.op.disk_template not in constants.DTS_NOT_LVM):
8573 raise errors.OpPrereqError("Cluster does not support lvm-based"
8574 " instances", errors.ECODE_STATE)
8576 if (self.op.hypervisor is None or
8577 self.op.hypervisor == constants.VALUE_AUTO):
8578 self.op.hypervisor = self.cfg.GetHypervisorType()
8580 cluster = self.cfg.GetClusterInfo()
8581 enabled_hvs = cluster.enabled_hypervisors
8582 if self.op.hypervisor not in enabled_hvs:
8583 raise errors.OpPrereqError("Selected hypervisor (%s) not enabled in the"
8584 " cluster (%s)" % (self.op.hypervisor,
8585 ",".join(enabled_hvs)),
8588 # Check tag validity
8589 for tag in self.op.tags:
8590 objects.TaggableObject.ValidateTag(tag)
8592 # check hypervisor parameter syntax (locally)
8593 utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
8594 filled_hvp = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type,
8596 hv_type = hypervisor.GetHypervisor(self.op.hypervisor)
8597 hv_type.CheckParameterSyntax(filled_hvp)
8598 self.hv_full = filled_hvp
8599 # check that we don't specify global parameters on an instance
8600 _CheckGlobalHvParams(self.op.hvparams)
8602 # fill and remember the beparams dict
8603 default_beparams = cluster.beparams[constants.PP_DEFAULT]
8604 for param, value in self.op.beparams.iteritems():
8605 if value == constants.VALUE_AUTO:
8606 self.op.beparams[param] = default_beparams[param]
8607 utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
8608 self.be_full = cluster.SimpleFillBE(self.op.beparams)
8610 # build os parameters
8611 self.os_full = cluster.SimpleFillOS(self.op.os_type, self.op.osparams)
8613 # now that hvp/bep are in final format, let's reset to defaults,
8615 if self.op.identify_defaults:
8616 self._RevertToDefaults(cluster)
8620 for idx, nic in enumerate(self.op.nics):
8621 nic_mode_req = nic.get(constants.INIC_MODE, None)
8622 nic_mode = nic_mode_req
8623 if nic_mode is None or nic_mode == constants.VALUE_AUTO:
8624 nic_mode = cluster.nicparams[constants.PP_DEFAULT][constants.NIC_MODE]
8626 # in routed mode, for the first nic, the default ip is 'auto'
8627 if nic_mode == constants.NIC_MODE_ROUTED and idx == 0:
8628 default_ip_mode = constants.VALUE_AUTO
8630 default_ip_mode = constants.VALUE_NONE
8632 # ip validity checks
8633 ip = nic.get(constants.INIC_IP, default_ip_mode)
8634 if ip is None or ip.lower() == constants.VALUE_NONE:
8636 elif ip.lower() == constants.VALUE_AUTO:
8637 if not self.op.name_check:
8638 raise errors.OpPrereqError("IP address set to auto but name checks"
8639 " have been skipped",
8641 nic_ip = self.hostname1.ip
8643 if not netutils.IPAddress.IsValid(ip):
8644 raise errors.OpPrereqError("Invalid IP address '%s'" % ip,
8648 # TODO: check the ip address for uniqueness
8649 if nic_mode == constants.NIC_MODE_ROUTED and not nic_ip:
8650 raise errors.OpPrereqError("Routed nic mode requires an ip address",
8653 # MAC address verification
8654 mac = nic.get(constants.INIC_MAC, constants.VALUE_AUTO)
8655 if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
8656 mac = utils.NormalizeAndValidateMac(mac)
8659 self.cfg.ReserveMAC(mac, self.proc.GetECId())
8660 except errors.ReservationError:
8661 raise errors.OpPrereqError("MAC address %s already in use"
8662 " in cluster" % mac,
8663 errors.ECODE_NOTUNIQUE)
8665 # Build nic parameters
8666 link = nic.get(constants.INIC_LINK, None)
8667 if link == constants.VALUE_AUTO:
8668 link = cluster.nicparams[constants.PP_DEFAULT][constants.NIC_LINK]
8671 nicparams[constants.NIC_MODE] = nic_mode
8673 nicparams[constants.NIC_LINK] = link
8675 check_params = cluster.SimpleFillNIC(nicparams)
8676 objects.NIC.CheckParameterSyntax(check_params)
8677 self.nics.append(objects.NIC(mac=mac, ip=nic_ip, nicparams=nicparams))
8679 # disk checks/pre-build
8680 default_vg = self.cfg.GetVGName()
8682 for disk in self.op.disks:
8683 mode = disk.get(constants.IDISK_MODE, constants.DISK_RDWR)
8684 if mode not in constants.DISK_ACCESS_SET:
8685 raise errors.OpPrereqError("Invalid disk access mode '%s'" %
8686 mode, errors.ECODE_INVAL)
8687 size = disk.get(constants.IDISK_SIZE, None)
8689 raise errors.OpPrereqError("Missing disk size", errors.ECODE_INVAL)
8692 except (TypeError, ValueError):
8693 raise errors.OpPrereqError("Invalid disk size '%s'" % size,
8696 data_vg = disk.get(constants.IDISK_VG, default_vg)
8698 constants.IDISK_SIZE: size,
8699 constants.IDISK_MODE: mode,
8700 constants.IDISK_VG: data_vg,
8701 constants.IDISK_METAVG: disk.get(constants.IDISK_METAVG, data_vg),
8703 if constants.IDISK_ADOPT in disk:
8704 new_disk[constants.IDISK_ADOPT] = disk[constants.IDISK_ADOPT]
8705 self.disks.append(new_disk)
8707 if self.op.mode == constants.INSTANCE_IMPORT:
8709 for idx in range(len(self.disks)):
8710 option = "disk%d_dump" % idx
8711 if export_info.has_option(constants.INISECT_INS, option):
8712 # FIXME: are the old os-es, disk sizes, etc. useful?
8713 export_name = export_info.get(constants.INISECT_INS, option)
8714 image = utils.PathJoin(self.op.src_path, export_name)
8715 disk_images.append(image)
8717 disk_images.append(False)
8719 self.src_images = disk_images
8721 old_name = export_info.get(constants.INISECT_INS, "name")
8722 if self.op.instance_name == old_name:
8723 for idx, nic in enumerate(self.nics):
8724 if nic.mac == constants.VALUE_AUTO:
8725 nic_mac_ini = "nic%d_mac" % idx
8726 nic.mac = export_info.get(constants.INISECT_INS, nic_mac_ini)
8728 # ENDIF: self.op.mode == constants.INSTANCE_IMPORT
8730 # ip ping checks (we use the same ip that was resolved in ExpandNames)
8731 if self.op.ip_check:
8732 if netutils.TcpPing(self.check_ip, constants.DEFAULT_NODED_PORT):
8733 raise errors.OpPrereqError("IP %s of instance %s already in use" %
8734 (self.check_ip, self.op.instance_name),
8735 errors.ECODE_NOTUNIQUE)
8737 #### mac address generation
8738 # By generating here the mac address both the allocator and the hooks get
8739 # the real final mac address rather than the 'auto' or 'generate' value.
8740 # There is a race condition between the generation and the instance object
8741 # creation, which means that we know the mac is valid now, but we're not
8742 # sure it will be when we actually add the instance. If things go bad
8743 # adding the instance will abort because of a duplicate mac, and the
8744 # creation job will fail.
8745 for nic in self.nics:
8746 if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
8747 nic.mac = self.cfg.GenerateMAC(self.proc.GetECId())
8751 if self.op.iallocator is not None:
8752 self._RunAllocator()
8754 #### node related checks
8756 # check primary node
8757 self.pnode = pnode = self.cfg.GetNodeInfo(self.op.pnode)
8758 assert self.pnode is not None, \
8759 "Cannot retrieve locked node %s" % self.op.pnode
8761 raise errors.OpPrereqError("Cannot use offline primary node '%s'" %
8762 pnode.name, errors.ECODE_STATE)
8764 raise errors.OpPrereqError("Cannot use drained primary node '%s'" %
8765 pnode.name, errors.ECODE_STATE)
8766 if not pnode.vm_capable:
8767 raise errors.OpPrereqError("Cannot use non-vm_capable primary node"
8768 " '%s'" % pnode.name, errors.ECODE_STATE)
8770 self.secondaries = []
8772 # mirror node verification
8773 if self.op.disk_template in constants.DTS_INT_MIRROR:
8774 if self.op.snode == pnode.name:
8775 raise errors.OpPrereqError("The secondary node cannot be the"
8776 " primary node", errors.ECODE_INVAL)
8777 _CheckNodeOnline(self, self.op.snode)
8778 _CheckNodeNotDrained(self, self.op.snode)
8779 _CheckNodeVmCapable(self, self.op.snode)
8780 self.secondaries.append(self.op.snode)
8782 nodenames = [pnode.name] + self.secondaries
8784 if not self.adopt_disks:
8785 # Check lv size requirements, if not adopting
8786 req_sizes = _ComputeDiskSizePerVG(self.op.disk_template, self.disks)
8787 _CheckNodesFreeDiskPerVG(self, nodenames, req_sizes)
8789 elif self.op.disk_template == constants.DT_PLAIN: # Check the adoption data
8790 all_lvs = set(["%s/%s" % (disk[constants.IDISK_VG],
8791 disk[constants.IDISK_ADOPT])
8792 for disk in self.disks])
8793 if len(all_lvs) != len(self.disks):
8794 raise errors.OpPrereqError("Duplicate volume names given for adoption",
8796 for lv_name in all_lvs:
8798 # FIXME: lv_name here is "vg/lv" need to ensure that other calls
8799 # to ReserveLV uses the same syntax
8800 self.cfg.ReserveLV(lv_name, self.proc.GetECId())
8801 except errors.ReservationError:
8802 raise errors.OpPrereqError("LV named %s used by another instance" %
8803 lv_name, errors.ECODE_NOTUNIQUE)
8805 vg_names = self.rpc.call_vg_list([pnode.name])[pnode.name]
8806 vg_names.Raise("Cannot get VG information from node %s" % pnode.name)
8808 node_lvs = self.rpc.call_lv_list([pnode.name],
8809 vg_names.payload.keys())[pnode.name]
8810 node_lvs.Raise("Cannot get LV information from node %s" % pnode.name)
8811 node_lvs = node_lvs.payload
8813 delta = all_lvs.difference(node_lvs.keys())
8815 raise errors.OpPrereqError("Missing logical volume(s): %s" %
8816 utils.CommaJoin(delta),
8818 online_lvs = [lv for lv in all_lvs if node_lvs[lv][2]]
8820 raise errors.OpPrereqError("Online logical volumes found, cannot"
8821 " adopt: %s" % utils.CommaJoin(online_lvs),
8823 # update the size of disk based on what is found
8824 for dsk in self.disks:
8825 dsk[constants.IDISK_SIZE] = \
8826 int(float(node_lvs["%s/%s" % (dsk[constants.IDISK_VG],
8827 dsk[constants.IDISK_ADOPT])][0]))
8829 elif self.op.disk_template == constants.DT_BLOCK:
8830 # Normalize and de-duplicate device paths
8831 all_disks = set([os.path.abspath(disk[constants.IDISK_ADOPT])
8832 for disk in self.disks])
8833 if len(all_disks) != len(self.disks):
8834 raise errors.OpPrereqError("Duplicate disk names given for adoption",
8836 baddisks = [d for d in all_disks
8837 if not d.startswith(constants.ADOPTABLE_BLOCKDEV_ROOT)]
8839 raise errors.OpPrereqError("Device node(s) %s lie outside %s and"
8840 " cannot be adopted" %
8841 (", ".join(baddisks),
8842 constants.ADOPTABLE_BLOCKDEV_ROOT),
8845 node_disks = self.rpc.call_bdev_sizes([pnode.name],
8846 list(all_disks))[pnode.name]
8847 node_disks.Raise("Cannot get block device information from node %s" %
8849 node_disks = node_disks.payload
8850 delta = all_disks.difference(node_disks.keys())
8852 raise errors.OpPrereqError("Missing block device(s): %s" %
8853 utils.CommaJoin(delta),
8855 for dsk in self.disks:
8856 dsk[constants.IDISK_SIZE] = \
8857 int(float(node_disks[dsk[constants.IDISK_ADOPT]]))
8859 _CheckHVParams(self, nodenames, self.op.hypervisor, self.op.hvparams)
8861 _CheckNodeHasOS(self, pnode.name, self.op.os_type, self.op.force_variant)
8862 # check OS parameters (remotely)
8863 _CheckOSParams(self, True, nodenames, self.op.os_type, self.os_full)
8865 _CheckNicsBridgesExist(self, self.nics, self.pnode.name)
8867 # memory check on primary node
8869 _CheckNodeFreeMemory(self, self.pnode.name,
8870 "creating instance %s" % self.op.instance_name,
8871 self.be_full[constants.BE_MEMORY],
8874 self.dry_run_result = list(nodenames)
8876 def Exec(self, feedback_fn):
8877 """Create and add the instance to the cluster.
8880 instance = self.op.instance_name
8881 pnode_name = self.pnode.name
8883 ht_kind = self.op.hypervisor
8884 if ht_kind in constants.HTS_REQ_PORT:
8885 network_port = self.cfg.AllocatePort()
8889 disks = _GenerateDiskTemplate(self,
8890 self.op.disk_template,
8891 instance, pnode_name,
8894 self.instance_file_storage_dir,
8895 self.op.file_driver,
8899 iobj = objects.Instance(name=instance, os=self.op.os_type,
8900 primary_node=pnode_name,
8901 nics=self.nics, disks=disks,
8902 disk_template=self.op.disk_template,
8904 network_port=network_port,
8905 beparams=self.op.beparams,
8906 hvparams=self.op.hvparams,
8907 hypervisor=self.op.hypervisor,
8908 osparams=self.op.osparams,
8912 for tag in self.op.tags:
8915 if self.adopt_disks:
8916 if self.op.disk_template == constants.DT_PLAIN:
8917 # rename LVs to the newly-generated names; we need to construct
8918 # 'fake' LV disks with the old data, plus the new unique_id
8919 tmp_disks = [objects.Disk.FromDict(v.ToDict()) for v in disks]
8921 for t_dsk, a_dsk in zip(tmp_disks, self.disks):
8922 rename_to.append(t_dsk.logical_id)
8923 t_dsk.logical_id = (t_dsk.logical_id[0], a_dsk[constants.IDISK_ADOPT])
8924 self.cfg.SetDiskID(t_dsk, pnode_name)
8925 result = self.rpc.call_blockdev_rename(pnode_name,
8926 zip(tmp_disks, rename_to))
8927 result.Raise("Failed to rename adoped LVs")
8929 feedback_fn("* creating instance disks...")
8931 _CreateDisks(self, iobj)
8932 except errors.OpExecError:
8933 self.LogWarning("Device creation failed, reverting...")
8935 _RemoveDisks(self, iobj)
8937 self.cfg.ReleaseDRBDMinors(instance)
8940 feedback_fn("adding instance %s to cluster config" % instance)
8942 self.cfg.AddInstance(iobj, self.proc.GetECId())
8944 # Declare that we don't want to remove the instance lock anymore, as we've
8945 # added the instance to the config
8946 del self.remove_locks[locking.LEVEL_INSTANCE]
8948 if self.op.mode == constants.INSTANCE_IMPORT:
8949 # Release unused nodes
8950 _ReleaseLocks(self, locking.LEVEL_NODE, keep=[self.op.src_node])
8953 _ReleaseLocks(self, locking.LEVEL_NODE)
8956 if not self.adopt_disks and self.cfg.GetClusterInfo().prealloc_wipe_disks:
8957 feedback_fn("* wiping instance disks...")
8959 _WipeDisks(self, iobj)
8960 except errors.OpExecError, err:
8961 logging.exception("Wiping disks failed")
8962 self.LogWarning("Wiping instance disks failed (%s)", err)
8966 # Something is already wrong with the disks, don't do anything else
8968 elif self.op.wait_for_sync:
8969 disk_abort = not _WaitForSync(self, iobj)
8970 elif iobj.disk_template in constants.DTS_INT_MIRROR:
8971 # make sure the disks are not degraded (still sync-ing is ok)
8972 feedback_fn("* checking mirrors status")
8973 disk_abort = not _WaitForSync(self, iobj, oneshot=True)
8978 _RemoveDisks(self, iobj)
8979 self.cfg.RemoveInstance(iobj.name)
8980 # Make sure the instance lock gets removed
8981 self.remove_locks[locking.LEVEL_INSTANCE] = iobj.name
8982 raise errors.OpExecError("There are some degraded disks for"
8985 if iobj.disk_template != constants.DT_DISKLESS and not self.adopt_disks:
8986 if self.op.mode == constants.INSTANCE_CREATE:
8987 if not self.op.no_install:
8988 pause_sync = (iobj.disk_template in constants.DTS_INT_MIRROR and
8989 not self.op.wait_for_sync)
8991 feedback_fn("* pausing disk sync to install instance OS")
8992 result = self.rpc.call_blockdev_pause_resume_sync(pnode_name,
8994 for idx, success in enumerate(result.payload):
8996 logging.warn("pause-sync of instance %s for disk %d failed",
8999 feedback_fn("* running the instance OS create scripts...")
9000 # FIXME: pass debug option from opcode to backend
9002 self.rpc.call_instance_os_add(pnode_name, iobj, False,
9003 self.op.debug_level)
9005 feedback_fn("* resuming disk sync")
9006 result = self.rpc.call_blockdev_pause_resume_sync(pnode_name,
9008 for idx, success in enumerate(result.payload):
9010 logging.warn("resume-sync of instance %s for disk %d failed",
9013 os_add_result.Raise("Could not add os for instance %s"
9014 " on node %s" % (instance, pnode_name))
9016 elif self.op.mode == constants.INSTANCE_IMPORT:
9017 feedback_fn("* running the instance OS import scripts...")
9021 for idx, image in enumerate(self.src_images):
9025 # FIXME: pass debug option from opcode to backend
9026 dt = masterd.instance.DiskTransfer("disk/%s" % idx,
9027 constants.IEIO_FILE, (image, ),
9028 constants.IEIO_SCRIPT,
9029 (iobj.disks[idx], idx),
9031 transfers.append(dt)
9034 masterd.instance.TransferInstanceData(self, feedback_fn,
9035 self.op.src_node, pnode_name,
9036 self.pnode.secondary_ip,
9038 if not compat.all(import_result):
9039 self.LogWarning("Some disks for instance %s on node %s were not"
9040 " imported successfully" % (instance, pnode_name))
9042 elif self.op.mode == constants.INSTANCE_REMOTE_IMPORT:
9043 feedback_fn("* preparing remote import...")
9044 # The source cluster will stop the instance before attempting to make a
9045 # connection. In some cases stopping an instance can take a long time,
9046 # hence the shutdown timeout is added to the connection timeout.
9047 connect_timeout = (constants.RIE_CONNECT_TIMEOUT +
9048 self.op.source_shutdown_timeout)
9049 timeouts = masterd.instance.ImportExportTimeouts(connect_timeout)
9051 assert iobj.primary_node == self.pnode.name
9053 masterd.instance.RemoteImport(self, feedback_fn, iobj, self.pnode,
9054 self.source_x509_ca,
9055 self._cds, timeouts)
9056 if not compat.all(disk_results):
9057 # TODO: Should the instance still be started, even if some disks
9058 # failed to import (valid for local imports, too)?
9059 self.LogWarning("Some disks for instance %s on node %s were not"
9060 " imported successfully" % (instance, pnode_name))
9062 # Run rename script on newly imported instance
9063 assert iobj.name == instance
9064 feedback_fn("Running rename script for %s" % instance)
9065 result = self.rpc.call_instance_run_rename(pnode_name, iobj,
9066 self.source_instance_name,
9067 self.op.debug_level)
9069 self.LogWarning("Failed to run rename script for %s on node"
9070 " %s: %s" % (instance, pnode_name, result.fail_msg))
9073 # also checked in the prereq part
9074 raise errors.ProgrammerError("Unknown OS initialization mode '%s'"
9078 iobj.admin_up = True
9079 self.cfg.Update(iobj, feedback_fn)
9080 logging.info("Starting instance %s on node %s", instance, pnode_name)
9081 feedback_fn("* starting instance...")
9082 result = self.rpc.call_instance_start(pnode_name, iobj,
9084 result.Raise("Could not start instance")
9086 return list(iobj.all_nodes)
9089 class LUInstanceConsole(NoHooksLU):
9090 """Connect to an instance's console.
9092 This is somewhat special in that it returns the command line that
9093 you need to run on the master node in order to connect to the
9099 def ExpandNames(self):
9100 self._ExpandAndLockInstance()
9102 def CheckPrereq(self):
9103 """Check prerequisites.
9105 This checks that the instance is in the cluster.
9108 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
9109 assert self.instance is not None, \
9110 "Cannot retrieve locked instance %s" % self.op.instance_name
9111 _CheckNodeOnline(self, self.instance.primary_node)
9113 def Exec(self, feedback_fn):
9114 """Connect to the console of an instance
9117 instance = self.instance
9118 node = instance.primary_node
9120 node_insts = self.rpc.call_instance_list([node],
9121 [instance.hypervisor])[node]
9122 node_insts.Raise("Can't get node information from %s" % node)
9124 if instance.name not in node_insts.payload:
9125 if instance.admin_up:
9126 state = constants.INSTST_ERRORDOWN
9128 state = constants.INSTST_ADMINDOWN
9129 raise errors.OpExecError("Instance %s is not running (state %s)" %
9130 (instance.name, state))
9132 logging.debug("Connecting to console of %s on %s", instance.name, node)
9134 return _GetInstanceConsole(self.cfg.GetClusterInfo(), instance)
9137 def _GetInstanceConsole(cluster, instance):
9138 """Returns console information for an instance.
9140 @type cluster: L{objects.Cluster}
9141 @type instance: L{objects.Instance}
9145 hyper = hypervisor.GetHypervisor(instance.hypervisor)
9146 # beparams and hvparams are passed separately, to avoid editing the
9147 # instance and then saving the defaults in the instance itself.
9148 hvparams = cluster.FillHV(instance)
9149 beparams = cluster.FillBE(instance)
9150 console = hyper.GetInstanceConsole(instance, hvparams, beparams)
9152 assert console.instance == instance.name
9153 assert console.Validate()
9155 return console.ToDict()
9158 class LUInstanceReplaceDisks(LogicalUnit):
9159 """Replace the disks of an instance.
9162 HPATH = "mirrors-replace"
9163 HTYPE = constants.HTYPE_INSTANCE
9166 def CheckArguments(self):
9167 TLReplaceDisks.CheckArguments(self.op.mode, self.op.remote_node,
9170 def ExpandNames(self):
9171 self._ExpandAndLockInstance()
9173 assert locking.LEVEL_NODE not in self.needed_locks
9174 assert locking.LEVEL_NODEGROUP not in self.needed_locks
9176 assert self.op.iallocator is None or self.op.remote_node is None, \
9177 "Conflicting options"
9179 if self.op.remote_node is not None:
9180 self.op.remote_node = _ExpandNodeName(self.cfg, self.op.remote_node)
9182 # Warning: do not remove the locking of the new secondary here
9183 # unless DRBD8.AddChildren is changed to work in parallel;
9184 # currently it doesn't since parallel invocations of
9185 # FindUnusedMinor will conflict
9186 self.needed_locks[locking.LEVEL_NODE] = [self.op.remote_node]
9187 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
9189 self.needed_locks[locking.LEVEL_NODE] = []
9190 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
9192 if self.op.iallocator is not None:
9193 # iallocator will select a new node in the same group
9194 self.needed_locks[locking.LEVEL_NODEGROUP] = []
9196 self.replacer = TLReplaceDisks(self, self.op.instance_name, self.op.mode,
9197 self.op.iallocator, self.op.remote_node,
9198 self.op.disks, False, self.op.early_release)
9200 self.tasklets = [self.replacer]
9202 def DeclareLocks(self, level):
9203 if level == locking.LEVEL_NODEGROUP:
9204 assert self.op.remote_node is None
9205 assert self.op.iallocator is not None
9206 assert not self.needed_locks[locking.LEVEL_NODEGROUP]
9208 self.share_locks[locking.LEVEL_NODEGROUP] = 1
9209 self.needed_locks[locking.LEVEL_NODEGROUP] = \
9210 self.cfg.GetInstanceNodeGroups(self.op.instance_name)
9212 elif level == locking.LEVEL_NODE:
9213 if self.op.iallocator is not None:
9214 assert self.op.remote_node is None
9215 assert not self.needed_locks[locking.LEVEL_NODE]
9217 # Lock member nodes of all locked groups
9218 self.needed_locks[locking.LEVEL_NODE] = [node_name
9219 for group_uuid in self.owned_locks(locking.LEVEL_NODEGROUP)
9220 for node_name in self.cfg.GetNodeGroup(group_uuid).members]
9222 self._LockInstancesNodes()
9224 def BuildHooksEnv(self):
9227 This runs on the master, the primary and all the secondaries.
9230 instance = self.replacer.instance
9232 "MODE": self.op.mode,
9233 "NEW_SECONDARY": self.op.remote_node,
9234 "OLD_SECONDARY": instance.secondary_nodes[0],
9236 env.update(_BuildInstanceHookEnvByObject(self, instance))
9239 def BuildHooksNodes(self):
9240 """Build hooks nodes.
9243 instance = self.replacer.instance
9245 self.cfg.GetMasterNode(),
9246 instance.primary_node,
9248 if self.op.remote_node is not None:
9249 nl.append(self.op.remote_node)
9252 def CheckPrereq(self):
9253 """Check prerequisites.
9256 assert (self.glm.is_owned(locking.LEVEL_NODEGROUP) or
9257 self.op.iallocator is None)
9259 owned_groups = self.owned_locks(locking.LEVEL_NODEGROUP)
9261 _CheckInstanceNodeGroups(self.cfg, self.op.instance_name, owned_groups)
9263 return LogicalUnit.CheckPrereq(self)
9266 class TLReplaceDisks(Tasklet):
9267 """Replaces disks for an instance.
9269 Note: Locking is not within the scope of this class.
9272 def __init__(self, lu, instance_name, mode, iallocator_name, remote_node,
9273 disks, delay_iallocator, early_release):
9274 """Initializes this class.
9277 Tasklet.__init__(self, lu)
9280 self.instance_name = instance_name
9282 self.iallocator_name = iallocator_name
9283 self.remote_node = remote_node
9285 self.delay_iallocator = delay_iallocator
9286 self.early_release = early_release
9289 self.instance = None
9290 self.new_node = None
9291 self.target_node = None
9292 self.other_node = None
9293 self.remote_node_info = None
9294 self.node_secondary_ip = None
9297 def CheckArguments(mode, remote_node, iallocator):
9298 """Helper function for users of this class.
9301 # check for valid parameter combination
9302 if mode == constants.REPLACE_DISK_CHG:
9303 if remote_node is None and iallocator is None:
9304 raise errors.OpPrereqError("When changing the secondary either an"
9305 " iallocator script must be used or the"
9306 " new node given", errors.ECODE_INVAL)
9308 if remote_node is not None and iallocator is not None:
9309 raise errors.OpPrereqError("Give either the iallocator or the new"
9310 " secondary, not both", errors.ECODE_INVAL)
9312 elif remote_node is not None or iallocator is not None:
9313 # Not replacing the secondary
9314 raise errors.OpPrereqError("The iallocator and new node options can"
9315 " only be used when changing the"
9316 " secondary node", errors.ECODE_INVAL)
9319 def _RunAllocator(lu, iallocator_name, instance_name, relocate_from):
9320 """Compute a new secondary node using an IAllocator.
9323 ial = IAllocator(lu.cfg, lu.rpc,
9324 mode=constants.IALLOCATOR_MODE_RELOC,
9326 relocate_from=list(relocate_from))
9328 ial.Run(iallocator_name)
9331 raise errors.OpPrereqError("Can't compute nodes using iallocator '%s':"
9332 " %s" % (iallocator_name, ial.info),
9335 if len(ial.result) != ial.required_nodes:
9336 raise errors.OpPrereqError("iallocator '%s' returned invalid number"
9337 " of nodes (%s), required %s" %
9339 len(ial.result), ial.required_nodes),
9342 remote_node_name = ial.result[0]
9344 lu.LogInfo("Selected new secondary for instance '%s': %s",
9345 instance_name, remote_node_name)
9347 return remote_node_name
9349 def _FindFaultyDisks(self, node_name):
9350 """Wrapper for L{_FindFaultyInstanceDisks}.
9353 return _FindFaultyInstanceDisks(self.cfg, self.rpc, self.instance,
9356 def _CheckDisksActivated(self, instance):
9357 """Checks if the instance disks are activated.
9359 @param instance: The instance to check disks
9360 @return: True if they are activated, False otherwise
9363 nodes = instance.all_nodes
9365 for idx, dev in enumerate(instance.disks):
9367 self.lu.LogInfo("Checking disk/%d on %s", idx, node)
9368 self.cfg.SetDiskID(dev, node)
9370 result = self.rpc.call_blockdev_find(node, dev)
9374 elif result.fail_msg or not result.payload:
9379 def CheckPrereq(self):
9380 """Check prerequisites.
9382 This checks that the instance is in the cluster.
9385 self.instance = instance = self.cfg.GetInstanceInfo(self.instance_name)
9386 assert instance is not None, \
9387 "Cannot retrieve locked instance %s" % self.instance_name
9389 if instance.disk_template != constants.DT_DRBD8:
9390 raise errors.OpPrereqError("Can only run replace disks for DRBD8-based"
9391 " instances", errors.ECODE_INVAL)
9393 if len(instance.secondary_nodes) != 1:
9394 raise errors.OpPrereqError("The instance has a strange layout,"
9395 " expected one secondary but found %d" %
9396 len(instance.secondary_nodes),
9399 if not self.delay_iallocator:
9400 self._CheckPrereq2()
9402 def _CheckPrereq2(self):
9403 """Check prerequisites, second part.
9405 This function should always be part of CheckPrereq. It was separated and is
9406 now called from Exec because during node evacuation iallocator was only
9407 called with an unmodified cluster model, not taking planned changes into
9411 instance = self.instance
9412 secondary_node = instance.secondary_nodes[0]
9414 if self.iallocator_name is None:
9415 remote_node = self.remote_node
9417 remote_node = self._RunAllocator(self.lu, self.iallocator_name,
9418 instance.name, instance.secondary_nodes)
9420 if remote_node is None:
9421 self.remote_node_info = None
9423 assert remote_node in self.lu.owned_locks(locking.LEVEL_NODE), \
9424 "Remote node '%s' is not locked" % remote_node
9426 self.remote_node_info = self.cfg.GetNodeInfo(remote_node)
9427 assert self.remote_node_info is not None, \
9428 "Cannot retrieve locked node %s" % remote_node
9430 if remote_node == self.instance.primary_node:
9431 raise errors.OpPrereqError("The specified node is the primary node of"
9432 " the instance", errors.ECODE_INVAL)
9434 if remote_node == secondary_node:
9435 raise errors.OpPrereqError("The specified node is already the"
9436 " secondary node of the instance",
9439 if self.disks and self.mode in (constants.REPLACE_DISK_AUTO,
9440 constants.REPLACE_DISK_CHG):
9441 raise errors.OpPrereqError("Cannot specify disks to be replaced",
9444 if self.mode == constants.REPLACE_DISK_AUTO:
9445 if not self._CheckDisksActivated(instance):
9446 raise errors.OpPrereqError("Please run activate-disks on instance %s"
9447 " first" % self.instance_name,
9449 faulty_primary = self._FindFaultyDisks(instance.primary_node)
9450 faulty_secondary = self._FindFaultyDisks(secondary_node)
9452 if faulty_primary and faulty_secondary:
9453 raise errors.OpPrereqError("Instance %s has faulty disks on more than"
9454 " one node and can not be repaired"
9455 " automatically" % self.instance_name,
9459 self.disks = faulty_primary
9460 self.target_node = instance.primary_node
9461 self.other_node = secondary_node
9462 check_nodes = [self.target_node, self.other_node]
9463 elif faulty_secondary:
9464 self.disks = faulty_secondary
9465 self.target_node = secondary_node
9466 self.other_node = instance.primary_node
9467 check_nodes = [self.target_node, self.other_node]
9473 # Non-automatic modes
9474 if self.mode == constants.REPLACE_DISK_PRI:
9475 self.target_node = instance.primary_node
9476 self.other_node = secondary_node
9477 check_nodes = [self.target_node, self.other_node]
9479 elif self.mode == constants.REPLACE_DISK_SEC:
9480 self.target_node = secondary_node
9481 self.other_node = instance.primary_node
9482 check_nodes = [self.target_node, self.other_node]
9484 elif self.mode == constants.REPLACE_DISK_CHG:
9485 self.new_node = remote_node
9486 self.other_node = instance.primary_node
9487 self.target_node = secondary_node
9488 check_nodes = [self.new_node, self.other_node]
9490 _CheckNodeNotDrained(self.lu, remote_node)
9491 _CheckNodeVmCapable(self.lu, remote_node)
9493 old_node_info = self.cfg.GetNodeInfo(secondary_node)
9494 assert old_node_info is not None
9495 if old_node_info.offline and not self.early_release:
9496 # doesn't make sense to delay the release
9497 self.early_release = True
9498 self.lu.LogInfo("Old secondary %s is offline, automatically enabling"
9499 " early-release mode", secondary_node)
9502 raise errors.ProgrammerError("Unhandled disk replace mode (%s)" %
9505 # If not specified all disks should be replaced
9507 self.disks = range(len(self.instance.disks))
9509 for node in check_nodes:
9510 _CheckNodeOnline(self.lu, node)
9512 touched_nodes = frozenset(node_name for node_name in [self.new_node,
9515 if node_name is not None)
9517 # Release unneeded node locks
9518 _ReleaseLocks(self.lu, locking.LEVEL_NODE, keep=touched_nodes)
9520 # Release any owned node group
9521 if self.lu.glm.is_owned(locking.LEVEL_NODEGROUP):
9522 _ReleaseLocks(self.lu, locking.LEVEL_NODEGROUP)
9524 # Check whether disks are valid
9525 for disk_idx in self.disks:
9526 instance.FindDisk(disk_idx)
9528 # Get secondary node IP addresses
9529 self.node_secondary_ip = dict((name, node.secondary_ip) for (name, node)
9530 in self.cfg.GetMultiNodeInfo(touched_nodes))
9532 def Exec(self, feedback_fn):
9533 """Execute disk replacement.
9535 This dispatches the disk replacement to the appropriate handler.
9538 if self.delay_iallocator:
9539 self._CheckPrereq2()
9542 # Verify owned locks before starting operation
9543 owned_nodes = self.lu.owned_locks(locking.LEVEL_NODE)
9544 assert set(owned_nodes) == set(self.node_secondary_ip), \
9545 ("Incorrect node locks, owning %s, expected %s" %
9546 (owned_nodes, self.node_secondary_ip.keys()))
9548 owned_instances = self.lu.owned_locks(locking.LEVEL_INSTANCE)
9549 assert list(owned_instances) == [self.instance_name], \
9550 "Instance '%s' not locked" % self.instance_name
9552 assert not self.lu.glm.is_owned(locking.LEVEL_NODEGROUP), \
9553 "Should not own any node group lock at this point"
9556 feedback_fn("No disks need replacement")
9559 feedback_fn("Replacing disk(s) %s for %s" %
9560 (utils.CommaJoin(self.disks), self.instance.name))
9562 activate_disks = (not self.instance.admin_up)
9564 # Activate the instance disks if we're replacing them on a down instance
9566 _StartInstanceDisks(self.lu, self.instance, True)
9569 # Should we replace the secondary node?
9570 if self.new_node is not None:
9571 fn = self._ExecDrbd8Secondary
9573 fn = self._ExecDrbd8DiskOnly
9575 result = fn(feedback_fn)
9577 # Deactivate the instance disks if we're replacing them on a
9580 _SafeShutdownInstanceDisks(self.lu, self.instance)
9583 # Verify owned locks
9584 owned_nodes = self.lu.owned_locks(locking.LEVEL_NODE)
9585 nodes = frozenset(self.node_secondary_ip)
9586 assert ((self.early_release and not owned_nodes) or
9587 (not self.early_release and not (set(owned_nodes) - nodes))), \
9588 ("Not owning the correct locks, early_release=%s, owned=%r,"
9589 " nodes=%r" % (self.early_release, owned_nodes, nodes))
9593 def _CheckVolumeGroup(self, nodes):
9594 self.lu.LogInfo("Checking volume groups")
9596 vgname = self.cfg.GetVGName()
9598 # Make sure volume group exists on all involved nodes
9599 results = self.rpc.call_vg_list(nodes)
9601 raise errors.OpExecError("Can't list volume groups on the nodes")
9605 res.Raise("Error checking node %s" % node)
9606 if vgname not in res.payload:
9607 raise errors.OpExecError("Volume group '%s' not found on node %s" %
9610 def _CheckDisksExistence(self, nodes):
9611 # Check disk existence
9612 for idx, dev in enumerate(self.instance.disks):
9613 if idx not in self.disks:
9617 self.lu.LogInfo("Checking disk/%d on %s" % (idx, node))
9618 self.cfg.SetDiskID(dev, node)
9620 result = self.rpc.call_blockdev_find(node, dev)
9622 msg = result.fail_msg
9623 if msg or not result.payload:
9625 msg = "disk not found"
9626 raise errors.OpExecError("Can't find disk/%d on node %s: %s" %
9629 def _CheckDisksConsistency(self, node_name, on_primary, ldisk):
9630 for idx, dev in enumerate(self.instance.disks):
9631 if idx not in self.disks:
9634 self.lu.LogInfo("Checking disk/%d consistency on node %s" %
9637 if not _CheckDiskConsistency(self.lu, dev, node_name, on_primary,
9639 raise errors.OpExecError("Node %s has degraded storage, unsafe to"
9640 " replace disks for instance %s" %
9641 (node_name, self.instance.name))
9643 def _CreateNewStorage(self, node_name):
9644 """Create new storage on the primary or secondary node.
9646 This is only used for same-node replaces, not for changing the
9647 secondary node, hence we don't want to modify the existing disk.
9652 for idx, dev in enumerate(self.instance.disks):
9653 if idx not in self.disks:
9656 self.lu.LogInfo("Adding storage on %s for disk/%d" % (node_name, idx))
9658 self.cfg.SetDiskID(dev, node_name)
9660 lv_names = [".disk%d_%s" % (idx, suffix) for suffix in ["data", "meta"]]
9661 names = _GenerateUniqueNames(self.lu, lv_names)
9663 vg_data = dev.children[0].logical_id[0]
9664 lv_data = objects.Disk(dev_type=constants.LD_LV, size=dev.size,
9665 logical_id=(vg_data, names[0]))
9666 vg_meta = dev.children[1].logical_id[0]
9667 lv_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
9668 logical_id=(vg_meta, names[1]))
9670 new_lvs = [lv_data, lv_meta]
9671 old_lvs = [child.Copy() for child in dev.children]
9672 iv_names[dev.iv_name] = (dev, old_lvs, new_lvs)
9674 # we pass force_create=True to force the LVM creation
9675 for new_lv in new_lvs:
9676 _CreateBlockDev(self.lu, node_name, self.instance, new_lv, True,
9677 _GetInstanceInfoText(self.instance), False)
9681 def _CheckDevices(self, node_name, iv_names):
9682 for name, (dev, _, _) in iv_names.iteritems():
9683 self.cfg.SetDiskID(dev, node_name)
9685 result = self.rpc.call_blockdev_find(node_name, dev)
9687 msg = result.fail_msg
9688 if msg or not result.payload:
9690 msg = "disk not found"
9691 raise errors.OpExecError("Can't find DRBD device %s: %s" %
9694 if result.payload.is_degraded:
9695 raise errors.OpExecError("DRBD device %s is degraded!" % name)
9697 def _RemoveOldStorage(self, node_name, iv_names):
9698 for name, (_, old_lvs, _) in iv_names.iteritems():
9699 self.lu.LogInfo("Remove logical volumes for %s" % name)
9702 self.cfg.SetDiskID(lv, node_name)
9704 msg = self.rpc.call_blockdev_remove(node_name, lv).fail_msg
9706 self.lu.LogWarning("Can't remove old LV: %s" % msg,
9707 hint="remove unused LVs manually")
9709 def _ExecDrbd8DiskOnly(self, feedback_fn): # pylint: disable=W0613
9710 """Replace a disk on the primary or secondary for DRBD 8.
9712 The algorithm for replace is quite complicated:
9714 1. for each disk to be replaced:
9716 1. create new LVs on the target node with unique names
9717 1. detach old LVs from the drbd device
9718 1. rename old LVs to name_replaced.<time_t>
9719 1. rename new LVs to old LVs
9720 1. attach the new LVs (with the old names now) to the drbd device
9722 1. wait for sync across all devices
9724 1. for each modified disk:
9726 1. remove old LVs (which have the name name_replaces.<time_t>)
9728 Failures are not very well handled.
9733 # Step: check device activation
9734 self.lu.LogStep(1, steps_total, "Check device existence")
9735 self._CheckDisksExistence([self.other_node, self.target_node])
9736 self._CheckVolumeGroup([self.target_node, self.other_node])
9738 # Step: check other node consistency
9739 self.lu.LogStep(2, steps_total, "Check peer consistency")
9740 self._CheckDisksConsistency(self.other_node,
9741 self.other_node == self.instance.primary_node,
9744 # Step: create new storage
9745 self.lu.LogStep(3, steps_total, "Allocate new storage")
9746 iv_names = self._CreateNewStorage(self.target_node)
9748 # Step: for each lv, detach+rename*2+attach
9749 self.lu.LogStep(4, steps_total, "Changing drbd configuration")
9750 for dev, old_lvs, new_lvs in iv_names.itervalues():
9751 self.lu.LogInfo("Detaching %s drbd from local storage" % dev.iv_name)
9753 result = self.rpc.call_blockdev_removechildren(self.target_node, dev,
9755 result.Raise("Can't detach drbd from local storage on node"
9756 " %s for device %s" % (self.target_node, dev.iv_name))
9758 #cfg.Update(instance)
9760 # ok, we created the new LVs, so now we know we have the needed
9761 # storage; as such, we proceed on the target node to rename
9762 # old_lv to _old, and new_lv to old_lv; note that we rename LVs
9763 # using the assumption that logical_id == physical_id (which in
9764 # turn is the unique_id on that node)
9766 # FIXME(iustin): use a better name for the replaced LVs
9767 temp_suffix = int(time.time())
9768 ren_fn = lambda d, suff: (d.physical_id[0],
9769 d.physical_id[1] + "_replaced-%s" % suff)
9771 # Build the rename list based on what LVs exist on the node
9772 rename_old_to_new = []
9773 for to_ren in old_lvs:
9774 result = self.rpc.call_blockdev_find(self.target_node, to_ren)
9775 if not result.fail_msg and result.payload:
9777 rename_old_to_new.append((to_ren, ren_fn(to_ren, temp_suffix)))
9779 self.lu.LogInfo("Renaming the old LVs on the target node")
9780 result = self.rpc.call_blockdev_rename(self.target_node,
9782 result.Raise("Can't rename old LVs on node %s" % self.target_node)
9784 # Now we rename the new LVs to the old LVs
9785 self.lu.LogInfo("Renaming the new LVs on the target node")
9786 rename_new_to_old = [(new, old.physical_id)
9787 for old, new in zip(old_lvs, new_lvs)]
9788 result = self.rpc.call_blockdev_rename(self.target_node,
9790 result.Raise("Can't rename new LVs on node %s" % self.target_node)
9792 # Intermediate steps of in memory modifications
9793 for old, new in zip(old_lvs, new_lvs):
9794 new.logical_id = old.logical_id
9795 self.cfg.SetDiskID(new, self.target_node)
9797 # We need to modify old_lvs so that removal later removes the
9798 # right LVs, not the newly added ones; note that old_lvs is a
9800 for disk in old_lvs:
9801 disk.logical_id = ren_fn(disk, temp_suffix)
9802 self.cfg.SetDiskID(disk, self.target_node)
9804 # Now that the new lvs have the old name, we can add them to the device
9805 self.lu.LogInfo("Adding new mirror component on %s" % self.target_node)
9806 result = self.rpc.call_blockdev_addchildren(self.target_node, dev,
9808 msg = result.fail_msg
9810 for new_lv in new_lvs:
9811 msg2 = self.rpc.call_blockdev_remove(self.target_node,
9814 self.lu.LogWarning("Can't rollback device %s: %s", dev, msg2,
9815 hint=("cleanup manually the unused logical"
9817 raise errors.OpExecError("Can't add local storage to drbd: %s" % msg)
9820 if self.early_release:
9821 self.lu.LogStep(cstep, steps_total, "Removing old storage")
9823 self._RemoveOldStorage(self.target_node, iv_names)
9824 # WARNING: we release both node locks here, do not do other RPCs
9825 # than WaitForSync to the primary node
9826 _ReleaseLocks(self.lu, locking.LEVEL_NODE,
9827 names=[self.target_node, self.other_node])
9830 # This can fail as the old devices are degraded and _WaitForSync
9831 # does a combined result over all disks, so we don't check its return value
9832 self.lu.LogStep(cstep, steps_total, "Sync devices")
9834 _WaitForSync(self.lu, self.instance)
9836 # Check all devices manually
9837 self._CheckDevices(self.instance.primary_node, iv_names)
9839 # Step: remove old storage
9840 if not self.early_release:
9841 self.lu.LogStep(cstep, steps_total, "Removing old storage")
9843 self._RemoveOldStorage(self.target_node, iv_names)
9845 def _ExecDrbd8Secondary(self, feedback_fn):
9846 """Replace the secondary node for DRBD 8.
9848 The algorithm for replace is quite complicated:
9849 - for all disks of the instance:
9850 - create new LVs on the new node with same names
9851 - shutdown the drbd device on the old secondary
9852 - disconnect the drbd network on the primary
9853 - create the drbd device on the new secondary
9854 - network attach the drbd on the primary, using an artifice:
9855 the drbd code for Attach() will connect to the network if it
9856 finds a device which is connected to the good local disks but
9858 - wait for sync across all devices
9859 - remove all disks from the old secondary
9861 Failures are not very well handled.
9866 pnode = self.instance.primary_node
9868 # Step: check device activation
9869 self.lu.LogStep(1, steps_total, "Check device existence")
9870 self._CheckDisksExistence([self.instance.primary_node])
9871 self._CheckVolumeGroup([self.instance.primary_node])
9873 # Step: check other node consistency
9874 self.lu.LogStep(2, steps_total, "Check peer consistency")
9875 self._CheckDisksConsistency(self.instance.primary_node, True, True)
9877 # Step: create new storage
9878 self.lu.LogStep(3, steps_total, "Allocate new storage")
9879 for idx, dev in enumerate(self.instance.disks):
9880 self.lu.LogInfo("Adding new local storage on %s for disk/%d" %
9881 (self.new_node, idx))
9882 # we pass force_create=True to force LVM creation
9883 for new_lv in dev.children:
9884 _CreateBlockDev(self.lu, self.new_node, self.instance, new_lv, True,
9885 _GetInstanceInfoText(self.instance), False)
9887 # Step 4: dbrd minors and drbd setups changes
9888 # after this, we must manually remove the drbd minors on both the
9889 # error and the success paths
9890 self.lu.LogStep(4, steps_total, "Changing drbd configuration")
9891 minors = self.cfg.AllocateDRBDMinor([self.new_node
9892 for dev in self.instance.disks],
9894 logging.debug("Allocated minors %r", minors)
9897 for idx, (dev, new_minor) in enumerate(zip(self.instance.disks, minors)):
9898 self.lu.LogInfo("activating a new drbd on %s for disk/%d" %
9899 (self.new_node, idx))
9900 # create new devices on new_node; note that we create two IDs:
9901 # one without port, so the drbd will be activated without
9902 # networking information on the new node at this stage, and one
9903 # with network, for the latter activation in step 4
9904 (o_node1, o_node2, o_port, o_minor1, o_minor2, o_secret) = dev.logical_id
9905 if self.instance.primary_node == o_node1:
9908 assert self.instance.primary_node == o_node2, "Three-node instance?"
9911 new_alone_id = (self.instance.primary_node, self.new_node, None,
9912 p_minor, new_minor, o_secret)
9913 new_net_id = (self.instance.primary_node, self.new_node, o_port,
9914 p_minor, new_minor, o_secret)
9916 iv_names[idx] = (dev, dev.children, new_net_id)
9917 logging.debug("Allocated new_minor: %s, new_logical_id: %s", new_minor,
9919 new_drbd = objects.Disk(dev_type=constants.LD_DRBD8,
9920 logical_id=new_alone_id,
9921 children=dev.children,
9924 _CreateSingleBlockDev(self.lu, self.new_node, self.instance, new_drbd,
9925 _GetInstanceInfoText(self.instance), False)
9926 except errors.GenericError:
9927 self.cfg.ReleaseDRBDMinors(self.instance.name)
9930 # We have new devices, shutdown the drbd on the old secondary
9931 for idx, dev in enumerate(self.instance.disks):
9932 self.lu.LogInfo("Shutting down drbd for disk/%d on old node" % idx)
9933 self.cfg.SetDiskID(dev, self.target_node)
9934 msg = self.rpc.call_blockdev_shutdown(self.target_node, dev).fail_msg
9936 self.lu.LogWarning("Failed to shutdown drbd for disk/%d on old"
9937 "node: %s" % (idx, msg),
9938 hint=("Please cleanup this device manually as"
9939 " soon as possible"))
9941 self.lu.LogInfo("Detaching primary drbds from the network (=> standalone)")
9942 result = self.rpc.call_drbd_disconnect_net([pnode], self.node_secondary_ip,
9943 self.instance.disks)[pnode]
9945 msg = result.fail_msg
9947 # detaches didn't succeed (unlikely)
9948 self.cfg.ReleaseDRBDMinors(self.instance.name)
9949 raise errors.OpExecError("Can't detach the disks from the network on"
9950 " old node: %s" % (msg,))
9952 # if we managed to detach at least one, we update all the disks of
9953 # the instance to point to the new secondary
9954 self.lu.LogInfo("Updating instance configuration")
9955 for dev, _, new_logical_id in iv_names.itervalues():
9956 dev.logical_id = new_logical_id
9957 self.cfg.SetDiskID(dev, self.instance.primary_node)
9959 self.cfg.Update(self.instance, feedback_fn)
9961 # and now perform the drbd attach
9962 self.lu.LogInfo("Attaching primary drbds to new secondary"
9963 " (standalone => connected)")
9964 result = self.rpc.call_drbd_attach_net([self.instance.primary_node,
9966 self.node_secondary_ip,
9967 self.instance.disks,
9970 for to_node, to_result in result.items():
9971 msg = to_result.fail_msg
9973 self.lu.LogWarning("Can't attach drbd disks on node %s: %s",
9975 hint=("please do a gnt-instance info to see the"
9976 " status of disks"))
9978 if self.early_release:
9979 self.lu.LogStep(cstep, steps_total, "Removing old storage")
9981 self._RemoveOldStorage(self.target_node, iv_names)
9982 # WARNING: we release all node locks here, do not do other RPCs
9983 # than WaitForSync to the primary node
9984 _ReleaseLocks(self.lu, locking.LEVEL_NODE,
9985 names=[self.instance.primary_node,
9990 # This can fail as the old devices are degraded and _WaitForSync
9991 # does a combined result over all disks, so we don't check its return value
9992 self.lu.LogStep(cstep, steps_total, "Sync devices")
9994 _WaitForSync(self.lu, self.instance)
9996 # Check all devices manually
9997 self._CheckDevices(self.instance.primary_node, iv_names)
9999 # Step: remove old storage
10000 if not self.early_release:
10001 self.lu.LogStep(cstep, steps_total, "Removing old storage")
10002 self._RemoveOldStorage(self.target_node, iv_names)
10005 class LURepairNodeStorage(NoHooksLU):
10006 """Repairs the volume group on a node.
10011 def CheckArguments(self):
10012 self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
10014 storage_type = self.op.storage_type
10016 if (constants.SO_FIX_CONSISTENCY not in
10017 constants.VALID_STORAGE_OPERATIONS.get(storage_type, [])):
10018 raise errors.OpPrereqError("Storage units of type '%s' can not be"
10019 " repaired" % storage_type,
10020 errors.ECODE_INVAL)
10022 def ExpandNames(self):
10023 self.needed_locks = {
10024 locking.LEVEL_NODE: [self.op.node_name],
10027 def _CheckFaultyDisks(self, instance, node_name):
10028 """Ensure faulty disks abort the opcode or at least warn."""
10030 if _FindFaultyInstanceDisks(self.cfg, self.rpc, instance,
10032 raise errors.OpPrereqError("Instance '%s' has faulty disks on"
10033 " node '%s'" % (instance.name, node_name),
10034 errors.ECODE_STATE)
10035 except errors.OpPrereqError, err:
10036 if self.op.ignore_consistency:
10037 self.proc.LogWarning(str(err.args[0]))
10041 def CheckPrereq(self):
10042 """Check prerequisites.
10045 # Check whether any instance on this node has faulty disks
10046 for inst in _GetNodeInstances(self.cfg, self.op.node_name):
10047 if not inst.admin_up:
10049 check_nodes = set(inst.all_nodes)
10050 check_nodes.discard(self.op.node_name)
10051 for inst_node_name in check_nodes:
10052 self._CheckFaultyDisks(inst, inst_node_name)
10054 def Exec(self, feedback_fn):
10055 feedback_fn("Repairing storage unit '%s' on %s ..." %
10056 (self.op.name, self.op.node_name))
10058 st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
10059 result = self.rpc.call_storage_execute(self.op.node_name,
10060 self.op.storage_type, st_args,
10062 constants.SO_FIX_CONSISTENCY)
10063 result.Raise("Failed to repair storage unit '%s' on %s" %
10064 (self.op.name, self.op.node_name))
10067 class LUNodeEvacuate(NoHooksLU):
10068 """Evacuates instances off a list of nodes.
10073 def CheckArguments(self):
10074 _CheckIAllocatorOrNode(self, "iallocator", "remote_node")
10076 def ExpandNames(self):
10077 self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
10079 if self.op.remote_node is not None:
10080 self.op.remote_node = _ExpandNodeName(self.cfg, self.op.remote_node)
10081 assert self.op.remote_node
10083 if self.op.remote_node == self.op.node_name:
10084 raise errors.OpPrereqError("Can not use evacuated node as a new"
10085 " secondary node", errors.ECODE_INVAL)
10087 if self.op.mode != constants.IALLOCATOR_NEVAC_SEC:
10088 raise errors.OpPrereqError("Without the use of an iallocator only"
10089 " secondary instances can be evacuated",
10090 errors.ECODE_INVAL)
10093 self.share_locks = _ShareAll()
10094 self.needed_locks = {
10095 locking.LEVEL_INSTANCE: [],
10096 locking.LEVEL_NODEGROUP: [],
10097 locking.LEVEL_NODE: [],
10100 if self.op.remote_node is None:
10101 # Iallocator will choose any node(s) in the same group
10102 group_nodes = self.cfg.GetNodeGroupMembersByNodes([self.op.node_name])
10104 group_nodes = frozenset([self.op.remote_node])
10106 # Determine nodes to be locked
10107 self.lock_nodes = set([self.op.node_name]) | group_nodes
10109 def _DetermineInstances(self):
10110 """Builds list of instances to operate on.
10113 assert self.op.mode in constants.IALLOCATOR_NEVAC_MODES
10115 if self.op.mode == constants.IALLOCATOR_NEVAC_PRI:
10116 # Primary instances only
10117 inst_fn = _GetNodePrimaryInstances
10118 assert self.op.remote_node is None, \
10119 "Evacuating primary instances requires iallocator"
10120 elif self.op.mode == constants.IALLOCATOR_NEVAC_SEC:
10121 # Secondary instances only
10122 inst_fn = _GetNodeSecondaryInstances
10125 assert self.op.mode == constants.IALLOCATOR_NEVAC_ALL
10126 inst_fn = _GetNodeInstances
10128 return inst_fn(self.cfg, self.op.node_name)
10130 def DeclareLocks(self, level):
10131 if level == locking.LEVEL_INSTANCE:
10132 # Lock instances optimistically, needs verification once node and group
10133 # locks have been acquired
10134 self.needed_locks[locking.LEVEL_INSTANCE] = \
10135 set(i.name for i in self._DetermineInstances())
10137 elif level == locking.LEVEL_NODEGROUP:
10138 # Lock node groups optimistically, needs verification once nodes have
10140 self.needed_locks[locking.LEVEL_NODEGROUP] = \
10141 self.cfg.GetNodeGroupsFromNodes(self.lock_nodes)
10143 elif level == locking.LEVEL_NODE:
10144 self.needed_locks[locking.LEVEL_NODE] = self.lock_nodes
10146 def CheckPrereq(self):
10148 owned_instances = self.owned_locks(locking.LEVEL_INSTANCE)
10149 owned_nodes = self.owned_locks(locking.LEVEL_NODE)
10150 owned_groups = self.owned_locks(locking.LEVEL_NODEGROUP)
10152 assert owned_nodes == self.lock_nodes
10154 wanted_groups = self.cfg.GetNodeGroupsFromNodes(owned_nodes)
10155 if owned_groups != wanted_groups:
10156 raise errors.OpExecError("Node groups changed since locks were acquired,"
10157 " current groups are '%s', used to be '%s'" %
10158 (utils.CommaJoin(wanted_groups),
10159 utils.CommaJoin(owned_groups)))
10161 # Determine affected instances
10162 self.instances = self._DetermineInstances()
10163 self.instance_names = [i.name for i in self.instances]
10165 if set(self.instance_names) != owned_instances:
10166 raise errors.OpExecError("Instances on node '%s' changed since locks"
10167 " were acquired, current instances are '%s',"
10168 " used to be '%s'" %
10169 (self.op.node_name,
10170 utils.CommaJoin(self.instance_names),
10171 utils.CommaJoin(owned_instances)))
10173 if self.instance_names:
10174 self.LogInfo("Evacuating instances from node '%s': %s",
10176 utils.CommaJoin(utils.NiceSort(self.instance_names)))
10178 self.LogInfo("No instances to evacuate from node '%s'",
10181 if self.op.remote_node is not None:
10182 for i in self.instances:
10183 if i.primary_node == self.op.remote_node:
10184 raise errors.OpPrereqError("Node %s is the primary node of"
10185 " instance %s, cannot use it as"
10187 (self.op.remote_node, i.name),
10188 errors.ECODE_INVAL)
10190 def Exec(self, feedback_fn):
10191 assert (self.op.iallocator is not None) ^ (self.op.remote_node is not None)
10193 if not self.instance_names:
10194 # No instances to evacuate
10197 elif self.op.iallocator is not None:
10198 # TODO: Implement relocation to other group
10199 ial = IAllocator(self.cfg, self.rpc, constants.IALLOCATOR_MODE_NODE_EVAC,
10200 evac_mode=self.op.mode,
10201 instances=list(self.instance_names))
10203 ial.Run(self.op.iallocator)
10205 if not ial.success:
10206 raise errors.OpPrereqError("Can't compute node evacuation using"
10207 " iallocator '%s': %s" %
10208 (self.op.iallocator, ial.info),
10209 errors.ECODE_NORES)
10211 jobs = _LoadNodeEvacResult(self, ial.result, self.op.early_release, True)
10213 elif self.op.remote_node is not None:
10214 assert self.op.mode == constants.IALLOCATOR_NEVAC_SEC
10216 [opcodes.OpInstanceReplaceDisks(instance_name=instance_name,
10217 remote_node=self.op.remote_node,
10219 mode=constants.REPLACE_DISK_CHG,
10220 early_release=self.op.early_release)]
10221 for instance_name in self.instance_names
10225 raise errors.ProgrammerError("No iallocator or remote node")
10227 return ResultWithJobs(jobs)
10230 def _SetOpEarlyRelease(early_release, op):
10231 """Sets C{early_release} flag on opcodes if available.
10235 op.early_release = early_release
10236 except AttributeError:
10237 assert not isinstance(op, opcodes.OpInstanceReplaceDisks)
10242 def _NodeEvacDest(use_nodes, group, nodes):
10243 """Returns group or nodes depending on caller's choice.
10247 return utils.CommaJoin(nodes)
10252 def _LoadNodeEvacResult(lu, alloc_result, early_release, use_nodes):
10253 """Unpacks the result of change-group and node-evacuate iallocator requests.
10255 Iallocator modes L{constants.IALLOCATOR_MODE_NODE_EVAC} and
10256 L{constants.IALLOCATOR_MODE_CHG_GROUP}.
10258 @type lu: L{LogicalUnit}
10259 @param lu: Logical unit instance
10260 @type alloc_result: tuple/list
10261 @param alloc_result: Result from iallocator
10262 @type early_release: bool
10263 @param early_release: Whether to release locks early if possible
10264 @type use_nodes: bool
10265 @param use_nodes: Whether to display node names instead of groups
10268 (moved, failed, jobs) = alloc_result
10271 lu.LogWarning("Unable to evacuate instances %s",
10272 utils.CommaJoin("%s (%s)" % (name, reason)
10273 for (name, reason) in failed))
10276 lu.LogInfo("Instances to be moved: %s",
10277 utils.CommaJoin("%s (to %s)" %
10278 (name, _NodeEvacDest(use_nodes, group, nodes))
10279 for (name, group, nodes) in moved))
10281 return [map(compat.partial(_SetOpEarlyRelease, early_release),
10282 map(opcodes.OpCode.LoadOpCode, ops))
10286 class LUInstanceGrowDisk(LogicalUnit):
10287 """Grow a disk of an instance.
10290 HPATH = "disk-grow"
10291 HTYPE = constants.HTYPE_INSTANCE
10294 def ExpandNames(self):
10295 self._ExpandAndLockInstance()
10296 self.needed_locks[locking.LEVEL_NODE] = []
10297 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
10299 def DeclareLocks(self, level):
10300 if level == locking.LEVEL_NODE:
10301 self._LockInstancesNodes()
10303 def BuildHooksEnv(self):
10304 """Build hooks env.
10306 This runs on the master, the primary and all the secondaries.
10310 "DISK": self.op.disk,
10311 "AMOUNT": self.op.amount,
10313 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
10316 def BuildHooksNodes(self):
10317 """Build hooks nodes.
10320 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
10323 def CheckPrereq(self):
10324 """Check prerequisites.
10326 This checks that the instance is in the cluster.
10329 instance = self.cfg.GetInstanceInfo(self.op.instance_name)
10330 assert instance is not None, \
10331 "Cannot retrieve locked instance %s" % self.op.instance_name
10332 nodenames = list(instance.all_nodes)
10333 for node in nodenames:
10334 _CheckNodeOnline(self, node)
10336 self.instance = instance
10338 if instance.disk_template not in constants.DTS_GROWABLE:
10339 raise errors.OpPrereqError("Instance's disk layout does not support"
10340 " growing", errors.ECODE_INVAL)
10342 self.disk = instance.FindDisk(self.op.disk)
10344 if instance.disk_template not in (constants.DT_FILE,
10345 constants.DT_SHARED_FILE):
10346 # TODO: check the free disk space for file, when that feature will be
10348 _CheckNodesFreeDiskPerVG(self, nodenames,
10349 self.disk.ComputeGrowth(self.op.amount))
10351 def Exec(self, feedback_fn):
10352 """Execute disk grow.
10355 instance = self.instance
10358 disks_ok, _ = _AssembleInstanceDisks(self, self.instance, disks=[disk])
10360 raise errors.OpExecError("Cannot activate block device to grow")
10362 # First run all grow ops in dry-run mode
10363 for node in instance.all_nodes:
10364 self.cfg.SetDiskID(disk, node)
10365 result = self.rpc.call_blockdev_grow(node, disk, self.op.amount, True)
10366 result.Raise("Grow request failed to node %s" % node)
10368 # We know that (as far as we can test) operations across different
10369 # nodes will succeed, time to run it for real
10370 for node in instance.all_nodes:
10371 self.cfg.SetDiskID(disk, node)
10372 result = self.rpc.call_blockdev_grow(node, disk, self.op.amount, False)
10373 result.Raise("Grow request failed to node %s" % node)
10375 # TODO: Rewrite code to work properly
10376 # DRBD goes into sync mode for a short amount of time after executing the
10377 # "resize" command. DRBD 8.x below version 8.0.13 contains a bug whereby
10378 # calling "resize" in sync mode fails. Sleeping for a short amount of
10379 # time is a work-around.
10382 disk.RecordGrow(self.op.amount)
10383 self.cfg.Update(instance, feedback_fn)
10384 if self.op.wait_for_sync:
10385 disk_abort = not _WaitForSync(self, instance, disks=[disk])
10387 self.proc.LogWarning("Disk sync-ing has not returned a good"
10388 " status; please check the instance")
10389 if not instance.admin_up:
10390 _SafeShutdownInstanceDisks(self, instance, disks=[disk])
10391 elif not instance.admin_up:
10392 self.proc.LogWarning("Not shutting down the disk even if the instance is"
10393 " not supposed to be running because no wait for"
10394 " sync mode was requested")
10397 class LUInstanceQueryData(NoHooksLU):
10398 """Query runtime instance data.
10403 def ExpandNames(self):
10404 self.needed_locks = {}
10406 # Use locking if requested or when non-static information is wanted
10407 if not (self.op.static or self.op.use_locking):
10408 self.LogWarning("Non-static data requested, locks need to be acquired")
10409 self.op.use_locking = True
10411 if self.op.instances or not self.op.use_locking:
10412 # Expand instance names right here
10413 self.wanted_names = _GetWantedInstances(self, self.op.instances)
10415 # Will use acquired locks
10416 self.wanted_names = None
10418 if self.op.use_locking:
10419 self.share_locks = _ShareAll()
10421 if self.wanted_names is None:
10422 self.needed_locks[locking.LEVEL_INSTANCE] = locking.ALL_SET
10424 self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted_names
10426 self.needed_locks[locking.LEVEL_NODE] = []
10427 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
10429 def DeclareLocks(self, level):
10430 if self.op.use_locking and level == locking.LEVEL_NODE:
10431 self._LockInstancesNodes()
10433 def CheckPrereq(self):
10434 """Check prerequisites.
10436 This only checks the optional instance list against the existing names.
10439 if self.wanted_names is None:
10440 assert self.op.use_locking, "Locking was not used"
10441 self.wanted_names = self.owned_locks(locking.LEVEL_INSTANCE)
10443 self.wanted_instances = \
10444 map(compat.snd, self.cfg.GetMultiInstanceInfo(self.wanted_names))
10446 def _ComputeBlockdevStatus(self, node, instance_name, dev):
10447 """Returns the status of a block device
10450 if self.op.static or not node:
10453 self.cfg.SetDiskID(dev, node)
10455 result = self.rpc.call_blockdev_find(node, dev)
10459 result.Raise("Can't compute disk status for %s" % instance_name)
10461 status = result.payload
10465 return (status.dev_path, status.major, status.minor,
10466 status.sync_percent, status.estimated_time,
10467 status.is_degraded, status.ldisk_status)
10469 def _ComputeDiskStatus(self, instance, snode, dev):
10470 """Compute block device status.
10473 if dev.dev_type in constants.LDS_DRBD:
10474 # we change the snode then (otherwise we use the one passed in)
10475 if dev.logical_id[0] == instance.primary_node:
10476 snode = dev.logical_id[1]
10478 snode = dev.logical_id[0]
10480 dev_pstatus = self._ComputeBlockdevStatus(instance.primary_node,
10481 instance.name, dev)
10482 dev_sstatus = self._ComputeBlockdevStatus(snode, instance.name, dev)
10485 dev_children = map(compat.partial(self._ComputeDiskStatus,
10492 "iv_name": dev.iv_name,
10493 "dev_type": dev.dev_type,
10494 "logical_id": dev.logical_id,
10495 "physical_id": dev.physical_id,
10496 "pstatus": dev_pstatus,
10497 "sstatus": dev_sstatus,
10498 "children": dev_children,
10503 def Exec(self, feedback_fn):
10504 """Gather and return data"""
10507 cluster = self.cfg.GetClusterInfo()
10509 pri_nodes = self.cfg.GetMultiNodeInfo(i.primary_node
10510 for i in self.wanted_instances)
10511 for instance, (_, pnode) in zip(self.wanted_instances, pri_nodes):
10512 if self.op.static or pnode.offline:
10513 remote_state = None
10515 self.LogWarning("Primary node %s is marked offline, returning static"
10516 " information only for instance %s" %
10517 (pnode.name, instance.name))
10519 remote_info = self.rpc.call_instance_info(instance.primary_node,
10521 instance.hypervisor)
10522 remote_info.Raise("Error checking node %s" % instance.primary_node)
10523 remote_info = remote_info.payload
10524 if remote_info and "state" in remote_info:
10525 remote_state = "up"
10527 remote_state = "down"
10529 if instance.admin_up:
10530 config_state = "up"
10532 config_state = "down"
10534 disks = map(compat.partial(self._ComputeDiskStatus, instance, None),
10537 result[instance.name] = {
10538 "name": instance.name,
10539 "config_state": config_state,
10540 "run_state": remote_state,
10541 "pnode": instance.primary_node,
10542 "snodes": instance.secondary_nodes,
10544 # this happens to be the same format used for hooks
10545 "nics": _NICListToTuple(self, instance.nics),
10546 "disk_template": instance.disk_template,
10548 "hypervisor": instance.hypervisor,
10549 "network_port": instance.network_port,
10550 "hv_instance": instance.hvparams,
10551 "hv_actual": cluster.FillHV(instance, skip_globals=True),
10552 "be_instance": instance.beparams,
10553 "be_actual": cluster.FillBE(instance),
10554 "os_instance": instance.osparams,
10555 "os_actual": cluster.SimpleFillOS(instance.os, instance.osparams),
10556 "serial_no": instance.serial_no,
10557 "mtime": instance.mtime,
10558 "ctime": instance.ctime,
10559 "uuid": instance.uuid,
10565 class LUInstanceSetParams(LogicalUnit):
10566 """Modifies an instances's parameters.
10569 HPATH = "instance-modify"
10570 HTYPE = constants.HTYPE_INSTANCE
10573 def CheckArguments(self):
10574 if not (self.op.nics or self.op.disks or self.op.disk_template or
10575 self.op.hvparams or self.op.beparams or self.op.os_name):
10576 raise errors.OpPrereqError("No changes submitted", errors.ECODE_INVAL)
10578 if self.op.hvparams:
10579 _CheckGlobalHvParams(self.op.hvparams)
10583 for disk_op, disk_dict in self.op.disks:
10584 utils.ForceDictType(disk_dict, constants.IDISK_PARAMS_TYPES)
10585 if disk_op == constants.DDM_REMOVE:
10586 disk_addremove += 1
10588 elif disk_op == constants.DDM_ADD:
10589 disk_addremove += 1
10591 if not isinstance(disk_op, int):
10592 raise errors.OpPrereqError("Invalid disk index", errors.ECODE_INVAL)
10593 if not isinstance(disk_dict, dict):
10594 msg = "Invalid disk value: expected dict, got '%s'" % disk_dict
10595 raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
10597 if disk_op == constants.DDM_ADD:
10598 mode = disk_dict.setdefault(constants.IDISK_MODE, constants.DISK_RDWR)
10599 if mode not in constants.DISK_ACCESS_SET:
10600 raise errors.OpPrereqError("Invalid disk access mode '%s'" % mode,
10601 errors.ECODE_INVAL)
10602 size = disk_dict.get(constants.IDISK_SIZE, None)
10604 raise errors.OpPrereqError("Required disk parameter size missing",
10605 errors.ECODE_INVAL)
10608 except (TypeError, ValueError), err:
10609 raise errors.OpPrereqError("Invalid disk size parameter: %s" %
10610 str(err), errors.ECODE_INVAL)
10611 disk_dict[constants.IDISK_SIZE] = size
10613 # modification of disk
10614 if constants.IDISK_SIZE in disk_dict:
10615 raise errors.OpPrereqError("Disk size change not possible, use"
10616 " grow-disk", errors.ECODE_INVAL)
10618 if disk_addremove > 1:
10619 raise errors.OpPrereqError("Only one disk add or remove operation"
10620 " supported at a time", errors.ECODE_INVAL)
10622 if self.op.disks and self.op.disk_template is not None:
10623 raise errors.OpPrereqError("Disk template conversion and other disk"
10624 " changes not supported at the same time",
10625 errors.ECODE_INVAL)
10627 if (self.op.disk_template and
10628 self.op.disk_template in constants.DTS_INT_MIRROR and
10629 self.op.remote_node is None):
10630 raise errors.OpPrereqError("Changing the disk template to a mirrored"
10631 " one requires specifying a secondary node",
10632 errors.ECODE_INVAL)
10636 for nic_op, nic_dict in self.op.nics:
10637 utils.ForceDictType(nic_dict, constants.INIC_PARAMS_TYPES)
10638 if nic_op == constants.DDM_REMOVE:
10641 elif nic_op == constants.DDM_ADD:
10644 if not isinstance(nic_op, int):
10645 raise errors.OpPrereqError("Invalid nic index", errors.ECODE_INVAL)
10646 if not isinstance(nic_dict, dict):
10647 msg = "Invalid nic value: expected dict, got '%s'" % nic_dict
10648 raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
10650 # nic_dict should be a dict
10651 nic_ip = nic_dict.get(constants.INIC_IP, None)
10652 if nic_ip is not None:
10653 if nic_ip.lower() == constants.VALUE_NONE:
10654 nic_dict[constants.INIC_IP] = None
10656 if not netutils.IPAddress.IsValid(nic_ip):
10657 raise errors.OpPrereqError("Invalid IP address '%s'" % nic_ip,
10658 errors.ECODE_INVAL)
10660 nic_bridge = nic_dict.get("bridge", None)
10661 nic_link = nic_dict.get(constants.INIC_LINK, None)
10662 if nic_bridge and nic_link:
10663 raise errors.OpPrereqError("Cannot pass 'bridge' and 'link'"
10664 " at the same time", errors.ECODE_INVAL)
10665 elif nic_bridge and nic_bridge.lower() == constants.VALUE_NONE:
10666 nic_dict["bridge"] = None
10667 elif nic_link and nic_link.lower() == constants.VALUE_NONE:
10668 nic_dict[constants.INIC_LINK] = None
10670 if nic_op == constants.DDM_ADD:
10671 nic_mac = nic_dict.get(constants.INIC_MAC, None)
10672 if nic_mac is None:
10673 nic_dict[constants.INIC_MAC] = constants.VALUE_AUTO
10675 if constants.INIC_MAC in nic_dict:
10676 nic_mac = nic_dict[constants.INIC_MAC]
10677 if nic_mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
10678 nic_mac = utils.NormalizeAndValidateMac(nic_mac)
10680 if nic_op != constants.DDM_ADD and nic_mac == constants.VALUE_AUTO:
10681 raise errors.OpPrereqError("'auto' is not a valid MAC address when"
10682 " modifying an existing nic",
10683 errors.ECODE_INVAL)
10685 if nic_addremove > 1:
10686 raise errors.OpPrereqError("Only one NIC add or remove operation"
10687 " supported at a time", errors.ECODE_INVAL)
10689 def ExpandNames(self):
10690 self._ExpandAndLockInstance()
10691 self.needed_locks[locking.LEVEL_NODE] = []
10692 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
10694 def DeclareLocks(self, level):
10695 if level == locking.LEVEL_NODE:
10696 self._LockInstancesNodes()
10697 if self.op.disk_template and self.op.remote_node:
10698 self.op.remote_node = _ExpandNodeName(self.cfg, self.op.remote_node)
10699 self.needed_locks[locking.LEVEL_NODE].append(self.op.remote_node)
10701 def BuildHooksEnv(self):
10702 """Build hooks env.
10704 This runs on the master, primary and secondaries.
10708 if constants.BE_MEMORY in self.be_new:
10709 args["memory"] = self.be_new[constants.BE_MEMORY]
10710 if constants.BE_VCPUS in self.be_new:
10711 args["vcpus"] = self.be_new[constants.BE_VCPUS]
10712 # TODO: export disk changes. Note: _BuildInstanceHookEnv* don't export disk
10713 # information at all.
10716 nic_override = dict(self.op.nics)
10717 for idx, nic in enumerate(self.instance.nics):
10718 if idx in nic_override:
10719 this_nic_override = nic_override[idx]
10721 this_nic_override = {}
10722 if constants.INIC_IP in this_nic_override:
10723 ip = this_nic_override[constants.INIC_IP]
10726 if constants.INIC_MAC in this_nic_override:
10727 mac = this_nic_override[constants.INIC_MAC]
10730 if idx in self.nic_pnew:
10731 nicparams = self.nic_pnew[idx]
10733 nicparams = self.cluster.SimpleFillNIC(nic.nicparams)
10734 mode = nicparams[constants.NIC_MODE]
10735 link = nicparams[constants.NIC_LINK]
10736 args["nics"].append((ip, mac, mode, link))
10737 if constants.DDM_ADD in nic_override:
10738 ip = nic_override[constants.DDM_ADD].get(constants.INIC_IP, None)
10739 mac = nic_override[constants.DDM_ADD][constants.INIC_MAC]
10740 nicparams = self.nic_pnew[constants.DDM_ADD]
10741 mode = nicparams[constants.NIC_MODE]
10742 link = nicparams[constants.NIC_LINK]
10743 args["nics"].append((ip, mac, mode, link))
10744 elif constants.DDM_REMOVE in nic_override:
10745 del args["nics"][-1]
10747 env = _BuildInstanceHookEnvByObject(self, self.instance, override=args)
10748 if self.op.disk_template:
10749 env["NEW_DISK_TEMPLATE"] = self.op.disk_template
10753 def BuildHooksNodes(self):
10754 """Build hooks nodes.
10757 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
10760 def CheckPrereq(self):
10761 """Check prerequisites.
10763 This only checks the instance list against the existing names.
10766 # checking the new params on the primary/secondary nodes
10768 instance = self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
10769 cluster = self.cluster = self.cfg.GetClusterInfo()
10770 assert self.instance is not None, \
10771 "Cannot retrieve locked instance %s" % self.op.instance_name
10772 pnode = instance.primary_node
10773 nodelist = list(instance.all_nodes)
10776 if self.op.os_name and not self.op.force:
10777 _CheckNodeHasOS(self, instance.primary_node, self.op.os_name,
10778 self.op.force_variant)
10779 instance_os = self.op.os_name
10781 instance_os = instance.os
10783 if self.op.disk_template:
10784 if instance.disk_template == self.op.disk_template:
10785 raise errors.OpPrereqError("Instance already has disk template %s" %
10786 instance.disk_template, errors.ECODE_INVAL)
10788 if (instance.disk_template,
10789 self.op.disk_template) not in self._DISK_CONVERSIONS:
10790 raise errors.OpPrereqError("Unsupported disk template conversion from"
10791 " %s to %s" % (instance.disk_template,
10792 self.op.disk_template),
10793 errors.ECODE_INVAL)
10794 _CheckInstanceDown(self, instance, "cannot change disk template")
10795 if self.op.disk_template in constants.DTS_INT_MIRROR:
10796 if self.op.remote_node == pnode:
10797 raise errors.OpPrereqError("Given new secondary node %s is the same"
10798 " as the primary node of the instance" %
10799 self.op.remote_node, errors.ECODE_STATE)
10800 _CheckNodeOnline(self, self.op.remote_node)
10801 _CheckNodeNotDrained(self, self.op.remote_node)
10802 # FIXME: here we assume that the old instance type is DT_PLAIN
10803 assert instance.disk_template == constants.DT_PLAIN
10804 disks = [{constants.IDISK_SIZE: d.size,
10805 constants.IDISK_VG: d.logical_id[0]}
10806 for d in instance.disks]
10807 required = _ComputeDiskSizePerVG(self.op.disk_template, disks)
10808 _CheckNodesFreeDiskPerVG(self, [self.op.remote_node], required)
10810 # hvparams processing
10811 if self.op.hvparams:
10812 hv_type = instance.hypervisor
10813 i_hvdict = _GetUpdatedParams(instance.hvparams, self.op.hvparams)
10814 utils.ForceDictType(i_hvdict, constants.HVS_PARAMETER_TYPES)
10815 hv_new = cluster.SimpleFillHV(hv_type, instance.os, i_hvdict)
10818 hypervisor.GetHypervisor(hv_type).CheckParameterSyntax(hv_new)
10819 _CheckHVParams(self, nodelist, instance.hypervisor, hv_new)
10820 self.hv_proposed = self.hv_new = hv_new # the new actual values
10821 self.hv_inst = i_hvdict # the new dict (without defaults)
10823 self.hv_proposed = cluster.SimpleFillHV(instance.hypervisor, instance.os,
10825 self.hv_new = self.hv_inst = {}
10827 # beparams processing
10828 if self.op.beparams:
10829 i_bedict = _GetUpdatedParams(instance.beparams, self.op.beparams,
10831 utils.ForceDictType(i_bedict, constants.BES_PARAMETER_TYPES)
10832 be_new = cluster.SimpleFillBE(i_bedict)
10833 self.be_proposed = self.be_new = be_new # the new actual values
10834 self.be_inst = i_bedict # the new dict (without defaults)
10836 self.be_new = self.be_inst = {}
10837 self.be_proposed = cluster.SimpleFillBE(instance.beparams)
10838 be_old = cluster.FillBE(instance)
10840 # CPU param validation -- checking every time a paramtere is
10841 # changed to cover all cases where either CPU mask or vcpus have
10843 if (constants.BE_VCPUS in self.be_proposed and
10844 constants.HV_CPU_MASK in self.hv_proposed):
10846 utils.ParseMultiCpuMask(self.hv_proposed[constants.HV_CPU_MASK])
10847 # Verify mask is consistent with number of vCPUs. Can skip this
10848 # test if only 1 entry in the CPU mask, which means same mask
10849 # is applied to all vCPUs.
10850 if (len(cpu_list) > 1 and
10851 len(cpu_list) != self.be_proposed[constants.BE_VCPUS]):
10852 raise errors.OpPrereqError("Number of vCPUs [%d] does not match the"
10854 (self.be_proposed[constants.BE_VCPUS],
10855 self.hv_proposed[constants.HV_CPU_MASK]),
10856 errors.ECODE_INVAL)
10858 # Only perform this test if a new CPU mask is given
10859 if constants.HV_CPU_MASK in self.hv_new:
10860 # Calculate the largest CPU number requested
10861 max_requested_cpu = max(map(max, cpu_list))
10862 # Check that all of the instance's nodes have enough physical CPUs to
10863 # satisfy the requested CPU mask
10864 _CheckNodesPhysicalCPUs(self, instance.all_nodes,
10865 max_requested_cpu + 1, instance.hypervisor)
10867 # osparams processing
10868 if self.op.osparams:
10869 i_osdict = _GetUpdatedParams(instance.osparams, self.op.osparams)
10870 _CheckOSParams(self, True, nodelist, instance_os, i_osdict)
10871 self.os_inst = i_osdict # the new dict (without defaults)
10877 if (constants.BE_MEMORY in self.op.beparams and not self.op.force and
10878 be_new[constants.BE_MEMORY] > be_old[constants.BE_MEMORY]):
10879 mem_check_list = [pnode]
10880 if be_new[constants.BE_AUTO_BALANCE]:
10881 # either we changed auto_balance to yes or it was from before
10882 mem_check_list.extend(instance.secondary_nodes)
10883 instance_info = self.rpc.call_instance_info(pnode, instance.name,
10884 instance.hypervisor)
10885 nodeinfo = self.rpc.call_node_info(mem_check_list, None,
10886 instance.hypervisor)
10887 pninfo = nodeinfo[pnode]
10888 msg = pninfo.fail_msg
10890 # Assume the primary node is unreachable and go ahead
10891 self.warn.append("Can't get info from primary node %s: %s" %
10893 elif not isinstance(pninfo.payload.get("memory_free", None), int):
10894 self.warn.append("Node data from primary node %s doesn't contain"
10895 " free memory information" % pnode)
10896 elif instance_info.fail_msg:
10897 self.warn.append("Can't get instance runtime information: %s" %
10898 instance_info.fail_msg)
10900 if instance_info.payload:
10901 current_mem = int(instance_info.payload["memory"])
10903 # Assume instance not running
10904 # (there is a slight race condition here, but it's not very probable,
10905 # and we have no other way to check)
10907 miss_mem = (be_new[constants.BE_MEMORY] - current_mem -
10908 pninfo.payload["memory_free"])
10910 raise errors.OpPrereqError("This change will prevent the instance"
10911 " from starting, due to %d MB of memory"
10912 " missing on its primary node" % miss_mem,
10913 errors.ECODE_NORES)
10915 if be_new[constants.BE_AUTO_BALANCE]:
10916 for node, nres in nodeinfo.items():
10917 if node not in instance.secondary_nodes:
10919 nres.Raise("Can't get info from secondary node %s" % node,
10920 prereq=True, ecode=errors.ECODE_STATE)
10921 if not isinstance(nres.payload.get("memory_free", None), int):
10922 raise errors.OpPrereqError("Secondary node %s didn't return free"
10923 " memory information" % node,
10924 errors.ECODE_STATE)
10925 elif be_new[constants.BE_MEMORY] > nres.payload["memory_free"]:
10926 raise errors.OpPrereqError("This change will prevent the instance"
10927 " from failover to its secondary node"
10928 " %s, due to not enough memory" % node,
10929 errors.ECODE_STATE)
10933 self.nic_pinst = {}
10934 for nic_op, nic_dict in self.op.nics:
10935 if nic_op == constants.DDM_REMOVE:
10936 if not instance.nics:
10937 raise errors.OpPrereqError("Instance has no NICs, cannot remove",
10938 errors.ECODE_INVAL)
10940 if nic_op != constants.DDM_ADD:
10942 if not instance.nics:
10943 raise errors.OpPrereqError("Invalid NIC index %s, instance has"
10944 " no NICs" % nic_op,
10945 errors.ECODE_INVAL)
10946 if nic_op < 0 or nic_op >= len(instance.nics):
10947 raise errors.OpPrereqError("Invalid NIC index %s, valid values"
10949 (nic_op, len(instance.nics) - 1),
10950 errors.ECODE_INVAL)
10951 old_nic_params = instance.nics[nic_op].nicparams
10952 old_nic_ip = instance.nics[nic_op].ip
10954 old_nic_params = {}
10957 update_params_dict = dict([(key, nic_dict[key])
10958 for key in constants.NICS_PARAMETERS
10959 if key in nic_dict])
10961 if "bridge" in nic_dict:
10962 update_params_dict[constants.NIC_LINK] = nic_dict["bridge"]
10964 new_nic_params = _GetUpdatedParams(old_nic_params,
10965 update_params_dict)
10966 utils.ForceDictType(new_nic_params, constants.NICS_PARAMETER_TYPES)
10967 new_filled_nic_params = cluster.SimpleFillNIC(new_nic_params)
10968 objects.NIC.CheckParameterSyntax(new_filled_nic_params)
10969 self.nic_pinst[nic_op] = new_nic_params
10970 self.nic_pnew[nic_op] = new_filled_nic_params
10971 new_nic_mode = new_filled_nic_params[constants.NIC_MODE]
10973 if new_nic_mode == constants.NIC_MODE_BRIDGED:
10974 nic_bridge = new_filled_nic_params[constants.NIC_LINK]
10975 msg = self.rpc.call_bridges_exist(pnode, [nic_bridge]).fail_msg
10977 msg = "Error checking bridges on node %s: %s" % (pnode, msg)
10979 self.warn.append(msg)
10981 raise errors.OpPrereqError(msg, errors.ECODE_ENVIRON)
10982 if new_nic_mode == constants.NIC_MODE_ROUTED:
10983 if constants.INIC_IP in nic_dict:
10984 nic_ip = nic_dict[constants.INIC_IP]
10986 nic_ip = old_nic_ip
10988 raise errors.OpPrereqError("Cannot set the nic ip to None"
10989 " on a routed nic", errors.ECODE_INVAL)
10990 if constants.INIC_MAC in nic_dict:
10991 nic_mac = nic_dict[constants.INIC_MAC]
10992 if nic_mac is None:
10993 raise errors.OpPrereqError("Cannot set the nic mac to None",
10994 errors.ECODE_INVAL)
10995 elif nic_mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
10996 # otherwise generate the mac
10997 nic_dict[constants.INIC_MAC] = \
10998 self.cfg.GenerateMAC(self.proc.GetECId())
11000 # or validate/reserve the current one
11002 self.cfg.ReserveMAC(nic_mac, self.proc.GetECId())
11003 except errors.ReservationError:
11004 raise errors.OpPrereqError("MAC address %s already in use"
11005 " in cluster" % nic_mac,
11006 errors.ECODE_NOTUNIQUE)
11009 if self.op.disks and instance.disk_template == constants.DT_DISKLESS:
11010 raise errors.OpPrereqError("Disk operations not supported for"
11011 " diskless instances",
11012 errors.ECODE_INVAL)
11013 for disk_op, _ in self.op.disks:
11014 if disk_op == constants.DDM_REMOVE:
11015 if len(instance.disks) == 1:
11016 raise errors.OpPrereqError("Cannot remove the last disk of"
11017 " an instance", errors.ECODE_INVAL)
11018 _CheckInstanceDown(self, instance, "cannot remove disks")
11020 if (disk_op == constants.DDM_ADD and
11021 len(instance.disks) >= constants.MAX_DISKS):
11022 raise errors.OpPrereqError("Instance has too many disks (%d), cannot"
11023 " add more" % constants.MAX_DISKS,
11024 errors.ECODE_STATE)
11025 if disk_op not in (constants.DDM_ADD, constants.DDM_REMOVE):
11027 if disk_op < 0 or disk_op >= len(instance.disks):
11028 raise errors.OpPrereqError("Invalid disk index %s, valid values"
11030 (disk_op, len(instance.disks)),
11031 errors.ECODE_INVAL)
11035 def _ConvertPlainToDrbd(self, feedback_fn):
11036 """Converts an instance from plain to drbd.
11039 feedback_fn("Converting template to drbd")
11040 instance = self.instance
11041 pnode = instance.primary_node
11042 snode = self.op.remote_node
11044 # create a fake disk info for _GenerateDiskTemplate
11045 disk_info = [{constants.IDISK_SIZE: d.size, constants.IDISK_MODE: d.mode,
11046 constants.IDISK_VG: d.logical_id[0]}
11047 for d in instance.disks]
11048 new_disks = _GenerateDiskTemplate(self, self.op.disk_template,
11049 instance.name, pnode, [snode],
11050 disk_info, None, None, 0, feedback_fn)
11051 info = _GetInstanceInfoText(instance)
11052 feedback_fn("Creating aditional volumes...")
11053 # first, create the missing data and meta devices
11054 for disk in new_disks:
11055 # unfortunately this is... not too nice
11056 _CreateSingleBlockDev(self, pnode, instance, disk.children[1],
11058 for child in disk.children:
11059 _CreateSingleBlockDev(self, snode, instance, child, info, True)
11060 # at this stage, all new LVs have been created, we can rename the
11062 feedback_fn("Renaming original volumes...")
11063 rename_list = [(o, n.children[0].logical_id)
11064 for (o, n) in zip(instance.disks, new_disks)]
11065 result = self.rpc.call_blockdev_rename(pnode, rename_list)
11066 result.Raise("Failed to rename original LVs")
11068 feedback_fn("Initializing DRBD devices...")
11069 # all child devices are in place, we can now create the DRBD devices
11070 for disk in new_disks:
11071 for node in [pnode, snode]:
11072 f_create = node == pnode
11073 _CreateSingleBlockDev(self, node, instance, disk, info, f_create)
11075 # at this point, the instance has been modified
11076 instance.disk_template = constants.DT_DRBD8
11077 instance.disks = new_disks
11078 self.cfg.Update(instance, feedback_fn)
11080 # disks are created, waiting for sync
11081 disk_abort = not _WaitForSync(self, instance,
11082 oneshot=not self.op.wait_for_sync)
11084 raise errors.OpExecError("There are some degraded disks for"
11085 " this instance, please cleanup manually")
11087 def _ConvertDrbdToPlain(self, feedback_fn):
11088 """Converts an instance from drbd to plain.
11091 instance = self.instance
11092 assert len(instance.secondary_nodes) == 1
11093 pnode = instance.primary_node
11094 snode = instance.secondary_nodes[0]
11095 feedback_fn("Converting template to plain")
11097 old_disks = instance.disks
11098 new_disks = [d.children[0] for d in old_disks]
11100 # copy over size and mode
11101 for parent, child in zip(old_disks, new_disks):
11102 child.size = parent.size
11103 child.mode = parent.mode
11105 # update instance structure
11106 instance.disks = new_disks
11107 instance.disk_template = constants.DT_PLAIN
11108 self.cfg.Update(instance, feedback_fn)
11110 feedback_fn("Removing volumes on the secondary node...")
11111 for disk in old_disks:
11112 self.cfg.SetDiskID(disk, snode)
11113 msg = self.rpc.call_blockdev_remove(snode, disk).fail_msg
11115 self.LogWarning("Could not remove block device %s on node %s,"
11116 " continuing anyway: %s", disk.iv_name, snode, msg)
11118 feedback_fn("Removing unneeded volumes on the primary node...")
11119 for idx, disk in enumerate(old_disks):
11120 meta = disk.children[1]
11121 self.cfg.SetDiskID(meta, pnode)
11122 msg = self.rpc.call_blockdev_remove(pnode, meta).fail_msg
11124 self.LogWarning("Could not remove metadata for disk %d on node %s,"
11125 " continuing anyway: %s", idx, pnode, msg)
11127 def Exec(self, feedback_fn):
11128 """Modifies an instance.
11130 All parameters take effect only at the next restart of the instance.
11133 # Process here the warnings from CheckPrereq, as we don't have a
11134 # feedback_fn there.
11135 for warn in self.warn:
11136 feedback_fn("WARNING: %s" % warn)
11139 instance = self.instance
11141 for disk_op, disk_dict in self.op.disks:
11142 if disk_op == constants.DDM_REMOVE:
11143 # remove the last disk
11144 device = instance.disks.pop()
11145 device_idx = len(instance.disks)
11146 for node, disk in device.ComputeNodeTree(instance.primary_node):
11147 self.cfg.SetDiskID(disk, node)
11148 msg = self.rpc.call_blockdev_remove(node, disk).fail_msg
11150 self.LogWarning("Could not remove disk/%d on node %s: %s,"
11151 " continuing anyway", device_idx, node, msg)
11152 result.append(("disk/%d" % device_idx, "remove"))
11153 elif disk_op == constants.DDM_ADD:
11155 if instance.disk_template in (constants.DT_FILE,
11156 constants.DT_SHARED_FILE):
11157 file_driver, file_path = instance.disks[0].logical_id
11158 file_path = os.path.dirname(file_path)
11160 file_driver = file_path = None
11161 disk_idx_base = len(instance.disks)
11162 new_disk = _GenerateDiskTemplate(self,
11163 instance.disk_template,
11164 instance.name, instance.primary_node,
11165 instance.secondary_nodes,
11169 disk_idx_base, feedback_fn)[0]
11170 instance.disks.append(new_disk)
11171 info = _GetInstanceInfoText(instance)
11173 logging.info("Creating volume %s for instance %s",
11174 new_disk.iv_name, instance.name)
11175 # Note: this needs to be kept in sync with _CreateDisks
11177 for node in instance.all_nodes:
11178 f_create = node == instance.primary_node
11180 _CreateBlockDev(self, node, instance, new_disk,
11181 f_create, info, f_create)
11182 except errors.OpExecError, err:
11183 self.LogWarning("Failed to create volume %s (%s) on"
11185 new_disk.iv_name, new_disk, node, err)
11186 result.append(("disk/%d" % disk_idx_base, "add:size=%s,mode=%s" %
11187 (new_disk.size, new_disk.mode)))
11189 # change a given disk
11190 instance.disks[disk_op].mode = disk_dict[constants.IDISK_MODE]
11191 result.append(("disk.mode/%d" % disk_op,
11192 disk_dict[constants.IDISK_MODE]))
11194 if self.op.disk_template:
11195 r_shut = _ShutdownInstanceDisks(self, instance)
11197 raise errors.OpExecError("Cannot shutdown instance disks, unable to"
11198 " proceed with disk template conversion")
11199 mode = (instance.disk_template, self.op.disk_template)
11201 self._DISK_CONVERSIONS[mode](self, feedback_fn)
11203 self.cfg.ReleaseDRBDMinors(instance.name)
11205 result.append(("disk_template", self.op.disk_template))
11208 for nic_op, nic_dict in self.op.nics:
11209 if nic_op == constants.DDM_REMOVE:
11210 # remove the last nic
11211 del instance.nics[-1]
11212 result.append(("nic.%d" % len(instance.nics), "remove"))
11213 elif nic_op == constants.DDM_ADD:
11214 # mac and bridge should be set, by now
11215 mac = nic_dict[constants.INIC_MAC]
11216 ip = nic_dict.get(constants.INIC_IP, None)
11217 nicparams = self.nic_pinst[constants.DDM_ADD]
11218 new_nic = objects.NIC(mac=mac, ip=ip, nicparams=nicparams)
11219 instance.nics.append(new_nic)
11220 result.append(("nic.%d" % (len(instance.nics) - 1),
11221 "add:mac=%s,ip=%s,mode=%s,link=%s" %
11222 (new_nic.mac, new_nic.ip,
11223 self.nic_pnew[constants.DDM_ADD][constants.NIC_MODE],
11224 self.nic_pnew[constants.DDM_ADD][constants.NIC_LINK]
11227 for key in (constants.INIC_MAC, constants.INIC_IP):
11228 if key in nic_dict:
11229 setattr(instance.nics[nic_op], key, nic_dict[key])
11230 if nic_op in self.nic_pinst:
11231 instance.nics[nic_op].nicparams = self.nic_pinst[nic_op]
11232 for key, val in nic_dict.iteritems():
11233 result.append(("nic.%s/%d" % (key, nic_op), val))
11236 if self.op.hvparams:
11237 instance.hvparams = self.hv_inst
11238 for key, val in self.op.hvparams.iteritems():
11239 result.append(("hv/%s" % key, val))
11242 if self.op.beparams:
11243 instance.beparams = self.be_inst
11244 for key, val in self.op.beparams.iteritems():
11245 result.append(("be/%s" % key, val))
11248 if self.op.os_name:
11249 instance.os = self.op.os_name
11252 if self.op.osparams:
11253 instance.osparams = self.os_inst
11254 for key, val in self.op.osparams.iteritems():
11255 result.append(("os/%s" % key, val))
11257 self.cfg.Update(instance, feedback_fn)
11261 _DISK_CONVERSIONS = {
11262 (constants.DT_PLAIN, constants.DT_DRBD8): _ConvertPlainToDrbd,
11263 (constants.DT_DRBD8, constants.DT_PLAIN): _ConvertDrbdToPlain,
11267 class LUInstanceChangeGroup(LogicalUnit):
11268 HPATH = "instance-change-group"
11269 HTYPE = constants.HTYPE_INSTANCE
11272 def ExpandNames(self):
11273 self.share_locks = _ShareAll()
11274 self.needed_locks = {
11275 locking.LEVEL_NODEGROUP: [],
11276 locking.LEVEL_NODE: [],
11279 self._ExpandAndLockInstance()
11281 if self.op.target_groups:
11282 self.req_target_uuids = map(self.cfg.LookupNodeGroup,
11283 self.op.target_groups)
11285 self.req_target_uuids = None
11287 self.op.iallocator = _GetDefaultIAllocator(self.cfg, self.op.iallocator)
11289 def DeclareLocks(self, level):
11290 if level == locking.LEVEL_NODEGROUP:
11291 assert not self.needed_locks[locking.LEVEL_NODEGROUP]
11293 if self.req_target_uuids:
11294 lock_groups = set(self.req_target_uuids)
11296 # Lock all groups used by instance optimistically; this requires going
11297 # via the node before it's locked, requiring verification later on
11298 instance_groups = self.cfg.GetInstanceNodeGroups(self.op.instance_name)
11299 lock_groups.update(instance_groups)
11301 # No target groups, need to lock all of them
11302 lock_groups = locking.ALL_SET
11304 self.needed_locks[locking.LEVEL_NODEGROUP] = lock_groups
11306 elif level == locking.LEVEL_NODE:
11307 if self.req_target_uuids:
11308 # Lock all nodes used by instances
11309 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
11310 self._LockInstancesNodes()
11312 # Lock all nodes in all potential target groups
11313 lock_groups = (frozenset(self.owned_locks(locking.LEVEL_NODEGROUP)) -
11314 self.cfg.GetInstanceNodeGroups(self.op.instance_name))
11315 member_nodes = [node_name
11316 for group in lock_groups
11317 for node_name in self.cfg.GetNodeGroup(group).members]
11318 self.needed_locks[locking.LEVEL_NODE].extend(member_nodes)
11320 # Lock all nodes as all groups are potential targets
11321 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
11323 def CheckPrereq(self):
11324 owned_instances = frozenset(self.owned_locks(locking.LEVEL_INSTANCE))
11325 owned_groups = frozenset(self.owned_locks(locking.LEVEL_NODEGROUP))
11326 owned_nodes = frozenset(self.owned_locks(locking.LEVEL_NODE))
11328 assert (self.req_target_uuids is None or
11329 owned_groups.issuperset(self.req_target_uuids))
11330 assert owned_instances == set([self.op.instance_name])
11332 # Get instance information
11333 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
11335 # Check if node groups for locked instance are still correct
11336 assert owned_nodes.issuperset(self.instance.all_nodes), \
11337 ("Instance %s's nodes changed while we kept the lock" %
11338 self.op.instance_name)
11340 inst_groups = _CheckInstanceNodeGroups(self.cfg, self.op.instance_name,
11343 if self.req_target_uuids:
11344 # User requested specific target groups
11345 self.target_uuids = self.req_target_uuids
11347 # All groups except those used by the instance are potential targets
11348 self.target_uuids = owned_groups - inst_groups
11350 conflicting_groups = self.target_uuids & inst_groups
11351 if conflicting_groups:
11352 raise errors.OpPrereqError("Can't use group(s) '%s' as targets, they are"
11353 " used by the instance '%s'" %
11354 (utils.CommaJoin(conflicting_groups),
11355 self.op.instance_name),
11356 errors.ECODE_INVAL)
11358 if not self.target_uuids:
11359 raise errors.OpPrereqError("There are no possible target groups",
11360 errors.ECODE_INVAL)
11362 def BuildHooksEnv(self):
11363 """Build hooks env.
11366 assert self.target_uuids
11369 "TARGET_GROUPS": " ".join(self.target_uuids),
11372 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
11376 def BuildHooksNodes(self):
11377 """Build hooks nodes.
11380 mn = self.cfg.GetMasterNode()
11381 return ([mn], [mn])
11383 def Exec(self, feedback_fn):
11384 instances = list(self.owned_locks(locking.LEVEL_INSTANCE))
11386 assert instances == [self.op.instance_name], "Instance not locked"
11388 ial = IAllocator(self.cfg, self.rpc, constants.IALLOCATOR_MODE_CHG_GROUP,
11389 instances=instances, target_groups=list(self.target_uuids))
11391 ial.Run(self.op.iallocator)
11393 if not ial.success:
11394 raise errors.OpPrereqError("Can't compute solution for changing group of"
11395 " instance '%s' using iallocator '%s': %s" %
11396 (self.op.instance_name, self.op.iallocator,
11398 errors.ECODE_NORES)
11400 jobs = _LoadNodeEvacResult(self, ial.result, self.op.early_release, False)
11402 self.LogInfo("Iallocator returned %s job(s) for changing group of"
11403 " instance '%s'", len(jobs), self.op.instance_name)
11405 return ResultWithJobs(jobs)
11408 class LUBackupQuery(NoHooksLU):
11409 """Query the exports list
11414 def ExpandNames(self):
11415 self.needed_locks = {}
11416 self.share_locks[locking.LEVEL_NODE] = 1
11417 if not self.op.nodes:
11418 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
11420 self.needed_locks[locking.LEVEL_NODE] = \
11421 _GetWantedNodes(self, self.op.nodes)
11423 def Exec(self, feedback_fn):
11424 """Compute the list of all the exported system images.
11427 @return: a dictionary with the structure node->(export-list)
11428 where export-list is a list of the instances exported on
11432 self.nodes = self.owned_locks(locking.LEVEL_NODE)
11433 rpcresult = self.rpc.call_export_list(self.nodes)
11435 for node in rpcresult:
11436 if rpcresult[node].fail_msg:
11437 result[node] = False
11439 result[node] = rpcresult[node].payload
11444 class LUBackupPrepare(NoHooksLU):
11445 """Prepares an instance for an export and returns useful information.
11450 def ExpandNames(self):
11451 self._ExpandAndLockInstance()
11453 def CheckPrereq(self):
11454 """Check prerequisites.
11457 instance_name = self.op.instance_name
11459 self.instance = self.cfg.GetInstanceInfo(instance_name)
11460 assert self.instance is not None, \
11461 "Cannot retrieve locked instance %s" % self.op.instance_name
11462 _CheckNodeOnline(self, self.instance.primary_node)
11464 self._cds = _GetClusterDomainSecret()
11466 def Exec(self, feedback_fn):
11467 """Prepares an instance for an export.
11470 instance = self.instance
11472 if self.op.mode == constants.EXPORT_MODE_REMOTE:
11473 salt = utils.GenerateSecret(8)
11475 feedback_fn("Generating X509 certificate on %s" % instance.primary_node)
11476 result = self.rpc.call_x509_cert_create(instance.primary_node,
11477 constants.RIE_CERT_VALIDITY)
11478 result.Raise("Can't create X509 key and certificate on %s" % result.node)
11480 (name, cert_pem) = result.payload
11482 cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
11486 "handshake": masterd.instance.ComputeRemoteExportHandshake(self._cds),
11487 "x509_key_name": (name, utils.Sha1Hmac(self._cds, name, salt=salt),
11489 "x509_ca": utils.SignX509Certificate(cert, self._cds, salt),
11495 class LUBackupExport(LogicalUnit):
11496 """Export an instance to an image in the cluster.
11499 HPATH = "instance-export"
11500 HTYPE = constants.HTYPE_INSTANCE
11503 def CheckArguments(self):
11504 """Check the arguments.
11507 self.x509_key_name = self.op.x509_key_name
11508 self.dest_x509_ca_pem = self.op.destination_x509_ca
11510 if self.op.mode == constants.EXPORT_MODE_REMOTE:
11511 if not self.x509_key_name:
11512 raise errors.OpPrereqError("Missing X509 key name for encryption",
11513 errors.ECODE_INVAL)
11515 if not self.dest_x509_ca_pem:
11516 raise errors.OpPrereqError("Missing destination X509 CA",
11517 errors.ECODE_INVAL)
11519 def ExpandNames(self):
11520 self._ExpandAndLockInstance()
11522 # Lock all nodes for local exports
11523 if self.op.mode == constants.EXPORT_MODE_LOCAL:
11524 # FIXME: lock only instance primary and destination node
11526 # Sad but true, for now we have do lock all nodes, as we don't know where
11527 # the previous export might be, and in this LU we search for it and
11528 # remove it from its current node. In the future we could fix this by:
11529 # - making a tasklet to search (share-lock all), then create the
11530 # new one, then one to remove, after
11531 # - removing the removal operation altogether
11532 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
11534 def DeclareLocks(self, level):
11535 """Last minute lock declaration."""
11536 # All nodes are locked anyway, so nothing to do here.
11538 def BuildHooksEnv(self):
11539 """Build hooks env.
11541 This will run on the master, primary node and target node.
11545 "EXPORT_MODE": self.op.mode,
11546 "EXPORT_NODE": self.op.target_node,
11547 "EXPORT_DO_SHUTDOWN": self.op.shutdown,
11548 "SHUTDOWN_TIMEOUT": self.op.shutdown_timeout,
11549 # TODO: Generic function for boolean env variables
11550 "REMOVE_INSTANCE": str(bool(self.op.remove_instance)),
11553 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
11557 def BuildHooksNodes(self):
11558 """Build hooks nodes.
11561 nl = [self.cfg.GetMasterNode(), self.instance.primary_node]
11563 if self.op.mode == constants.EXPORT_MODE_LOCAL:
11564 nl.append(self.op.target_node)
11568 def CheckPrereq(self):
11569 """Check prerequisites.
11571 This checks that the instance and node names are valid.
11574 instance_name = self.op.instance_name
11576 self.instance = self.cfg.GetInstanceInfo(instance_name)
11577 assert self.instance is not None, \
11578 "Cannot retrieve locked instance %s" % self.op.instance_name
11579 _CheckNodeOnline(self, self.instance.primary_node)
11581 if (self.op.remove_instance and self.instance.admin_up and
11582 not self.op.shutdown):
11583 raise errors.OpPrereqError("Can not remove instance without shutting it"
11586 if self.op.mode == constants.EXPORT_MODE_LOCAL:
11587 self.op.target_node = _ExpandNodeName(self.cfg, self.op.target_node)
11588 self.dst_node = self.cfg.GetNodeInfo(self.op.target_node)
11589 assert self.dst_node is not None
11591 _CheckNodeOnline(self, self.dst_node.name)
11592 _CheckNodeNotDrained(self, self.dst_node.name)
11595 self.dest_disk_info = None
11596 self.dest_x509_ca = None
11598 elif self.op.mode == constants.EXPORT_MODE_REMOTE:
11599 self.dst_node = None
11601 if len(self.op.target_node) != len(self.instance.disks):
11602 raise errors.OpPrereqError(("Received destination information for %s"
11603 " disks, but instance %s has %s disks") %
11604 (len(self.op.target_node), instance_name,
11605 len(self.instance.disks)),
11606 errors.ECODE_INVAL)
11608 cds = _GetClusterDomainSecret()
11610 # Check X509 key name
11612 (key_name, hmac_digest, hmac_salt) = self.x509_key_name
11613 except (TypeError, ValueError), err:
11614 raise errors.OpPrereqError("Invalid data for X509 key name: %s" % err)
11616 if not utils.VerifySha1Hmac(cds, key_name, hmac_digest, salt=hmac_salt):
11617 raise errors.OpPrereqError("HMAC for X509 key name is wrong",
11618 errors.ECODE_INVAL)
11620 # Load and verify CA
11622 (cert, _) = utils.LoadSignedX509Certificate(self.dest_x509_ca_pem, cds)
11623 except OpenSSL.crypto.Error, err:
11624 raise errors.OpPrereqError("Unable to load destination X509 CA (%s)" %
11625 (err, ), errors.ECODE_INVAL)
11627 (errcode, msg) = utils.VerifyX509Certificate(cert, None, None)
11628 if errcode is not None:
11629 raise errors.OpPrereqError("Invalid destination X509 CA (%s)" %
11630 (msg, ), errors.ECODE_INVAL)
11632 self.dest_x509_ca = cert
11634 # Verify target information
11636 for idx, disk_data in enumerate(self.op.target_node):
11638 (host, port, magic) = \
11639 masterd.instance.CheckRemoteExportDiskInfo(cds, idx, disk_data)
11640 except errors.GenericError, err:
11641 raise errors.OpPrereqError("Target info for disk %s: %s" %
11642 (idx, err), errors.ECODE_INVAL)
11644 disk_info.append((host, port, magic))
11646 assert len(disk_info) == len(self.op.target_node)
11647 self.dest_disk_info = disk_info
11650 raise errors.ProgrammerError("Unhandled export mode %r" %
11653 # instance disk type verification
11654 # TODO: Implement export support for file-based disks
11655 for disk in self.instance.disks:
11656 if disk.dev_type == constants.LD_FILE:
11657 raise errors.OpPrereqError("Export not supported for instances with"
11658 " file-based disks", errors.ECODE_INVAL)
11660 def _CleanupExports(self, feedback_fn):
11661 """Removes exports of current instance from all other nodes.
11663 If an instance in a cluster with nodes A..D was exported to node C, its
11664 exports will be removed from the nodes A, B and D.
11667 assert self.op.mode != constants.EXPORT_MODE_REMOTE
11669 nodelist = self.cfg.GetNodeList()
11670 nodelist.remove(self.dst_node.name)
11672 # on one-node clusters nodelist will be empty after the removal
11673 # if we proceed the backup would be removed because OpBackupQuery
11674 # substitutes an empty list with the full cluster node list.
11675 iname = self.instance.name
11677 feedback_fn("Removing old exports for instance %s" % iname)
11678 exportlist = self.rpc.call_export_list(nodelist)
11679 for node in exportlist:
11680 if exportlist[node].fail_msg:
11682 if iname in exportlist[node].payload:
11683 msg = self.rpc.call_export_remove(node, iname).fail_msg
11685 self.LogWarning("Could not remove older export for instance %s"
11686 " on node %s: %s", iname, node, msg)
11688 def Exec(self, feedback_fn):
11689 """Export an instance to an image in the cluster.
11692 assert self.op.mode in constants.EXPORT_MODES
11694 instance = self.instance
11695 src_node = instance.primary_node
11697 if self.op.shutdown:
11698 # shutdown the instance, but not the disks
11699 feedback_fn("Shutting down instance %s" % instance.name)
11700 result = self.rpc.call_instance_shutdown(src_node, instance,
11701 self.op.shutdown_timeout)
11702 # TODO: Maybe ignore failures if ignore_remove_failures is set
11703 result.Raise("Could not shutdown instance %s on"
11704 " node %s" % (instance.name, src_node))
11706 # set the disks ID correctly since call_instance_start needs the
11707 # correct drbd minor to create the symlinks
11708 for disk in instance.disks:
11709 self.cfg.SetDiskID(disk, src_node)
11711 activate_disks = (not instance.admin_up)
11714 # Activate the instance disks if we'exporting a stopped instance
11715 feedback_fn("Activating disks for %s" % instance.name)
11716 _StartInstanceDisks(self, instance, None)
11719 helper = masterd.instance.ExportInstanceHelper(self, feedback_fn,
11722 helper.CreateSnapshots()
11724 if (self.op.shutdown and instance.admin_up and
11725 not self.op.remove_instance):
11726 assert not activate_disks
11727 feedback_fn("Starting instance %s" % instance.name)
11728 result = self.rpc.call_instance_start(src_node, instance,
11730 msg = result.fail_msg
11732 feedback_fn("Failed to start instance: %s" % msg)
11733 _ShutdownInstanceDisks(self, instance)
11734 raise errors.OpExecError("Could not start instance: %s" % msg)
11736 if self.op.mode == constants.EXPORT_MODE_LOCAL:
11737 (fin_resu, dresults) = helper.LocalExport(self.dst_node)
11738 elif self.op.mode == constants.EXPORT_MODE_REMOTE:
11739 connect_timeout = constants.RIE_CONNECT_TIMEOUT
11740 timeouts = masterd.instance.ImportExportTimeouts(connect_timeout)
11742 (key_name, _, _) = self.x509_key_name
11745 OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM,
11748 (fin_resu, dresults) = helper.RemoteExport(self.dest_disk_info,
11749 key_name, dest_ca_pem,
11754 # Check for backwards compatibility
11755 assert len(dresults) == len(instance.disks)
11756 assert compat.all(isinstance(i, bool) for i in dresults), \
11757 "Not all results are boolean: %r" % dresults
11761 feedback_fn("Deactivating disks for %s" % instance.name)
11762 _ShutdownInstanceDisks(self, instance)
11764 if not (compat.all(dresults) and fin_resu):
11767 failures.append("export finalization")
11768 if not compat.all(dresults):
11769 fdsk = utils.CommaJoin(idx for (idx, dsk) in enumerate(dresults)
11771 failures.append("disk export: disk(s) %s" % fdsk)
11773 raise errors.OpExecError("Export failed, errors in %s" %
11774 utils.CommaJoin(failures))
11776 # At this point, the export was successful, we can cleanup/finish
11778 # Remove instance if requested
11779 if self.op.remove_instance:
11780 feedback_fn("Removing instance %s" % instance.name)
11781 _RemoveInstance(self, feedback_fn, instance,
11782 self.op.ignore_remove_failures)
11784 if self.op.mode == constants.EXPORT_MODE_LOCAL:
11785 self._CleanupExports(feedback_fn)
11787 return fin_resu, dresults
11790 class LUBackupRemove(NoHooksLU):
11791 """Remove exports related to the named instance.
11796 def ExpandNames(self):
11797 self.needed_locks = {}
11798 # We need all nodes to be locked in order for RemoveExport to work, but we
11799 # don't need to lock the instance itself, as nothing will happen to it (and
11800 # we can remove exports also for a removed instance)
11801 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
11803 def Exec(self, feedback_fn):
11804 """Remove any export.
11807 instance_name = self.cfg.ExpandInstanceName(self.op.instance_name)
11808 # If the instance was not found we'll try with the name that was passed in.
11809 # This will only work if it was an FQDN, though.
11811 if not instance_name:
11813 instance_name = self.op.instance_name
11815 locked_nodes = self.owned_locks(locking.LEVEL_NODE)
11816 exportlist = self.rpc.call_export_list(locked_nodes)
11818 for node in exportlist:
11819 msg = exportlist[node].fail_msg
11821 self.LogWarning("Failed to query node %s (continuing): %s", node, msg)
11823 if instance_name in exportlist[node].payload:
11825 result = self.rpc.call_export_remove(node, instance_name)
11826 msg = result.fail_msg
11828 logging.error("Could not remove export for instance %s"
11829 " on node %s: %s", instance_name, node, msg)
11831 if fqdn_warn and not found:
11832 feedback_fn("Export not found. If trying to remove an export belonging"
11833 " to a deleted instance please use its Fully Qualified"
11837 class LUGroupAdd(LogicalUnit):
11838 """Logical unit for creating node groups.
11841 HPATH = "group-add"
11842 HTYPE = constants.HTYPE_GROUP
11845 def ExpandNames(self):
11846 # We need the new group's UUID here so that we can create and acquire the
11847 # corresponding lock. Later, in Exec(), we'll indicate to cfg.AddNodeGroup
11848 # that it should not check whether the UUID exists in the configuration.
11849 self.group_uuid = self.cfg.GenerateUniqueID(self.proc.GetECId())
11850 self.needed_locks = {}
11851 self.add_locks[locking.LEVEL_NODEGROUP] = self.group_uuid
11853 def CheckPrereq(self):
11854 """Check prerequisites.
11856 This checks that the given group name is not an existing node group
11861 existing_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
11862 except errors.OpPrereqError:
11865 raise errors.OpPrereqError("Desired group name '%s' already exists as a"
11866 " node group (UUID: %s)" %
11867 (self.op.group_name, existing_uuid),
11868 errors.ECODE_EXISTS)
11870 if self.op.ndparams:
11871 utils.ForceDictType(self.op.ndparams, constants.NDS_PARAMETER_TYPES)
11873 def BuildHooksEnv(self):
11874 """Build hooks env.
11878 "GROUP_NAME": self.op.group_name,
11881 def BuildHooksNodes(self):
11882 """Build hooks nodes.
11885 mn = self.cfg.GetMasterNode()
11886 return ([mn], [mn])
11888 def Exec(self, feedback_fn):
11889 """Add the node group to the cluster.
11892 group_obj = objects.NodeGroup(name=self.op.group_name, members=[],
11893 uuid=self.group_uuid,
11894 alloc_policy=self.op.alloc_policy,
11895 ndparams=self.op.ndparams)
11897 self.cfg.AddNodeGroup(group_obj, self.proc.GetECId(), check_uuid=False)
11898 del self.remove_locks[locking.LEVEL_NODEGROUP]
11901 class LUGroupAssignNodes(NoHooksLU):
11902 """Logical unit for assigning nodes to groups.
11907 def ExpandNames(self):
11908 # These raise errors.OpPrereqError on their own:
11909 self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
11910 self.op.nodes = _GetWantedNodes(self, self.op.nodes)
11912 # We want to lock all the affected nodes and groups. We have readily
11913 # available the list of nodes, and the *destination* group. To gather the
11914 # list of "source" groups, we need to fetch node information later on.
11915 self.needed_locks = {
11916 locking.LEVEL_NODEGROUP: set([self.group_uuid]),
11917 locking.LEVEL_NODE: self.op.nodes,
11920 def DeclareLocks(self, level):
11921 if level == locking.LEVEL_NODEGROUP:
11922 assert len(self.needed_locks[locking.LEVEL_NODEGROUP]) == 1
11924 # Try to get all affected nodes' groups without having the group or node
11925 # lock yet. Needs verification later in the code flow.
11926 groups = self.cfg.GetNodeGroupsFromNodes(self.op.nodes)
11928 self.needed_locks[locking.LEVEL_NODEGROUP].update(groups)
11930 def CheckPrereq(self):
11931 """Check prerequisites.
11934 assert self.needed_locks[locking.LEVEL_NODEGROUP]
11935 assert (frozenset(self.owned_locks(locking.LEVEL_NODE)) ==
11936 frozenset(self.op.nodes))
11938 expected_locks = (set([self.group_uuid]) |
11939 self.cfg.GetNodeGroupsFromNodes(self.op.nodes))
11940 actual_locks = self.owned_locks(locking.LEVEL_NODEGROUP)
11941 if actual_locks != expected_locks:
11942 raise errors.OpExecError("Nodes changed groups since locks were acquired,"
11943 " current groups are '%s', used to be '%s'" %
11944 (utils.CommaJoin(expected_locks),
11945 utils.CommaJoin(actual_locks)))
11947 self.node_data = self.cfg.GetAllNodesInfo()
11948 self.group = self.cfg.GetNodeGroup(self.group_uuid)
11949 instance_data = self.cfg.GetAllInstancesInfo()
11951 if self.group is None:
11952 raise errors.OpExecError("Could not retrieve group '%s' (UUID: %s)" %
11953 (self.op.group_name, self.group_uuid))
11955 (new_splits, previous_splits) = \
11956 self.CheckAssignmentForSplitInstances([(node, self.group_uuid)
11957 for node in self.op.nodes],
11958 self.node_data, instance_data)
11961 fmt_new_splits = utils.CommaJoin(utils.NiceSort(new_splits))
11963 if not self.op.force:
11964 raise errors.OpExecError("The following instances get split by this"
11965 " change and --force was not given: %s" %
11968 self.LogWarning("This operation will split the following instances: %s",
11971 if previous_splits:
11972 self.LogWarning("In addition, these already-split instances continue"
11973 " to be split across groups: %s",
11974 utils.CommaJoin(utils.NiceSort(previous_splits)))
11976 def Exec(self, feedback_fn):
11977 """Assign nodes to a new group.
11980 for node in self.op.nodes:
11981 self.node_data[node].group = self.group_uuid
11983 # FIXME: Depends on side-effects of modifying the result of
11984 # C{cfg.GetAllNodesInfo}
11986 self.cfg.Update(self.group, feedback_fn) # Saves all modified nodes.
11989 def CheckAssignmentForSplitInstances(changes, node_data, instance_data):
11990 """Check for split instances after a node assignment.
11992 This method considers a series of node assignments as an atomic operation,
11993 and returns information about split instances after applying the set of
11996 In particular, it returns information about newly split instances, and
11997 instances that were already split, and remain so after the change.
11999 Only instances whose disk template is listed in constants.DTS_INT_MIRROR are
12002 @type changes: list of (node_name, new_group_uuid) pairs.
12003 @param changes: list of node assignments to consider.
12004 @param node_data: a dict with data for all nodes
12005 @param instance_data: a dict with all instances to consider
12006 @rtype: a two-tuple
12007 @return: a list of instances that were previously okay and result split as a
12008 consequence of this change, and a list of instances that were previously
12009 split and this change does not fix.
12012 changed_nodes = dict((node, group) for node, group in changes
12013 if node_data[node].group != group)
12015 all_split_instances = set()
12016 previously_split_instances = set()
12018 def InstanceNodes(instance):
12019 return [instance.primary_node] + list(instance.secondary_nodes)
12021 for inst in instance_data.values():
12022 if inst.disk_template not in constants.DTS_INT_MIRROR:
12025 instance_nodes = InstanceNodes(inst)
12027 if len(set(node_data[node].group for node in instance_nodes)) > 1:
12028 previously_split_instances.add(inst.name)
12030 if len(set(changed_nodes.get(node, node_data[node].group)
12031 for node in instance_nodes)) > 1:
12032 all_split_instances.add(inst.name)
12034 return (list(all_split_instances - previously_split_instances),
12035 list(previously_split_instances & all_split_instances))
12038 class _GroupQuery(_QueryBase):
12039 FIELDS = query.GROUP_FIELDS
12041 def ExpandNames(self, lu):
12042 lu.needed_locks = {}
12044 self._all_groups = lu.cfg.GetAllNodeGroupsInfo()
12045 name_to_uuid = dict((g.name, g.uuid) for g in self._all_groups.values())
12048 self.wanted = [name_to_uuid[name]
12049 for name in utils.NiceSort(name_to_uuid.keys())]
12051 # Accept names to be either names or UUIDs.
12054 all_uuid = frozenset(self._all_groups.keys())
12056 for name in self.names:
12057 if name in all_uuid:
12058 self.wanted.append(name)
12059 elif name in name_to_uuid:
12060 self.wanted.append(name_to_uuid[name])
12062 missing.append(name)
12065 raise errors.OpPrereqError("Some groups do not exist: %s" %
12066 utils.CommaJoin(missing),
12067 errors.ECODE_NOENT)
12069 def DeclareLocks(self, lu, level):
12072 def _GetQueryData(self, lu):
12073 """Computes the list of node groups and their attributes.
12076 do_nodes = query.GQ_NODE in self.requested_data
12077 do_instances = query.GQ_INST in self.requested_data
12079 group_to_nodes = None
12080 group_to_instances = None
12082 # For GQ_NODE, we need to map group->[nodes], and group->[instances] for
12083 # GQ_INST. The former is attainable with just GetAllNodesInfo(), but for the
12084 # latter GetAllInstancesInfo() is not enough, for we have to go through
12085 # instance->node. Hence, we will need to process nodes even if we only need
12086 # instance information.
12087 if do_nodes or do_instances:
12088 all_nodes = lu.cfg.GetAllNodesInfo()
12089 group_to_nodes = dict((uuid, []) for uuid in self.wanted)
12092 for node in all_nodes.values():
12093 if node.group in group_to_nodes:
12094 group_to_nodes[node.group].append(node.name)
12095 node_to_group[node.name] = node.group
12098 all_instances = lu.cfg.GetAllInstancesInfo()
12099 group_to_instances = dict((uuid, []) for uuid in self.wanted)
12101 for instance in all_instances.values():
12102 node = instance.primary_node
12103 if node in node_to_group:
12104 group_to_instances[node_to_group[node]].append(instance.name)
12107 # Do not pass on node information if it was not requested.
12108 group_to_nodes = None
12110 return query.GroupQueryData([self._all_groups[uuid]
12111 for uuid in self.wanted],
12112 group_to_nodes, group_to_instances)
12115 class LUGroupQuery(NoHooksLU):
12116 """Logical unit for querying node groups.
12121 def CheckArguments(self):
12122 self.gq = _GroupQuery(qlang.MakeSimpleFilter("name", self.op.names),
12123 self.op.output_fields, False)
12125 def ExpandNames(self):
12126 self.gq.ExpandNames(self)
12128 def DeclareLocks(self, level):
12129 self.gq.DeclareLocks(self, level)
12131 def Exec(self, feedback_fn):
12132 return self.gq.OldStyleQuery(self)
12135 class LUGroupSetParams(LogicalUnit):
12136 """Modifies the parameters of a node group.
12139 HPATH = "group-modify"
12140 HTYPE = constants.HTYPE_GROUP
12143 def CheckArguments(self):
12146 self.op.alloc_policy,
12149 if all_changes.count(None) == len(all_changes):
12150 raise errors.OpPrereqError("Please pass at least one modification",
12151 errors.ECODE_INVAL)
12153 def ExpandNames(self):
12154 # This raises errors.OpPrereqError on its own:
12155 self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
12157 self.needed_locks = {
12158 locking.LEVEL_NODEGROUP: [self.group_uuid],
12161 def CheckPrereq(self):
12162 """Check prerequisites.
12165 self.group = self.cfg.GetNodeGroup(self.group_uuid)
12167 if self.group is None:
12168 raise errors.OpExecError("Could not retrieve group '%s' (UUID: %s)" %
12169 (self.op.group_name, self.group_uuid))
12171 if self.op.ndparams:
12172 new_ndparams = _GetUpdatedParams(self.group.ndparams, self.op.ndparams)
12173 utils.ForceDictType(self.op.ndparams, constants.NDS_PARAMETER_TYPES)
12174 self.new_ndparams = new_ndparams
12176 def BuildHooksEnv(self):
12177 """Build hooks env.
12181 "GROUP_NAME": self.op.group_name,
12182 "NEW_ALLOC_POLICY": self.op.alloc_policy,
12185 def BuildHooksNodes(self):
12186 """Build hooks nodes.
12189 mn = self.cfg.GetMasterNode()
12190 return ([mn], [mn])
12192 def Exec(self, feedback_fn):
12193 """Modifies the node group.
12198 if self.op.ndparams:
12199 self.group.ndparams = self.new_ndparams
12200 result.append(("ndparams", str(self.group.ndparams)))
12202 if self.op.alloc_policy:
12203 self.group.alloc_policy = self.op.alloc_policy
12205 self.cfg.Update(self.group, feedback_fn)
12209 class LUGroupRemove(LogicalUnit):
12210 HPATH = "group-remove"
12211 HTYPE = constants.HTYPE_GROUP
12214 def ExpandNames(self):
12215 # This will raises errors.OpPrereqError on its own:
12216 self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
12217 self.needed_locks = {
12218 locking.LEVEL_NODEGROUP: [self.group_uuid],
12221 def CheckPrereq(self):
12222 """Check prerequisites.
12224 This checks that the given group name exists as a node group, that is
12225 empty (i.e., contains no nodes), and that is not the last group of the
12229 # Verify that the group is empty.
12230 group_nodes = [node.name
12231 for node in self.cfg.GetAllNodesInfo().values()
12232 if node.group == self.group_uuid]
12235 raise errors.OpPrereqError("Group '%s' not empty, has the following"
12237 (self.op.group_name,
12238 utils.CommaJoin(utils.NiceSort(group_nodes))),
12239 errors.ECODE_STATE)
12241 # Verify the cluster would not be left group-less.
12242 if len(self.cfg.GetNodeGroupList()) == 1:
12243 raise errors.OpPrereqError("Group '%s' is the only group,"
12244 " cannot be removed" %
12245 self.op.group_name,
12246 errors.ECODE_STATE)
12248 def BuildHooksEnv(self):
12249 """Build hooks env.
12253 "GROUP_NAME": self.op.group_name,
12256 def BuildHooksNodes(self):
12257 """Build hooks nodes.
12260 mn = self.cfg.GetMasterNode()
12261 return ([mn], [mn])
12263 def Exec(self, feedback_fn):
12264 """Remove the node group.
12268 self.cfg.RemoveNodeGroup(self.group_uuid)
12269 except errors.ConfigurationError:
12270 raise errors.OpExecError("Group '%s' with UUID %s disappeared" %
12271 (self.op.group_name, self.group_uuid))
12273 self.remove_locks[locking.LEVEL_NODEGROUP] = self.group_uuid
12276 class LUGroupRename(LogicalUnit):
12277 HPATH = "group-rename"
12278 HTYPE = constants.HTYPE_GROUP
12281 def ExpandNames(self):
12282 # This raises errors.OpPrereqError on its own:
12283 self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
12285 self.needed_locks = {
12286 locking.LEVEL_NODEGROUP: [self.group_uuid],
12289 def CheckPrereq(self):
12290 """Check prerequisites.
12292 Ensures requested new name is not yet used.
12296 new_name_uuid = self.cfg.LookupNodeGroup(self.op.new_name)
12297 except errors.OpPrereqError:
12300 raise errors.OpPrereqError("Desired new name '%s' clashes with existing"
12301 " node group (UUID: %s)" %
12302 (self.op.new_name, new_name_uuid),
12303 errors.ECODE_EXISTS)
12305 def BuildHooksEnv(self):
12306 """Build hooks env.
12310 "OLD_NAME": self.op.group_name,
12311 "NEW_NAME": self.op.new_name,
12314 def BuildHooksNodes(self):
12315 """Build hooks nodes.
12318 mn = self.cfg.GetMasterNode()
12320 all_nodes = self.cfg.GetAllNodesInfo()
12321 all_nodes.pop(mn, None)
12324 run_nodes.extend(node.name for node in all_nodes.values()
12325 if node.group == self.group_uuid)
12327 return (run_nodes, run_nodes)
12329 def Exec(self, feedback_fn):
12330 """Rename the node group.
12333 group = self.cfg.GetNodeGroup(self.group_uuid)
12336 raise errors.OpExecError("Could not retrieve group '%s' (UUID: %s)" %
12337 (self.op.group_name, self.group_uuid))
12339 group.name = self.op.new_name
12340 self.cfg.Update(group, feedback_fn)
12342 return self.op.new_name
12345 class LUGroupEvacuate(LogicalUnit):
12346 HPATH = "group-evacuate"
12347 HTYPE = constants.HTYPE_GROUP
12350 def ExpandNames(self):
12351 # This raises errors.OpPrereqError on its own:
12352 self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
12354 if self.op.target_groups:
12355 self.req_target_uuids = map(self.cfg.LookupNodeGroup,
12356 self.op.target_groups)
12358 self.req_target_uuids = []
12360 if self.group_uuid in self.req_target_uuids:
12361 raise errors.OpPrereqError("Group to be evacuated (%s) can not be used"
12362 " as a target group (targets are %s)" %
12364 utils.CommaJoin(self.req_target_uuids)),
12365 errors.ECODE_INVAL)
12367 self.op.iallocator = _GetDefaultIAllocator(self.cfg, self.op.iallocator)
12369 self.share_locks = _ShareAll()
12370 self.needed_locks = {
12371 locking.LEVEL_INSTANCE: [],
12372 locking.LEVEL_NODEGROUP: [],
12373 locking.LEVEL_NODE: [],
12376 def DeclareLocks(self, level):
12377 if level == locking.LEVEL_INSTANCE:
12378 assert not self.needed_locks[locking.LEVEL_INSTANCE]
12380 # Lock instances optimistically, needs verification once node and group
12381 # locks have been acquired
12382 self.needed_locks[locking.LEVEL_INSTANCE] = \
12383 self.cfg.GetNodeGroupInstances(self.group_uuid)
12385 elif level == locking.LEVEL_NODEGROUP:
12386 assert not self.needed_locks[locking.LEVEL_NODEGROUP]
12388 if self.req_target_uuids:
12389 lock_groups = set([self.group_uuid] + self.req_target_uuids)
12391 # Lock all groups used by instances optimistically; this requires going
12392 # via the node before it's locked, requiring verification later on
12393 lock_groups.update(group_uuid
12394 for instance_name in
12395 self.owned_locks(locking.LEVEL_INSTANCE)
12397 self.cfg.GetInstanceNodeGroups(instance_name))
12399 # No target groups, need to lock all of them
12400 lock_groups = locking.ALL_SET
12402 self.needed_locks[locking.LEVEL_NODEGROUP] = lock_groups
12404 elif level == locking.LEVEL_NODE:
12405 # This will only lock the nodes in the group to be evacuated which
12406 # contain actual instances
12407 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
12408 self._LockInstancesNodes()
12410 # Lock all nodes in group to be evacuated and target groups
12411 owned_groups = frozenset(self.owned_locks(locking.LEVEL_NODEGROUP))
12412 assert self.group_uuid in owned_groups
12413 member_nodes = [node_name
12414 for group in owned_groups
12415 for node_name in self.cfg.GetNodeGroup(group).members]
12416 self.needed_locks[locking.LEVEL_NODE].extend(member_nodes)
12418 def CheckPrereq(self):
12419 owned_instances = frozenset(self.owned_locks(locking.LEVEL_INSTANCE))
12420 owned_groups = frozenset(self.owned_locks(locking.LEVEL_NODEGROUP))
12421 owned_nodes = frozenset(self.owned_locks(locking.LEVEL_NODE))
12423 assert owned_groups.issuperset(self.req_target_uuids)
12424 assert self.group_uuid in owned_groups
12426 # Check if locked instances are still correct
12427 _CheckNodeGroupInstances(self.cfg, self.group_uuid, owned_instances)
12429 # Get instance information
12430 self.instances = dict(self.cfg.GetMultiInstanceInfo(owned_instances))
12432 # Check if node groups for locked instances are still correct
12433 for instance_name in owned_instances:
12434 inst = self.instances[instance_name]
12435 assert owned_nodes.issuperset(inst.all_nodes), \
12436 "Instance %s's nodes changed while we kept the lock" % instance_name
12438 inst_groups = _CheckInstanceNodeGroups(self.cfg, instance_name,
12441 assert self.group_uuid in inst_groups, \
12442 "Instance %s has no node in group %s" % (instance_name, self.group_uuid)
12444 if self.req_target_uuids:
12445 # User requested specific target groups
12446 self.target_uuids = self.req_target_uuids
12448 # All groups except the one to be evacuated are potential targets
12449 self.target_uuids = [group_uuid for group_uuid in owned_groups
12450 if group_uuid != self.group_uuid]
12452 if not self.target_uuids:
12453 raise errors.OpPrereqError("There are no possible target groups",
12454 errors.ECODE_INVAL)
12456 def BuildHooksEnv(self):
12457 """Build hooks env.
12461 "GROUP_NAME": self.op.group_name,
12462 "TARGET_GROUPS": " ".join(self.target_uuids),
12465 def BuildHooksNodes(self):
12466 """Build hooks nodes.
12469 mn = self.cfg.GetMasterNode()
12471 assert self.group_uuid in self.owned_locks(locking.LEVEL_NODEGROUP)
12473 run_nodes = [mn] + self.cfg.GetNodeGroup(self.group_uuid).members
12475 return (run_nodes, run_nodes)
12477 def Exec(self, feedback_fn):
12478 instances = list(self.owned_locks(locking.LEVEL_INSTANCE))
12480 assert self.group_uuid not in self.target_uuids
12482 ial = IAllocator(self.cfg, self.rpc, constants.IALLOCATOR_MODE_CHG_GROUP,
12483 instances=instances, target_groups=self.target_uuids)
12485 ial.Run(self.op.iallocator)
12487 if not ial.success:
12488 raise errors.OpPrereqError("Can't compute group evacuation using"
12489 " iallocator '%s': %s" %
12490 (self.op.iallocator, ial.info),
12491 errors.ECODE_NORES)
12493 jobs = _LoadNodeEvacResult(self, ial.result, self.op.early_release, False)
12495 self.LogInfo("Iallocator returned %s job(s) for evacuating node group %s",
12496 len(jobs), self.op.group_name)
12498 return ResultWithJobs(jobs)
12501 class TagsLU(NoHooksLU): # pylint: disable=W0223
12502 """Generic tags LU.
12504 This is an abstract class which is the parent of all the other tags LUs.
12507 def ExpandNames(self):
12508 self.group_uuid = None
12509 self.needed_locks = {}
12510 if self.op.kind == constants.TAG_NODE:
12511 self.op.name = _ExpandNodeName(self.cfg, self.op.name)
12512 self.needed_locks[locking.LEVEL_NODE] = self.op.name
12513 elif self.op.kind == constants.TAG_INSTANCE:
12514 self.op.name = _ExpandInstanceName(self.cfg, self.op.name)
12515 self.needed_locks[locking.LEVEL_INSTANCE] = self.op.name
12516 elif self.op.kind == constants.TAG_NODEGROUP:
12517 self.group_uuid = self.cfg.LookupNodeGroup(self.op.name)
12519 # FIXME: Acquire BGL for cluster tag operations (as of this writing it's
12520 # not possible to acquire the BGL based on opcode parameters)
12522 def CheckPrereq(self):
12523 """Check prerequisites.
12526 if self.op.kind == constants.TAG_CLUSTER:
12527 self.target = self.cfg.GetClusterInfo()
12528 elif self.op.kind == constants.TAG_NODE:
12529 self.target = self.cfg.GetNodeInfo(self.op.name)
12530 elif self.op.kind == constants.TAG_INSTANCE:
12531 self.target = self.cfg.GetInstanceInfo(self.op.name)
12532 elif self.op.kind == constants.TAG_NODEGROUP:
12533 self.target = self.cfg.GetNodeGroup(self.group_uuid)
12535 raise errors.OpPrereqError("Wrong tag type requested (%s)" %
12536 str(self.op.kind), errors.ECODE_INVAL)
12539 class LUTagsGet(TagsLU):
12540 """Returns the tags of a given object.
12545 def ExpandNames(self):
12546 TagsLU.ExpandNames(self)
12548 # Share locks as this is only a read operation
12549 self.share_locks = _ShareAll()
12551 def Exec(self, feedback_fn):
12552 """Returns the tag list.
12555 return list(self.target.GetTags())
12558 class LUTagsSearch(NoHooksLU):
12559 """Searches the tags for a given pattern.
12564 def ExpandNames(self):
12565 self.needed_locks = {}
12567 def CheckPrereq(self):
12568 """Check prerequisites.
12570 This checks the pattern passed for validity by compiling it.
12574 self.re = re.compile(self.op.pattern)
12575 except re.error, err:
12576 raise errors.OpPrereqError("Invalid search pattern '%s': %s" %
12577 (self.op.pattern, err), errors.ECODE_INVAL)
12579 def Exec(self, feedback_fn):
12580 """Returns the tag list.
12584 tgts = [("/cluster", cfg.GetClusterInfo())]
12585 ilist = cfg.GetAllInstancesInfo().values()
12586 tgts.extend([("/instances/%s" % i.name, i) for i in ilist])
12587 nlist = cfg.GetAllNodesInfo().values()
12588 tgts.extend([("/nodes/%s" % n.name, n) for n in nlist])
12589 tgts.extend(("/nodegroup/%s" % n.name, n)
12590 for n in cfg.GetAllNodeGroupsInfo().values())
12592 for path, target in tgts:
12593 for tag in target.GetTags():
12594 if self.re.search(tag):
12595 results.append((path, tag))
12599 class LUTagsSet(TagsLU):
12600 """Sets a tag on a given object.
12605 def CheckPrereq(self):
12606 """Check prerequisites.
12608 This checks the type and length of the tag name and value.
12611 TagsLU.CheckPrereq(self)
12612 for tag in self.op.tags:
12613 objects.TaggableObject.ValidateTag(tag)
12615 def Exec(self, feedback_fn):
12620 for tag in self.op.tags:
12621 self.target.AddTag(tag)
12622 except errors.TagError, err:
12623 raise errors.OpExecError("Error while setting tag: %s" % str(err))
12624 self.cfg.Update(self.target, feedback_fn)
12627 class LUTagsDel(TagsLU):
12628 """Delete a list of tags from a given object.
12633 def CheckPrereq(self):
12634 """Check prerequisites.
12636 This checks that we have the given tag.
12639 TagsLU.CheckPrereq(self)
12640 for tag in self.op.tags:
12641 objects.TaggableObject.ValidateTag(tag)
12642 del_tags = frozenset(self.op.tags)
12643 cur_tags = self.target.GetTags()
12645 diff_tags = del_tags - cur_tags
12647 diff_names = ("'%s'" % i for i in sorted(diff_tags))
12648 raise errors.OpPrereqError("Tag(s) %s not found" %
12649 (utils.CommaJoin(diff_names), ),
12650 errors.ECODE_NOENT)
12652 def Exec(self, feedback_fn):
12653 """Remove the tag from the object.
12656 for tag in self.op.tags:
12657 self.target.RemoveTag(tag)
12658 self.cfg.Update(self.target, feedback_fn)
12661 class LUTestDelay(NoHooksLU):
12662 """Sleep for a specified amount of time.
12664 This LU sleeps on the master and/or nodes for a specified amount of
12670 def ExpandNames(self):
12671 """Expand names and set required locks.
12673 This expands the node list, if any.
12676 self.needed_locks = {}
12677 if self.op.on_nodes:
12678 # _GetWantedNodes can be used here, but is not always appropriate to use
12679 # this way in ExpandNames. Check LogicalUnit.ExpandNames docstring for
12680 # more information.
12681 self.op.on_nodes = _GetWantedNodes(self, self.op.on_nodes)
12682 self.needed_locks[locking.LEVEL_NODE] = self.op.on_nodes
12684 def _TestDelay(self):
12685 """Do the actual sleep.
12688 if self.op.on_master:
12689 if not utils.TestDelay(self.op.duration):
12690 raise errors.OpExecError("Error during master delay test")
12691 if self.op.on_nodes:
12692 result = self.rpc.call_test_delay(self.op.on_nodes, self.op.duration)
12693 for node, node_result in result.items():
12694 node_result.Raise("Failure during rpc call to node %s" % node)
12696 def Exec(self, feedback_fn):
12697 """Execute the test delay opcode, with the wanted repetitions.
12700 if self.op.repeat == 0:
12703 top_value = self.op.repeat - 1
12704 for i in range(self.op.repeat):
12705 self.LogInfo("Test delay iteration %d/%d" % (i, top_value))
12709 class LUTestJqueue(NoHooksLU):
12710 """Utility LU to test some aspects of the job queue.
12715 # Must be lower than default timeout for WaitForJobChange to see whether it
12716 # notices changed jobs
12717 _CLIENT_CONNECT_TIMEOUT = 20.0
12718 _CLIENT_CONFIRM_TIMEOUT = 60.0
12721 def _NotifyUsingSocket(cls, cb, errcls):
12722 """Opens a Unix socket and waits for another program to connect.
12725 @param cb: Callback to send socket name to client
12726 @type errcls: class
12727 @param errcls: Exception class to use for errors
12730 # Using a temporary directory as there's no easy way to create temporary
12731 # sockets without writing a custom loop around tempfile.mktemp and
12733 tmpdir = tempfile.mkdtemp()
12735 tmpsock = utils.PathJoin(tmpdir, "sock")
12737 logging.debug("Creating temporary socket at %s", tmpsock)
12738 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
12743 # Send details to client
12746 # Wait for client to connect before continuing
12747 sock.settimeout(cls._CLIENT_CONNECT_TIMEOUT)
12749 (conn, _) = sock.accept()
12750 except socket.error, err:
12751 raise errcls("Client didn't connect in time (%s)" % err)
12755 # Remove as soon as client is connected
12756 shutil.rmtree(tmpdir)
12758 # Wait for client to close
12761 # pylint: disable=E1101
12762 # Instance of '_socketobject' has no ... member
12763 conn.settimeout(cls._CLIENT_CONFIRM_TIMEOUT)
12765 except socket.error, err:
12766 raise errcls("Client failed to confirm notification (%s)" % err)
12770 def _SendNotification(self, test, arg, sockname):
12771 """Sends a notification to the client.
12774 @param test: Test name
12775 @param arg: Test argument (depends on test)
12776 @type sockname: string
12777 @param sockname: Socket path
12780 self.Log(constants.ELOG_JQUEUE_TEST, (sockname, test, arg))
12782 def _Notify(self, prereq, test, arg):
12783 """Notifies the client of a test.
12786 @param prereq: Whether this is a prereq-phase test
12788 @param test: Test name
12789 @param arg: Test argument (depends on test)
12793 errcls = errors.OpPrereqError
12795 errcls = errors.OpExecError
12797 return self._NotifyUsingSocket(compat.partial(self._SendNotification,
12801 def CheckArguments(self):
12802 self.checkargs_calls = getattr(self, "checkargs_calls", 0) + 1
12803 self.expandnames_calls = 0
12805 def ExpandNames(self):
12806 checkargs_calls = getattr(self, "checkargs_calls", 0)
12807 if checkargs_calls < 1:
12808 raise errors.ProgrammerError("CheckArguments was not called")
12810 self.expandnames_calls += 1
12812 if self.op.notify_waitlock:
12813 self._Notify(True, constants.JQT_EXPANDNAMES, None)
12815 self.LogInfo("Expanding names")
12817 # Get lock on master node (just to get a lock, not for a particular reason)
12818 self.needed_locks = {
12819 locking.LEVEL_NODE: self.cfg.GetMasterNode(),
12822 def Exec(self, feedback_fn):
12823 if self.expandnames_calls < 1:
12824 raise errors.ProgrammerError("ExpandNames was not called")
12826 if self.op.notify_exec:
12827 self._Notify(False, constants.JQT_EXEC, None)
12829 self.LogInfo("Executing")
12831 if self.op.log_messages:
12832 self._Notify(False, constants.JQT_STARTMSG, len(self.op.log_messages))
12833 for idx, msg in enumerate(self.op.log_messages):
12834 self.LogInfo("Sending log message %s", idx + 1)
12835 feedback_fn(constants.JQT_MSGPREFIX + msg)
12836 # Report how many test messages have been sent
12837 self._Notify(False, constants.JQT_LOGMSG, idx + 1)
12840 raise errors.OpExecError("Opcode failure was requested")
12845 class IAllocator(object):
12846 """IAllocator framework.
12848 An IAllocator instance has three sets of attributes:
12849 - cfg that is needed to query the cluster
12850 - input data (all members of the _KEYS class attribute are required)
12851 - four buffer attributes (in|out_data|text), that represent the
12852 input (to the external script) in text and data structure format,
12853 and the output from it, again in two formats
12854 - the result variables from the script (success, info, nodes) for
12858 # pylint: disable=R0902
12859 # lots of instance attributes
12861 def __init__(self, cfg, rpc, mode, **kwargs):
12864 # init buffer variables
12865 self.in_text = self.out_text = self.in_data = self.out_data = None
12866 # init all input fields so that pylint is happy
12868 self.memory = self.disks = self.disk_template = None
12869 self.os = self.tags = self.nics = self.vcpus = None
12870 self.hypervisor = None
12871 self.relocate_from = None
12873 self.instances = None
12874 self.evac_mode = None
12875 self.target_groups = []
12877 self.required_nodes = None
12878 # init result fields
12879 self.success = self.info = self.result = None
12882 (fn, keydata, self._result_check) = self._MODE_DATA[self.mode]
12884 raise errors.ProgrammerError("Unknown mode '%s' passed to the"
12885 " IAllocator" % self.mode)
12887 keyset = [n for (n, _) in keydata]
12890 if key not in keyset:
12891 raise errors.ProgrammerError("Invalid input parameter '%s' to"
12892 " IAllocator" % key)
12893 setattr(self, key, kwargs[key])
12896 if key not in kwargs:
12897 raise errors.ProgrammerError("Missing input parameter '%s' to"
12898 " IAllocator" % key)
12899 self._BuildInputData(compat.partial(fn, self), keydata)
12901 def _ComputeClusterData(self):
12902 """Compute the generic allocator input data.
12904 This is the data that is independent of the actual operation.
12908 cluster_info = cfg.GetClusterInfo()
12911 "version": constants.IALLOCATOR_VERSION,
12912 "cluster_name": cfg.GetClusterName(),
12913 "cluster_tags": list(cluster_info.GetTags()),
12914 "enabled_hypervisors": list(cluster_info.enabled_hypervisors),
12915 # we don't have job IDs
12917 ninfo = cfg.GetAllNodesInfo()
12918 iinfo = cfg.GetAllInstancesInfo().values()
12919 i_list = [(inst, cluster_info.FillBE(inst)) for inst in iinfo]
12922 node_list = [n.name for n in ninfo.values() if n.vm_capable]
12924 if self.mode == constants.IALLOCATOR_MODE_ALLOC:
12925 hypervisor_name = self.hypervisor
12926 elif self.mode == constants.IALLOCATOR_MODE_RELOC:
12927 hypervisor_name = cfg.GetInstanceInfo(self.name).hypervisor
12929 hypervisor_name = cluster_info.enabled_hypervisors[0]
12931 node_data = self.rpc.call_node_info(node_list, cfg.GetVGName(),
12934 self.rpc.call_all_instances_info(node_list,
12935 cluster_info.enabled_hypervisors)
12937 data["nodegroups"] = self._ComputeNodeGroupData(cfg)
12939 config_ndata = self._ComputeBasicNodeData(ninfo)
12940 data["nodes"] = self._ComputeDynamicNodeData(ninfo, node_data, node_iinfo,
12941 i_list, config_ndata)
12942 assert len(data["nodes"]) == len(ninfo), \
12943 "Incomplete node data computed"
12945 data["instances"] = self._ComputeInstanceData(cluster_info, i_list)
12947 self.in_data = data
12950 def _ComputeNodeGroupData(cfg):
12951 """Compute node groups data.
12954 ng = dict((guuid, {
12955 "name": gdata.name,
12956 "alloc_policy": gdata.alloc_policy,
12958 for guuid, gdata in cfg.GetAllNodeGroupsInfo().items())
12963 def _ComputeBasicNodeData(node_cfg):
12964 """Compute global node data.
12967 @returns: a dict of name: (node dict, node config)
12970 # fill in static (config-based) values
12971 node_results = dict((ninfo.name, {
12972 "tags": list(ninfo.GetTags()),
12973 "primary_ip": ninfo.primary_ip,
12974 "secondary_ip": ninfo.secondary_ip,
12975 "offline": ninfo.offline,
12976 "drained": ninfo.drained,
12977 "master_candidate": ninfo.master_candidate,
12978 "group": ninfo.group,
12979 "master_capable": ninfo.master_capable,
12980 "vm_capable": ninfo.vm_capable,
12982 for ninfo in node_cfg.values())
12984 return node_results
12987 def _ComputeDynamicNodeData(node_cfg, node_data, node_iinfo, i_list,
12989 """Compute global node data.
12991 @param node_results: the basic node structures as filled from the config
12994 # make a copy of the current dict
12995 node_results = dict(node_results)
12996 for nname, nresult in node_data.items():
12997 assert nname in node_results, "Missing basic data for node %s" % nname
12998 ninfo = node_cfg[nname]
13000 if not (ninfo.offline or ninfo.drained):
13001 nresult.Raise("Can't get data for node %s" % nname)
13002 node_iinfo[nname].Raise("Can't get node instance info from node %s" %
13004 remote_info = nresult.payload
13006 for attr in ["memory_total", "memory_free", "memory_dom0",
13007 "vg_size", "vg_free", "cpu_total"]:
13008 if attr not in remote_info:
13009 raise errors.OpExecError("Node '%s' didn't return attribute"
13010 " '%s'" % (nname, attr))
13011 if not isinstance(remote_info[attr], int):
13012 raise errors.OpExecError("Node '%s' returned invalid value"
13014 (nname, attr, remote_info[attr]))
13015 # compute memory used by primary instances
13016 i_p_mem = i_p_up_mem = 0
13017 for iinfo, beinfo in i_list:
13018 if iinfo.primary_node == nname:
13019 i_p_mem += beinfo[constants.BE_MEMORY]
13020 if iinfo.name not in node_iinfo[nname].payload:
13023 i_used_mem = int(node_iinfo[nname].payload[iinfo.name]["memory"])
13024 i_mem_diff = beinfo[constants.BE_MEMORY] - i_used_mem
13025 remote_info["memory_free"] -= max(0, i_mem_diff)
13028 i_p_up_mem += beinfo[constants.BE_MEMORY]
13030 # compute memory used by instances
13032 "total_memory": remote_info["memory_total"],
13033 "reserved_memory": remote_info["memory_dom0"],
13034 "free_memory": remote_info["memory_free"],
13035 "total_disk": remote_info["vg_size"],
13036 "free_disk": remote_info["vg_free"],
13037 "total_cpus": remote_info["cpu_total"],
13038 "i_pri_memory": i_p_mem,
13039 "i_pri_up_memory": i_p_up_mem,
13041 pnr_dyn.update(node_results[nname])
13042 node_results[nname] = pnr_dyn
13044 return node_results
13047 def _ComputeInstanceData(cluster_info, i_list):
13048 """Compute global instance data.
13052 for iinfo, beinfo in i_list:
13054 for nic in iinfo.nics:
13055 filled_params = cluster_info.SimpleFillNIC(nic.nicparams)
13059 "mode": filled_params[constants.NIC_MODE],
13060 "link": filled_params[constants.NIC_LINK],
13062 if filled_params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
13063 nic_dict["bridge"] = filled_params[constants.NIC_LINK]
13064 nic_data.append(nic_dict)
13066 "tags": list(iinfo.GetTags()),
13067 "admin_up": iinfo.admin_up,
13068 "vcpus": beinfo[constants.BE_VCPUS],
13069 "memory": beinfo[constants.BE_MEMORY],
13071 "nodes": [iinfo.primary_node] + list(iinfo.secondary_nodes),
13073 "disks": [{constants.IDISK_SIZE: dsk.size,
13074 constants.IDISK_MODE: dsk.mode}
13075 for dsk in iinfo.disks],
13076 "disk_template": iinfo.disk_template,
13077 "hypervisor": iinfo.hypervisor,
13079 pir["disk_space_total"] = _ComputeDiskSize(iinfo.disk_template,
13081 instance_data[iinfo.name] = pir
13083 return instance_data
13085 def _AddNewInstance(self):
13086 """Add new instance data to allocator structure.
13088 This in combination with _AllocatorGetClusterData will create the
13089 correct structure needed as input for the allocator.
13091 The checks for the completeness of the opcode must have already been
13095 disk_space = _ComputeDiskSize(self.disk_template, self.disks)
13097 if self.disk_template in constants.DTS_INT_MIRROR:
13098 self.required_nodes = 2
13100 self.required_nodes = 1
13104 "disk_template": self.disk_template,
13107 "vcpus": self.vcpus,
13108 "memory": self.memory,
13109 "disks": self.disks,
13110 "disk_space_total": disk_space,
13112 "required_nodes": self.required_nodes,
13113 "hypervisor": self.hypervisor,
13118 def _AddRelocateInstance(self):
13119 """Add relocate instance data to allocator structure.
13121 This in combination with _IAllocatorGetClusterData will create the
13122 correct structure needed as input for the allocator.
13124 The checks for the completeness of the opcode must have already been
13128 instance = self.cfg.GetInstanceInfo(self.name)
13129 if instance is None:
13130 raise errors.ProgrammerError("Unknown instance '%s' passed to"
13131 " IAllocator" % self.name)
13133 if instance.disk_template not in constants.DTS_MIRRORED:
13134 raise errors.OpPrereqError("Can't relocate non-mirrored instances",
13135 errors.ECODE_INVAL)
13137 if instance.disk_template in constants.DTS_INT_MIRROR and \
13138 len(instance.secondary_nodes) != 1:
13139 raise errors.OpPrereqError("Instance has not exactly one secondary node",
13140 errors.ECODE_STATE)
13142 self.required_nodes = 1
13143 disk_sizes = [{constants.IDISK_SIZE: disk.size} for disk in instance.disks]
13144 disk_space = _ComputeDiskSize(instance.disk_template, disk_sizes)
13148 "disk_space_total": disk_space,
13149 "required_nodes": self.required_nodes,
13150 "relocate_from": self.relocate_from,
13154 def _AddNodeEvacuate(self):
13155 """Get data for node-evacuate requests.
13159 "instances": self.instances,
13160 "evac_mode": self.evac_mode,
13163 def _AddChangeGroup(self):
13164 """Get data for node-evacuate requests.
13168 "instances": self.instances,
13169 "target_groups": self.target_groups,
13172 def _BuildInputData(self, fn, keydata):
13173 """Build input data structures.
13176 self._ComputeClusterData()
13179 request["type"] = self.mode
13180 for keyname, keytype in keydata:
13181 if keyname not in request:
13182 raise errors.ProgrammerError("Request parameter %s is missing" %
13184 val = request[keyname]
13185 if not keytype(val):
13186 raise errors.ProgrammerError("Request parameter %s doesn't pass"
13187 " validation, value %s, expected"
13188 " type %s" % (keyname, val, keytype))
13189 self.in_data["request"] = request
13191 self.in_text = serializer.Dump(self.in_data)
13193 _STRING_LIST = ht.TListOf(ht.TString)
13194 _JOB_LIST = ht.TListOf(ht.TListOf(ht.TStrictDict(True, False, {
13195 # pylint: disable=E1101
13196 # Class '...' has no 'OP_ID' member
13197 "OP_ID": ht.TElemOf([opcodes.OpInstanceFailover.OP_ID,
13198 opcodes.OpInstanceMigrate.OP_ID,
13199 opcodes.OpInstanceReplaceDisks.OP_ID])
13203 ht.TListOf(ht.TAnd(ht.TIsLength(3),
13204 ht.TItems([ht.TNonEmptyString,
13205 ht.TNonEmptyString,
13206 ht.TListOf(ht.TNonEmptyString),
13209 ht.TListOf(ht.TAnd(ht.TIsLength(2),
13210 ht.TItems([ht.TNonEmptyString,
13213 _NEVAC_RESULT = ht.TAnd(ht.TIsLength(3),
13214 ht.TItems([_NEVAC_MOVED, _NEVAC_FAILED, _JOB_LIST]))
13217 constants.IALLOCATOR_MODE_ALLOC:
13220 ("name", ht.TString),
13221 ("memory", ht.TInt),
13222 ("disks", ht.TListOf(ht.TDict)),
13223 ("disk_template", ht.TString),
13224 ("os", ht.TString),
13225 ("tags", _STRING_LIST),
13226 ("nics", ht.TListOf(ht.TDict)),
13227 ("vcpus", ht.TInt),
13228 ("hypervisor", ht.TString),
13230 constants.IALLOCATOR_MODE_RELOC:
13231 (_AddRelocateInstance,
13232 [("name", ht.TString), ("relocate_from", _STRING_LIST)],
13234 constants.IALLOCATOR_MODE_NODE_EVAC:
13235 (_AddNodeEvacuate, [
13236 ("instances", _STRING_LIST),
13237 ("evac_mode", ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)),
13239 constants.IALLOCATOR_MODE_CHG_GROUP:
13240 (_AddChangeGroup, [
13241 ("instances", _STRING_LIST),
13242 ("target_groups", _STRING_LIST),
13246 def Run(self, name, validate=True, call_fn=None):
13247 """Run an instance allocator and return the results.
13250 if call_fn is None:
13251 call_fn = self.rpc.call_iallocator_runner
13253 result = call_fn(self.cfg.GetMasterNode(), name, self.in_text)
13254 result.Raise("Failure while running the iallocator script")
13256 self.out_text = result.payload
13258 self._ValidateResult()
13260 def _ValidateResult(self):
13261 """Process the allocator results.
13263 This will process and if successful save the result in
13264 self.out_data and the other parameters.
13268 rdict = serializer.Load(self.out_text)
13269 except Exception, err:
13270 raise errors.OpExecError("Can't parse iallocator results: %s" % str(err))
13272 if not isinstance(rdict, dict):
13273 raise errors.OpExecError("Can't parse iallocator results: not a dict")
13275 # TODO: remove backwards compatiblity in later versions
13276 if "nodes" in rdict and "result" not in rdict:
13277 rdict["result"] = rdict["nodes"]
13280 for key in "success", "info", "result":
13281 if key not in rdict:
13282 raise errors.OpExecError("Can't parse iallocator results:"
13283 " missing key '%s'" % key)
13284 setattr(self, key, rdict[key])
13286 if not self._result_check(self.result):
13287 raise errors.OpExecError("Iallocator returned invalid result,"
13288 " expected %s, got %s" %
13289 (self._result_check, self.result),
13290 errors.ECODE_INVAL)
13292 if self.mode == constants.IALLOCATOR_MODE_RELOC:
13293 assert self.relocate_from is not None
13294 assert self.required_nodes == 1
13296 node2group = dict((name, ndata["group"])
13297 for (name, ndata) in self.in_data["nodes"].items())
13299 fn = compat.partial(self._NodesToGroups, node2group,
13300 self.in_data["nodegroups"])
13302 instance = self.cfg.GetInstanceInfo(self.name)
13303 request_groups = fn(self.relocate_from + [instance.primary_node])
13304 result_groups = fn(rdict["result"] + [instance.primary_node])
13306 if self.success and not set(result_groups).issubset(request_groups):
13307 raise errors.OpExecError("Groups of nodes returned by iallocator (%s)"
13308 " differ from original groups (%s)" %
13309 (utils.CommaJoin(result_groups),
13310 utils.CommaJoin(request_groups)))
13312 elif self.mode == constants.IALLOCATOR_MODE_NODE_EVAC:
13313 assert self.evac_mode in constants.IALLOCATOR_NEVAC_MODES
13315 self.out_data = rdict
13318 def _NodesToGroups(node2group, groups, nodes):
13319 """Returns a list of unique group names for a list of nodes.
13321 @type node2group: dict
13322 @param node2group: Map from node name to group UUID
13324 @param groups: Group information
13326 @param nodes: Node names
13333 group_uuid = node2group[node]
13335 # Ignore unknown node
13339 group = groups[group_uuid]
13341 # Can't find group, let's use UUID
13342 group_name = group_uuid
13344 group_name = group["name"]
13346 result.add(group_name)
13348 return sorted(result)
13351 class LUTestAllocator(NoHooksLU):
13352 """Run allocator tests.
13354 This LU runs the allocator tests
13357 def CheckPrereq(self):
13358 """Check prerequisites.
13360 This checks the opcode parameters depending on the director and mode test.
13363 if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
13364 for attr in ["memory", "disks", "disk_template",
13365 "os", "tags", "nics", "vcpus"]:
13366 if not hasattr(self.op, attr):
13367 raise errors.OpPrereqError("Missing attribute '%s' on opcode input" %
13368 attr, errors.ECODE_INVAL)
13369 iname = self.cfg.ExpandInstanceName(self.op.name)
13370 if iname is not None:
13371 raise errors.OpPrereqError("Instance '%s' already in the cluster" %
13372 iname, errors.ECODE_EXISTS)
13373 if not isinstance(self.op.nics, list):
13374 raise errors.OpPrereqError("Invalid parameter 'nics'",
13375 errors.ECODE_INVAL)
13376 if not isinstance(self.op.disks, list):
13377 raise errors.OpPrereqError("Invalid parameter 'disks'",
13378 errors.ECODE_INVAL)
13379 for row in self.op.disks:
13380 if (not isinstance(row, dict) or
13381 constants.IDISK_SIZE not in row or
13382 not isinstance(row[constants.IDISK_SIZE], int) or
13383 constants.IDISK_MODE not in row or
13384 row[constants.IDISK_MODE] not in constants.DISK_ACCESS_SET):
13385 raise errors.OpPrereqError("Invalid contents of the 'disks'"
13386 " parameter", errors.ECODE_INVAL)
13387 if self.op.hypervisor is None:
13388 self.op.hypervisor = self.cfg.GetHypervisorType()
13389 elif self.op.mode == constants.IALLOCATOR_MODE_RELOC:
13390 fname = _ExpandInstanceName(self.cfg, self.op.name)
13391 self.op.name = fname
13392 self.relocate_from = \
13393 list(self.cfg.GetInstanceInfo(fname).secondary_nodes)
13394 elif self.op.mode in (constants.IALLOCATOR_MODE_CHG_GROUP,
13395 constants.IALLOCATOR_MODE_NODE_EVAC):
13396 if not self.op.instances:
13397 raise errors.OpPrereqError("Missing instances", errors.ECODE_INVAL)
13398 self.op.instances = _GetWantedInstances(self, self.op.instances)
13400 raise errors.OpPrereqError("Invalid test allocator mode '%s'" %
13401 self.op.mode, errors.ECODE_INVAL)
13403 if self.op.direction == constants.IALLOCATOR_DIR_OUT:
13404 if self.op.allocator is None:
13405 raise errors.OpPrereqError("Missing allocator name",
13406 errors.ECODE_INVAL)
13407 elif self.op.direction != constants.IALLOCATOR_DIR_IN:
13408 raise errors.OpPrereqError("Wrong allocator test '%s'" %
13409 self.op.direction, errors.ECODE_INVAL)
13411 def Exec(self, feedback_fn):
13412 """Run the allocator test.
13415 if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
13416 ial = IAllocator(self.cfg, self.rpc,
13419 memory=self.op.memory,
13420 disks=self.op.disks,
13421 disk_template=self.op.disk_template,
13425 vcpus=self.op.vcpus,
13426 hypervisor=self.op.hypervisor,
13428 elif self.op.mode == constants.IALLOCATOR_MODE_RELOC:
13429 ial = IAllocator(self.cfg, self.rpc,
13432 relocate_from=list(self.relocate_from),
13434 elif self.op.mode == constants.IALLOCATOR_MODE_CHG_GROUP:
13435 ial = IAllocator(self.cfg, self.rpc,
13437 instances=self.op.instances,
13438 target_groups=self.op.target_groups)
13439 elif self.op.mode == constants.IALLOCATOR_MODE_NODE_EVAC:
13440 ial = IAllocator(self.cfg, self.rpc,
13442 instances=self.op.instances,
13443 evac_mode=self.op.evac_mode)
13445 raise errors.ProgrammerError("Uncatched mode %s in"
13446 " LUTestAllocator.Exec", self.op.mode)
13448 if self.op.direction == constants.IALLOCATOR_DIR_IN:
13449 result = ial.in_text
13451 ial.Run(self.op.allocator, validate=False)
13452 result = ial.out_text
13456 #: Query type implementations
13458 constants.QR_INSTANCE: _InstanceQuery,
13459 constants.QR_NODE: _NodeQuery,
13460 constants.QR_GROUP: _GroupQuery,
13461 constants.QR_OS: _OsQuery,
13464 assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP
13467 def _GetQueryImplementation(name):
13468 """Returns the implemtnation for a query type.
13470 @param name: Query type, must be one of L{constants.QR_VIA_OP}
13474 return _QUERY_IMPL[name]
13476 raise errors.OpPrereqError("Unknown query resource '%s'" % name,
13477 errors.ECODE_INVAL)