4 # Copyright (C) 2006, 2007, 2008 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-msg=W0613,W0201
37 from ganeti import ssh
38 from ganeti import utils
39 from ganeti import errors
40 from ganeti import hypervisor
41 from ganeti import locking
42 from ganeti import constants
43 from ganeti import objects
44 from ganeti import opcodes
45 from ganeti import serializer
46 from ganeti import ssconf
49 class LogicalUnit(object):
50 """Logical Unit base class.
52 Subclasses must follow these rules:
53 - implement ExpandNames
54 - implement CheckPrereq
56 - implement BuildHooksEnv
57 - redefine HPATH and HTYPE
58 - optionally redefine their run requirements:
59 REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
61 Note that all commands require root permissions.
69 def __init__(self, processor, op, context, rpc):
70 """Constructor for LogicalUnit.
72 This needs to be overriden in derived classes in order to check op
78 self.cfg = context.cfg
79 self.context = context
81 # Dicts used to declare locking needs to mcpu
82 self.needed_locks = None
83 self.acquired_locks = {}
84 self.share_locks = dict(((i, 0) for i in locking.LEVELS))
86 self.remove_locks = {}
87 # Used to force good behavior when calling helper functions
88 self.recalculate_locks = {}
91 self.LogWarning = processor.LogWarning
92 self.LogInfo = processor.LogInfo
94 for attr_name in self._OP_REQP:
95 attr_val = getattr(op, attr_name, None)
97 raise errors.OpPrereqError("Required parameter '%s' missing" %
102 """Returns the SshRunner object
106 self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
109 ssh = property(fget=__GetSSH)
111 def CheckArguments(self):
112 """Check syntactic validity for the opcode arguments.
114 This method is for doing a simple syntactic check and ensure
115 validity of opcode parameters, without any cluster-related
116 checks. While the same can be accomplished in ExpandNames and/or
117 CheckPrereq, doing these separate is better because:
119 - ExpandNames is left as as purely a lock-related function
120 - CheckPrereq is run after we have aquired locks (and possible
123 The function is allowed to change the self.op attribute so that
124 later methods can no longer worry about missing parameters.
129 def ExpandNames(self):
130 """Expand names for this LU.
132 This method is called before starting to execute the opcode, and it should
133 update all the parameters of the opcode to their canonical form (e.g. a
134 short node name must be fully expanded after this method has successfully
135 completed). This way locking, hooks, logging, ecc. can work correctly.
137 LUs which implement this method must also populate the self.needed_locks
138 member, as a dict with lock levels as keys, and a list of needed lock names
141 - use an empty dict if you don't need any lock
142 - if you don't need any lock at a particular level omit that level
143 - don't put anything for the BGL level
144 - if you want all locks at a level use locking.ALL_SET as a value
146 If you need to share locks (rather than acquire them exclusively) at one
147 level you can modify self.share_locks, setting a true value (usually 1) for
148 that level. By default locks are not shared.
152 # Acquire all nodes and one instance
153 self.needed_locks = {
154 locking.LEVEL_NODE: locking.ALL_SET,
155 locking.LEVEL_INSTANCE: ['instance1.example.tld'],
157 # Acquire just two nodes
158 self.needed_locks = {
159 locking.LEVEL_NODE: ['node1.example.tld', 'node2.example.tld'],
162 self.needed_locks = {} # No, you can't leave it to the default value None
165 # The implementation of this method is mandatory only if the new LU is
166 # concurrent, so that old LUs don't need to be changed all at the same
169 self.needed_locks = {} # Exclusive LUs don't need locks.
171 raise NotImplementedError
173 def DeclareLocks(self, level):
174 """Declare LU locking needs for a level
176 While most LUs can just declare their locking needs at ExpandNames time,
177 sometimes there's the need to calculate some locks after having acquired
178 the ones before. This function is called just before acquiring locks at a
179 particular level, but after acquiring the ones at lower levels, and permits
180 such calculations. It can be used to modify self.needed_locks, and by
181 default it does nothing.
183 This function is only called if you have something already set in
184 self.needed_locks for the level.
186 @param level: Locking level which is going to be locked
187 @type level: member of ganeti.locking.LEVELS
191 def CheckPrereq(self):
192 """Check prerequisites for this LU.
194 This method should check that the prerequisites for the execution
195 of this LU are fulfilled. It can do internode communication, but
196 it should be idempotent - no cluster or system changes are
199 The method should raise errors.OpPrereqError in case something is
200 not fulfilled. Its return value is ignored.
202 This method should also update all the parameters of the opcode to
203 their canonical form if it hasn't been done by ExpandNames before.
206 raise NotImplementedError
208 def Exec(self, feedback_fn):
211 This method should implement the actual work. It should raise
212 errors.OpExecError for failures that are somewhat dealt with in
216 raise NotImplementedError
218 def BuildHooksEnv(self):
219 """Build hooks environment for this LU.
221 This method should return a three-node tuple consisting of: a dict
222 containing the environment that will be used for running the
223 specific hook for this LU, a list of node names on which the hook
224 should run before the execution, and a list of node names on which
225 the hook should run after the execution.
227 The keys of the dict must not have 'GANETI_' prefixed as this will
228 be handled in the hooks runner. Also note additional keys will be
229 added by the hooks runner. If the LU doesn't define any
230 environment, an empty dict (and not None) should be returned.
232 No nodes should be returned as an empty list (and not None).
234 Note that if the HPATH for a LU class is None, this function will
238 raise NotImplementedError
240 def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
241 """Notify the LU about the results of its hooks.
243 This method is called every time a hooks phase is executed, and notifies
244 the Logical Unit about the hooks' result. The LU can then use it to alter
245 its result based on the hooks. By default the method does nothing and the
246 previous result is passed back unchanged but any LU can define it if it
247 wants to use the local cluster hook-scripts somehow.
249 @param phase: one of L{constants.HOOKS_PHASE_POST} or
250 L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
251 @param hook_results: the results of the multi-node hooks rpc call
252 @param feedback_fn: function used send feedback back to the caller
253 @param lu_result: the previous Exec result this LU had, or None
255 @return: the new Exec result, based on the previous result
261 def _ExpandAndLockInstance(self):
262 """Helper function to expand and lock an instance.
264 Many LUs that work on an instance take its name in self.op.instance_name
265 and need to expand it and then declare the expanded name for locking. This
266 function does it, and then updates self.op.instance_name to the expanded
267 name. It also initializes needed_locks as a dict, if this hasn't been done
271 if self.needed_locks is None:
272 self.needed_locks = {}
274 assert locking.LEVEL_INSTANCE not in self.needed_locks, \
275 "_ExpandAndLockInstance called with instance-level locks set"
276 expanded_name = self.cfg.ExpandInstanceName(self.op.instance_name)
277 if expanded_name is None:
278 raise errors.OpPrereqError("Instance '%s' not known" %
279 self.op.instance_name)
280 self.needed_locks[locking.LEVEL_INSTANCE] = expanded_name
281 self.op.instance_name = expanded_name
283 def _LockInstancesNodes(self, primary_only=False):
284 """Helper function to declare instances' nodes for locking.
286 This function should be called after locking one or more instances to lock
287 their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
288 with all primary or secondary nodes for instances already locked and
289 present in self.needed_locks[locking.LEVEL_INSTANCE].
291 It should be called from DeclareLocks, and for safety only works if
292 self.recalculate_locks[locking.LEVEL_NODE] is set.
294 In the future it may grow parameters to just lock some instance's nodes, or
295 to just lock primaries or secondary nodes, if needed.
297 If should be called in DeclareLocks in a way similar to::
299 if level == locking.LEVEL_NODE:
300 self._LockInstancesNodes()
302 @type primary_only: boolean
303 @param primary_only: only lock primary nodes of locked instances
306 assert locking.LEVEL_NODE in self.recalculate_locks, \
307 "_LockInstancesNodes helper function called with no nodes to recalculate"
309 # TODO: check if we're really been called with the instance locks held
311 # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
312 # future we might want to have different behaviors depending on the value
313 # of self.recalculate_locks[locking.LEVEL_NODE]
315 for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]:
316 instance = self.context.cfg.GetInstanceInfo(instance_name)
317 wanted_nodes.append(instance.primary_node)
319 wanted_nodes.extend(instance.secondary_nodes)
321 if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
322 self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
323 elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
324 self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
326 del self.recalculate_locks[locking.LEVEL_NODE]
329 class NoHooksLU(LogicalUnit):
330 """Simple LU which runs no hooks.
332 This LU is intended as a parent for other LogicalUnits which will
333 run no hooks, in order to reduce duplicate code.
340 def _GetWantedNodes(lu, nodes):
341 """Returns list of checked and expanded node names.
343 @type lu: L{LogicalUnit}
344 @param lu: the logical unit on whose behalf we execute
346 @param nodes: list of node names or None for all nodes
348 @return: the list of nodes, sorted
349 @raise errors.OpProgrammerError: if the nodes parameter is wrong type
352 if not isinstance(nodes, list):
353 raise errors.OpPrereqError("Invalid argument type 'nodes'")
356 raise errors.ProgrammerError("_GetWantedNodes should only be called with a"
357 " non-empty list of nodes whose name is to be expanded.")
361 node = lu.cfg.ExpandNodeName(name)
363 raise errors.OpPrereqError("No such node name '%s'" % name)
366 return utils.NiceSort(wanted)
369 def _GetWantedInstances(lu, instances):
370 """Returns list of checked and expanded instance names.
372 @type lu: L{LogicalUnit}
373 @param lu: the logical unit on whose behalf we execute
374 @type instances: list
375 @param instances: list of instance names or None for all instances
377 @return: the list of instances, sorted
378 @raise errors.OpPrereqError: if the instances parameter is wrong type
379 @raise errors.OpPrereqError: if any of the passed instances is not found
382 if not isinstance(instances, list):
383 raise errors.OpPrereqError("Invalid argument type 'instances'")
388 for name in instances:
389 instance = lu.cfg.ExpandInstanceName(name)
391 raise errors.OpPrereqError("No such instance name '%s'" % name)
392 wanted.append(instance)
395 wanted = utils.NiceSort(lu.cfg.GetInstanceList())
399 def _CheckOutputFields(static, dynamic, selected):
400 """Checks whether all selected fields are valid.
402 @type static: L{utils.FieldSet}
403 @param static: static fields set
404 @type dynamic: L{utils.FieldSet}
405 @param dynamic: dynamic fields set
412 delta = f.NonMatching(selected)
414 raise errors.OpPrereqError("Unknown output fields selected: %s"
418 def _CheckBooleanOpField(op, name):
419 """Validates boolean opcode parameters.
421 This will ensure that an opcode parameter is either a boolean value,
422 or None (but that it always exists).
425 val = getattr(op, name, None)
426 if not (val is None or isinstance(val, bool)):
427 raise errors.OpPrereqError("Invalid boolean parameter '%s' (%s)" %
429 setattr(op, name, val)
432 def _CheckNodeOnline(lu, node):
433 """Ensure that a given node is online.
435 @param lu: the LU on behalf of which we make the check
436 @param node: the node to check
437 @raise errors.OpPrereqError: if the nodes is offline
440 if lu.cfg.GetNodeInfo(node).offline:
441 raise errors.OpPrereqError("Can't use offline node %s" % node)
444 def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
445 memory, vcpus, nics):
446 """Builds instance related env variables for hooks
448 This builds the hook environment from individual variables.
451 @param name: the name of the instance
452 @type primary_node: string
453 @param primary_node: the name of the instance's primary node
454 @type secondary_nodes: list
455 @param secondary_nodes: list of secondary nodes as strings
456 @type os_type: string
457 @param os_type: the name of the instance's OS
458 @type status: boolean
459 @param status: the should_run status of the instance
461 @param memory: the memory size of the instance
463 @param vcpus: the count of VCPUs the instance has
465 @param nics: list of tuples (ip, bridge, mac) representing
466 the NICs the instance has
468 @return: the hook environment for this instance
477 "INSTANCE_NAME": name,
478 "INSTANCE_PRIMARY": primary_node,
479 "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
480 "INSTANCE_OS_TYPE": os_type,
481 "INSTANCE_STATUS": str_status,
482 "INSTANCE_MEMORY": memory,
483 "INSTANCE_VCPUS": vcpus,
487 nic_count = len(nics)
488 for idx, (ip, bridge, mac) in enumerate(nics):
491 env["INSTANCE_NIC%d_IP" % idx] = ip
492 env["INSTANCE_NIC%d_BRIDGE" % idx] = bridge
493 env["INSTANCE_NIC%d_HWADDR" % idx] = mac
497 env["INSTANCE_NIC_COUNT"] = nic_count
502 def _BuildInstanceHookEnvByObject(lu, instance, override=None):
503 """Builds instance related env variables for hooks from an object.
505 @type lu: L{LogicalUnit}
506 @param lu: the logical unit on whose behalf we execute
507 @type instance: L{objects.Instance}
508 @param instance: the instance for which we should build the
511 @param override: dictionary with key/values that will override
514 @return: the hook environment dictionary
517 bep = lu.cfg.GetClusterInfo().FillBE(instance)
519 'name': instance.name,
520 'primary_node': instance.primary_node,
521 'secondary_nodes': instance.secondary_nodes,
522 'os_type': instance.os,
523 'status': instance.admin_up,
524 'memory': bep[constants.BE_MEMORY],
525 'vcpus': bep[constants.BE_VCPUS],
526 'nics': [(nic.ip, nic.bridge, nic.mac) for nic in instance.nics],
529 args.update(override)
530 return _BuildInstanceHookEnv(**args)
533 def _AdjustCandidatePool(lu):
534 """Adjust the candidate pool after node operations.
537 mod_list = lu.cfg.MaintainCandidatePool()
539 lu.LogInfo("Promoted nodes to master candidate role: %s",
540 ", ".join(node.name for node in mod_list))
541 for name in mod_list:
542 lu.context.ReaddNode(name)
543 mc_now, mc_max = lu.cfg.GetMasterCandidateStats()
545 lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
549 def _CheckInstanceBridgesExist(lu, instance):
550 """Check that the brigdes needed by an instance exist.
553 # check bridges existance
554 brlist = [nic.bridge for nic in instance.nics]
555 result = lu.rpc.call_bridges_exist(instance.primary_node, brlist)
558 raise errors.OpPrereqError("One or more target bridges %s does not"
559 " exist on destination node '%s'" %
560 (brlist, instance.primary_node))
563 class LUDestroyCluster(NoHooksLU):
564 """Logical unit for destroying the cluster.
569 def CheckPrereq(self):
570 """Check prerequisites.
572 This checks whether the cluster is empty.
574 Any errors are signalled by raising errors.OpPrereqError.
577 master = self.cfg.GetMasterNode()
579 nodelist = self.cfg.GetNodeList()
580 if len(nodelist) != 1 or nodelist[0] != master:
581 raise errors.OpPrereqError("There are still %d node(s) in"
582 " this cluster." % (len(nodelist) - 1))
583 instancelist = self.cfg.GetInstanceList()
585 raise errors.OpPrereqError("There are still %d instance(s) in"
586 " this cluster." % len(instancelist))
588 def Exec(self, feedback_fn):
589 """Destroys the cluster.
592 master = self.cfg.GetMasterNode()
593 result = self.rpc.call_node_stop_master(master, False)
596 raise errors.OpExecError("Could not disable the master role")
597 priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
598 utils.CreateBackup(priv_key)
599 utils.CreateBackup(pub_key)
603 class LUVerifyCluster(LogicalUnit):
604 """Verifies the cluster status.
607 HPATH = "cluster-verify"
608 HTYPE = constants.HTYPE_CLUSTER
609 _OP_REQP = ["skip_checks"]
612 def ExpandNames(self):
613 self.needed_locks = {
614 locking.LEVEL_NODE: locking.ALL_SET,
615 locking.LEVEL_INSTANCE: locking.ALL_SET,
617 self.share_locks = dict(((i, 1) for i in locking.LEVELS))
619 def _VerifyNode(self, nodeinfo, file_list, local_cksum,
620 node_result, feedback_fn, master_files,
622 """Run multiple tests against a node.
626 - compares ganeti version
627 - checks vg existance and size > 20G
628 - checks config file checksum
629 - checks ssh to other nodes
631 @type nodeinfo: L{objects.Node}
632 @param nodeinfo: the node to check
633 @param file_list: required list of files
634 @param local_cksum: dictionary of local files and their checksums
635 @param node_result: the results from the node
636 @param feedback_fn: function used to accumulate results
637 @param master_files: list of files that only masters should have
638 @param drbd_map: the useddrbd minors for this node, in
639 form of minor: (instance, must_exist) which correspond to instances
640 and their running status
645 # main result, node_result should be a non-empty dict
646 if not node_result or not isinstance(node_result, dict):
647 feedback_fn(" - ERROR: unable to verify node %s." % (node,))
650 # compares ganeti version
651 local_version = constants.PROTOCOL_VERSION
652 remote_version = node_result.get('version', None)
653 if not (remote_version and isinstance(remote_version, (list, tuple)) and
654 len(remote_version) == 2):
655 feedback_fn(" - ERROR: connection to %s failed" % (node))
658 if local_version != remote_version[0]:
659 feedback_fn(" - ERROR: incompatible protocol versions: master %s,"
660 " node %s %s" % (local_version, node, remote_version[0]))
663 # node seems compatible, we can actually try to look into its results
667 # full package version
668 if constants.RELEASE_VERSION != remote_version[1]:
669 feedback_fn(" - WARNING: software version mismatch: master %s,"
671 (constants.RELEASE_VERSION, node, remote_version[1]))
673 # checks vg existence and size > 20G
675 vglist = node_result.get(constants.NV_VGLIST, None)
677 feedback_fn(" - ERROR: unable to check volume groups on node %s." %
681 vgstatus = utils.CheckVolumeGroupSize(vglist, self.cfg.GetVGName(),
682 constants.MIN_VG_SIZE)
684 feedback_fn(" - ERROR: %s on node %s" % (vgstatus, node))
687 # checks config file checksum
689 remote_cksum = node_result.get(constants.NV_FILELIST, None)
690 if not isinstance(remote_cksum, dict):
692 feedback_fn(" - ERROR: node hasn't returned file checksum data")
694 for file_name in file_list:
695 node_is_mc = nodeinfo.master_candidate
696 must_have_file = file_name not in master_files
697 if file_name not in remote_cksum:
698 if node_is_mc or must_have_file:
700 feedback_fn(" - ERROR: file '%s' missing" % file_name)
701 elif remote_cksum[file_name] != local_cksum[file_name]:
702 if node_is_mc or must_have_file:
704 feedback_fn(" - ERROR: file '%s' has wrong checksum" % file_name)
706 # not candidate and this is not a must-have file
708 feedback_fn(" - ERROR: non master-candidate has old/wrong file"
711 # all good, except non-master/non-must have combination
712 if not node_is_mc and not must_have_file:
713 feedback_fn(" - ERROR: file '%s' should not exist on non master"
714 " candidates" % file_name)
718 if constants.NV_NODELIST not in node_result:
720 feedback_fn(" - ERROR: node hasn't returned node ssh connectivity data")
722 if node_result[constants.NV_NODELIST]:
724 for node in node_result[constants.NV_NODELIST]:
725 feedback_fn(" - ERROR: ssh communication with node '%s': %s" %
726 (node, node_result[constants.NV_NODELIST][node]))
728 if constants.NV_NODENETTEST not in node_result:
730 feedback_fn(" - ERROR: node hasn't returned node tcp connectivity data")
732 if node_result[constants.NV_NODENETTEST]:
734 nlist = utils.NiceSort(node_result[constants.NV_NODENETTEST].keys())
736 feedback_fn(" - ERROR: tcp communication with node '%s': %s" %
737 (node, node_result[constants.NV_NODENETTEST][node]))
739 hyp_result = node_result.get(constants.NV_HYPERVISOR, None)
740 if isinstance(hyp_result, dict):
741 for hv_name, hv_result in hyp_result.iteritems():
742 if hv_result is not None:
743 feedback_fn(" - ERROR: hypervisor %s verify failure: '%s'" %
744 (hv_name, hv_result))
746 # check used drbd list
747 used_minors = node_result.get(constants.NV_DRBDLIST, [])
748 for minor, (iname, must_exist) in drbd_map.items():
749 if minor not in used_minors and must_exist:
750 feedback_fn(" - ERROR: drbd minor %d of instance %s is not active" %
753 for minor in used_minors:
754 if minor not in drbd_map:
755 feedback_fn(" - ERROR: unallocated drbd minor %d is in use" % minor)
760 def _VerifyInstance(self, instance, instanceconfig, node_vol_is,
761 node_instance, feedback_fn, n_offline):
762 """Verify an instance.
764 This function checks to see if the required block devices are
765 available on the instance's node.
770 node_current = instanceconfig.primary_node
773 instanceconfig.MapLVsByNode(node_vol_should)
775 for node in node_vol_should:
776 if node in n_offline:
777 # ignore missing volumes on offline nodes
779 for volume in node_vol_should[node]:
780 if node not in node_vol_is or volume not in node_vol_is[node]:
781 feedback_fn(" - ERROR: volume %s missing on node %s" %
785 if instanceconfig.admin_up:
786 if ((node_current not in node_instance or
787 not instance in node_instance[node_current]) and
788 node_current not in n_offline):
789 feedback_fn(" - ERROR: instance %s not running on node %s" %
790 (instance, node_current))
793 for node in node_instance:
794 if (not node == node_current):
795 if instance in node_instance[node]:
796 feedback_fn(" - ERROR: instance %s should not run on node %s" %
802 def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is, feedback_fn):
803 """Verify if there are any unknown volumes in the cluster.
805 The .os, .swap and backup volumes are ignored. All other volumes are
811 for node in node_vol_is:
812 for volume in node_vol_is[node]:
813 if node not in node_vol_should or volume not in node_vol_should[node]:
814 feedback_fn(" - ERROR: volume %s on node %s should not exist" %
819 def _VerifyOrphanInstances(self, instancelist, node_instance, feedback_fn):
820 """Verify the list of running instances.
822 This checks what instances are running but unknown to the cluster.
826 for node in node_instance:
827 for runninginstance in node_instance[node]:
828 if runninginstance not in instancelist:
829 feedback_fn(" - ERROR: instance %s on node %s should not exist" %
830 (runninginstance, node))
834 def _VerifyNPlusOneMemory(self, node_info, instance_cfg, feedback_fn):
835 """Verify N+1 Memory Resilience.
837 Check that if one single node dies we can still start all the instances it
843 for node, nodeinfo in node_info.iteritems():
844 # This code checks that every node which is now listed as secondary has
845 # enough memory to host all instances it is supposed to should a single
846 # other node in the cluster fail.
847 # FIXME: not ready for failover to an arbitrary node
848 # FIXME: does not support file-backed instances
849 # WARNING: we currently take into account down instances as well as up
850 # ones, considering that even if they're down someone might want to start
851 # them even in the event of a node failure.
852 for prinode, instances in nodeinfo['sinst-by-pnode'].iteritems():
854 for instance in instances:
855 bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
856 if bep[constants.BE_AUTO_BALANCE]:
857 needed_mem += bep[constants.BE_MEMORY]
858 if nodeinfo['mfree'] < needed_mem:
859 feedback_fn(" - ERROR: not enough memory on node %s to accomodate"
860 " failovers should node %s fail" % (node, prinode))
864 def CheckPrereq(self):
865 """Check prerequisites.
867 Transform the list of checks we're going to skip into a set and check that
868 all its members are valid.
871 self.skip_set = frozenset(self.op.skip_checks)
872 if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
873 raise errors.OpPrereqError("Invalid checks to be skipped specified")
875 def BuildHooksEnv(self):
878 Cluster-Verify hooks just rone in the post phase and their failure makes
879 the output be logged in the verify output and the verification to fail.
882 all_nodes = self.cfg.GetNodeList()
883 # TODO: populate the environment with useful information for verify hooks
885 return env, [], all_nodes
887 def Exec(self, feedback_fn):
888 """Verify integrity of cluster, performing various test on nodes.
892 feedback_fn("* Verifying global settings")
893 for msg in self.cfg.VerifyConfig():
894 feedback_fn(" - ERROR: %s" % msg)
896 vg_name = self.cfg.GetVGName()
897 hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
898 nodelist = utils.NiceSort(self.cfg.GetNodeList())
899 nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
900 instancelist = utils.NiceSort(self.cfg.GetInstanceList())
901 instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
902 for iname in instancelist)
903 i_non_redundant = [] # Non redundant instances
904 i_non_a_balanced = [] # Non auto-balanced instances
905 n_offline = [] # List of offline nodes
911 # FIXME: verify OS list
913 master_files = [constants.CLUSTER_CONF_FILE]
915 file_names = ssconf.SimpleStore().GetFileList()
916 file_names.append(constants.SSL_CERT_FILE)
917 file_names.append(constants.RAPI_CERT_FILE)
918 file_names.extend(master_files)
920 local_checksums = utils.FingerprintFiles(file_names)
922 feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
923 node_verify_param = {
924 constants.NV_FILELIST: file_names,
925 constants.NV_NODELIST: [node.name for node in nodeinfo
926 if not node.offline],
927 constants.NV_HYPERVISOR: hypervisors,
928 constants.NV_NODENETTEST: [(node.name, node.primary_ip,
929 node.secondary_ip) for node in nodeinfo
930 if not node.offline],
931 constants.NV_LVLIST: vg_name,
932 constants.NV_INSTANCELIST: hypervisors,
933 constants.NV_VGLIST: None,
934 constants.NV_VERSION: None,
935 constants.NV_HVINFO: self.cfg.GetHypervisorType(),
936 constants.NV_DRBDLIST: None,
938 all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
939 self.cfg.GetClusterName())
941 cluster = self.cfg.GetClusterInfo()
942 master_node = self.cfg.GetMasterNode()
943 all_drbd_map = self.cfg.ComputeDRBDMap()
945 for node_i in nodeinfo:
947 nresult = all_nvinfo[node].data
950 feedback_fn("* Skipping offline node %s" % (node,))
951 n_offline.append(node)
954 if node == master_node:
956 elif node_i.master_candidate:
957 ntype = "master candidate"
960 feedback_fn("* Verifying node %s (%s)" % (node, ntype))
962 if all_nvinfo[node].failed or not isinstance(nresult, dict):
963 feedback_fn(" - ERROR: connection to %s failed" % (node,))
968 for minor, instance in all_drbd_map[node].items():
969 instance = instanceinfo[instance]
970 node_drbd[minor] = (instance.name, instance.admin_up)
971 result = self._VerifyNode(node_i, file_names, local_checksums,
972 nresult, feedback_fn, master_files,
976 lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
977 if isinstance(lvdata, basestring):
978 feedback_fn(" - ERROR: LVM problem on node %s: %s" %
979 (node, utils.SafeEncode(lvdata)))
981 node_volume[node] = {}
982 elif not isinstance(lvdata, dict):
983 feedback_fn(" - ERROR: connection to %s failed (lvlist)" % (node,))
987 node_volume[node] = lvdata
990 idata = nresult.get(constants.NV_INSTANCELIST, None)
991 if not isinstance(idata, list):
992 feedback_fn(" - ERROR: connection to %s failed (instancelist)" %
997 node_instance[node] = idata
1000 nodeinfo = nresult.get(constants.NV_HVINFO, None)
1001 if not isinstance(nodeinfo, dict):
1002 feedback_fn(" - ERROR: connection to %s failed (hvinfo)" % (node,))
1008 "mfree": int(nodeinfo['memory_free']),
1009 "dfree": int(nresult[constants.NV_VGLIST][vg_name]),
1012 # dictionary holding all instances this node is secondary for,
1013 # grouped by their primary node. Each key is a cluster node, and each
1014 # value is a list of instances which have the key as primary and the
1015 # current node as secondary. this is handy to calculate N+1 memory
1016 # availability if you can only failover from a primary to its
1018 "sinst-by-pnode": {},
1021 feedback_fn(" - ERROR: invalid value returned from node %s" % (node,))
1025 node_vol_should = {}
1027 for instance in instancelist:
1028 feedback_fn("* Verifying instance %s" % instance)
1029 inst_config = instanceinfo[instance]
1030 result = self._VerifyInstance(instance, inst_config, node_volume,
1031 node_instance, feedback_fn, n_offline)
1033 inst_nodes_offline = []
1035 inst_config.MapLVsByNode(node_vol_should)
1037 instance_cfg[instance] = inst_config
1039 pnode = inst_config.primary_node
1040 if pnode in node_info:
1041 node_info[pnode]['pinst'].append(instance)
1042 elif pnode not in n_offline:
1043 feedback_fn(" - ERROR: instance %s, connection to primary node"
1044 " %s failed" % (instance, pnode))
1047 if pnode in n_offline:
1048 inst_nodes_offline.append(pnode)
1050 # If the instance is non-redundant we cannot survive losing its primary
1051 # node, so we are not N+1 compliant. On the other hand we have no disk
1052 # templates with more than one secondary so that situation is not well
1054 # FIXME: does not support file-backed instances
1055 if len(inst_config.secondary_nodes) == 0:
1056 i_non_redundant.append(instance)
1057 elif len(inst_config.secondary_nodes) > 1:
1058 feedback_fn(" - WARNING: multiple secondaries for instance %s"
1061 if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1062 i_non_a_balanced.append(instance)
1064 for snode in inst_config.secondary_nodes:
1065 if snode in node_info:
1066 node_info[snode]['sinst'].append(instance)
1067 if pnode not in node_info[snode]['sinst-by-pnode']:
1068 node_info[snode]['sinst-by-pnode'][pnode] = []
1069 node_info[snode]['sinst-by-pnode'][pnode].append(instance)
1070 elif snode not in n_offline:
1071 feedback_fn(" - ERROR: instance %s, connection to secondary node"
1072 " %s failed" % (instance, snode))
1074 if snode in n_offline:
1075 inst_nodes_offline.append(snode)
1077 if inst_nodes_offline:
1078 # warn that the instance lives on offline nodes, and set bad=True
1079 feedback_fn(" - ERROR: instance lives on offline node(s) %s" %
1080 ", ".join(inst_nodes_offline))
1083 feedback_fn("* Verifying orphan volumes")
1084 result = self._VerifyOrphanVolumes(node_vol_should, node_volume,
1088 feedback_fn("* Verifying remaining instances")
1089 result = self._VerifyOrphanInstances(instancelist, node_instance,
1093 if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1094 feedback_fn("* Verifying N+1 Memory redundancy")
1095 result = self._VerifyNPlusOneMemory(node_info, instance_cfg, feedback_fn)
1098 feedback_fn("* Other Notes")
1100 feedback_fn(" - NOTICE: %d non-redundant instance(s) found."
1101 % len(i_non_redundant))
1103 if i_non_a_balanced:
1104 feedback_fn(" - NOTICE: %d non-auto-balanced instance(s) found."
1105 % len(i_non_a_balanced))
1108 feedback_fn(" - NOTICE: %d offline node(s) found." % len(n_offline))
1112 def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1113 """Analize the post-hooks' result
1115 This method analyses the hook result, handles it, and sends some
1116 nicely-formatted feedback back to the user.
1118 @param phase: one of L{constants.HOOKS_PHASE_POST} or
1119 L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
1120 @param hooks_results: the results of the multi-node hooks rpc call
1121 @param feedback_fn: function used send feedback back to the caller
1122 @param lu_result: previous Exec result
1123 @return: the new Exec result, based on the previous result
1127 # We only really run POST phase hooks, and are only interested in
1129 if phase == constants.HOOKS_PHASE_POST:
1130 # Used to change hooks' output to proper indentation
1131 indent_re = re.compile('^', re.M)
1132 feedback_fn("* Hooks Results")
1133 if not hooks_results:
1134 feedback_fn(" - ERROR: general communication failure")
1137 for node_name in hooks_results:
1138 show_node_header = True
1139 res = hooks_results[node_name]
1140 if res.failed or res.data is False or not isinstance(res.data, list):
1142 # no need to warn or set fail return value
1144 feedback_fn(" Communication failure in hooks execution")
1147 for script, hkr, output in res.data:
1148 if hkr == constants.HKR_FAIL:
1149 # The node header is only shown once, if there are
1150 # failing hooks on that node
1151 if show_node_header:
1152 feedback_fn(" Node %s:" % node_name)
1153 show_node_header = False
1154 feedback_fn(" ERROR: Script %s failed, output:" % script)
1155 output = indent_re.sub(' ', output)
1156 feedback_fn("%s" % output)
1162 class LUVerifyDisks(NoHooksLU):
1163 """Verifies the cluster disks status.
1169 def ExpandNames(self):
1170 self.needed_locks = {
1171 locking.LEVEL_NODE: locking.ALL_SET,
1172 locking.LEVEL_INSTANCE: locking.ALL_SET,
1174 self.share_locks = dict(((i, 1) for i in locking.LEVELS))
1176 def CheckPrereq(self):
1177 """Check prerequisites.
1179 This has no prerequisites.
1184 def Exec(self, feedback_fn):
1185 """Verify integrity of cluster disks.
1188 result = res_nodes, res_nlvm, res_instances, res_missing = [], {}, [], {}
1190 vg_name = self.cfg.GetVGName()
1191 nodes = utils.NiceSort(self.cfg.GetNodeList())
1192 instances = [self.cfg.GetInstanceInfo(name)
1193 for name in self.cfg.GetInstanceList()]
1196 for inst in instances:
1198 if (not inst.admin_up or
1199 inst.disk_template not in constants.DTS_NET_MIRROR):
1201 inst.MapLVsByNode(inst_lvs)
1202 # transform { iname: {node: [vol,],},} to {(node, vol): iname}
1203 for node, vol_list in inst_lvs.iteritems():
1204 for vol in vol_list:
1205 nv_dict[(node, vol)] = inst
1210 node_lvs = self.rpc.call_volume_list(nodes, vg_name)
1215 lvs = node_lvs[node]
1218 self.LogWarning("Connection to node %s failed: %s" %
1222 if isinstance(lvs, basestring):
1223 logging.warning("Error enumerating LVs on node %s: %s", node, lvs)
1224 res_nlvm[node] = lvs
1225 elif not isinstance(lvs, dict):
1226 logging.warning("Connection to node %s failed or invalid data"
1228 res_nodes.append(node)
1231 for lv_name, (_, lv_inactive, lv_online) in lvs.iteritems():
1232 inst = nv_dict.pop((node, lv_name), None)
1233 if (not lv_online and inst is not None
1234 and inst.name not in res_instances):
1235 res_instances.append(inst.name)
1237 # any leftover items in nv_dict are missing LVs, let's arrange the
1239 for key, inst in nv_dict.iteritems():
1240 if inst.name not in res_missing:
1241 res_missing[inst.name] = []
1242 res_missing[inst.name].append(key)
1247 class LURenameCluster(LogicalUnit):
1248 """Rename the cluster.
1251 HPATH = "cluster-rename"
1252 HTYPE = constants.HTYPE_CLUSTER
1255 def BuildHooksEnv(self):
1260 "OP_TARGET": self.cfg.GetClusterName(),
1261 "NEW_NAME": self.op.name,
1263 mn = self.cfg.GetMasterNode()
1264 return env, [mn], [mn]
1266 def CheckPrereq(self):
1267 """Verify that the passed name is a valid one.
1270 hostname = utils.HostInfo(self.op.name)
1272 new_name = hostname.name
1273 self.ip = new_ip = hostname.ip
1274 old_name = self.cfg.GetClusterName()
1275 old_ip = self.cfg.GetMasterIP()
1276 if new_name == old_name and new_ip == old_ip:
1277 raise errors.OpPrereqError("Neither the name nor the IP address of the"
1278 " cluster has changed")
1279 if new_ip != old_ip:
1280 if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
1281 raise errors.OpPrereqError("The given cluster IP address (%s) is"
1282 " reachable on the network. Aborting." %
1285 self.op.name = new_name
1287 def Exec(self, feedback_fn):
1288 """Rename the cluster.
1291 clustername = self.op.name
1294 # shutdown the master IP
1295 master = self.cfg.GetMasterNode()
1296 result = self.rpc.call_node_stop_master(master, False)
1297 if result.failed or not result.data:
1298 raise errors.OpExecError("Could not disable the master role")
1301 cluster = self.cfg.GetClusterInfo()
1302 cluster.cluster_name = clustername
1303 cluster.master_ip = ip
1304 self.cfg.Update(cluster)
1306 # update the known hosts file
1307 ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
1308 node_list = self.cfg.GetNodeList()
1310 node_list.remove(master)
1313 result = self.rpc.call_upload_file(node_list,
1314 constants.SSH_KNOWN_HOSTS_FILE)
1315 for to_node, to_result in result.iteritems():
1316 if to_result.failed or not to_result.data:
1317 logging.error("Copy of file %s to node %s failed",
1318 constants.SSH_KNOWN_HOSTS_FILE, to_node)
1321 result = self.rpc.call_node_start_master(master, False)
1322 if result.failed or not result.data:
1323 self.LogWarning("Could not re-enable the master role on"
1324 " the master, please restart manually.")
1327 def _RecursiveCheckIfLVMBased(disk):
1328 """Check if the given disk or its children are lvm-based.
1330 @type disk: L{objects.Disk}
1331 @param disk: the disk to check
1333 @return: boolean indicating whether a LD_LV dev_type was found or not
1337 for chdisk in disk.children:
1338 if _RecursiveCheckIfLVMBased(chdisk):
1340 return disk.dev_type == constants.LD_LV
1343 class LUSetClusterParams(LogicalUnit):
1344 """Change the parameters of the cluster.
1347 HPATH = "cluster-modify"
1348 HTYPE = constants.HTYPE_CLUSTER
1352 def CheckParameters(self):
1356 if not hasattr(self.op, "candidate_pool_size"):
1357 self.op.candidate_pool_size = None
1358 if self.op.candidate_pool_size is not None:
1360 self.op.candidate_pool_size = int(self.op.candidate_pool_size)
1361 except ValueError, err:
1362 raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
1364 if self.op.candidate_pool_size < 1:
1365 raise errors.OpPrereqError("At least one master candidate needed")
1367 def ExpandNames(self):
1368 # FIXME: in the future maybe other cluster params won't require checking on
1369 # all nodes to be modified.
1370 self.needed_locks = {
1371 locking.LEVEL_NODE: locking.ALL_SET,
1373 self.share_locks[locking.LEVEL_NODE] = 1
1375 def BuildHooksEnv(self):
1380 "OP_TARGET": self.cfg.GetClusterName(),
1381 "NEW_VG_NAME": self.op.vg_name,
1383 mn = self.cfg.GetMasterNode()
1384 return env, [mn], [mn]
1386 def CheckPrereq(self):
1387 """Check prerequisites.
1389 This checks whether the given params don't conflict and
1390 if the given volume group is valid.
1393 # FIXME: This only works because there is only one parameter that can be
1394 # changed or removed.
1395 if self.op.vg_name is not None and not self.op.vg_name:
1396 instances = self.cfg.GetAllInstancesInfo().values()
1397 for inst in instances:
1398 for disk in inst.disks:
1399 if _RecursiveCheckIfLVMBased(disk):
1400 raise errors.OpPrereqError("Cannot disable lvm storage while"
1401 " lvm-based instances exist")
1403 node_list = self.acquired_locks[locking.LEVEL_NODE]
1405 # if vg_name not None, checks given volume group on all nodes
1407 vglist = self.rpc.call_vg_list(node_list)
1408 for node in node_list:
1409 if vglist[node].failed:
1410 # ignoring down node
1411 self.LogWarning("Node %s unreachable/error, ignoring" % node)
1413 vgstatus = utils.CheckVolumeGroupSize(vglist[node].data,
1415 constants.MIN_VG_SIZE)
1417 raise errors.OpPrereqError("Error on node '%s': %s" %
1420 self.cluster = cluster = self.cfg.GetClusterInfo()
1421 # validate beparams changes
1422 if self.op.beparams:
1423 utils.CheckBEParams(self.op.beparams)
1424 self.new_beparams = cluster.FillDict(
1425 cluster.beparams[constants.BEGR_DEFAULT], self.op.beparams)
1427 # hypervisor list/parameters
1428 self.new_hvparams = cluster.FillDict(cluster.hvparams, {})
1429 if self.op.hvparams:
1430 if not isinstance(self.op.hvparams, dict):
1431 raise errors.OpPrereqError("Invalid 'hvparams' parameter on input")
1432 for hv_name, hv_dict in self.op.hvparams.items():
1433 if hv_name not in self.new_hvparams:
1434 self.new_hvparams[hv_name] = hv_dict
1436 self.new_hvparams[hv_name].update(hv_dict)
1438 if self.op.enabled_hypervisors is not None:
1439 self.hv_list = self.op.enabled_hypervisors
1441 self.hv_list = cluster.enabled_hypervisors
1443 if self.op.hvparams or self.op.enabled_hypervisors is not None:
1444 # either the enabled list has changed, or the parameters have, validate
1445 for hv_name, hv_params in self.new_hvparams.items():
1446 if ((self.op.hvparams and hv_name in self.op.hvparams) or
1447 (self.op.enabled_hypervisors and
1448 hv_name in self.op.enabled_hypervisors)):
1449 # either this is a new hypervisor, or its parameters have changed
1450 hv_class = hypervisor.GetHypervisor(hv_name)
1451 hv_class.CheckParameterSyntax(hv_params)
1452 _CheckHVParams(self, node_list, hv_name, hv_params)
1454 def Exec(self, feedback_fn):
1455 """Change the parameters of the cluster.
1458 if self.op.vg_name is not None:
1459 if self.op.vg_name != self.cfg.GetVGName():
1460 self.cfg.SetVGName(self.op.vg_name)
1462 feedback_fn("Cluster LVM configuration already in desired"
1463 " state, not changing")
1464 if self.op.hvparams:
1465 self.cluster.hvparams = self.new_hvparams
1466 if self.op.enabled_hypervisors is not None:
1467 self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
1468 if self.op.beparams:
1469 self.cluster.beparams[constants.BEGR_DEFAULT] = self.new_beparams
1470 if self.op.candidate_pool_size is not None:
1471 self.cluster.candidate_pool_size = self.op.candidate_pool_size
1473 self.cfg.Update(self.cluster)
1475 # we want to update nodes after the cluster so that if any errors
1476 # happen, we have recorded and saved the cluster info
1477 if self.op.candidate_pool_size is not None:
1478 _AdjustCandidatePool(self)
1481 class LURedistributeConfig(NoHooksLU):
1482 """Force the redistribution of cluster configuration.
1484 This is a very simple LU.
1490 def ExpandNames(self):
1491 self.needed_locks = {
1492 locking.LEVEL_NODE: locking.ALL_SET,
1494 self.share_locks[locking.LEVEL_NODE] = 1
1496 def CheckPrereq(self):
1497 """Check prerequisites.
1501 def Exec(self, feedback_fn):
1502 """Redistribute the configuration.
1505 self.cfg.Update(self.cfg.GetClusterInfo())
1508 def _WaitForSync(lu, instance, oneshot=False, unlock=False):
1509 """Sleep and poll for an instance's disk to sync.
1512 if not instance.disks:
1516 lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
1518 node = instance.primary_node
1520 for dev in instance.disks:
1521 lu.cfg.SetDiskID(dev, node)
1527 cumul_degraded = False
1528 rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks)
1529 if rstats.failed or not rstats.data:
1530 lu.LogWarning("Can't get any data from node %s", node)
1533 raise errors.RemoteError("Can't contact node %s for mirror data,"
1534 " aborting." % node)
1537 rstats = rstats.data
1539 for i, mstat in enumerate(rstats):
1541 lu.LogWarning("Can't compute data for node %s/%s",
1542 node, instance.disks[i].iv_name)
1544 # we ignore the ldisk parameter
1545 perc_done, est_time, is_degraded, _ = mstat
1546 cumul_degraded = cumul_degraded or (is_degraded and perc_done is None)
1547 if perc_done is not None:
1549 if est_time is not None:
1550 rem_time = "%d estimated seconds remaining" % est_time
1553 rem_time = "no time estimate"
1554 lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
1555 (instance.disks[i].iv_name, perc_done, rem_time))
1559 time.sleep(min(60, max_time))
1562 lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
1563 return not cumul_degraded
1566 def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
1567 """Check that mirrors are not degraded.
1569 The ldisk parameter, if True, will change the test from the
1570 is_degraded attribute (which represents overall non-ok status for
1571 the device(s)) to the ldisk (representing the local storage status).
1574 lu.cfg.SetDiskID(dev, node)
1581 if on_primary or dev.AssembleOnSecondary():
1582 rstats = lu.rpc.call_blockdev_find(node, dev)
1583 if rstats.failed or not rstats.data:
1584 logging.warning("Node %s: disk degraded, not found or node down", node)
1587 result = result and (not rstats.data[idx])
1589 for child in dev.children:
1590 result = result and _CheckDiskConsistency(lu, child, node, on_primary)
1595 class LUDiagnoseOS(NoHooksLU):
1596 """Logical unit for OS diagnose/query.
1599 _OP_REQP = ["output_fields", "names"]
1601 _FIELDS_STATIC = utils.FieldSet()
1602 _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status")
1604 def ExpandNames(self):
1606 raise errors.OpPrereqError("Selective OS query not supported")
1608 _CheckOutputFields(static=self._FIELDS_STATIC,
1609 dynamic=self._FIELDS_DYNAMIC,
1610 selected=self.op.output_fields)
1612 # Lock all nodes, in shared mode
1613 self.needed_locks = {}
1614 self.share_locks[locking.LEVEL_NODE] = 1
1615 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
1617 def CheckPrereq(self):
1618 """Check prerequisites.
1623 def _DiagnoseByOS(node_list, rlist):
1624 """Remaps a per-node return list into an a per-os per-node dictionary
1626 @param node_list: a list with the names of all nodes
1627 @param rlist: a map with node names as keys and OS objects as values
1630 @returns: a dictionary with osnames as keys and as value another map, with
1631 nodes as keys and list of OS objects as values, eg::
1633 {"debian-etch": {"node1": [<object>,...],
1634 "node2": [<object>,]}
1639 for node_name, nr in rlist.iteritems():
1640 if nr.failed or not nr.data:
1642 for os_obj in nr.data:
1643 if os_obj.name not in all_os:
1644 # build a list of nodes for this os containing empty lists
1645 # for each node in node_list
1646 all_os[os_obj.name] = {}
1647 for nname in node_list:
1648 all_os[os_obj.name][nname] = []
1649 all_os[os_obj.name][node_name].append(os_obj)
1652 def Exec(self, feedback_fn):
1653 """Compute the list of OSes.
1656 node_list = self.acquired_locks[locking.LEVEL_NODE]
1657 valid_nodes = [node for node in self.cfg.GetOnlineNodeList()
1658 if node in node_list]
1659 node_data = self.rpc.call_os_diagnose(valid_nodes)
1660 if node_data == False:
1661 raise errors.OpExecError("Can't gather the list of OSes")
1662 pol = self._DiagnoseByOS(valid_nodes, node_data)
1664 for os_name, os_data in pol.iteritems():
1666 for field in self.op.output_fields:
1669 elif field == "valid":
1670 val = utils.all([osl and osl[0] for osl in os_data.values()])
1671 elif field == "node_status":
1673 for node_name, nos_list in os_data.iteritems():
1674 val[node_name] = [(v.status, v.path) for v in nos_list]
1676 raise errors.ParameterError(field)
1683 class LURemoveNode(LogicalUnit):
1684 """Logical unit for removing a node.
1687 HPATH = "node-remove"
1688 HTYPE = constants.HTYPE_NODE
1689 _OP_REQP = ["node_name"]
1691 def BuildHooksEnv(self):
1694 This doesn't run on the target node in the pre phase as a failed
1695 node would then be impossible to remove.
1699 "OP_TARGET": self.op.node_name,
1700 "NODE_NAME": self.op.node_name,
1702 all_nodes = self.cfg.GetNodeList()
1703 all_nodes.remove(self.op.node_name)
1704 return env, all_nodes, all_nodes
1706 def CheckPrereq(self):
1707 """Check prerequisites.
1710 - the node exists in the configuration
1711 - it does not have primary or secondary instances
1712 - it's not the master
1714 Any errors are signalled by raising errors.OpPrereqError.
1717 node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name))
1719 raise errors.OpPrereqError, ("Node '%s' is unknown." % self.op.node_name)
1721 instance_list = self.cfg.GetInstanceList()
1723 masternode = self.cfg.GetMasterNode()
1724 if node.name == masternode:
1725 raise errors.OpPrereqError("Node is the master node,"
1726 " you need to failover first.")
1728 for instance_name in instance_list:
1729 instance = self.cfg.GetInstanceInfo(instance_name)
1730 if node.name in instance.all_nodes:
1731 raise errors.OpPrereqError("Instance %s is still running on the node,"
1732 " please remove first." % instance_name)
1733 self.op.node_name = node.name
1736 def Exec(self, feedback_fn):
1737 """Removes the node from the cluster.
1741 logging.info("Stopping the node daemon and removing configs from node %s",
1744 self.context.RemoveNode(node.name)
1746 self.rpc.call_node_leave_cluster(node.name)
1748 # Promote nodes to master candidate as needed
1749 _AdjustCandidatePool(self)
1752 class LUQueryNodes(NoHooksLU):
1753 """Logical unit for querying nodes.
1756 _OP_REQP = ["output_fields", "names", "use_locking"]
1758 _FIELDS_DYNAMIC = utils.FieldSet(
1760 "mtotal", "mnode", "mfree",
1765 _FIELDS_STATIC = utils.FieldSet(
1766 "name", "pinst_cnt", "sinst_cnt",
1767 "pinst_list", "sinst_list",
1768 "pip", "sip", "tags",
1775 def ExpandNames(self):
1776 _CheckOutputFields(static=self._FIELDS_STATIC,
1777 dynamic=self._FIELDS_DYNAMIC,
1778 selected=self.op.output_fields)
1780 self.needed_locks = {}
1781 self.share_locks[locking.LEVEL_NODE] = 1
1784 self.wanted = _GetWantedNodes(self, self.op.names)
1786 self.wanted = locking.ALL_SET
1788 self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
1789 self.do_locking = self.do_node_query and self.op.use_locking
1791 # if we don't request only static fields, we need to lock the nodes
1792 self.needed_locks[locking.LEVEL_NODE] = self.wanted
1795 def CheckPrereq(self):
1796 """Check prerequisites.
1799 # The validation of the node list is done in the _GetWantedNodes,
1800 # if non empty, and if empty, there's no validation to do
1803 def Exec(self, feedback_fn):
1804 """Computes the list of nodes and their attributes.
1807 all_info = self.cfg.GetAllNodesInfo()
1809 nodenames = self.acquired_locks[locking.LEVEL_NODE]
1810 elif self.wanted != locking.ALL_SET:
1811 nodenames = self.wanted
1812 missing = set(nodenames).difference(all_info.keys())
1814 raise errors.OpExecError(
1815 "Some nodes were removed before retrieving their data: %s" % missing)
1817 nodenames = all_info.keys()
1819 nodenames = utils.NiceSort(nodenames)
1820 nodelist = [all_info[name] for name in nodenames]
1822 # begin data gathering
1824 if self.do_node_query:
1826 node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
1827 self.cfg.GetHypervisorType())
1828 for name in nodenames:
1829 nodeinfo = node_data[name]
1830 if not nodeinfo.failed and nodeinfo.data:
1831 nodeinfo = nodeinfo.data
1832 fn = utils.TryConvert
1834 "mtotal": fn(int, nodeinfo.get('memory_total', None)),
1835 "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
1836 "mfree": fn(int, nodeinfo.get('memory_free', None)),
1837 "dtotal": fn(int, nodeinfo.get('vg_size', None)),
1838 "dfree": fn(int, nodeinfo.get('vg_free', None)),
1839 "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
1840 "bootid": nodeinfo.get('bootid', None),
1843 live_data[name] = {}
1845 live_data = dict.fromkeys(nodenames, {})
1847 node_to_primary = dict([(name, set()) for name in nodenames])
1848 node_to_secondary = dict([(name, set()) for name in nodenames])
1850 inst_fields = frozenset(("pinst_cnt", "pinst_list",
1851 "sinst_cnt", "sinst_list"))
1852 if inst_fields & frozenset(self.op.output_fields):
1853 instancelist = self.cfg.GetInstanceList()
1855 for instance_name in instancelist:
1856 inst = self.cfg.GetInstanceInfo(instance_name)
1857 if inst.primary_node in node_to_primary:
1858 node_to_primary[inst.primary_node].add(inst.name)
1859 for secnode in inst.secondary_nodes:
1860 if secnode in node_to_secondary:
1861 node_to_secondary[secnode].add(inst.name)
1863 master_node = self.cfg.GetMasterNode()
1865 # end data gathering
1868 for node in nodelist:
1870 for field in self.op.output_fields:
1873 elif field == "pinst_list":
1874 val = list(node_to_primary[node.name])
1875 elif field == "sinst_list":
1876 val = list(node_to_secondary[node.name])
1877 elif field == "pinst_cnt":
1878 val = len(node_to_primary[node.name])
1879 elif field == "sinst_cnt":
1880 val = len(node_to_secondary[node.name])
1881 elif field == "pip":
1882 val = node.primary_ip
1883 elif field == "sip":
1884 val = node.secondary_ip
1885 elif field == "tags":
1886 val = list(node.GetTags())
1887 elif field == "serial_no":
1888 val = node.serial_no
1889 elif field == "master_candidate":
1890 val = node.master_candidate
1891 elif field == "master":
1892 val = node.name == master_node
1893 elif field == "offline":
1895 elif self._FIELDS_DYNAMIC.Matches(field):
1896 val = live_data[node.name].get(field, None)
1898 raise errors.ParameterError(field)
1899 node_output.append(val)
1900 output.append(node_output)
1905 class LUQueryNodeVolumes(NoHooksLU):
1906 """Logical unit for getting volumes on node(s).
1909 _OP_REQP = ["nodes", "output_fields"]
1911 _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
1912 _FIELDS_STATIC = utils.FieldSet("node")
1914 def ExpandNames(self):
1915 _CheckOutputFields(static=self._FIELDS_STATIC,
1916 dynamic=self._FIELDS_DYNAMIC,
1917 selected=self.op.output_fields)
1919 self.needed_locks = {}
1920 self.share_locks[locking.LEVEL_NODE] = 1
1921 if not self.op.nodes:
1922 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
1924 self.needed_locks[locking.LEVEL_NODE] = \
1925 _GetWantedNodes(self, self.op.nodes)
1927 def CheckPrereq(self):
1928 """Check prerequisites.
1930 This checks that the fields required are valid output fields.
1933 self.nodes = self.acquired_locks[locking.LEVEL_NODE]
1935 def Exec(self, feedback_fn):
1936 """Computes the list of nodes and their attributes.
1939 nodenames = self.nodes
1940 volumes = self.rpc.call_node_volumes(nodenames)
1942 ilist = [self.cfg.GetInstanceInfo(iname) for iname
1943 in self.cfg.GetInstanceList()]
1945 lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
1948 for node in nodenames:
1949 if node not in volumes or volumes[node].failed or not volumes[node].data:
1952 node_vols = volumes[node].data[:]
1953 node_vols.sort(key=lambda vol: vol['dev'])
1955 for vol in node_vols:
1957 for field in self.op.output_fields:
1960 elif field == "phys":
1964 elif field == "name":
1966 elif field == "size":
1967 val = int(float(vol['size']))
1968 elif field == "instance":
1970 if node not in lv_by_node[inst]:
1972 if vol['name'] in lv_by_node[inst][node]:
1978 raise errors.ParameterError(field)
1979 node_output.append(str(val))
1981 output.append(node_output)
1986 class LUAddNode(LogicalUnit):
1987 """Logical unit for adding node to the cluster.
1991 HTYPE = constants.HTYPE_NODE
1992 _OP_REQP = ["node_name"]
1994 def BuildHooksEnv(self):
1997 This will run on all nodes before, and on all nodes + the new node after.
2001 "OP_TARGET": self.op.node_name,
2002 "NODE_NAME": self.op.node_name,
2003 "NODE_PIP": self.op.primary_ip,
2004 "NODE_SIP": self.op.secondary_ip,
2006 nodes_0 = self.cfg.GetNodeList()
2007 nodes_1 = nodes_0 + [self.op.node_name, ]
2008 return env, nodes_0, nodes_1
2010 def CheckPrereq(self):
2011 """Check prerequisites.
2014 - the new node is not already in the config
2016 - its parameters (single/dual homed) matches the cluster
2018 Any errors are signalled by raising errors.OpPrereqError.
2021 node_name = self.op.node_name
2024 dns_data = utils.HostInfo(node_name)
2026 node = dns_data.name
2027 primary_ip = self.op.primary_ip = dns_data.ip
2028 secondary_ip = getattr(self.op, "secondary_ip", None)
2029 if secondary_ip is None:
2030 secondary_ip = primary_ip
2031 if not utils.IsValidIP(secondary_ip):
2032 raise errors.OpPrereqError("Invalid secondary IP given")
2033 self.op.secondary_ip = secondary_ip
2035 node_list = cfg.GetNodeList()
2036 if not self.op.readd and node in node_list:
2037 raise errors.OpPrereqError("Node %s is already in the configuration" %
2039 elif self.op.readd and node not in node_list:
2040 raise errors.OpPrereqError("Node %s is not in the configuration" % node)
2042 for existing_node_name in node_list:
2043 existing_node = cfg.GetNodeInfo(existing_node_name)
2045 if self.op.readd and node == existing_node_name:
2046 if (existing_node.primary_ip != primary_ip or
2047 existing_node.secondary_ip != secondary_ip):
2048 raise errors.OpPrereqError("Readded node doesn't have the same IP"
2049 " address configuration as before")
2052 if (existing_node.primary_ip == primary_ip or
2053 existing_node.secondary_ip == primary_ip or
2054 existing_node.primary_ip == secondary_ip or
2055 existing_node.secondary_ip == secondary_ip):
2056 raise errors.OpPrereqError("New node ip address(es) conflict with"
2057 " existing node %s" % existing_node.name)
2059 # check that the type of the node (single versus dual homed) is the
2060 # same as for the master
2061 myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
2062 master_singlehomed = myself.secondary_ip == myself.primary_ip
2063 newbie_singlehomed = secondary_ip == primary_ip
2064 if master_singlehomed != newbie_singlehomed:
2065 if master_singlehomed:
2066 raise errors.OpPrereqError("The master has no private ip but the"
2067 " new node has one")
2069 raise errors.OpPrereqError("The master has a private ip but the"
2070 " new node doesn't have one")
2072 # checks reachablity
2073 if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
2074 raise errors.OpPrereqError("Node not reachable by ping")
2076 if not newbie_singlehomed:
2077 # check reachability from my secondary ip to newbie's secondary ip
2078 if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
2079 source=myself.secondary_ip):
2080 raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
2081 " based ping to noded port")
2083 cp_size = self.cfg.GetClusterInfo().candidate_pool_size
2084 mc_now, _ = self.cfg.GetMasterCandidateStats()
2085 master_candidate = mc_now < cp_size
2087 self.new_node = objects.Node(name=node,
2088 primary_ip=primary_ip,
2089 secondary_ip=secondary_ip,
2090 master_candidate=master_candidate,
2093 def Exec(self, feedback_fn):
2094 """Adds the new node to the cluster.
2097 new_node = self.new_node
2098 node = new_node.name
2100 # check connectivity
2101 result = self.rpc.call_version([node])[node]
2104 if constants.PROTOCOL_VERSION == result.data:
2105 logging.info("Communication to node %s fine, sw version %s match",
2108 raise errors.OpExecError("Version mismatch master version %s,"
2109 " node version %s" %
2110 (constants.PROTOCOL_VERSION, result.data))
2112 raise errors.OpExecError("Cannot get version from the new node")
2115 logging.info("Copy ssh key to node %s", node)
2116 priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
2118 keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
2119 constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
2125 keyarray.append(f.read())
2129 result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
2131 keyarray[3], keyarray[4], keyarray[5])
2133 msg = result.RemoteFailMsg()
2135 raise errors.OpExecError("Cannot transfer ssh keys to the"
2136 " new node: %s" % msg)
2138 # Add node to our /etc/hosts, and add key to known_hosts
2139 utils.AddHostToEtcHosts(new_node.name)
2141 if new_node.secondary_ip != new_node.primary_ip:
2142 result = self.rpc.call_node_has_ip_address(new_node.name,
2143 new_node.secondary_ip)
2144 if result.failed or not result.data:
2145 raise errors.OpExecError("Node claims it doesn't have the secondary ip"
2146 " you gave (%s). Please fix and re-run this"
2147 " command." % new_node.secondary_ip)
2149 node_verify_list = [self.cfg.GetMasterNode()]
2150 node_verify_param = {
2152 # TODO: do a node-net-test as well?
2155 result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
2156 self.cfg.GetClusterName())
2157 for verifier in node_verify_list:
2158 if result[verifier].failed or not result[verifier].data:
2159 raise errors.OpExecError("Cannot communicate with %s's node daemon"
2160 " for remote verification" % verifier)
2161 if result[verifier].data['nodelist']:
2162 for failed in result[verifier].data['nodelist']:
2163 feedback_fn("ssh/hostname verification failed %s -> %s" %
2164 (verifier, result[verifier].data['nodelist'][failed]))
2165 raise errors.OpExecError("ssh/hostname verification failed.")
2167 # Distribute updated /etc/hosts and known_hosts to all nodes,
2168 # including the node just added
2169 myself = self.cfg.GetNodeInfo(self.cfg.GetMasterNode())
2170 dist_nodes = self.cfg.GetNodeList()
2171 if not self.op.readd:
2172 dist_nodes.append(node)
2173 if myself.name in dist_nodes:
2174 dist_nodes.remove(myself.name)
2176 logging.debug("Copying hosts and known_hosts to all nodes")
2177 for fname in (constants.ETC_HOSTS, constants.SSH_KNOWN_HOSTS_FILE):
2178 result = self.rpc.call_upload_file(dist_nodes, fname)
2179 for to_node, to_result in result.iteritems():
2180 if to_result.failed or not to_result.data:
2181 logging.error("Copy of file %s to node %s failed", fname, to_node)
2184 enabled_hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
2185 if constants.HTS_USE_VNC.intersection(enabled_hypervisors):
2186 to_copy.append(constants.VNC_PASSWORD_FILE)
2188 for fname in to_copy:
2189 result = self.rpc.call_upload_file([node], fname)
2190 if result[node].failed or not result[node]:
2191 logging.error("Could not copy file %s to node %s", fname, node)
2194 self.context.ReaddNode(new_node)
2196 self.context.AddNode(new_node)
2199 class LUSetNodeParams(LogicalUnit):
2200 """Modifies the parameters of a node.
2203 HPATH = "node-modify"
2204 HTYPE = constants.HTYPE_NODE
2205 _OP_REQP = ["node_name"]
2208 def CheckArguments(self):
2209 node_name = self.cfg.ExpandNodeName(self.op.node_name)
2210 if node_name is None:
2211 raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name)
2212 self.op.node_name = node_name
2213 _CheckBooleanOpField(self.op, 'master_candidate')
2214 _CheckBooleanOpField(self.op, 'offline')
2215 if self.op.master_candidate is None and self.op.offline is None:
2216 raise errors.OpPrereqError("Please pass at least one modification")
2217 if self.op.offline == True and self.op.master_candidate == True:
2218 raise errors.OpPrereqError("Can't set the node into offline and"
2219 " master_candidate at the same time")
2221 def ExpandNames(self):
2222 self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
2224 def BuildHooksEnv(self):
2227 This runs on the master node.
2231 "OP_TARGET": self.op.node_name,
2232 "MASTER_CANDIDATE": str(self.op.master_candidate),
2233 "OFFLINE": str(self.op.offline),
2235 nl = [self.cfg.GetMasterNode(),
2239 def CheckPrereq(self):
2240 """Check prerequisites.
2242 This only checks the instance list against the existing names.
2245 node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
2247 if ((self.op.master_candidate == False or self.op.offline == True)
2248 and node.master_candidate):
2249 # we will demote the node from master_candidate
2250 if self.op.node_name == self.cfg.GetMasterNode():
2251 raise errors.OpPrereqError("The master node has to be a"
2252 " master candidate and online")
2253 cp_size = self.cfg.GetClusterInfo().candidate_pool_size
2254 num_candidates, _ = self.cfg.GetMasterCandidateStats()
2255 if num_candidates <= cp_size:
2256 msg = ("Not enough master candidates (desired"
2257 " %d, new value will be %d)" % (cp_size, num_candidates-1))
2259 self.LogWarning(msg)
2261 raise errors.OpPrereqError(msg)
2263 if (self.op.master_candidate == True and node.offline and
2264 not self.op.offline == False):
2265 raise errors.OpPrereqError("Can't set an offline node to"
2266 " master_candidate")
2270 def Exec(self, feedback_fn):
2278 if self.op.offline is not None:
2279 node.offline = self.op.offline
2280 result.append(("offline", str(self.op.offline)))
2281 if self.op.offline == True and node.master_candidate:
2282 node.master_candidate = False
2283 result.append(("master_candidate", "auto-demotion due to offline"))
2285 if self.op.master_candidate is not None:
2286 node.master_candidate = self.op.master_candidate
2287 result.append(("master_candidate", str(self.op.master_candidate)))
2288 if self.op.master_candidate == False:
2289 rrc = self.rpc.call_node_demote_from_mc(node.name)
2290 if (rrc.failed or not isinstance(rrc.data, (tuple, list))
2291 or len(rrc.data) != 2):
2292 self.LogWarning("Node rpc error: %s" % rrc.error)
2293 elif not rrc.data[0]:
2294 self.LogWarning("Node failed to demote itself: %s" % rrc.data[1])
2296 # this will trigger configuration file update, if needed
2297 self.cfg.Update(node)
2298 # this will trigger job queue propagation or cleanup
2299 if self.op.node_name != self.cfg.GetMasterNode():
2300 self.context.ReaddNode(node)
2305 class LUQueryClusterInfo(NoHooksLU):
2306 """Query cluster configuration.
2312 def ExpandNames(self):
2313 self.needed_locks = {}
2315 def CheckPrereq(self):
2316 """No prerequsites needed for this LU.
2321 def Exec(self, feedback_fn):
2322 """Return cluster config.
2325 cluster = self.cfg.GetClusterInfo()
2327 "software_version": constants.RELEASE_VERSION,
2328 "protocol_version": constants.PROTOCOL_VERSION,
2329 "config_version": constants.CONFIG_VERSION,
2330 "os_api_version": constants.OS_API_VERSION,
2331 "export_version": constants.EXPORT_VERSION,
2332 "architecture": (platform.architecture()[0], platform.machine()),
2333 "name": cluster.cluster_name,
2334 "master": cluster.master_node,
2335 "default_hypervisor": cluster.default_hypervisor,
2336 "enabled_hypervisors": cluster.enabled_hypervisors,
2337 "hvparams": dict([(hypervisor, cluster.hvparams[hypervisor])
2338 for hypervisor in cluster.enabled_hypervisors]),
2339 "beparams": cluster.beparams,
2340 "candidate_pool_size": cluster.candidate_pool_size,
2346 class LUQueryConfigValues(NoHooksLU):
2347 """Return configuration values.
2352 _FIELDS_DYNAMIC = utils.FieldSet()
2353 _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag")
2355 def ExpandNames(self):
2356 self.needed_locks = {}
2358 _CheckOutputFields(static=self._FIELDS_STATIC,
2359 dynamic=self._FIELDS_DYNAMIC,
2360 selected=self.op.output_fields)
2362 def CheckPrereq(self):
2363 """No prerequisites.
2368 def Exec(self, feedback_fn):
2369 """Dump a representation of the cluster config to the standard output.
2373 for field in self.op.output_fields:
2374 if field == "cluster_name":
2375 entry = self.cfg.GetClusterName()
2376 elif field == "master_node":
2377 entry = self.cfg.GetMasterNode()
2378 elif field == "drain_flag":
2379 entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
2381 raise errors.ParameterError(field)
2382 values.append(entry)
2386 class LUActivateInstanceDisks(NoHooksLU):
2387 """Bring up an instance's disks.
2390 _OP_REQP = ["instance_name"]
2393 def ExpandNames(self):
2394 self._ExpandAndLockInstance()
2395 self.needed_locks[locking.LEVEL_NODE] = []
2396 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
2398 def DeclareLocks(self, level):
2399 if level == locking.LEVEL_NODE:
2400 self._LockInstancesNodes()
2402 def CheckPrereq(self):
2403 """Check prerequisites.
2405 This checks that the instance is in the cluster.
2408 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
2409 assert self.instance is not None, \
2410 "Cannot retrieve locked instance %s" % self.op.instance_name
2411 _CheckNodeOnline(self, self.instance.primary_node)
2413 def Exec(self, feedback_fn):
2414 """Activate the disks.
2417 disks_ok, disks_info = _AssembleInstanceDisks(self, self.instance)
2419 raise errors.OpExecError("Cannot activate block devices")
2424 def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False):
2425 """Prepare the block devices for an instance.
2427 This sets up the block devices on all nodes.
2429 @type lu: L{LogicalUnit}
2430 @param lu: the logical unit on whose behalf we execute
2431 @type instance: L{objects.Instance}
2432 @param instance: the instance for whose disks we assemble
2433 @type ignore_secondaries: boolean
2434 @param ignore_secondaries: if true, errors on secondary nodes
2435 won't result in an error return from the function
2436 @return: False if the operation failed, otherwise a list of
2437 (host, instance_visible_name, node_visible_name)
2438 with the mapping from node devices to instance devices
2443 iname = instance.name
2444 # With the two passes mechanism we try to reduce the window of
2445 # opportunity for the race condition of switching DRBD to primary
2446 # before handshaking occured, but we do not eliminate it
2448 # The proper fix would be to wait (with some limits) until the
2449 # connection has been made and drbd transitions from WFConnection
2450 # into any other network-connected state (Connected, SyncTarget,
2453 # 1st pass, assemble on all nodes in secondary mode
2454 for inst_disk in instance.disks:
2455 for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
2456 lu.cfg.SetDiskID(node_disk, node)
2457 result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
2458 if result.failed or not result:
2459 lu.proc.LogWarning("Could not prepare block device %s on node %s"
2460 " (is_primary=False, pass=1)",
2461 inst_disk.iv_name, node)
2462 if not ignore_secondaries:
2465 # FIXME: race condition on drbd migration to primary
2467 # 2nd pass, do only the primary node
2468 for inst_disk in instance.disks:
2469 for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
2470 if node != instance.primary_node:
2472 lu.cfg.SetDiskID(node_disk, node)
2473 result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
2474 if result.failed or not result:
2475 lu.proc.LogWarning("Could not prepare block device %s on node %s"
2476 " (is_primary=True, pass=2)",
2477 inst_disk.iv_name, node)
2479 device_info.append((instance.primary_node, inst_disk.iv_name, result.data))
2481 # leave the disks configured for the primary node
2482 # this is a workaround that would be fixed better by
2483 # improving the logical/physical id handling
2484 for disk in instance.disks:
2485 lu.cfg.SetDiskID(disk, instance.primary_node)
2487 return disks_ok, device_info
2490 def _StartInstanceDisks(lu, instance, force):
2491 """Start the disks of an instance.
2494 disks_ok, dummy = _AssembleInstanceDisks(lu, instance,
2495 ignore_secondaries=force)
2497 _ShutdownInstanceDisks(lu, instance)
2498 if force is not None and not force:
2499 lu.proc.LogWarning("", hint="If the message above refers to a"
2501 " you can retry the operation using '--force'.")
2502 raise errors.OpExecError("Disk consistency error")
2505 class LUDeactivateInstanceDisks(NoHooksLU):
2506 """Shutdown an instance's disks.
2509 _OP_REQP = ["instance_name"]
2512 def ExpandNames(self):
2513 self._ExpandAndLockInstance()
2514 self.needed_locks[locking.LEVEL_NODE] = []
2515 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
2517 def DeclareLocks(self, level):
2518 if level == locking.LEVEL_NODE:
2519 self._LockInstancesNodes()
2521 def CheckPrereq(self):
2522 """Check prerequisites.
2524 This checks that the instance is in the cluster.
2527 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
2528 assert self.instance is not None, \
2529 "Cannot retrieve locked instance %s" % self.op.instance_name
2531 def Exec(self, feedback_fn):
2532 """Deactivate the disks
2535 instance = self.instance
2536 _SafeShutdownInstanceDisks(self, instance)
2539 def _SafeShutdownInstanceDisks(lu, instance):
2540 """Shutdown block devices of an instance.
2542 This function checks if an instance is running, before calling
2543 _ShutdownInstanceDisks.
2546 ins_l = lu.rpc.call_instance_list([instance.primary_node],
2547 [instance.hypervisor])
2548 ins_l = ins_l[instance.primary_node]
2549 if ins_l.failed or not isinstance(ins_l.data, list):
2550 raise errors.OpExecError("Can't contact node '%s'" %
2551 instance.primary_node)
2553 if instance.name in ins_l.data:
2554 raise errors.OpExecError("Instance is running, can't shutdown"
2557 _ShutdownInstanceDisks(lu, instance)
2560 def _ShutdownInstanceDisks(lu, instance, ignore_primary=False):
2561 """Shutdown block devices of an instance.
2563 This does the shutdown on all nodes of the instance.
2565 If the ignore_primary is false, errors on the primary node are
2570 for disk in instance.disks:
2571 for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
2572 lu.cfg.SetDiskID(top_disk, node)
2573 result = lu.rpc.call_blockdev_shutdown(node, top_disk)
2574 if result.failed or not result.data:
2575 logging.error("Could not shutdown block device %s on node %s",
2577 if not ignore_primary or node != instance.primary_node:
2582 def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
2583 """Checks if a node has enough free memory.
2585 This function check if a given node has the needed amount of free
2586 memory. In case the node has less memory or we cannot get the
2587 information from the node, this function raise an OpPrereqError
2590 @type lu: C{LogicalUnit}
2591 @param lu: a logical unit from which we get configuration data
2593 @param node: the node to check
2594 @type reason: C{str}
2595 @param reason: string to use in the error message
2596 @type requested: C{int}
2597 @param requested: the amount of memory in MiB to check for
2598 @type hypervisor_name: C{str}
2599 @param hypervisor_name: the hypervisor to ask for memory stats
2600 @raise errors.OpPrereqError: if the node doesn't have enough memory, or
2601 we cannot check the node
2604 nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name)
2605 nodeinfo[node].Raise()
2606 free_mem = nodeinfo[node].data.get('memory_free')
2607 if not isinstance(free_mem, int):
2608 raise errors.OpPrereqError("Can't compute free memory on node %s, result"
2609 " was '%s'" % (node, free_mem))
2610 if requested > free_mem:
2611 raise errors.OpPrereqError("Not enough memory on node %s for %s:"
2612 " needed %s MiB, available %s MiB" %
2613 (node, reason, requested, free_mem))
2616 class LUStartupInstance(LogicalUnit):
2617 """Starts an instance.
2620 HPATH = "instance-start"
2621 HTYPE = constants.HTYPE_INSTANCE
2622 _OP_REQP = ["instance_name", "force"]
2625 def ExpandNames(self):
2626 self._ExpandAndLockInstance()
2628 def BuildHooksEnv(self):
2631 This runs on master, primary and secondary nodes of the instance.
2635 "FORCE": self.op.force,
2637 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
2638 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
2641 def CheckPrereq(self):
2642 """Check prerequisites.
2644 This checks that the instance is in the cluster.
2647 self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
2648 assert self.instance is not None, \
2649 "Cannot retrieve locked instance %s" % self.op.instance_name
2651 _CheckNodeOnline(self, instance.primary_node)
2653 bep = self.cfg.GetClusterInfo().FillBE(instance)
2654 # check bridges existance
2655 _CheckInstanceBridgesExist(self, instance)
2657 _CheckNodeFreeMemory(self, instance.primary_node,
2658 "starting instance %s" % instance.name,
2659 bep[constants.BE_MEMORY], instance.hypervisor)
2661 def Exec(self, feedback_fn):
2662 """Start the instance.
2665 instance = self.instance
2666 force = self.op.force
2667 extra_args = getattr(self.op, "extra_args", "")
2669 self.cfg.MarkInstanceUp(instance.name)
2671 node_current = instance.primary_node
2673 _StartInstanceDisks(self, instance, force)
2675 result = self.rpc.call_instance_start(node_current, instance, extra_args)
2676 msg = result.RemoteFailMsg()
2678 _ShutdownInstanceDisks(self, instance)
2679 raise errors.OpExecError("Could not start instance: %s" % msg)
2682 class LURebootInstance(LogicalUnit):
2683 """Reboot an instance.
2686 HPATH = "instance-reboot"
2687 HTYPE = constants.HTYPE_INSTANCE
2688 _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"]
2691 def ExpandNames(self):
2692 if self.op.reboot_type not in [constants.INSTANCE_REBOOT_SOFT,
2693 constants.INSTANCE_REBOOT_HARD,
2694 constants.INSTANCE_REBOOT_FULL]:
2695 raise errors.ParameterError("reboot type not in [%s, %s, %s]" %
2696 (constants.INSTANCE_REBOOT_SOFT,
2697 constants.INSTANCE_REBOOT_HARD,
2698 constants.INSTANCE_REBOOT_FULL))
2699 self._ExpandAndLockInstance()
2701 def BuildHooksEnv(self):
2704 This runs on master, primary and secondary nodes of the instance.
2708 "IGNORE_SECONDARIES": self.op.ignore_secondaries,
2710 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
2711 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
2714 def CheckPrereq(self):
2715 """Check prerequisites.
2717 This checks that the instance is in the cluster.
2720 self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
2721 assert self.instance is not None, \
2722 "Cannot retrieve locked instance %s" % self.op.instance_name
2724 _CheckNodeOnline(self, instance.primary_node)
2726 # check bridges existance
2727 _CheckInstanceBridgesExist(self, instance)
2729 def Exec(self, feedback_fn):
2730 """Reboot the instance.
2733 instance = self.instance
2734 ignore_secondaries = self.op.ignore_secondaries
2735 reboot_type = self.op.reboot_type
2736 extra_args = getattr(self.op, "extra_args", "")
2738 node_current = instance.primary_node
2740 if reboot_type in [constants.INSTANCE_REBOOT_SOFT,
2741 constants.INSTANCE_REBOOT_HARD]:
2742 result = self.rpc.call_instance_reboot(node_current, instance,
2743 reboot_type, extra_args)
2744 if result.failed or not result.data:
2745 raise errors.OpExecError("Could not reboot instance")
2747 if not self.rpc.call_instance_shutdown(node_current, instance):
2748 raise errors.OpExecError("could not shutdown instance for full reboot")
2749 _ShutdownInstanceDisks(self, instance)
2750 _StartInstanceDisks(self, instance, ignore_secondaries)
2751 result = self.rpc.call_instance_start(node_current, instance, extra_args)
2752 msg = result.RemoteFailMsg()
2754 _ShutdownInstanceDisks(self, instance)
2755 raise errors.OpExecError("Could not start instance for"
2756 " full reboot: %s" % msg)
2758 self.cfg.MarkInstanceUp(instance.name)
2761 class LUShutdownInstance(LogicalUnit):
2762 """Shutdown an instance.
2765 HPATH = "instance-stop"
2766 HTYPE = constants.HTYPE_INSTANCE
2767 _OP_REQP = ["instance_name"]
2770 def ExpandNames(self):
2771 self._ExpandAndLockInstance()
2773 def BuildHooksEnv(self):
2776 This runs on master, primary and secondary nodes of the instance.
2779 env = _BuildInstanceHookEnvByObject(self, self.instance)
2780 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
2783 def CheckPrereq(self):
2784 """Check prerequisites.
2786 This checks that the instance is in the cluster.
2789 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
2790 assert self.instance is not None, \
2791 "Cannot retrieve locked instance %s" % self.op.instance_name
2792 _CheckNodeOnline(self, self.instance.primary_node)
2794 def Exec(self, feedback_fn):
2795 """Shutdown the instance.
2798 instance = self.instance
2799 node_current = instance.primary_node
2800 self.cfg.MarkInstanceDown(instance.name)
2801 result = self.rpc.call_instance_shutdown(node_current, instance)
2802 if result.failed or not result.data:
2803 self.proc.LogWarning("Could not shutdown instance")
2805 _ShutdownInstanceDisks(self, instance)
2808 class LUReinstallInstance(LogicalUnit):
2809 """Reinstall an instance.
2812 HPATH = "instance-reinstall"
2813 HTYPE = constants.HTYPE_INSTANCE
2814 _OP_REQP = ["instance_name"]
2817 def ExpandNames(self):
2818 self._ExpandAndLockInstance()
2820 def BuildHooksEnv(self):
2823 This runs on master, primary and secondary nodes of the instance.
2826 env = _BuildInstanceHookEnvByObject(self, self.instance)
2827 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
2830 def CheckPrereq(self):
2831 """Check prerequisites.
2833 This checks that the instance is in the cluster and is not running.
2836 instance = self.cfg.GetInstanceInfo(self.op.instance_name)
2837 assert instance is not None, \
2838 "Cannot retrieve locked instance %s" % self.op.instance_name
2839 _CheckNodeOnline(self, instance.primary_node)
2841 if instance.disk_template == constants.DT_DISKLESS:
2842 raise errors.OpPrereqError("Instance '%s' has no disks" %
2843 self.op.instance_name)
2844 if instance.admin_up:
2845 raise errors.OpPrereqError("Instance '%s' is marked to be up" %
2846 self.op.instance_name)
2847 remote_info = self.rpc.call_instance_info(instance.primary_node,
2849 instance.hypervisor)
2850 if remote_info.failed or remote_info.data:
2851 raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
2852 (self.op.instance_name,
2853 instance.primary_node))
2855 self.op.os_type = getattr(self.op, "os_type", None)
2856 if self.op.os_type is not None:
2858 pnode = self.cfg.GetNodeInfo(
2859 self.cfg.ExpandNodeName(instance.primary_node))
2861 raise errors.OpPrereqError("Primary node '%s' is unknown" %
2863 result = self.rpc.call_os_get(pnode.name, self.op.os_type)
2865 if not isinstance(result.data, objects.OS):
2866 raise errors.OpPrereqError("OS '%s' not in supported OS list for"
2867 " primary node" % self.op.os_type)
2869 self.instance = instance
2871 def Exec(self, feedback_fn):
2872 """Reinstall the instance.
2875 inst = self.instance
2877 if self.op.os_type is not None:
2878 feedback_fn("Changing OS to '%s'..." % self.op.os_type)
2879 inst.os = self.op.os_type
2880 self.cfg.Update(inst)
2882 _StartInstanceDisks(self, inst, None)
2884 feedback_fn("Running the instance OS create scripts...")
2885 result = self.rpc.call_instance_os_add(inst.primary_node, inst)
2886 msg = result.RemoteFailMsg()
2888 raise errors.OpExecError("Could not install OS for instance %s"
2890 (inst.name, inst.primary_node, msg))
2892 _ShutdownInstanceDisks(self, inst)
2895 class LURenameInstance(LogicalUnit):
2896 """Rename an instance.
2899 HPATH = "instance-rename"
2900 HTYPE = constants.HTYPE_INSTANCE
2901 _OP_REQP = ["instance_name", "new_name"]
2903 def BuildHooksEnv(self):
2906 This runs on master, primary and secondary nodes of the instance.
2909 env = _BuildInstanceHookEnvByObject(self, self.instance)
2910 env["INSTANCE_NEW_NAME"] = self.op.new_name
2911 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
2914 def CheckPrereq(self):
2915 """Check prerequisites.
2917 This checks that the instance is in the cluster and is not running.
2920 instance = self.cfg.GetInstanceInfo(
2921 self.cfg.ExpandInstanceName(self.op.instance_name))
2922 if instance is None:
2923 raise errors.OpPrereqError("Instance '%s' not known" %
2924 self.op.instance_name)
2925 _CheckNodeOnline(self, instance.primary_node)
2927 if instance.admin_up:
2928 raise errors.OpPrereqError("Instance '%s' is marked to be up" %
2929 self.op.instance_name)
2930 remote_info = self.rpc.call_instance_info(instance.primary_node,
2932 instance.hypervisor)
2934 if remote_info.data:
2935 raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
2936 (self.op.instance_name,
2937 instance.primary_node))
2938 self.instance = instance
2940 # new name verification
2941 name_info = utils.HostInfo(self.op.new_name)
2943 self.op.new_name = new_name = name_info.name
2944 instance_list = self.cfg.GetInstanceList()
2945 if new_name in instance_list:
2946 raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
2949 if not getattr(self.op, "ignore_ip", False):
2950 if utils.TcpPing(name_info.ip, constants.DEFAULT_NODED_PORT):
2951 raise errors.OpPrereqError("IP %s of instance %s already in use" %
2952 (name_info.ip, new_name))
2955 def Exec(self, feedback_fn):
2956 """Reinstall the instance.
2959 inst = self.instance
2960 old_name = inst.name
2962 if inst.disk_template == constants.DT_FILE:
2963 old_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
2965 self.cfg.RenameInstance(inst.name, self.op.new_name)
2966 # Change the instance lock. This is definitely safe while we hold the BGL
2967 self.context.glm.remove(locking.LEVEL_INSTANCE, old_name)
2968 self.context.glm.add(locking.LEVEL_INSTANCE, self.op.new_name)
2970 # re-read the instance from the configuration after rename
2971 inst = self.cfg.GetInstanceInfo(self.op.new_name)
2973 if inst.disk_template == constants.DT_FILE:
2974 new_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
2975 result = self.rpc.call_file_storage_dir_rename(inst.primary_node,
2976 old_file_storage_dir,
2977 new_file_storage_dir)
2980 raise errors.OpExecError("Could not connect to node '%s' to rename"
2981 " directory '%s' to '%s' (but the instance"
2982 " has been renamed in Ganeti)" % (
2983 inst.primary_node, old_file_storage_dir,
2984 new_file_storage_dir))
2986 if not result.data[0]:
2987 raise errors.OpExecError("Could not rename directory '%s' to '%s'"
2988 " (but the instance has been renamed in"
2989 " Ganeti)" % (old_file_storage_dir,
2990 new_file_storage_dir))
2992 _StartInstanceDisks(self, inst, None)
2994 result = self.rpc.call_instance_run_rename(inst.primary_node, inst,
2996 msg = result.RemoteFailMsg()
2998 msg = ("Could not run OS rename script for instance %s on node %s"
2999 " (but the instance has been renamed in Ganeti): %s" %
3000 (inst.name, inst.primary_node, msg))
3001 self.proc.LogWarning(msg)
3003 _ShutdownInstanceDisks(self, inst)
3006 class LURemoveInstance(LogicalUnit):
3007 """Remove an instance.
3010 HPATH = "instance-remove"
3011 HTYPE = constants.HTYPE_INSTANCE
3012 _OP_REQP = ["instance_name", "ignore_failures"]
3015 def ExpandNames(self):
3016 self._ExpandAndLockInstance()
3017 self.needed_locks[locking.LEVEL_NODE] = []
3018 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3020 def DeclareLocks(self, level):
3021 if level == locking.LEVEL_NODE:
3022 self._LockInstancesNodes()
3024 def BuildHooksEnv(self):
3027 This runs on master, primary and secondary nodes of the instance.
3030 env = _BuildInstanceHookEnvByObject(self, self.instance)
3031 nl = [self.cfg.GetMasterNode()]
3034 def CheckPrereq(self):
3035 """Check prerequisites.
3037 This checks that the instance is in the cluster.
3040 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3041 assert self.instance is not None, \
3042 "Cannot retrieve locked instance %s" % self.op.instance_name
3044 def Exec(self, feedback_fn):
3045 """Remove the instance.
3048 instance = self.instance
3049 logging.info("Shutting down instance %s on node %s",
3050 instance.name, instance.primary_node)
3052 result = self.rpc.call_instance_shutdown(instance.primary_node, instance)
3053 if result.failed or not result.data:
3054 if self.op.ignore_failures:
3055 feedback_fn("Warning: can't shutdown instance")
3057 raise errors.OpExecError("Could not shutdown instance %s on node %s" %
3058 (instance.name, instance.primary_node))
3060 logging.info("Removing block devices for instance %s", instance.name)
3062 if not _RemoveDisks(self, instance):
3063 if self.op.ignore_failures:
3064 feedback_fn("Warning: can't remove instance's disks")
3066 raise errors.OpExecError("Can't remove instance's disks")
3068 logging.info("Removing instance %s out of cluster config", instance.name)
3070 self.cfg.RemoveInstance(instance.name)
3071 self.remove_locks[locking.LEVEL_INSTANCE] = instance.name
3074 class LUQueryInstances(NoHooksLU):
3075 """Logical unit for querying instances.
3078 _OP_REQP = ["output_fields", "names", "use_locking"]
3080 _FIELDS_STATIC = utils.FieldSet(*["name", "os", "pnode", "snodes",
3081 "admin_state", "admin_ram",
3082 "disk_template", "ip", "mac", "bridge",
3083 "sda_size", "sdb_size", "vcpus", "tags",
3084 "network_port", "beparams",
3085 "(disk).(size)/([0-9]+)",
3087 "(nic).(mac|ip|bridge)/([0-9]+)",
3088 "(nic).(macs|ips|bridges)",
3089 "(disk|nic).(count)",
3090 "serial_no", "hypervisor", "hvparams",] +
3092 for name in constants.HVS_PARAMETERS] +
3094 for name in constants.BES_PARAMETERS])
3095 _FIELDS_DYNAMIC = utils.FieldSet("oper_state", "oper_ram", "status")
3098 def ExpandNames(self):
3099 _CheckOutputFields(static=self._FIELDS_STATIC,
3100 dynamic=self._FIELDS_DYNAMIC,
3101 selected=self.op.output_fields)
3103 self.needed_locks = {}
3104 self.share_locks[locking.LEVEL_INSTANCE] = 1
3105 self.share_locks[locking.LEVEL_NODE] = 1
3108 self.wanted = _GetWantedInstances(self, self.op.names)
3110 self.wanted = locking.ALL_SET
3112 self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
3113 self.do_locking = self.do_node_query and self.op.use_locking
3115 self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted
3116 self.needed_locks[locking.LEVEL_NODE] = []
3117 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3119 def DeclareLocks(self, level):
3120 if level == locking.LEVEL_NODE and self.do_locking:
3121 self._LockInstancesNodes()
3123 def CheckPrereq(self):
3124 """Check prerequisites.
3129 def Exec(self, feedback_fn):
3130 """Computes the list of nodes and their attributes.
3133 all_info = self.cfg.GetAllInstancesInfo()
3134 if self.wanted == locking.ALL_SET:
3135 # caller didn't specify instance names, so ordering is not important
3137 instance_names = self.acquired_locks[locking.LEVEL_INSTANCE]
3139 instance_names = all_info.keys()
3140 instance_names = utils.NiceSort(instance_names)
3142 # caller did specify names, so we must keep the ordering
3144 tgt_set = self.acquired_locks[locking.LEVEL_INSTANCE]
3146 tgt_set = all_info.keys()
3147 missing = set(self.wanted).difference(tgt_set)
3149 raise errors.OpExecError("Some instances were removed before"
3150 " retrieving their data: %s" % missing)
3151 instance_names = self.wanted
3153 instance_list = [all_info[iname] for iname in instance_names]
3155 # begin data gathering
3157 nodes = frozenset([inst.primary_node for inst in instance_list])
3158 hv_list = list(set([inst.hypervisor for inst in instance_list]))
3162 if self.do_node_query:
3164 node_data = self.rpc.call_all_instances_info(nodes, hv_list)
3166 result = node_data[name]
3168 # offline nodes will be in both lists
3169 off_nodes.append(name)
3171 bad_nodes.append(name)
3174 live_data.update(result.data)
3175 # else no instance is alive
3177 live_data = dict([(name, {}) for name in instance_names])
3179 # end data gathering
3184 for instance in instance_list:
3186 i_hv = self.cfg.GetClusterInfo().FillHV(instance)
3187 i_be = self.cfg.GetClusterInfo().FillBE(instance)
3188 for field in self.op.output_fields:
3189 st_match = self._FIELDS_STATIC.Matches(field)
3194 elif field == "pnode":
3195 val = instance.primary_node
3196 elif field == "snodes":
3197 val = list(instance.secondary_nodes)
3198 elif field == "admin_state":
3199 val = instance.admin_up
3200 elif field == "oper_state":
3201 if instance.primary_node in bad_nodes:
3204 val = bool(live_data.get(instance.name))
3205 elif field == "status":
3206 if instance.primary_node in off_nodes:
3207 val = "ERROR_nodeoffline"
3208 elif instance.primary_node in bad_nodes:
3209 val = "ERROR_nodedown"
3211 running = bool(live_data.get(instance.name))
3213 if instance.admin_up:
3218 if instance.admin_up:
3222 elif field == "oper_ram":
3223 if instance.primary_node in bad_nodes:
3225 elif instance.name in live_data:
3226 val = live_data[instance.name].get("memory", "?")
3229 elif field == "disk_template":
3230 val = instance.disk_template
3232 val = instance.nics[0].ip
3233 elif field == "bridge":
3234 val = instance.nics[0].bridge
3235 elif field == "mac":
3236 val = instance.nics[0].mac
3237 elif field == "sda_size" or field == "sdb_size":
3238 idx = ord(field[2]) - ord('a')
3240 val = instance.FindDisk(idx).size
3241 except errors.OpPrereqError:
3243 elif field == "tags":
3244 val = list(instance.GetTags())
3245 elif field == "serial_no":
3246 val = instance.serial_no
3247 elif field == "network_port":
3248 val = instance.network_port
3249 elif field == "hypervisor":
3250 val = instance.hypervisor
3251 elif field == "hvparams":
3253 elif (field.startswith(HVPREFIX) and
3254 field[len(HVPREFIX):] in constants.HVS_PARAMETERS):
3255 val = i_hv.get(field[len(HVPREFIX):], None)
3256 elif field == "beparams":
3258 elif (field.startswith(BEPREFIX) and
3259 field[len(BEPREFIX):] in constants.BES_PARAMETERS):
3260 val = i_be.get(field[len(BEPREFIX):], None)
3261 elif st_match and st_match.groups():
3262 # matches a variable list
3263 st_groups = st_match.groups()
3264 if st_groups and st_groups[0] == "disk":
3265 if st_groups[1] == "count":
3266 val = len(instance.disks)
3267 elif st_groups[1] == "sizes":
3268 val = [disk.size for disk in instance.disks]
3269 elif st_groups[1] == "size":
3271 val = instance.FindDisk(st_groups[2]).size
3272 except errors.OpPrereqError:
3275 assert False, "Unhandled disk parameter"
3276 elif st_groups[0] == "nic":
3277 if st_groups[1] == "count":
3278 val = len(instance.nics)
3279 elif st_groups[1] == "macs":
3280 val = [nic.mac for nic in instance.nics]
3281 elif st_groups[1] == "ips":
3282 val = [nic.ip for nic in instance.nics]
3283 elif st_groups[1] == "bridges":
3284 val = [nic.bridge for nic in instance.nics]
3287 nic_idx = int(st_groups[2])
3288 if nic_idx >= len(instance.nics):
3291 if st_groups[1] == "mac":
3292 val = instance.nics[nic_idx].mac
3293 elif st_groups[1] == "ip":
3294 val = instance.nics[nic_idx].ip
3295 elif st_groups[1] == "bridge":
3296 val = instance.nics[nic_idx].bridge
3298 assert False, "Unhandled NIC parameter"
3300 assert False, "Unhandled variable parameter"
3302 raise errors.ParameterError(field)
3309 class LUFailoverInstance(LogicalUnit):
3310 """Failover an instance.
3313 HPATH = "instance-failover"
3314 HTYPE = constants.HTYPE_INSTANCE
3315 _OP_REQP = ["instance_name", "ignore_consistency"]
3318 def ExpandNames(self):
3319 self._ExpandAndLockInstance()
3320 self.needed_locks[locking.LEVEL_NODE] = []
3321 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3323 def DeclareLocks(self, level):
3324 if level == locking.LEVEL_NODE:
3325 self._LockInstancesNodes()
3327 def BuildHooksEnv(self):
3330 This runs on master, primary and secondary nodes of the instance.
3334 "IGNORE_CONSISTENCY": self.op.ignore_consistency,
3336 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3337 nl = [self.cfg.GetMasterNode()] + list(self.instance.secondary_nodes)
3340 def CheckPrereq(self):
3341 """Check prerequisites.
3343 This checks that the instance is in the cluster.
3346 self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3347 assert self.instance is not None, \
3348 "Cannot retrieve locked instance %s" % self.op.instance_name
3350 bep = self.cfg.GetClusterInfo().FillBE(instance)
3351 if instance.disk_template not in constants.DTS_NET_MIRROR:
3352 raise errors.OpPrereqError("Instance's disk layout is not"
3353 " network mirrored, cannot failover.")
3355 secondary_nodes = instance.secondary_nodes
3356 if not secondary_nodes:
3357 raise errors.ProgrammerError("no secondary node but using "
3358 "a mirrored disk template")
3360 target_node = secondary_nodes[0]
3361 _CheckNodeOnline(self, target_node)
3362 # check memory requirements on the secondary node
3363 _CheckNodeFreeMemory(self, target_node, "failing over instance %s" %
3364 instance.name, bep[constants.BE_MEMORY],
3365 instance.hypervisor)
3367 # check bridge existance
3368 brlist = [nic.bridge for nic in instance.nics]
3369 result = self.rpc.call_bridges_exist(target_node, brlist)
3372 raise errors.OpPrereqError("One or more target bridges %s does not"
3373 " exist on destination node '%s'" %
3374 (brlist, target_node))
3376 def Exec(self, feedback_fn):
3377 """Failover an instance.
3379 The failover is done by shutting it down on its present node and
3380 starting it on the secondary.
3383 instance = self.instance
3385 source_node = instance.primary_node
3386 target_node = instance.secondary_nodes[0]
3388 feedback_fn("* checking disk consistency between source and target")
3389 for dev in instance.disks:
3390 # for drbd, these are drbd over lvm
3391 if not _CheckDiskConsistency(self, dev, target_node, False):
3392 if instance.admin_up and not self.op.ignore_consistency:
3393 raise errors.OpExecError("Disk %s is degraded on target node,"
3394 " aborting failover." % dev.iv_name)
3396 feedback_fn("* shutting down instance on source node")
3397 logging.info("Shutting down instance %s on node %s",
3398 instance.name, source_node)
3400 result = self.rpc.call_instance_shutdown(source_node, instance)
3401 if result.failed or not result.data:
3402 if self.op.ignore_consistency:
3403 self.proc.LogWarning("Could not shutdown instance %s on node %s."
3405 " anyway. Please make sure node %s is down",
3406 instance.name, source_node, source_node)
3408 raise errors.OpExecError("Could not shutdown instance %s on node %s" %
3409 (instance.name, source_node))
3411 feedback_fn("* deactivating the instance's disks on source node")
3412 if not _ShutdownInstanceDisks(self, instance, ignore_primary=True):
3413 raise errors.OpExecError("Can't shut down the instance's disks.")
3415 instance.primary_node = target_node
3416 # distribute new instance config to the other nodes
3417 self.cfg.Update(instance)
3419 # Only start the instance if it's marked as up
3420 if instance.admin_up:
3421 feedback_fn("* activating the instance's disks on target node")
3422 logging.info("Starting instance %s on node %s",
3423 instance.name, target_node)
3425 disks_ok, dummy = _AssembleInstanceDisks(self, instance,
3426 ignore_secondaries=True)
3428 _ShutdownInstanceDisks(self, instance)
3429 raise errors.OpExecError("Can't activate the instance's disks")
3431 feedback_fn("* starting the instance on the target node")
3432 result = self.rpc.call_instance_start(target_node, instance, None)
3433 msg = result.RemoteFailMsg()
3435 _ShutdownInstanceDisks(self, instance)
3436 raise errors.OpExecError("Could not start instance %s on node %s: %s" %
3437 (instance.name, target_node, msg))
3440 class LUMigrateInstance(LogicalUnit):
3441 """Migrate an instance.
3443 This is migration without shutting down, compared to the failover,
3444 which is done with shutdown.
3447 HPATH = "instance-migrate"
3448 HTYPE = constants.HTYPE_INSTANCE
3449 _OP_REQP = ["instance_name", "live", "cleanup"]
3453 def ExpandNames(self):
3454 self._ExpandAndLockInstance()
3455 self.needed_locks[locking.LEVEL_NODE] = []
3456 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3458 def DeclareLocks(self, level):
3459 if level == locking.LEVEL_NODE:
3460 self._LockInstancesNodes()
3462 def BuildHooksEnv(self):
3465 This runs on master, primary and secondary nodes of the instance.
3468 env = _BuildInstanceHookEnvByObject(self, self.instance)
3469 nl = [self.cfg.GetMasterNode()] + list(self.instance.secondary_nodes)
3472 def CheckPrereq(self):
3473 """Check prerequisites.
3475 This checks that the instance is in the cluster.
3478 instance = self.cfg.GetInstanceInfo(
3479 self.cfg.ExpandInstanceName(self.op.instance_name))
3480 if instance is None:
3481 raise errors.OpPrereqError("Instance '%s' not known" %
3482 self.op.instance_name)
3484 if instance.disk_template != constants.DT_DRBD8:
3485 raise errors.OpPrereqError("Instance's disk layout is not"
3486 " drbd8, cannot migrate.")
3488 secondary_nodes = instance.secondary_nodes
3489 if not secondary_nodes:
3490 raise errors.ProgrammerError("no secondary node but using "
3491 "drbd8 disk template")
3493 i_be = self.cfg.GetClusterInfo().FillBE(instance)
3495 target_node = secondary_nodes[0]
3496 # check memory requirements on the secondary node
3497 _CheckNodeFreeMemory(self, target_node, "migrating instance %s" %
3498 instance.name, i_be[constants.BE_MEMORY],
3499 instance.hypervisor)
3501 # check bridge existance
3502 brlist = [nic.bridge for nic in instance.nics]
3503 result = self.rpc.call_bridges_exist(target_node, brlist)
3504 if result.failed or not result.data:
3505 raise errors.OpPrereqError("One or more target bridges %s does not"
3506 " exist on destination node '%s'" %
3507 (brlist, target_node))
3509 if not self.op.cleanup:
3510 result = self.rpc.call_instance_migratable(instance.primary_node,
3512 msg = result.RemoteFailMsg()
3514 raise errors.OpPrereqError("Can't migrate: %s - please use failover" %
3517 self.instance = instance
3519 def _WaitUntilSync(self):
3520 """Poll with custom rpc for disk sync.
3522 This uses our own step-based rpc call.
3525 self.feedback_fn("* wait until resync is done")
3529 result = self.rpc.call_drbd_wait_sync(self.all_nodes,
3531 self.instance.disks)
3533 for node, nres in result.items():
3534 msg = nres.RemoteFailMsg()
3536 raise errors.OpExecError("Cannot resync disks on node %s: %s" %
3538 node_done, node_percent = nres.data[1]
3539 all_done = all_done and node_done
3540 if node_percent is not None:
3541 min_percent = min(min_percent, node_percent)
3543 if min_percent < 100:
3544 self.feedback_fn(" - progress: %.1f%%" % min_percent)
3547 def _EnsureSecondary(self, node):
3548 """Demote a node to secondary.
3551 self.feedback_fn("* switching node %s to secondary mode" % node)
3553 for dev in self.instance.disks:
3554 self.cfg.SetDiskID(dev, node)
3556 result = self.rpc.call_blockdev_close(node, self.instance.name,
3557 self.instance.disks)
3558 msg = result.RemoteFailMsg()
3560 raise errors.OpExecError("Cannot change disk to secondary on node %s,"
3561 " error %s" % (node, msg))
3563 def _GoStandalone(self):
3564 """Disconnect from the network.
3567 self.feedback_fn("* changing into standalone mode")
3568 result = self.rpc.call_drbd_disconnect_net(self.all_nodes, self.nodes_ip,
3569 self.instance.disks)
3570 for node, nres in result.items():
3571 msg = nres.RemoteFailMsg()
3573 raise errors.OpExecError("Cannot disconnect disks node %s,"
3574 " error %s" % (node, msg))
3576 def _GoReconnect(self, multimaster):
3577 """Reconnect to the network.
3583 msg = "single-master"
3584 self.feedback_fn("* changing disks into %s mode" % msg)
3585 result = self.rpc.call_drbd_attach_net(self.all_nodes, self.nodes_ip,
3586 self.instance.disks,
3587 self.instance.name, multimaster)
3588 for node, nres in result.items():
3589 msg = nres.RemoteFailMsg()
3591 raise errors.OpExecError("Cannot change disks config on node %s,"
3592 " error: %s" % (node, msg))
3594 def _ExecCleanup(self):
3595 """Try to cleanup after a failed migration.
3597 The cleanup is done by:
3598 - check that the instance is running only on one node
3599 (and update the config if needed)
3600 - change disks on its secondary node to secondary
3601 - wait until disks are fully synchronized
3602 - disconnect from the network
3603 - change disks into single-master mode
3604 - wait again until disks are fully synchronized
3607 instance = self.instance
3608 target_node = self.target_node
3609 source_node = self.source_node
3611 # check running on only one node
3612 self.feedback_fn("* checking where the instance actually runs"
3613 " (if this hangs, the hypervisor might be in"
3615 ins_l = self.rpc.call_instance_list(self.all_nodes, [instance.hypervisor])
3616 for node, result in ins_l.items():
3618 if not isinstance(result.data, list):
3619 raise errors.OpExecError("Can't contact node '%s'" % node)
3621 runningon_source = instance.name in ins_l[source_node].data
3622 runningon_target = instance.name in ins_l[target_node].data
3624 if runningon_source and runningon_target:
3625 raise errors.OpExecError("Instance seems to be running on two nodes,"
3626 " or the hypervisor is confused. You will have"
3627 " to ensure manually that it runs only on one"
3628 " and restart this operation.")
3630 if not (runningon_source or runningon_target):
3631 raise errors.OpExecError("Instance does not seem to be running at all."
3632 " In this case, it's safer to repair by"
3633 " running 'gnt-instance stop' to ensure disk"
3634 " shutdown, and then restarting it.")
3636 if runningon_target:
3637 # the migration has actually succeeded, we need to update the config
3638 self.feedback_fn("* instance running on secondary node (%s),"
3639 " updating config" % target_node)
3640 instance.primary_node = target_node
3641 self.cfg.Update(instance)
3642 demoted_node = source_node
3644 self.feedback_fn("* instance confirmed to be running on its"
3645 " primary node (%s)" % source_node)
3646 demoted_node = target_node
3648 self._EnsureSecondary(demoted_node)
3650 self._WaitUntilSync()
3651 except errors.OpExecError:
3652 # we ignore here errors, since if the device is standalone, it
3653 # won't be able to sync
3655 self._GoStandalone()
3656 self._GoReconnect(False)
3657 self._WaitUntilSync()
3659 self.feedback_fn("* done")
3661 def _RevertDiskStatus(self):
3662 """Try to revert the disk status after a failed migration.
3665 target_node = self.target_node
3667 self._EnsureSecondary(target_node)
3668 self._GoStandalone()
3669 self._GoReconnect(False)
3670 self._WaitUntilSync()
3671 except errors.OpExecError, err:
3672 self.LogWarning("Migration failed and I can't reconnect the"
3673 " drives: error '%s'\n"
3674 "Please look and recover the instance status" %
3677 def _AbortMigration(self):
3678 """Call the hypervisor code to abort a started migration.
3681 instance = self.instance
3682 target_node = self.target_node
3683 migration_info = self.migration_info
3685 abort_result = self.rpc.call_finalize_migration(target_node,
3689 abort_msg = abort_result.RemoteFailMsg()
3691 logging.error("Aborting migration failed on target node %s: %s" %
3692 (target_node, abort_msg))
3693 # Don't raise an exception here, as we stil have to try to revert the
3694 # disk status, even if this step failed.
3696 def _ExecMigration(self):
3697 """Migrate an instance.
3699 The migrate is done by:
3700 - change the disks into dual-master mode
3701 - wait until disks are fully synchronized again
3702 - migrate the instance
3703 - change disks on the new secondary node (the old primary) to secondary
3704 - wait until disks are fully synchronized
3705 - change disks into single-master mode
3708 instance = self.instance
3709 target_node = self.target_node
3710 source_node = self.source_node
3712 self.feedback_fn("* checking disk consistency between source and target")
3713 for dev in instance.disks:
3714 if not _CheckDiskConsistency(self, dev, target_node, False):
3715 raise errors.OpExecError("Disk %s is degraded or not fully"
3716 " synchronized on target node,"
3717 " aborting migrate." % dev.iv_name)
3719 # First get the migration information from the remote node
3720 result = self.rpc.call_migration_info(source_node, instance)
3721 msg = result.RemoteFailMsg()
3723 log_err = ("Failed fetching source migration information from %s: %s" %
3725 logging.error(log_err)
3726 raise errors.OpExecError(log_err)
3728 self.migration_info = migration_info = result.data[1]
3730 # Then switch the disks to master/master mode
3731 self._EnsureSecondary(target_node)
3732 self._GoStandalone()
3733 self._GoReconnect(True)
3734 self._WaitUntilSync()
3736 self.feedback_fn("* preparing %s to accept the instance" % target_node)
3737 result = self.rpc.call_accept_instance(target_node,
3740 self.nodes_ip[target_node])
3742 msg = result.RemoteFailMsg()
3744 logging.error("Instance pre-migration failed, trying to revert"
3745 " disk status: %s", msg)
3746 self._AbortMigration()
3747 self._RevertDiskStatus()
3748 raise errors.OpExecError("Could not pre-migrate instance %s: %s" %
3749 (instance.name, msg))
3751 self.feedback_fn("* migrating instance to %s" % target_node)
3753 result = self.rpc.call_instance_migrate(source_node, instance,
3754 self.nodes_ip[target_node],
3756 msg = result.RemoteFailMsg()
3758 logging.error("Instance migration failed, trying to revert"
3759 " disk status: %s", msg)
3760 self._AbortMigration()
3761 self._RevertDiskStatus()
3762 raise errors.OpExecError("Could not migrate instance %s: %s" %
3763 (instance.name, msg))
3766 instance.primary_node = target_node
3767 # distribute new instance config to the other nodes
3768 self.cfg.Update(instance)
3770 result = self.rpc.call_finalize_migration(target_node,
3774 msg = result.RemoteFailMsg()
3776 logging.error("Instance migration succeeded, but finalization failed:"
3778 raise errors.OpExecError("Could not finalize instance migration: %s" %
3781 self._EnsureSecondary(source_node)
3782 self._WaitUntilSync()
3783 self._GoStandalone()
3784 self._GoReconnect(False)
3785 self._WaitUntilSync()
3787 self.feedback_fn("* done")
3789 def Exec(self, feedback_fn):
3790 """Perform the migration.
3793 self.feedback_fn = feedback_fn
3795 self.source_node = self.instance.primary_node
3796 self.target_node = self.instance.secondary_nodes[0]
3797 self.all_nodes = [self.source_node, self.target_node]
3799 self.source_node: self.cfg.GetNodeInfo(self.source_node).secondary_ip,
3800 self.target_node: self.cfg.GetNodeInfo(self.target_node).secondary_ip,
3803 return self._ExecCleanup()
3805 return self._ExecMigration()
3808 def _CreateBlockDev(lu, node, instance, device, force_create,
3810 """Create a tree of block devices on a given node.
3812 If this device type has to be created on secondaries, create it and
3815 If not, just recurse to children keeping the same 'force' value.
3817 @param lu: the lu on whose behalf we execute
3818 @param node: the node on which to create the device
3819 @type instance: L{objects.Instance}
3820 @param instance: the instance which owns the device
3821 @type device: L{objects.Disk}
3822 @param device: the device to create
3823 @type force_create: boolean
3824 @param force_create: whether to force creation of this device; this
3825 will be change to True whenever we find a device which has
3826 CreateOnSecondary() attribute
3827 @param info: the extra 'metadata' we should attach to the device
3828 (this will be represented as a LVM tag)
3829 @type force_open: boolean
3830 @param force_open: this parameter will be passes to the
3831 L{backend.CreateBlockDevice} function where it specifies
3832 whether we run on primary or not, and it affects both
3833 the child assembly and the device own Open() execution
3836 if device.CreateOnSecondary():
3840 for child in device.children:
3841 _CreateBlockDev(lu, node, instance, child, force_create,
3844 if not force_create:
3847 _CreateSingleBlockDev(lu, node, instance, device, info, force_open)
3850 def _CreateSingleBlockDev(lu, node, instance, device, info, force_open):
3851 """Create a single block device on a given node.
3853 This will not recurse over children of the device, so they must be
3856 @param lu: the lu on whose behalf we execute
3857 @param node: the node on which to create the device
3858 @type instance: L{objects.Instance}
3859 @param instance: the instance which owns the device
3860 @type device: L{objects.Disk}
3861 @param device: the device to create
3862 @param info: the extra 'metadata' we should attach to the device
3863 (this will be represented as a LVM tag)
3864 @type force_open: boolean
3865 @param force_open: this parameter will be passes to the
3866 L{backend.CreateBlockDevice} function where it specifies
3867 whether we run on primary or not, and it affects both
3868 the child assembly and the device own Open() execution
3871 lu.cfg.SetDiskID(device, node)
3872 result = lu.rpc.call_blockdev_create(node, device, device.size,
3873 instance.name, force_open, info)
3874 msg = result.RemoteFailMsg()
3876 raise errors.OpExecError("Can't create block device %s on"
3877 " node %s for instance %s: %s" %
3878 (device, node, instance.name, msg))
3879 if device.physical_id is None:
3880 device.physical_id = result.data[1]
3883 def _GenerateUniqueNames(lu, exts):
3884 """Generate a suitable LV name.
3886 This will generate a logical volume name for the given instance.
3891 new_id = lu.cfg.GenerateUniqueID()
3892 results.append("%s%s" % (new_id, val))
3896 def _GenerateDRBD8Branch(lu, primary, secondary, size, names, iv_name,
3898 """Generate a drbd8 device complete with its children.
3901 port = lu.cfg.AllocatePort()
3902 vgname = lu.cfg.GetVGName()
3903 shared_secret = lu.cfg.GenerateDRBDSecret()
3904 dev_data = objects.Disk(dev_type=constants.LD_LV, size=size,
3905 logical_id=(vgname, names[0]))
3906 dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
3907 logical_id=(vgname, names[1]))
3908 drbd_dev = objects.Disk(dev_type=constants.LD_DRBD8, size=size,
3909 logical_id=(primary, secondary, port,
3912 children=[dev_data, dev_meta],
3917 def _GenerateDiskTemplate(lu, template_name,
3918 instance_name, primary_node,
3919 secondary_nodes, disk_info,
3920 file_storage_dir, file_driver,
3922 """Generate the entire disk layout for a given template type.
3925 #TODO: compute space requirements
3927 vgname = lu.cfg.GetVGName()
3928 disk_count = len(disk_info)
3930 if template_name == constants.DT_DISKLESS:
3932 elif template_name == constants.DT_PLAIN:
3933 if len(secondary_nodes) != 0:
3934 raise errors.ProgrammerError("Wrong template configuration")
3936 names = _GenerateUniqueNames(lu, [".disk%d" % i
3937 for i in range(disk_count)])
3938 for idx, disk in enumerate(disk_info):
3939 disk_index = idx + base_index
3940 disk_dev = objects.Disk(dev_type=constants.LD_LV, size=disk["size"],
3941 logical_id=(vgname, names[idx]),
3942 iv_name="disk/%d" % disk_index,
3944 disks.append(disk_dev)
3945 elif template_name == constants.DT_DRBD8:
3946 if len(secondary_nodes) != 1:
3947 raise errors.ProgrammerError("Wrong template configuration")
3948 remote_node = secondary_nodes[0]
3949 minors = lu.cfg.AllocateDRBDMinor(
3950 [primary_node, remote_node] * len(disk_info), instance_name)
3953 for lv_prefix in _GenerateUniqueNames(lu, [".disk%d" % i
3954 for i in range(disk_count)]):
3955 names.append(lv_prefix + "_data")
3956 names.append(lv_prefix + "_meta")
3957 for idx, disk in enumerate(disk_info):
3958 disk_index = idx + base_index
3959 disk_dev = _GenerateDRBD8Branch(lu, primary_node, remote_node,
3960 disk["size"], names[idx*2:idx*2+2],
3961 "disk/%d" % disk_index,
3962 minors[idx*2], minors[idx*2+1])
3963 disk_dev.mode = disk["mode"]
3964 disks.append(disk_dev)
3965 elif template_name == constants.DT_FILE:
3966 if len(secondary_nodes) != 0:
3967 raise errors.ProgrammerError("Wrong template configuration")
3969 for idx, disk in enumerate(disk_info):
3970 disk_index = idx + base_index
3971 disk_dev = objects.Disk(dev_type=constants.LD_FILE, size=disk["size"],
3972 iv_name="disk/%d" % disk_index,
3973 logical_id=(file_driver,
3974 "%s/disk%d" % (file_storage_dir,
3977 disks.append(disk_dev)
3979 raise errors.ProgrammerError("Invalid disk template '%s'" % template_name)
3983 def _GetInstanceInfoText(instance):
3984 """Compute that text that should be added to the disk's metadata.
3987 return "originstname+%s" % instance.name
3990 def _CreateDisks(lu, instance):
3991 """Create all disks for an instance.
3993 This abstracts away some work from AddInstance.
3995 @type lu: L{LogicalUnit}
3996 @param lu: the logical unit on whose behalf we execute
3997 @type instance: L{objects.Instance}
3998 @param instance: the instance whose disks we should create
4000 @return: the success of the creation
4003 info = _GetInstanceInfoText(instance)
4004 pnode = instance.primary_node
4006 if instance.disk_template == constants.DT_FILE:
4007 file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1])
4008 result = lu.rpc.call_file_storage_dir_create(pnode, file_storage_dir)
4010 if result.failed or not result.data:
4011 raise errors.OpExecError("Could not connect to node '%s'" % pnode)
4013 if not result.data[0]:
4014 raise errors.OpExecError("Failed to create directory '%s'" %
4017 # Note: this needs to be kept in sync with adding of disks in
4018 # LUSetInstanceParams
4019 for device in instance.disks:
4020 logging.info("Creating volume %s for instance %s",
4021 device.iv_name, instance.name)
4023 for node in instance.all_nodes:
4024 f_create = node == pnode
4025 _CreateBlockDev(lu, node, instance, device, f_create, info, f_create)
4028 def _RemoveDisks(lu, instance):
4029 """Remove all disks for an instance.
4031 This abstracts away some work from `AddInstance()` and
4032 `RemoveInstance()`. Note that in case some of the devices couldn't
4033 be removed, the removal will continue with the other ones (compare
4034 with `_CreateDisks()`).
4036 @type lu: L{LogicalUnit}
4037 @param lu: the logical unit on whose behalf we execute
4038 @type instance: L{objects.Instance}
4039 @param instance: the instance whose disks we should remove
4041 @return: the success of the removal
4044 logging.info("Removing block devices for instance %s", instance.name)
4047 for device in instance.disks:
4048 for node, disk in device.ComputeNodeTree(instance.primary_node):
4049 lu.cfg.SetDiskID(disk, node)
4050 result = lu.rpc.call_blockdev_remove(node, disk)
4051 if result.failed or not result.data:
4052 lu.proc.LogWarning("Could not remove block device %s on node %s,"
4053 " continuing anyway", device.iv_name, node)
4056 if instance.disk_template == constants.DT_FILE:
4057 file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1])
4058 result = lu.rpc.call_file_storage_dir_remove(instance.primary_node,
4060 if result.failed or not result.data:
4061 logging.error("Could not remove directory '%s'", file_storage_dir)
4067 def _ComputeDiskSize(disk_template, disks):
4068 """Compute disk size requirements in the volume group
4071 # Required free disk space as a function of disk and swap space
4073 constants.DT_DISKLESS: None,
4074 constants.DT_PLAIN: sum(d["size"] for d in disks),
4075 # 128 MB are added for drbd metadata for each disk
4076 constants.DT_DRBD8: sum(d["size"] + 128 for d in disks),
4077 constants.DT_FILE: None,
4080 if disk_template not in req_size_dict:
4081 raise errors.ProgrammerError("Disk template '%s' size requirement"
4082 " is unknown" % disk_template)
4084 return req_size_dict[disk_template]
4087 def _CheckHVParams(lu, nodenames, hvname, hvparams):
4088 """Hypervisor parameter validation.
4090 This function abstract the hypervisor parameter validation to be
4091 used in both instance create and instance modify.
4093 @type lu: L{LogicalUnit}
4094 @param lu: the logical unit for which we check
4095 @type nodenames: list
4096 @param nodenames: the list of nodes on which we should check
4097 @type hvname: string
4098 @param hvname: the name of the hypervisor we should use
4099 @type hvparams: dict
4100 @param hvparams: the parameters which we need to check
4101 @raise errors.OpPrereqError: if the parameters are not valid
4104 hvinfo = lu.rpc.call_hypervisor_validate_params(nodenames,
4107 for node in nodenames:
4112 if not info.data or not isinstance(info.data, (tuple, list)):
4113 raise errors.OpPrereqError("Cannot get current information"
4114 " from node '%s' (%s)" % (node, info.data))
4115 if not info.data[0]:
4116 raise errors.OpPrereqError("Hypervisor parameter validation failed:"
4117 " %s" % info.data[1])
4120 class LUCreateInstance(LogicalUnit):
4121 """Create an instance.
4124 HPATH = "instance-add"
4125 HTYPE = constants.HTYPE_INSTANCE
4126 _OP_REQP = ["instance_name", "disks", "disk_template",
4128 "wait_for_sync", "ip_check", "nics",
4129 "hvparams", "beparams"]
4132 def _ExpandNode(self, node):
4133 """Expands and checks one node name.
4136 node_full = self.cfg.ExpandNodeName(node)
4137 if node_full is None:
4138 raise errors.OpPrereqError("Unknown node %s" % node)
4141 def ExpandNames(self):
4142 """ExpandNames for CreateInstance.
4144 Figure out the right locks for instance creation.
4147 self.needed_locks = {}
4149 # set optional parameters to none if they don't exist
4150 for attr in ["pnode", "snode", "iallocator", "hypervisor"]:
4151 if not hasattr(self.op, attr):
4152 setattr(self.op, attr, None)
4154 # cheap checks, mostly valid constants given
4156 # verify creation mode
4157 if self.op.mode not in (constants.INSTANCE_CREATE,
4158 constants.INSTANCE_IMPORT):
4159 raise errors.OpPrereqError("Invalid instance creation mode '%s'" %
4162 # disk template and mirror node verification
4163 if self.op.disk_template not in constants.DISK_TEMPLATES:
4164 raise errors.OpPrereqError("Invalid disk template name")
4166 if self.op.hypervisor is None:
4167 self.op.hypervisor = self.cfg.GetHypervisorType()
4169 cluster = self.cfg.GetClusterInfo()
4170 enabled_hvs = cluster.enabled_hypervisors
4171 if self.op.hypervisor not in enabled_hvs:
4172 raise errors.OpPrereqError("Selected hypervisor (%s) not enabled in the"
4173 " cluster (%s)" % (self.op.hypervisor,
4174 ",".join(enabled_hvs)))
4176 # check hypervisor parameter syntax (locally)
4178 filled_hvp = cluster.FillDict(cluster.hvparams[self.op.hypervisor],
4180 hv_type = hypervisor.GetHypervisor(self.op.hypervisor)
4181 hv_type.CheckParameterSyntax(filled_hvp)
4183 # fill and remember the beparams dict
4184 utils.CheckBEParams(self.op.beparams)
4185 self.be_full = cluster.FillDict(cluster.beparams[constants.BEGR_DEFAULT],
4188 #### instance parameters check
4190 # instance name verification
4191 hostname1 = utils.HostInfo(self.op.instance_name)
4192 self.op.instance_name = instance_name = hostname1.name
4194 # this is just a preventive check, but someone might still add this
4195 # instance in the meantime, and creation will fail at lock-add time
4196 if instance_name in self.cfg.GetInstanceList():
4197 raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
4200 self.add_locks[locking.LEVEL_INSTANCE] = instance_name
4204 for nic in self.op.nics:
4205 # ip validity checks
4206 ip = nic.get("ip", None)
4207 if ip is None or ip.lower() == "none":
4209 elif ip.lower() == constants.VALUE_AUTO:
4210 nic_ip = hostname1.ip
4212 if not utils.IsValidIP(ip):
4213 raise errors.OpPrereqError("Given IP address '%s' doesn't look"
4214 " like a valid IP" % ip)
4217 # MAC address verification
4218 mac = nic.get("mac", constants.VALUE_AUTO)
4219 if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
4220 if not utils.IsValidMac(mac.lower()):
4221 raise errors.OpPrereqError("Invalid MAC address specified: %s" %
4223 # bridge verification
4224 bridge = nic.get("bridge", None)
4226 bridge = self.cfg.GetDefBridge()
4227 self.nics.append(objects.NIC(mac=mac, ip=nic_ip, bridge=bridge))
4229 # disk checks/pre-build
4231 for disk in self.op.disks:
4232 mode = disk.get("mode", constants.DISK_RDWR)
4233 if mode not in constants.DISK_ACCESS_SET:
4234 raise errors.OpPrereqError("Invalid disk access mode '%s'" %
4236 size = disk.get("size", None)
4238 raise errors.OpPrereqError("Missing disk size")
4242 raise errors.OpPrereqError("Invalid disk size '%s'" % size)
4243 self.disks.append({"size": size, "mode": mode})
4245 # used in CheckPrereq for ip ping check
4246 self.check_ip = hostname1.ip
4248 # file storage checks
4249 if (self.op.file_driver and
4250 not self.op.file_driver in constants.FILE_DRIVER):
4251 raise errors.OpPrereqError("Invalid file driver name '%s'" %
4252 self.op.file_driver)
4254 if self.op.file_storage_dir and os.path.isabs(self.op.file_storage_dir):
4255 raise errors.OpPrereqError("File storage directory path not absolute")
4257 ### Node/iallocator related checks
4258 if [self.op.iallocator, self.op.pnode].count(None) != 1:
4259 raise errors.OpPrereqError("One and only one of iallocator and primary"
4260 " node must be given")
4262 if self.op.iallocator:
4263 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
4265 self.op.pnode = self._ExpandNode(self.op.pnode)
4266 nodelist = [self.op.pnode]
4267 if self.op.snode is not None:
4268 self.op.snode = self._ExpandNode(self.op.snode)
4269 nodelist.append(self.op.snode)
4270 self.needed_locks[locking.LEVEL_NODE] = nodelist
4272 # in case of import lock the source node too
4273 if self.op.mode == constants.INSTANCE_IMPORT:
4274 src_node = getattr(self.op, "src_node", None)
4275 src_path = getattr(self.op, "src_path", None)
4277 if src_path is None:
4278 self.op.src_path = src_path = self.op.instance_name
4280 if src_node is None:
4281 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
4282 self.op.src_node = None
4283 if os.path.isabs(src_path):
4284 raise errors.OpPrereqError("Importing an instance from an absolute"
4285 " path requires a source node option.")
4287 self.op.src_node = src_node = self._ExpandNode(src_node)
4288 if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET:
4289 self.needed_locks[locking.LEVEL_NODE].append(src_node)
4290 if not os.path.isabs(src_path):
4291 self.op.src_path = src_path = \
4292 os.path.join(constants.EXPORT_DIR, src_path)
4294 else: # INSTANCE_CREATE
4295 if getattr(self.op, "os_type", None) is None:
4296 raise errors.OpPrereqError("No guest OS specified")
4298 def _RunAllocator(self):
4299 """Run the allocator based on input opcode.
4302 nics = [n.ToDict() for n in self.nics]
4303 ial = IAllocator(self,
4304 mode=constants.IALLOCATOR_MODE_ALLOC,
4305 name=self.op.instance_name,
4306 disk_template=self.op.disk_template,
4309 vcpus=self.be_full[constants.BE_VCPUS],
4310 mem_size=self.be_full[constants.BE_MEMORY],
4313 hypervisor=self.op.hypervisor,
4316 ial.Run(self.op.iallocator)
4319 raise errors.OpPrereqError("Can't compute nodes using"
4320 " iallocator '%s': %s" % (self.op.iallocator,
4322 if len(ial.nodes) != ial.required_nodes:
4323 raise errors.OpPrereqError("iallocator '%s' returned invalid number"
4324 " of nodes (%s), required %s" %
4325 (self.op.iallocator, len(ial.nodes),
4326 ial.required_nodes))
4327 self.op.pnode = ial.nodes[0]
4328 self.LogInfo("Selected nodes for instance %s via iallocator %s: %s",
4329 self.op.instance_name, self.op.iallocator,
4330 ", ".join(ial.nodes))
4331 if ial.required_nodes == 2:
4332 self.op.snode = ial.nodes[1]
4334 def BuildHooksEnv(self):
4337 This runs on master, primary and secondary nodes of the instance.
4341 "INSTANCE_DISK_TEMPLATE": self.op.disk_template,
4342 "INSTANCE_DISK_SIZE": ",".join(str(d["size"]) for d in self.disks),
4343 "INSTANCE_ADD_MODE": self.op.mode,
4345 if self.op.mode == constants.INSTANCE_IMPORT:
4346 env["INSTANCE_SRC_NODE"] = self.op.src_node
4347 env["INSTANCE_SRC_PATH"] = self.op.src_path
4348 env["INSTANCE_SRC_IMAGES"] = self.src_images
4350 env.update(_BuildInstanceHookEnv(name=self.op.instance_name,
4351 primary_node=self.op.pnode,
4352 secondary_nodes=self.secondaries,
4353 status=self.instance_status,
4354 os_type=self.op.os_type,
4355 memory=self.be_full[constants.BE_MEMORY],
4356 vcpus=self.be_full[constants.BE_VCPUS],
4357 nics=[(n.ip, n.bridge, n.mac) for n in self.nics],
4360 nl = ([self.cfg.GetMasterNode(), self.op.pnode] +
4365 def CheckPrereq(self):
4366 """Check prerequisites.
4369 if (not self.cfg.GetVGName() and
4370 self.op.disk_template not in constants.DTS_NOT_LVM):
4371 raise errors.OpPrereqError("Cluster does not support lvm-based"
4375 if self.op.mode == constants.INSTANCE_IMPORT:
4376 src_node = self.op.src_node
4377 src_path = self.op.src_path
4379 if src_node is None:
4380 exp_list = self.rpc.call_export_list(
4381 self.acquired_locks[locking.LEVEL_NODE])
4383 for node in exp_list:
4384 if not exp_list[node].failed and src_path in exp_list[node].data:
4386 self.op.src_node = src_node = node
4387 self.op.src_path = src_path = os.path.join(constants.EXPORT_DIR,
4391 raise errors.OpPrereqError("No export found for relative path %s" %
4394 _CheckNodeOnline(self, src_node)
4395 result = self.rpc.call_export_info(src_node, src_path)
4398 raise errors.OpPrereqError("No export found in dir %s" % src_path)
4400 export_info = result.data
4401 if not export_info.has_section(constants.INISECT_EXP):
4402 raise errors.ProgrammerError("Corrupted export config")
4404 ei_version = export_info.get(constants.INISECT_EXP, 'version')
4405 if (int(ei_version) != constants.EXPORT_VERSION):
4406 raise errors.OpPrereqError("Wrong export version %s (wanted %d)" %
4407 (ei_version, constants.EXPORT_VERSION))
4409 # Check that the new instance doesn't have less disks than the export
4410 instance_disks = len(self.disks)
4411 export_disks = export_info.getint(constants.INISECT_INS, 'disk_count')
4412 if instance_disks < export_disks:
4413 raise errors.OpPrereqError("Not enough disks to import."
4414 " (instance: %d, export: %d)" %
4415 (instance_disks, export_disks))
4417 self.op.os_type = export_info.get(constants.INISECT_EXP, 'os')
4419 for idx in range(export_disks):
4420 option = 'disk%d_dump' % idx
4421 if export_info.has_option(constants.INISECT_INS, option):
4422 # FIXME: are the old os-es, disk sizes, etc. useful?
4423 export_name = export_info.get(constants.INISECT_INS, option)
4424 image = os.path.join(src_path, export_name)
4425 disk_images.append(image)
4427 disk_images.append(False)
4429 self.src_images = disk_images
4431 old_name = export_info.get(constants.INISECT_INS, 'name')
4432 # FIXME: int() here could throw a ValueError on broken exports
4433 exp_nic_count = int(export_info.get(constants.INISECT_INS, 'nic_count'))
4434 if self.op.instance_name == old_name:
4435 for idx, nic in enumerate(self.nics):
4436 if nic.mac == constants.VALUE_AUTO and exp_nic_count >= idx:
4437 nic_mac_ini = 'nic%d_mac' % idx
4438 nic.mac = export_info.get(constants.INISECT_INS, nic_mac_ini)
4440 # ip ping checks (we use the same ip that was resolved in ExpandNames)
4441 if self.op.start and not self.op.ip_check:
4442 raise errors.OpPrereqError("Cannot ignore IP address conflicts when"
4443 " adding an instance in start mode")
4445 if self.op.ip_check:
4446 if utils.TcpPing(self.check_ip, constants.DEFAULT_NODED_PORT):
4447 raise errors.OpPrereqError("IP %s of instance %s already in use" %
4448 (self.check_ip, self.op.instance_name))
4452 if self.op.iallocator is not None:
4453 self._RunAllocator()
4455 #### node related checks
4457 # check primary node
4458 self.pnode = pnode = self.cfg.GetNodeInfo(self.op.pnode)
4459 assert self.pnode is not None, \
4460 "Cannot retrieve locked node %s" % self.op.pnode
4462 raise errors.OpPrereqError("Cannot use offline primary node '%s'" %
4465 self.secondaries = []
4467 # mirror node verification
4468 if self.op.disk_template in constants.DTS_NET_MIRROR:
4469 if self.op.snode is None:
4470 raise errors.OpPrereqError("The networked disk templates need"
4472 if self.op.snode == pnode.name:
4473 raise errors.OpPrereqError("The secondary node cannot be"
4474 " the primary node.")
4475 self.secondaries.append(self.op.snode)
4476 _CheckNodeOnline(self, self.op.snode)
4478 nodenames = [pnode.name] + self.secondaries
4480 req_size = _ComputeDiskSize(self.op.disk_template,
4483 # Check lv size requirements
4484 if req_size is not None:
4485 nodeinfo = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
4487 for node in nodenames:
4488 info = nodeinfo[node]
4492 raise errors.OpPrereqError("Cannot get current information"
4493 " from node '%s'" % node)
4494 vg_free = info.get('vg_free', None)
4495 if not isinstance(vg_free, int):
4496 raise errors.OpPrereqError("Can't compute free disk space on"
4498 if req_size > info['vg_free']:
4499 raise errors.OpPrereqError("Not enough disk space on target node %s."
4500 " %d MB available, %d MB required" %
4501 (node, info['vg_free'], req_size))
4503 _CheckHVParams(self, nodenames, self.op.hypervisor, self.op.hvparams)
4506 result = self.rpc.call_os_get(pnode.name, self.op.os_type)
4508 if not isinstance(result.data, objects.OS):
4509 raise errors.OpPrereqError("OS '%s' not in supported os list for"
4510 " primary node" % self.op.os_type)
4512 # bridge check on primary node
4513 bridges = [n.bridge for n in self.nics]
4514 result = self.rpc.call_bridges_exist(self.pnode.name, bridges)
4517 raise errors.OpPrereqError("One of the target bridges '%s' does not"
4518 " exist on destination node '%s'" %
4519 (",".join(bridges), pnode.name))
4521 # memory check on primary node
4523 _CheckNodeFreeMemory(self, self.pnode.name,
4524 "creating instance %s" % self.op.instance_name,
4525 self.be_full[constants.BE_MEMORY],
4528 self.instance_status = self.op.start
4530 def Exec(self, feedback_fn):
4531 """Create and add the instance to the cluster.
4534 instance = self.op.instance_name
4535 pnode_name = self.pnode.name
4537 for nic in self.nics:
4538 if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
4539 nic.mac = self.cfg.GenerateMAC()
4541 ht_kind = self.op.hypervisor
4542 if ht_kind in constants.HTS_REQ_PORT:
4543 network_port = self.cfg.AllocatePort()
4547 ##if self.op.vnc_bind_address is None:
4548 ## self.op.vnc_bind_address = constants.VNC_DEFAULT_BIND_ADDRESS
4550 # this is needed because os.path.join does not accept None arguments
4551 if self.op.file_storage_dir is None:
4552 string_file_storage_dir = ""
4554 string_file_storage_dir = self.op.file_storage_dir
4556 # build the full file storage dir path
4557 file_storage_dir = os.path.normpath(os.path.join(
4558 self.cfg.GetFileStorageDir(),
4559 string_file_storage_dir, instance))
4562 disks = _GenerateDiskTemplate(self,
4563 self.op.disk_template,
4564 instance, pnode_name,
4568 self.op.file_driver,
4571 iobj = objects.Instance(name=instance, os=self.op.os_type,
4572 primary_node=pnode_name,
4573 nics=self.nics, disks=disks,
4574 disk_template=self.op.disk_template,
4575 admin_up=self.instance_status,
4576 network_port=network_port,
4577 beparams=self.op.beparams,
4578 hvparams=self.op.hvparams,
4579 hypervisor=self.op.hypervisor,
4582 feedback_fn("* creating instance disks...")
4584 _CreateDisks(self, iobj)
4585 except errors.OpExecError:
4586 self.LogWarning("Device creation failed, reverting...")
4588 _RemoveDisks(self, iobj)
4590 self.cfg.ReleaseDRBDMinors(instance)
4593 feedback_fn("adding instance %s to cluster config" % instance)
4595 self.cfg.AddInstance(iobj)
4596 # Declare that we don't want to remove the instance lock anymore, as we've
4597 # added the instance to the config
4598 del self.remove_locks[locking.LEVEL_INSTANCE]
4599 # Unlock all the nodes
4600 if self.op.mode == constants.INSTANCE_IMPORT:
4601 nodes_keep = [self.op.src_node]
4602 nodes_release = [node for node in self.acquired_locks[locking.LEVEL_NODE]
4603 if node != self.op.src_node]
4604 self.context.glm.release(locking.LEVEL_NODE, nodes_release)
4605 self.acquired_locks[locking.LEVEL_NODE] = nodes_keep
4607 self.context.glm.release(locking.LEVEL_NODE)
4608 del self.acquired_locks[locking.LEVEL_NODE]
4610 if self.op.wait_for_sync:
4611 disk_abort = not _WaitForSync(self, iobj)
4612 elif iobj.disk_template in constants.DTS_NET_MIRROR:
4613 # make sure the disks are not degraded (still sync-ing is ok)
4615 feedback_fn("* checking mirrors status")
4616 disk_abort = not _WaitForSync(self, iobj, oneshot=True)
4621 _RemoveDisks(self, iobj)
4622 self.cfg.RemoveInstance(iobj.name)
4623 # Make sure the instance lock gets removed
4624 self.remove_locks[locking.LEVEL_INSTANCE] = iobj.name
4625 raise errors.OpExecError("There are some degraded disks for"
4628 feedback_fn("creating os for instance %s on node %s" %
4629 (instance, pnode_name))
4631 if iobj.disk_template != constants.DT_DISKLESS:
4632 if self.op.mode == constants.INSTANCE_CREATE:
4633 feedback_fn("* running the instance OS create scripts...")
4634 result = self.rpc.call_instance_os_add(pnode_name, iobj)
4635 msg = result.RemoteFailMsg()
4637 raise errors.OpExecError("Could not add os for instance %s"
4639 (instance, pnode_name, msg))
4641 elif self.op.mode == constants.INSTANCE_IMPORT:
4642 feedback_fn("* running the instance OS import scripts...")
4643 src_node = self.op.src_node
4644 src_images = self.src_images
4645 cluster_name = self.cfg.GetClusterName()
4646 import_result = self.rpc.call_instance_os_import(pnode_name, iobj,
4647 src_node, src_images,
4649 import_result.Raise()
4650 for idx, result in enumerate(import_result.data):
4652 self.LogWarning("Could not import the image %s for instance"
4653 " %s, disk %d, on node %s" %
4654 (src_images[idx], instance, idx, pnode_name))
4656 # also checked in the prereq part
4657 raise errors.ProgrammerError("Unknown OS initialization mode '%s'"
4661 logging.info("Starting instance %s on node %s", instance, pnode_name)
4662 feedback_fn("* starting instance...")
4663 result = self.rpc.call_instance_start(pnode_name, iobj, None)
4664 msg = result.RemoteFailMsg()
4666 raise errors.OpExecError("Could not start instance: %s" % msg)
4669 class LUConnectConsole(NoHooksLU):
4670 """Connect to an instance's console.
4672 This is somewhat special in that it returns the command line that
4673 you need to run on the master node in order to connect to the
4677 _OP_REQP = ["instance_name"]
4680 def ExpandNames(self):
4681 self._ExpandAndLockInstance()
4683 def CheckPrereq(self):
4684 """Check prerequisites.
4686 This checks that the instance is in the cluster.
4689 self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
4690 assert self.instance is not None, \
4691 "Cannot retrieve locked instance %s" % self.op.instance_name
4692 _CheckNodeOnline(self, self.instance.primary_node)
4694 def Exec(self, feedback_fn):
4695 """Connect to the console of an instance
4698 instance = self.instance
4699 node = instance.primary_node
4701 node_insts = self.rpc.call_instance_list([node],
4702 [instance.hypervisor])[node]
4705 if instance.name not in node_insts.data:
4706 raise errors.OpExecError("Instance %s is not running." % instance.name)
4708 logging.debug("Connecting to console of %s on %s", instance.name, node)
4710 hyper = hypervisor.GetHypervisor(instance.hypervisor)
4711 cluster = self.cfg.GetClusterInfo()
4712 # beparams and hvparams are passed separately, to avoid editing the
4713 # instance and then saving the defaults in the instance itself.
4714 hvparams = cluster.FillHV(instance)
4715 beparams = cluster.FillBE(instance)
4716 console_cmd = hyper.GetShellCommandForConsole(instance, hvparams, beparams)
4719 return self.ssh.BuildCmd(node, "root", console_cmd, batch=True, tty=True)
4722 class LUReplaceDisks(LogicalUnit):
4723 """Replace the disks of an instance.
4726 HPATH = "mirrors-replace"
4727 HTYPE = constants.HTYPE_INSTANCE
4728 _OP_REQP = ["instance_name", "mode", "disks"]
4731 def CheckArguments(self):
4732 if not hasattr(self.op, "remote_node"):
4733 self.op.remote_node = None
4734 if not hasattr(self.op, "iallocator"):
4735 self.op.iallocator = None
4737 # check for valid parameter combination
4738 cnt = [self.op.remote_node, self.op.iallocator].count(None)
4739 if self.op.mode == constants.REPLACE_DISK_CHG:
4741 raise errors.OpPrereqError("When changing the secondary either an"
4742 " iallocator script must be used or the"
4745 raise errors.OpPrereqError("Give either the iallocator or the new"
4746 " secondary, not both")
4747 else: # not replacing the secondary
4749 raise errors.OpPrereqError("The iallocator and new node options can"
4750 " be used only when changing the"
4753 def ExpandNames(self):
4754 self._ExpandAndLockInstance()
4756 if self.op.iallocator is not None:
4757 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
4758 elif self.op.remote_node is not None:
4759 remote_node = self.cfg.ExpandNodeName(self.op.remote_node)
4760 if remote_node is None:
4761 raise errors.OpPrereqError("Node '%s' not known" %
4762 self.op.remote_node)
4763 self.op.remote_node = remote_node
4764 # Warning: do not remove the locking of the new secondary here
4765 # unless DRBD8.AddChildren is changed to work in parallel;
4766 # currently it doesn't since parallel invocations of
4767 # FindUnusedMinor will conflict
4768 self.needed_locks[locking.LEVEL_NODE] = [remote_node]
4769 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
4771 self.needed_locks[locking.LEVEL_NODE] = []
4772 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
4774 def DeclareLocks(self, level):
4775 # If we're not already locking all nodes in the set we have to declare the
4776 # instance's primary/secondary nodes.
4777 if (level == locking.LEVEL_NODE and
4778 self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET):
4779 self._LockInstancesNodes()
4781 def _RunAllocator(self):
4782 """Compute a new secondary node using an IAllocator.
4785 ial = IAllocator(self,
4786 mode=constants.IALLOCATOR_MODE_RELOC,
4787 name=self.op.instance_name,
4788 relocate_from=[self.sec_node])
4790 ial.Run(self.op.iallocator)
4793 raise errors.OpPrereqError("Can't compute nodes using"
4794 " iallocator '%s': %s" % (self.op.iallocator,
4796 if len(ial.nodes) != ial.required_nodes:
4797 raise errors.OpPrereqError("iallocator '%s' returned invalid number"
4798 " of nodes (%s), required %s" %
4799 (len(ial.nodes), ial.required_nodes))
4800 self.op.remote_node = ial.nodes[0]
4801 self.LogInfo("Selected new secondary for the instance: %s",
4802 self.op.remote_node)
4804 def BuildHooksEnv(self):
4807 This runs on the master, the primary and all the secondaries.
4811 "MODE": self.op.mode,
4812 "NEW_SECONDARY": self.op.remote_node,
4813 "OLD_SECONDARY": self.instance.secondary_nodes[0],
4815 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
4817 self.cfg.GetMasterNode(),
4818 self.instance.primary_node,
4820 if self.op.remote_node is not None:
4821 nl.append(self.op.remote_node)
4824 def CheckPrereq(self):
4825 """Check prerequisites.
4827 This checks that the instance is in the cluster.
4830 instance = self.cfg.GetInstanceInfo(self.op.instance_name)
4831 assert instance is not None, \
4832 "Cannot retrieve locked instance %s" % self.op.instance_name
4833 self.instance = instance
4835 if instance.disk_template != constants.DT_DRBD8:
4836 raise errors.OpPrereqError("Can only run replace disks for DRBD8-based"
4839 if len(instance.secondary_nodes) != 1:
4840 raise errors.OpPrereqError("The instance has a strange layout,"
4841 " expected one secondary but found %d" %
4842 len(instance.secondary_nodes))
4844 self.sec_node = instance.secondary_nodes[0]
4846 if self.op.iallocator is not None:
4847 self._RunAllocator()
4849 remote_node = self.op.remote_node
4850 if remote_node is not None:
4851 self.remote_node_info = self.cfg.GetNodeInfo(remote_node)
4852 assert self.remote_node_info is not None, \
4853 "Cannot retrieve locked node %s" % remote_node
4855 self.remote_node_info = None
4856 if remote_node == instance.primary_node:
4857 raise errors.OpPrereqError("The specified node is the primary node of"
4859 elif remote_node == self.sec_node:
4860 raise errors.OpPrereqError("The specified node is already the"
4861 " secondary node of the instance.")
4863 if self.op.mode == constants.REPLACE_DISK_PRI:
4864 n1 = self.tgt_node = instance.primary_node
4865 n2 = self.oth_node = self.sec_node
4866 elif self.op.mode == constants.REPLACE_DISK_SEC:
4867 n1 = self.tgt_node = self.sec_node
4868 n2 = self.oth_node = instance.primary_node
4869 elif self.op.mode == constants.REPLACE_DISK_CHG:
4870 n1 = self.new_node = remote_node
4871 n2 = self.oth_node = instance.primary_node
4872 self.tgt_node = self.sec_node
4874 raise errors.ProgrammerError("Unhandled disk replace mode")
4876 _CheckNodeOnline(self, n1)
4877 _CheckNodeOnline(self, n2)
4879 if not self.op.disks:
4880 self.op.disks = range(len(instance.disks))
4882 for disk_idx in self.op.disks:
4883 instance.FindDisk(disk_idx)
4885 def _ExecD8DiskOnly(self, feedback_fn):
4886 """Replace a disk on the primary or secondary for dbrd8.
4888 The algorithm for replace is quite complicated:
4890 1. for each disk to be replaced:
4892 1. create new LVs on the target node with unique names
4893 1. detach old LVs from the drbd device
4894 1. rename old LVs to name_replaced.<time_t>
4895 1. rename new LVs to old LVs
4896 1. attach the new LVs (with the old names now) to the drbd device
4898 1. wait for sync across all devices
4900 1. for each modified disk:
4902 1. remove old LVs (which have the name name_replaces.<time_t>)
4904 Failures are not very well handled.
4908 warning, info = (self.proc.LogWarning, self.proc.LogInfo)
4909 instance = self.instance
4911 vgname = self.cfg.GetVGName()
4914 tgt_node = self.tgt_node
4915 oth_node = self.oth_node
4917 # Step: check device activation
4918 self.proc.LogStep(1, steps_total, "check device existence")
4919 info("checking volume groups")
4920 my_vg = cfg.GetVGName()
4921 results = self.rpc.call_vg_list([oth_node, tgt_node])
4923 raise errors.OpExecError("Can't list volume groups on the nodes")
4924 for node in oth_node, tgt_node:
4926 if res.failed or not res.data or my_vg not in res.data:
4927 raise errors.OpExecError("Volume group '%s' not found on %s" %
4929 for idx, dev in enumerate(instance.disks):
4930 if idx not in self.op.disks:
4932 for node in tgt_node, oth_node:
4933 info("checking disk/%d on %s" % (idx, node))
4934 cfg.SetDiskID(dev, node)
4935 if not self.rpc.call_blockdev_find(node, dev):
4936 raise errors.OpExecError("Can't find disk/%d on node %s" %
4939 # Step: check other node consistency
4940 self.proc.LogStep(2, steps_total, "check peer consistency")
4941 for idx, dev in enumerate(instance.disks):
4942 if idx not in self.op.disks:
4944 info("checking disk/%d consistency on %s" % (idx, oth_node))
4945 if not _CheckDiskConsistency(self, dev, oth_node,
4946 oth_node==instance.primary_node):
4947 raise errors.OpExecError("Peer node (%s) has degraded storage, unsafe"
4948 " to replace disks on this node (%s)" %
4949 (oth_node, tgt_node))
4951 # Step: create new storage
4952 self.proc.LogStep(3, steps_total, "allocate new storage")
4953 for idx, dev in enumerate(instance.disks):
4954 if idx not in self.op.disks:
4957 cfg.SetDiskID(dev, tgt_node)
4958 lv_names = [".disk%d_%s" % (idx, suf)
4959 for suf in ["data", "meta"]]
4960 names = _GenerateUniqueNames(self, lv_names)
4961 lv_data = objects.Disk(dev_type=constants.LD_LV, size=size,
4962 logical_id=(vgname, names[0]))
4963 lv_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
4964 logical_id=(vgname, names[1]))
4965 new_lvs = [lv_data, lv_meta]
4966 old_lvs = dev.children
4967 iv_names[dev.iv_name] = (dev, old_lvs, new_lvs)
4968 info("creating new local storage on %s for %s" %
4969 (tgt_node, dev.iv_name))
4970 # we pass force_create=True to force the LVM creation
4971 for new_lv in new_lvs:
4972 _CreateBlockDev(self, tgt_node, instance, new_lv, True,
4973 _GetInstanceInfoText(instance), False)
4975 # Step: for each lv, detach+rename*2+attach
4976 self.proc.LogStep(4, steps_total, "change drbd configuration")
4977 for dev, old_lvs, new_lvs in iv_names.itervalues():
4978 info("detaching %s drbd from local storage" % dev.iv_name)
4979 result = self.rpc.call_blockdev_removechildren(tgt_node, dev, old_lvs)
4982 raise errors.OpExecError("Can't detach drbd from local storage on node"
4983 " %s for device %s" % (tgt_node, dev.iv_name))
4985 #cfg.Update(instance)
4987 # ok, we created the new LVs, so now we know we have the needed
4988 # storage; as such, we proceed on the target node to rename
4989 # old_lv to _old, and new_lv to old_lv; note that we rename LVs
4990 # using the assumption that logical_id == physical_id (which in
4991 # turn is the unique_id on that node)
4993 # FIXME(iustin): use a better name for the replaced LVs
4994 temp_suffix = int(time.time())
4995 ren_fn = lambda d, suff: (d.physical_id[0],
4996 d.physical_id[1] + "_replaced-%s" % suff)
4997 # build the rename list based on what LVs exist on the node
4999 for to_ren in old_lvs:
5000 find_res = self.rpc.call_blockdev_find(tgt_node, to_ren)
5001 if not find_res.failed and find_res.data is not None: # device exists
5002 rlist.append((to_ren, ren_fn(to_ren, temp_suffix)))
5004 info("renaming the old LVs on the target node")
5005 result = self.rpc.call_blockdev_rename(tgt_node, rlist)
5008 raise errors.OpExecError("Can't rename old LVs on node %s" % tgt_node)
5009 # now we rename the new LVs to the old LVs
5010 info("renaming the new LVs on the target node")
5011 rlist = [(new, old.physical_id) for old, new in zip(old_lvs, new_lvs)]
5012 result = self.rpc.call_blockdev_rename(tgt_node, rlist)
5015 raise errors.OpExecError("Can't rename new LVs on node %s" % tgt_node)
5017 for old, new in zip(old_lvs, new_lvs):
5018 new.logical_id = old.logical_id
5019 cfg.SetDiskID(new, tgt_node)
5021 for disk in old_lvs:
5022 disk.logical_id = ren_fn(disk, temp_suffix)
5023 cfg.SetDiskID(disk, tgt_node)
5025 # now that the new lvs have the old name, we can add them to the device
5026 info("adding new mirror component on %s" % tgt_node)
5027 result = self.rpc.call_blockdev_addchildren(tgt_node, dev, new_lvs)
5028 if result.failed or not result.data:
5029 for new_lv in new_lvs:
5030 result = self.rpc.call_blockdev_remove(tgt_node, new_lv)
5031 if result.failed or not result.data:
5032 warning("Can't rollback device %s", hint="manually cleanup unused"
5034 raise errors.OpExecError("Can't add local storage to drbd")
5036 dev.children = new_lvs
5037 cfg.Update(instance)
5039 # Step: wait for sync
5041 # this can fail as the old devices are degraded and _WaitForSync
5042 # does a combined result over all disks, so we don't check its
5044 self.proc.LogStep(5, steps_total, "sync devices")
5045 _WaitForSync(self, instance, unlock=True)
5047 # so check manually all the devices
5048 for name, (dev, old_lvs, new_lvs) in iv_names.iteritems():
5049 cfg.SetDiskID(dev, instance.primary_node)
5050 result = self.rpc.call_blockdev_find(instance.primary_node, dev)
5051 if result.failed or result.data[5]:
5052 raise errors.OpExecError("DRBD device %s is degraded!" % name)
5054 # Step: remove old storage
5055 self.proc.LogStep(6, steps_total, "removing old storage")
5056 for name, (dev, old_lvs, new_lvs) in iv_names.iteritems():
5057 info("remove logical volumes for %s" % name)
5059 cfg.SetDiskID(lv, tgt_node)
5060 result = self.rpc.call_blockdev_remove(tgt_node, lv)
5061 if result.failed or not result.data:
5062 warning("Can't remove old LV", hint="manually remove unused LVs")
5065 def _ExecD8Secondary(self, feedback_fn):
5066 """Replace the secondary node for drbd8.
5068 The algorithm for replace is quite complicated:
5069 - for all disks of the instance:
5070 - create new LVs on the new node with same names
5071 - shutdown the drbd device on the old secondary
5072 - disconnect the drbd network on the primary
5073 - create the drbd device on the new secondary
5074 - network attach the drbd on the primary, using an artifice:
5075 the drbd code for Attach() will connect to the network if it
5076 finds a device which is connected to the good local disks but
5078 - wait for sync across all devices
5079 - remove all disks from the old secondary
5081 Failures are not very well handled.
5085 warning, info = (self.proc.LogWarning, self.proc.LogInfo)
5086 instance = self.instance
5090 old_node = self.tgt_node
5091 new_node = self.new_node
5092 pri_node = instance.primary_node
5094 old_node: self.cfg.GetNodeInfo(old_node).secondary_ip,
5095 new_node: self.cfg.GetNodeInfo(new_node).secondary_ip,
5096 pri_node: self.cfg.GetNodeInfo(pri_node).secondary_ip,
5099 # Step: check device activation
5100 self.proc.LogStep(1, steps_total, "check device existence")
5101 info("checking volume groups")
5102 my_vg = cfg.GetVGName()
5103 results = self.rpc.call_vg_list([pri_node, new_node])
5104 for node in pri_node, new_node:
5106 if res.failed or not res.data or my_vg not in res.data:
5107 raise errors.OpExecError("Volume group '%s' not found on %s" %
5109 for idx, dev in enumerate(instance.disks):
5110 if idx not in self.op.disks:
5112 info("checking disk/%d on %s" % (idx, pri_node))
5113 cfg.SetDiskID(dev, pri_node)
5114 result = self.rpc.call_blockdev_find(pri_node, dev)
5117 raise errors.OpExecError("Can't find disk/%d on node %s" %
5120 # Step: check other node consistency
5121 self.proc.LogStep(2, steps_total, "check peer consistency")
5122 for idx, dev in enumerate(instance.disks):
5123 if idx not in self.op.disks:
5125 info("checking disk/%d consistency on %s" % (idx, pri_node))
5126 if not _CheckDiskConsistency(self, dev, pri_node, True, ldisk=True):
5127 raise errors.OpExecError("Primary node (%s) has degraded storage,"
5128 " unsafe to replace the secondary" %
5131 # Step: create new storage
5132 self.proc.LogStep(3, steps_total, "allocate new storage")
5133 for idx, dev in enumerate(instance.disks):
5134 info("adding new local storage on %s for disk/%d" %
5136 # we pass force_create=True to force LVM creation
5137 for new_lv in dev.children:
5138 _CreateBlockDev(self, new_node, instance, new_lv, True,
5139 _GetInstanceInfoText(instance), False)
5141 # Step 4: dbrd minors and drbd setups changes
5142 # after this, we must manually remove the drbd minors on both the
5143 # error and the success paths
5144 minors = cfg.AllocateDRBDMinor([new_node for dev in instance.disks],
5146 logging.debug("Allocated minors %s" % (minors,))
5147 self.proc.LogStep(4, steps_total, "changing drbd configuration")
5148 for idx, (dev, new_minor) in enumerate(zip(instance.disks, minors)):
5150 info("activating a new drbd on %s for disk/%d" % (new_node, idx))
5151 # create new devices on new_node; note that we create two IDs:
5152 # one without port, so the drbd will be activated without
5153 # networking information on the new node at this stage, and one
5154 # with network, for the latter activation in step 4
5155 (o_node1, o_node2, o_port, o_minor1, o_minor2, o_secret) = dev.logical_id
5156 if pri_node == o_node1:
5161 new_alone_id = (pri_node, new_node, None, p_minor, new_minor, o_secret)
5162 new_net_id = (pri_node, new_node, o_port, p_minor, new_minor, o_secret)
5164 iv_names[idx] = (dev, dev.children, new_net_id)
5165 logging.debug("Allocated new_minor: %s, new_logical_id: %s", new_minor,
5167 new_drbd = objects.Disk(dev_type=constants.LD_DRBD8,
5168 logical_id=new_alone_id,
5169 children=dev.children)
5171 _CreateSingleBlockDev(self, new_node, instance, new_drbd,
5172 _GetInstanceInfoText(instance), False)
5173 except errors.BlockDeviceError:
5174 self.cfg.ReleaseDRBDMinors(instance.name)
5177 for idx, dev in enumerate(instance.disks):
5178 # we have new devices, shutdown the drbd on the old secondary
5179 info("shutting down drbd for disk/%d on old node" % idx)
5180 cfg.SetDiskID(dev, old_node)
5181 result = self.rpc.call_blockdev_shutdown(old_node, dev)
5182 if result.failed or not result.data:
5183 warning("Failed to shutdown drbd for disk/%d on old node" % idx,
5184 hint="Please cleanup this device manually as soon as possible")
5186 info("detaching primary drbds from the network (=> standalone)")
5187 result = self.rpc.call_drbd_disconnect_net([pri_node], nodes_ip,
5188 instance.disks)[pri_node]
5190 msg = result.RemoteFailMsg()
5192 # detaches didn't succeed (unlikely)
5193 self.cfg.ReleaseDRBDMinors(instance.name)
5194 raise errors.OpExecError("Can't detach the disks from the network on"
5195 " old node: %s" % (msg,))
5197 # if we managed to detach at least one, we update all the disks of
5198 # the instance to point to the new secondary
5199 info("updating instance configuration")
5200 for dev, _, new_logical_id in iv_names.itervalues():
5201 dev.logical_id = new_logical_id
5202 cfg.SetDiskID(dev, pri_node)
5203 cfg.Update(instance)
5205 # and now perform the drbd attach
5206 info("attaching primary drbds to new secondary (standalone => connected)")
5207 result = self.rpc.call_drbd_attach_net([pri_node, new_node], nodes_ip,
5208 instance.disks, instance.name,
5210 for to_node, to_result in result.items():
5211 msg = to_result.RemoteFailMsg()
5213 warning("can't attach drbd disks on node %s: %s", to_node, msg,
5214 hint="please do a gnt-instance info to see the"
5217 # this can fail as the old devices are degraded and _WaitForSync
5218 # does a combined result over all disks, so we don't check its
5220 self.proc.LogStep(5, steps_total, "sync devices")
5221 _WaitForSync(self, instance, unlock=True)
5223 # so check manually all the devices
5224 for idx, (dev, old_lvs, _) in iv_names.iteritems():
5225 cfg.SetDiskID(dev, pri_node)
5226 result = self.rpc.call_blockdev_find(pri_node, dev)
5229 raise errors.OpExecError("DRBD device disk/%d is degraded!" % idx)
5231 self.proc.LogStep(6, steps_total, "removing old storage")
5232 for idx, (dev, old_lvs, _) in iv_names.iteritems():
5233 info("remove logical volumes for disk/%d" % idx)
5235 cfg.SetDiskID(lv, old_node)
5236 result = self.rpc.call_blockdev_remove(old_node, lv)
5237 if result.failed or not result.data:
5238 warning("Can't remove LV on old secondary",
5239 hint="Cleanup stale volumes by hand")
5241 def Exec(self, feedback_fn):
5242 """Execute disk replacement.
5244 This dispatches the disk replacement to the appropriate handler.
5247 instance = self.instance
5249 # Activate the instance disks if we're replacing them on a down instance
5250 if not instance.admin_up:
5251 _StartInstanceDisks(self, instance, True)
5253 if self.op.mode == constants.REPLACE_DISK_CHG:
5254 fn = self._ExecD8Secondary
5256 fn = self._ExecD8DiskOnly
5258 ret = fn(feedback_fn)
5260 # Deactivate the instance disks if we're replacing them on a down instance
5261 if not instance.admin_up:
5262 _SafeShutdownInstanceDisks(self, instance)
5267 class LUGrowDisk(LogicalUnit):
5268 """Grow a disk of an instance.
5272 HTYPE = constants.HTYPE_INSTANCE
5273 _OP_REQP = ["instance_name", "disk", "amount", "wait_for_sync"]
5276 def ExpandNames(self):
5277 self._ExpandAndLockInstance()
5278 self.needed_locks[locking.LEVEL_NODE] = []
5279 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
5281 def DeclareLocks(self, level):
5282 if level == locking.LEVEL_NODE:
5283 self._LockInstancesNodes()
5285 def BuildHooksEnv(self):
5288 This runs on the master, the primary and all the secondaries.
5292 "DISK": self.op.disk,
5293 "AMOUNT": self.op.amount,
5295 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
5297 self.cfg.GetMasterNode(),
5298 self.instance.primary_node,
5302 def CheckPrereq(self):
5303 """Check prerequisites.
5305 This checks that the instance is in the cluster.
5308 instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5309 assert instance is not None, \
5310 "Cannot retrieve locked instance %s" % self.op.instance_name
5311 nodenames = list(instance.all_nodes)
5312 for node in nodenames:
5313 _CheckNodeOnline(self, node)
5316 self.instance = instance
5318 if instance.disk_template not in (constants.DT_PLAIN, constants.DT_DRBD8):
5319 raise errors.OpPrereqError("Instance's disk layout does not support"
5322 self.disk = instance.FindDisk(self.op.disk)
5324 nodeinfo = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
5325 instance.hypervisor)
5326 for node in nodenames:
5327 info = nodeinfo[node]
5328 if info.failed or not info.data:
5329 raise errors.OpPrereqError("Cannot get current information"
5330 " from node '%s'" % node)
5331 vg_free = info.data.get('vg_free', None)
5332 if not isinstance(vg_free, int):
5333 raise errors.OpPrereqError("Can't compute free disk space on"
5335 if self.op.amount > vg_free:
5336 raise errors.OpPrereqError("Not enough disk space on target node %s:"
5337 " %d MiB available, %d MiB required" %
5338 (node, vg_free, self.op.amount))
5340 def Exec(self, feedback_fn):
5341 """Execute disk grow.
5344 instance = self.instance
5346 for node in instance.all_nodes:
5347 self.cfg.SetDiskID(disk, node)
5348 result = self.rpc.call_blockdev_grow(node, disk, self.op.amount)
5350 if (not result.data or not isinstance(result.data, (list, tuple)) or
5351 len(result.data) != 2):
5352 raise errors.OpExecError("Grow request failed to node %s" % node)
5353 elif not result.data[0]:
5354 raise errors.OpExecError("Grow request failed to node %s: %s" %
5355 (node, result.data[1]))
5356 disk.RecordGrow(self.op.amount)
5357 self.cfg.Update(instance)
5358 if self.op.wait_for_sync:
5359 disk_abort = not _WaitForSync(self, instance)
5361 self.proc.LogWarning("Warning: disk sync-ing has not returned a good"
5362 " status.\nPlease check the instance.")
5365 class LUQueryInstanceData(NoHooksLU):
5366 """Query runtime instance data.
5369 _OP_REQP = ["instances", "static"]
5372 def ExpandNames(self):
5373 self.needed_locks = {}
5374 self.share_locks = dict(((i, 1) for i in locking.LEVELS))
5376 if not isinstance(self.op.instances, list):
5377 raise errors.OpPrereqError("Invalid argument type 'instances'")
5379 if self.op.instances:
5380 self.wanted_names = []
5381 for name in self.op.instances:
5382 full_name = self.cfg.ExpandInstanceName(name)
5383 if full_name is None:
5384 raise errors.OpPrereqError("Instance '%s' not known" % name)
5385 self.wanted_names.append(full_name)
5386 self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted_names
5388 self.wanted_names = None
5389 self.needed_locks[locking.LEVEL_INSTANCE] = locking.ALL_SET
5391 self.needed_locks[locking.LEVEL_NODE] = []
5392 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
5394 def DeclareLocks(self, level):
5395 if level == locking.LEVEL_NODE:
5396 self._LockInstancesNodes()
5398 def CheckPrereq(self):
5399 """Check prerequisites.
5401 This only checks the optional instance list against the existing names.
5404 if self.wanted_names is None:
5405 self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
5407 self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
5408 in self.wanted_names]
5411 def _ComputeDiskStatus(self, instance, snode, dev):
5412 """Compute block device status.
5415 static = self.op.static
5417 self.cfg.SetDiskID(dev, instance.primary_node)
5418 dev_pstatus = self.rpc.call_blockdev_find(instance.primary_node, dev)
5420 dev_pstatus = dev_pstatus.data
5424 if dev.dev_type in constants.LDS_DRBD:
5425 # we change the snode then (otherwise we use the one passed in)
5426 if dev.logical_id[0] == instance.primary_node:
5427 snode = dev.logical_id[1]
5429 snode = dev.logical_id[0]
5431 if snode and not static:
5432 self.cfg.SetDiskID(dev, snode)
5433 dev_sstatus = self.rpc.call_blockdev_find(snode, dev)
5435 dev_sstatus = dev_sstatus.data
5440 dev_children = [self._ComputeDiskStatus(instance, snode, child)
5441 for child in dev.children]
5446 "iv_name": dev.iv_name,
5447 "dev_type": dev.dev_type,
5448 "logical_id": dev.logical_id,
5449 "physical_id": dev.physical_id,
5450 "pstatus": dev_pstatus,
5451 "sstatus": dev_sstatus,
5452 "children": dev_children,
5458 def Exec(self, feedback_fn):
5459 """Gather and return data"""
5462 cluster = self.cfg.GetClusterInfo()
5464 for instance in self.wanted_instances:
5465 if not self.op.static:
5466 remote_info = self.rpc.call_instance_info(instance.primary_node,
5468 instance.hypervisor)
5470 remote_info = remote_info.data
5471 if remote_info and "state" in remote_info:
5474 remote_state = "down"
5477 if instance.admin_up:
5480 config_state = "down"
5482 disks = [self._ComputeDiskStatus(instance, None, device)
5483 for device in instance.disks]
5486 "name": instance.name,
5487 "config_state": config_state,
5488 "run_state": remote_state,
5489 "pnode": instance.primary_node,
5490 "snodes": instance.secondary_nodes,
5492 "nics": [(nic.mac, nic.ip, nic.bridge) for nic in instance.nics],
5494 "hypervisor": instance.hypervisor,
5495 "network_port": instance.network_port,
5496 "hv_instance": instance.hvparams,
5497 "hv_actual": cluster.FillHV(instance),
5498 "be_instance": instance.beparams,
5499 "be_actual": cluster.FillBE(instance),
5502 result[instance.name] = idict
5507 class LUSetInstanceParams(LogicalUnit):
5508 """Modifies an instances's parameters.
5511 HPATH = "instance-modify"
5512 HTYPE = constants.HTYPE_INSTANCE
5513 _OP_REQP = ["instance_name"]
5516 def CheckArguments(self):
5517 if not hasattr(self.op, 'nics'):
5519 if not hasattr(self.op, 'disks'):
5521 if not hasattr(self.op, 'beparams'):
5522 self.op.beparams = {}
5523 if not hasattr(self.op, 'hvparams'):
5524 self.op.hvparams = {}
5525 self.op.force = getattr(self.op, "force", False)
5526 if not (self.op.nics or self.op.disks or
5527 self.op.hvparams or self.op.beparams):
5528 raise errors.OpPrereqError("No changes submitted")
5530 utils.CheckBEParams(self.op.beparams)
5534 for disk_op, disk_dict in self.op.disks:
5535 if disk_op == constants.DDM_REMOVE:
5538 elif disk_op == constants.DDM_ADD:
5541 if not isinstance(disk_op, int):
5542 raise errors.OpPrereqError("Invalid disk index")
5543 if disk_op == constants.DDM_ADD:
5544 mode = disk_dict.setdefault('mode', constants.DISK_RDWR)
5545 if mode not in constants.DISK_ACCESS_SET:
5546 raise errors.OpPrereqError("Invalid disk access mode '%s'" % mode)
5547 size = disk_dict.get('size', None)
5549 raise errors.OpPrereqError("Required disk parameter size missing")
5552 except ValueError, err:
5553 raise errors.OpPrereqError("Invalid disk size parameter: %s" %
5555 disk_dict['size'] = size
5557 # modification of disk
5558 if 'size' in disk_dict:
5559 raise errors.OpPrereqError("Disk size change not possible, use"
5562 if disk_addremove > 1:
5563 raise errors.OpPrereqError("Only one disk add or remove operation"
5564 " supported at a time")
5568 for nic_op, nic_dict in self.op.nics:
5569 if nic_op == constants.DDM_REMOVE:
5572 elif nic_op == constants.DDM_ADD:
5575 if not isinstance(nic_op, int):
5576 raise errors.OpPrereqError("Invalid nic index")
5578 # nic_dict should be a dict
5579 nic_ip = nic_dict.get('ip', None)
5580 if nic_ip is not None:
5581 if nic_ip.lower() == "none":
5582 nic_dict['ip'] = None
5584 if not utils.IsValidIP(nic_ip):
5585 raise errors.OpPrereqError("Invalid IP address '%s'" % nic_ip)
5586 # we can only check None bridges and assign the default one
5587 nic_bridge = nic_dict.get('bridge', None)
5588 if nic_bridge is None:
5589 nic_dict['bridge'] = self.cfg.GetDefBridge()
5590 # but we can validate MACs
5591 nic_mac = nic_dict.get('mac', None)
5592 if nic_mac is not None:
5593 if self.cfg.IsMacInUse(nic_mac):
5594 raise errors.OpPrereqError("MAC address %s already in use"
5595 " in cluster" % nic_mac)
5596 if nic_mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
5597 if not utils.IsValidMac(nic_mac):
5598 raise errors.OpPrereqError("Invalid MAC address %s" % nic_mac)
5599 if nic_addremove > 1:
5600 raise errors.OpPrereqError("Only one NIC add or remove operation"
5601 " supported at a time")
5603 def ExpandNames(self):
5604 self._ExpandAndLockInstance()
5605 self.needed_locks[locking.LEVEL_NODE] = []
5606 self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
5608 def DeclareLocks(self, level):
5609 if level == locking.LEVEL_NODE:
5610 self._LockInstancesNodes()
5612 def BuildHooksEnv(self):
5615 This runs on the master, primary and secondaries.
5619 if constants.BE_MEMORY in self.be_new:
5620 args['memory'] = self.be_new[constants.BE_MEMORY]
5621 if constants.BE_VCPUS in self.be_new:
5622 args['vcpus'] = self.be_new[constants.BE_VCPUS]
5623 # FIXME: readd disk/nic changes
5624 env = _BuildInstanceHookEnvByObject(self, self.instance, override=args)
5625 nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
5628 def CheckPrereq(self):
5629 """Check prerequisites.
5631 This only checks the instance list against the existing names.
5634 force = self.force = self.op.force
5636 # checking the new params on the primary/secondary nodes
5638 instance = self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5639 assert self.instance is not None, \
5640 "Cannot retrieve locked instance %s" % self.op.instance_name
5641 pnode = instance.primary_node
5642 nodelist = list(instance.all_nodes)
5644 # hvparams processing
5645 if self.op.hvparams:
5646 i_hvdict = copy.deepcopy(instance.hvparams)
5647 for key, val in self.op.hvparams.iteritems():
5648 if val == constants.VALUE_DEFAULT:
5653 elif val == constants.VALUE_NONE:
5654 i_hvdict[key] = None
5657 cluster = self.cfg.GetClusterInfo()
5658 hv_new = cluster.FillDict(cluster.hvparams[instance.hypervisor],
5661 hypervisor.GetHypervisor(
5662 instance.hypervisor).CheckParameterSyntax(hv_new)
5663 _CheckHVParams(self, nodelist, instance.hypervisor, hv_new)
5664 self.hv_new = hv_new # the new actual values
5665 self.hv_inst = i_hvdict # the new dict (without defaults)
5667 self.hv_new = self.hv_inst = {}
5669 # beparams processing
5670 if self.op.beparams:
5671 i_bedict = copy.deepcopy(instance.beparams)
5672 for key, val in self.op.beparams.iteritems():
5673 if val == constants.VALUE_DEFAULT:
5680 cluster = self.cfg.GetClusterInfo()
5681 be_new = cluster.FillDict(cluster.beparams[constants.BEGR_DEFAULT],
5683 self.be_new = be_new # the new actual values
5684 self.be_inst = i_bedict # the new dict (without defaults)
5686 self.be_new = self.be_inst = {}
5690 if constants.BE_MEMORY in self.op.beparams and not self.force:
5691 mem_check_list = [pnode]
5692 if be_new[constants.BE_AUTO_BALANCE]:
5693 # either we changed auto_balance to yes or it was from before
5694 mem_check_list.extend(instance.secondary_nodes)
5695 instance_info = self.rpc.call_instance_info(pnode, instance.name,
5696 instance.hypervisor)
5697 nodeinfo = self.rpc.call_node_info(mem_check_list, self.cfg.GetVGName(),
5698 instance.hypervisor)
5699 if nodeinfo[pnode].failed or not isinstance(nodeinfo[pnode].data, dict):
5700 # Assume the primary node is unreachable and go ahead
5701 self.warn.append("Can't get info from primary node %s" % pnode)
5703 if not instance_info.failed and instance_info.data:
5704 current_mem = instance_info.data['memory']
5706 # Assume instance not running
5707 # (there is a slight race condition here, but it's not very probable,
5708 # and we have no other way to check)
5710 miss_mem = (be_new[constants.BE_MEMORY] - current_mem -
5711 nodeinfo[pnode].data['memory_free'])
5713 raise errors.OpPrereqError("This change will prevent the instance"
5714 " from starting, due to %d MB of memory"
5715 " missing on its primary node" % miss_mem)
5717 if be_new[constants.BE_AUTO_BALANCE]:
5718 for node, nres in nodeinfo.iteritems():
5719 if node not in instance.secondary_nodes:
5721 if nres.failed or not isinstance(nres.data, dict):
5722 self.warn.append("Can't get info from secondary node %s" % node)
5723 elif be_new[constants.BE_MEMORY] > nres.data['memory_free']:
5724 self.warn.append("Not enough memory to failover instance to"
5725 " secondary node %s" % node)
5728 for nic_op, nic_dict in self.op.nics:
5729 if nic_op == constants.DDM_REMOVE:
5730 if not instance.nics:
5731 raise errors.OpPrereqError("Instance has no NICs, cannot remove")
5733 if nic_op != constants.DDM_ADD:
5735 if nic_op < 0 or nic_op >= len(instance.nics):
5736 raise errors.OpPrereqError("Invalid NIC index %s, valid values"
5738 (nic_op, len(instance.nics)))
5739 nic_bridge = nic_dict.get('bridge', None)
5740 if nic_bridge is not None:
5741 if not self.rpc.call_bridges_exist(pnode, [nic_bridge]):
5742 msg = ("Bridge '%s' doesn't exist on one of"
5743 " the instance nodes" % nic_bridge)
5745 self.warn.append(msg)
5747 raise errors.OpPrereqError(msg)
5750 if self.op.disks and instance.disk_template == constants.DT_DISKLESS:
5751 raise errors.OpPrereqError("Disk operations not supported for"
5752 " diskless instances")
5753 for disk_op, disk_dict in self.op.disks:
5754 if disk_op == constants.DDM_REMOVE:
5755 if len(instance.disks) == 1:
5756 raise errors.OpPrereqError("Cannot remove the last disk of"
5758 ins_l = self.rpc.call_instance_list([pnode], [instance.hypervisor])
5759 ins_l = ins_l[pnode]
5760 if ins_l.failed or not isinstance(ins_l.data, list):
5761 raise errors.OpPrereqError("Can't contact node '%s'" % pnode)
5762 if instance.name in ins_l.data:
5763 raise errors.OpPrereqError("Instance is running, can't remove"
5766 if (disk_op == constants.DDM_ADD and
5767 len(instance.nics) >= constants.MAX_DISKS):
5768 raise errors.OpPrereqError("Instance has too many disks (%d), cannot"
5769 " add more" % constants.MAX_DISKS)
5770 if disk_op not in (constants.DDM_ADD, constants.DDM_REMOVE):
5772 if disk_op < 0 or disk_op >= len(instance.disks):
5773 raise errors.OpPrereqError("Invalid disk index %s, valid values"
5775 (disk_op, len(instance.disks)))
5779 def Exec(self, feedback_fn):
5780 """Modifies an instance.
5782 All parameters take effect only at the next restart of the instance.
5785 # Process here the warnings from CheckPrereq, as we don't have a
5786 # feedback_fn there.
5787 for warn in self.warn:
5788 feedback_fn("WARNING: %s" % warn)
5791 instance = self.instance
5793 for disk_op, disk_dict in self.op.disks:
5794 if disk_op == constants.DDM_REMOVE:
5795 # remove the last disk
5796 device = instance.disks.pop()
5797 device_idx = len(instance.disks)
5798 for node, disk in device.ComputeNodeTree(instance.primary_node):
5799 self.cfg.SetDiskID(disk, node)
5800 rpc_result = self.rpc.call_blockdev_remove(node, disk)
5801 if rpc_result.failed or not rpc_result.data:
5802 self.proc.LogWarning("Could not remove disk/%d on node %s,"
5803 " continuing anyway", device_idx, node)
5804 result.append(("disk/%d" % device_idx, "remove"))
5805 elif disk_op == constants.DDM_ADD:
5807 if instance.disk_template == constants.DT_FILE:
5808 file_driver, file_path = instance.disks[0].logical_id
5809 file_path = os.path.dirname(file_path)
5811 file_driver = file_path = None
5812 disk_idx_base = len(instance.disks)
5813 new_disk = _GenerateDiskTemplate(self,
5814 instance.disk_template,
5815 instance.name, instance.primary_node,
5816 instance.secondary_nodes,
5821 instance.disks.append(new_disk)
5822 info = _GetInstanceInfoText(instance)
5824 logging.info("Creating volume %s for instance %s",
5825 new_disk.iv_name, instance.name)
5826 # Note: this needs to be kept in sync with _CreateDisks
5828 for node in instance.all_nodes:
5829 f_create = node == instance.primary_node
5831 _CreateBlockDev(self, node, instance, new_disk,
5832 f_create, info, f_create)
5833 except errors.OpExecError, err:
5834 self.LogWarning("Failed to create volume %s (%s) on"
5836 new_disk.iv_name, new_disk, node, err)
5837 result.append(("disk/%d" % disk_idx_base, "add:size=%s,mode=%s" %
5838 (new_disk.size, new_disk.mode)))
5840 # change a given disk
5841 instance.disks[disk_op].mode = disk_dict['mode']
5842 result.append(("disk.mode/%d" % disk_op, disk_dict['mode']))
5844 for nic_op, nic_dict in self.op.nics:
5845 if nic_op == constants.DDM_REMOVE:
5846 # remove the last nic
5847 del instance.nics[-1]
5848 result.append(("nic.%d" % len(instance.nics), "remove"))
5849 elif nic_op == constants.DDM_ADD:
5851 if 'mac' not in nic_dict:
5852 mac = constants.VALUE_GENERATE
5854 mac = nic_dict['mac']
5855 if mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
5856 mac = self.cfg.GenerateMAC()
5857 new_nic = objects.NIC(mac=mac, ip=nic_dict.get('ip', None),
5858 bridge=nic_dict.get('bridge', None))
5859 instance.nics.append(new_nic)
5860 result.append(("nic.%d" % (len(instance.nics) - 1),
5861 "add:mac=%s,ip=%s,bridge=%s" %
5862 (new_nic.mac, new_nic.ip, new_nic.bridge)))
5864 # change a given nic
5865 for key in 'mac', 'ip', 'bridge':
5867 setattr(instance.nics[nic_op], key, nic_dict[key])
5868 result.append(("nic.%s/%d" % (key, nic_op), nic_dict[key]))
5871 if self.op.hvparams:
5872 instance.hvparams = self.hv_new
5873 for key, val in self.op.hvparams.iteritems():
5874 result.append(("hv/%s" % key, val))
5877 if self.op.beparams:
5878 instance.beparams = self.be_inst
5879 for key, val in self.op.beparams.iteritems():
5880 result.append(("be/%s" % key, val))
5882 self.cfg.Update(instance)
5887 class LUQueryExports(NoHooksLU):
5888 """Query the exports list
5891 _OP_REQP = ['nodes']
5894 def ExpandNames(self):
5895 self.needed_locks = {}
5896 self.share_locks[locking.LEVEL_NODE] = 1
5897 if not self.op.nodes:
5898 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
5900 self.needed_locks[locking.LEVEL_NODE] = \
5901 _GetWantedNodes(self, self.op.nodes)
5903 def CheckPrereq(self):
5904 """Check prerequisites.
5907 self.nodes = self.acquired_locks[locking.LEVEL_NODE]
5909 def Exec(self, feedback_fn):
5910 """Compute the list of all the exported system images.
5913 @return: a dictionary with the structure node->(export-list)
5914 where export-list is a list of the instances exported on
5918 rpcresult = self.rpc.call_export_list(self.nodes)
5920 for node in rpcresult:
5921 if rpcresult[node].failed:
5922 result[node] = False
5924 result[node] = rpcresult[node].data
5929 class LUExportInstance(LogicalUnit):
5930 """Export an instance to an image in the cluster.
5933 HPATH = "instance-export"
5934 HTYPE = constants.HTYPE_INSTANCE
5935 _OP_REQP = ["instance_name", "target_node", "shutdown"]
5938 def ExpandNames(self):
5939 self._ExpandAndLockInstance()
5940 # FIXME: lock only instance primary and destination node
5942 # Sad but true, for now we have do lock all nodes, as we don't know where
5943 # the previous export might be, and and in this LU we search for it and
5944 # remove it from its current node. In the future we could fix this by:
5945 # - making a tasklet to search (share-lock all), then create the new one,
5946 # then one to remove, after
5947 # - removing the removal operation altoghether
5948 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
5950 def DeclareLocks(self, level):
5951 """Last minute lock declaration."""
5952 # All nodes are locked anyway, so nothing to do here.
5954 def BuildHooksEnv(self):
5957 This will run on the master, primary node and target node.
5961 "EXPORT_NODE": self.op.target_node,
5962 "EXPORT_DO_SHUTDOWN": self.op.shutdown,
5964 env.update(_BuildInstanceHookEnvByObject(self, self.instance))
5965 nl = [self.cfg.GetMasterNode(), self.instance.primary_node,
5966 self.op.target_node]
5969 def CheckPrereq(self):
5970 """Check prerequisites.
5972 This checks that the instance and node names are valid.
5975 instance_name = self.op.instance_name
5976 self.instance = self.cfg.GetInstanceInfo(instance_name)
5977 assert self.instance is not None, \
5978 "Cannot retrieve locked instance %s" % self.op.instance_name
5979 _CheckNodeOnline(self, self.instance.primary_node)
5981 self.dst_node = self.cfg.GetNodeInfo(
5982 self.cfg.ExpandNodeName(self.op.target_node))
5984 if self.dst_node is None:
5985 # This is wrong node name, not a non-locked node
5986 raise errors.OpPrereqError("Wrong node name %s" % self.op.target_node)
5987 _CheckNodeOnline(self, self.dst_node.name)
5989 # instance disk type verification
5990 for disk in self.instance.disks:
5991 if disk.dev_type == constants.LD_FILE:
5992 raise errors.OpPrereqError("Export not supported for instances with"
5993 " file-based disks")
5995 def Exec(self, feedback_fn):
5996 """Export an instance to an image in the cluster.
5999 instance = self.instance
6000 dst_node = self.dst_node
6001 src_node = instance.primary_node
6002 if self.op.shutdown:
6003 # shutdown the instance, but not the disks
6004 result = self.rpc.call_instance_shutdown(src_node, instance)
6007 raise errors.OpExecError("Could not shutdown instance %s on node %s" %
6008 (instance.name, src_node))
6010 vgname = self.cfg.GetVGName()
6014 # set the disks ID correctly since call_instance_start needs the
6015 # correct drbd minor to create the symlinks
6016 for disk in instance.disks:
6017 self.cfg.SetDiskID(disk, src_node)
6020 for disk in instance.disks:
6021 # new_dev_name will be a snapshot of an lvm leaf of the one we passed
6022 new_dev_name = self.rpc.call_blockdev_snapshot(src_node, disk)
6023 if new_dev_name.failed or not new_dev_name.data:
6024 self.LogWarning("Could not snapshot block device %s on node %s",
6025 disk.logical_id[1], src_node)
6026 snap_disks.append(False)
6028 new_dev = objects.Disk(dev_type=constants.LD_LV, size=disk.size,
6029 logical_id=(vgname, new_dev_name.data),
6030 physical_id=(vgname, new_dev_name.data),
6031 iv_name=disk.iv_name)
6032 snap_disks.append(new_dev)
6035 if self.op.shutdown and instance.admin_up:
6036 result = self.rpc.call_instance_start(src_node, instance, None)
6037 msg = result.RemoteFailMsg()
6039 _ShutdownInstanceDisks(self, instance)
6040 raise errors.OpExecError("Could not start instance: %s" % msg)
6042 # TODO: check for size
6044 cluster_name = self.cfg.GetClusterName()
6045 for idx, dev in enumerate(snap_disks):
6047 result = self.rpc.call_snapshot_export(src_node, dev, dst_node.name,
6048 instance, cluster_name, idx)
6049 if result.failed or not result.data:
6050 self.LogWarning("Could not export block device %s from node %s to"
6051 " node %s", dev.logical_id[1], src_node,
6053 result = self.rpc.call_blockdev_remove(src_node, dev)
6054 if result.failed or not result.data:
6055 self.LogWarning("Could not remove snapshot block device %s from node"
6056 " %s", dev.logical_id[1], src_node)
6058 result = self.rpc.call_finalize_export(dst_node.name, instance, snap_disks)
6059 if result.failed or not result.data:
6060 self.LogWarning("Could not finalize export for instance %s on node %s",
6061 instance.name, dst_node.name)
6063 nodelist = self.cfg.GetNodeList()
6064 nodelist.remove(dst_node.name)
6066 # on one-node clusters nodelist will be empty after the removal
6067 # if we proceed the backup would be removed because OpQueryExports
6068 # substitutes an empty list with the full cluster node list.
6070 exportlist = self.rpc.call_export_list(nodelist)
6071 for node in exportlist:
6072 if exportlist[node].failed:
6074 if instance.name in exportlist[node].data:
6075 if not self.rpc.call_export_remove(node, instance.name):
6076 self.LogWarning("Could not remove older export for instance %s"
6077 " on node %s", instance.name, node)
6080 class LURemoveExport(NoHooksLU):
6081 """Remove exports related to the named instance.
6084 _OP_REQP = ["instance_name"]
6087 def ExpandNames(self):
6088 self.needed_locks = {}
6089 # We need all nodes to be locked in order for RemoveExport to work, but we
6090 # don't need to lock the instance itself, as nothing will happen to it (and
6091 # we can remove exports also for a removed instance)
6092 self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
6094 def CheckPrereq(self):
6095 """Check prerequisites.
6099 def Exec(self, feedback_fn):
6100 """Remove any export.
6103 instance_name = self.cfg.ExpandInstanceName(self.op.instance_name)
6104 # If the instance was not found we'll try with the name that was passed in.
6105 # This will only work if it was an FQDN, though.
6107 if not instance_name:
6109 instance_name = self.op.instance_name
6111 exportlist = self.rpc.call_export_list(self.acquired_locks[
6112 locking.LEVEL_NODE])
6114 for node in exportlist:
6115 if exportlist[node].failed:
6116 self.LogWarning("Failed to query node %s, continuing" % node)
6118 if instance_name in exportlist[node].data:
6120 result = self.rpc.call_export_remove(node, instance_name)
6121 if result.failed or not result.data:
6122 logging.error("Could not remove export for instance %s"
6123 " on node %s", instance_name, node)
6125 if fqdn_warn and not found:
6126 feedback_fn("Export not found. If trying to remove an export belonging"
6127 " to a deleted instance please use its Fully Qualified"
6131 class TagsLU(NoHooksLU):
6134 This is an abstract class which is the parent of all the other tags LUs.
6138 def ExpandNames(self):
6139 self.needed_locks = {}
6140 if self.op.kind == constants.TAG_NODE:
6141 name = self.cfg.ExpandNodeName(self.op.name)
6143 raise errors.OpPrereqError("Invalid node name (%s)" %
6146 self.needed_locks[locking.LEVEL_NODE] = name
6147 elif self.op.kind == constants.TAG_INSTANCE:
6148 name = self.cfg.ExpandInstanceName(self.op.name)
6150 raise errors.OpPrereqError("Invalid instance name (%s)" %
6153 self.needed_locks[locking.LEVEL_INSTANCE] = name
6155 def CheckPrereq(self):
6156 """Check prerequisites.
6159 if self.op.kind == constants.TAG_CLUSTER:
6160 self.target = self.cfg.GetClusterInfo()
6161 elif self.op.kind == constants.TAG_NODE:
6162 self.target = self.cfg.GetNodeInfo(self.op.name)
6163 elif self.op.kind == constants.TAG_INSTANCE:
6164 self.target = self.cfg.GetInstanceInfo(self.op.name)
6166 raise errors.OpPrereqError("Wrong tag type requested (%s)" %
6170 class LUGetTags(TagsLU):
6171 """Returns the tags of a given object.
6174 _OP_REQP = ["kind", "name"]
6177 def Exec(self, feedback_fn):
6178 """Returns the tag list.
6181 return list(self.target.GetTags())
6184 class LUSearchTags(NoHooksLU):
6185 """Searches the tags for a given pattern.
6188 _OP_REQP = ["pattern"]
6191 def ExpandNames(self):
6192 self.needed_locks = {}
6194 def CheckPrereq(self):
6195 """Check prerequisites.
6197 This checks the pattern passed for validity by compiling it.
6201 self.re = re.compile(self.op.pattern)
6202 except re.error, err:
6203 raise errors.OpPrereqError("Invalid search pattern '%s': %s" %
6204 (self.op.pattern, err))
6206 def Exec(self, feedback_fn):
6207 """Returns the tag list.
6211 tgts = [("/cluster", cfg.GetClusterInfo())]
6212 ilist = cfg.GetAllInstancesInfo().values()
6213 tgts.extend([("/instances/%s" % i.name, i) for i in ilist])
6214 nlist = cfg.GetAllNodesInfo().values()
6215 tgts.extend([("/nodes/%s" % n.name, n) for n in nlist])
6217 for path, target in tgts:
6218 for tag in target.GetTags():
6219 if self.re.search(tag):
6220 results.append((path, tag))
6224 class LUAddTags(TagsLU):
6225 """Sets a tag on a given object.
6228 _OP_REQP = ["kind", "name", "tags"]
6231 def CheckPrereq(self):
6232 """Check prerequisites.
6234 This checks the type and length of the tag name and value.
6237 TagsLU.CheckPrereq(self)
6238 for tag in self.op.tags:
6239 objects.TaggableObject.ValidateTag(tag)
6241 def Exec(self, feedback_fn):
6246 for tag in self.op.tags:
6247 self.target.AddTag(tag)
6248 except errors.TagError, err:
6249 raise errors.OpExecError("Error while setting tag: %s" % str(err))
6251 self.cfg.Update(self.target)
6252 except errors.ConfigurationError:
6253 raise errors.OpRetryError("There has been a modification to the"
6254 " config file and the operation has been"
6255 " aborted. Please retry.")
6258 class LUDelTags(TagsLU):
6259 """Delete a list of tags from a given object.
6262 _OP_REQP = ["kind", "name", "tags"]
6265 def CheckPrereq(self):
6266 """Check prerequisites.
6268 This checks that we have the given tag.
6271 TagsLU.CheckPrereq(self)
6272 for tag in self.op.tags:
6273 objects.TaggableObject.ValidateTag(tag)
6274 del_tags = frozenset(self.op.tags)
6275 cur_tags = self.target.GetTags()
6276 if not del_tags <= cur_tags:
6277 diff_tags = del_tags - cur_tags
6278 diff_names = ["'%s'" % tag for tag in diff_tags]
6280 raise errors.OpPrereqError("Tag(s) %s not found" %
6281 (",".join(diff_names)))
6283 def Exec(self, feedback_fn):
6284 """Remove the tag from the object.
6287 for tag in self.op.tags:
6288 self.target.RemoveTag(tag)
6290 self.cfg.Update(self.target)
6291 except errors.ConfigurationError:
6292 raise errors.OpRetryError("There has been a modification to the"
6293 " config file and the operation has been"
6294 " aborted. Please retry.")
6297 class LUTestDelay(NoHooksLU):
6298 """Sleep for a specified amount of time.
6300 This LU sleeps on the master and/or nodes for a specified amount of
6304 _OP_REQP = ["duration", "on_master", "on_nodes"]
6307 def ExpandNames(self):
6308 """Expand names and set required locks.
6310 This expands the node list, if any.
6313 self.needed_locks = {}
6314 if self.op.on_nodes:
6315 # _GetWantedNodes can be used here, but is not always appropriate to use
6316 # this way in ExpandNames. Check LogicalUnit.ExpandNames docstring for
6318 self.op.on_nodes = _GetWantedNodes(self, self.op.on_nodes)
6319 self.needed_locks[locking.LEVEL_NODE] = self.op.on_nodes
6321 def CheckPrereq(self):
6322 """Check prerequisites.
6326 def Exec(self, feedback_fn):
6327 """Do the actual sleep.
6330 if self.op.on_master:
6331 if not utils.TestDelay(self.op.duration):
6332 raise errors.OpExecError("Error during master delay test")
6333 if self.op.on_nodes:
6334 result = self.rpc.call_test_delay(self.op.on_nodes, self.op.duration)
6336 raise errors.OpExecError("Complete failure from rpc call")
6337 for node, node_result in result.items():
6339 if not node_result.data:
6340 raise errors.OpExecError("Failure during rpc call to node %s,"
6341 " result: %s" % (node, node_result.data))
6344 class IAllocator(object):
6345 """IAllocator framework.
6347 An IAllocator instance has three sets of attributes:
6348 - cfg that is needed to query the cluster
6349 - input data (all members of the _KEYS class attribute are required)
6350 - four buffer attributes (in|out_data|text), that represent the
6351 input (to the external script) in text and data structure format,
6352 and the output from it, again in two formats
6353 - the result variables from the script (success, info, nodes) for
6358 "mem_size", "disks", "disk_template",
6359 "os", "tags", "nics", "vcpus", "hypervisor",
6365 def __init__(self, lu, mode, name, **kwargs):
6367 # init buffer variables
6368 self.in_text = self.out_text = self.in_data = self.out_data = None
6369 # init all input fields so that pylint is happy
6372 self.mem_size = self.disks = self.disk_template = None
6373 self.os = self.tags = self.nics = self.vcpus = None
6374 self.hypervisor = None
6375 self.relocate_from = None
6377 self.required_nodes = None
6378 # init result fields
6379 self.success = self.info = self.nodes = None
6380 if self.mode == constants.IALLOCATOR_MODE_ALLOC:
6381 keyset = self._ALLO_KEYS
6382 elif self.mode == constants.IALLOCATOR_MODE_RELOC:
6383 keyset = self._RELO_KEYS
6385 raise errors.ProgrammerError("Unknown mode '%s' passed to the"
6386 " IAllocator" % self.mode)
6388 if key not in keyset:
6389 raise errors.ProgrammerError("Invalid input parameter '%s' to"
6390 " IAllocator" % key)
6391 setattr(self, key, kwargs[key])
6393 if key not in kwargs:
6394 raise errors.ProgrammerError("Missing input parameter '%s' to"
6395 " IAllocator" % key)
6396 self._BuildInputData()
6398 def _ComputeClusterData(self):
6399 """Compute the generic allocator input data.
6401 This is the data that is independent of the actual operation.
6405 cluster_info = cfg.GetClusterInfo()
6409 "cluster_name": cfg.GetClusterName(),
6410 "cluster_tags": list(cluster_info.GetTags()),
6411 "enabled_hypervisors": list(cluster_info.enabled_hypervisors),
6412 # we don't have job IDs
6414 iinfo = cfg.GetAllInstancesInfo().values()
6415 i_list = [(inst, cluster_info.FillBE(inst)) for inst in iinfo]
6419 node_list = cfg.GetNodeList()
6421 if self.mode == constants.IALLOCATOR_MODE_ALLOC:
6422 hypervisor_name = self.hypervisor
6423 elif self.mode == constants.IALLOCATOR_MODE_RELOC:
6424 hypervisor_name = cfg.GetInstanceInfo(self.name).hypervisor
6426 node_data = self.lu.rpc.call_node_info(node_list, cfg.GetVGName(),
6428 node_iinfo = self.lu.rpc.call_all_instances_info(node_list,
6429 cluster_info.enabled_hypervisors)
6430 for nname, nresult in node_data.items():
6431 # first fill in static (config-based) values
6432 ninfo = cfg.GetNodeInfo(nname)
6434 "tags": list(ninfo.GetTags()),
6435 "primary_ip": ninfo.primary_ip,
6436 "secondary_ip": ninfo.secondary_ip,
6437 "offline": ninfo.offline,
6438 "master_candidate": ninfo.master_candidate,
6441 if not ninfo.offline:
6443 if not isinstance(nresult.data, dict):
6444 raise errors.OpExecError("Can't get data for node %s" % nname)
6445 remote_info = nresult.data
6446 for attr in ['memory_total', 'memory_free', 'memory_dom0',
6447 'vg_size', 'vg_free', 'cpu_total']:
6448 if attr not in remote_info:
6449 raise errors.OpExecError("Node '%s' didn't return attribute"
6450 " '%s'" % (nname, attr))
6452 remote_info[attr] = int(remote_info[attr])
6453 except ValueError, err:
6454 raise errors.OpExecError("Node '%s' returned invalid value"
6455 " for '%s': %s" % (nname, attr, err))
6456 # compute memory used by primary instances
6457 i_p_mem = i_p_up_mem = 0
6458 for iinfo, beinfo in i_list:
6459 if iinfo.primary_node == nname:
6460 i_p_mem += beinfo[constants.BE_MEMORY]
6461 if iinfo.name not in node_iinfo[nname].data:
6464 i_used_mem = int(node_iinfo[nname].data[iinfo.name]['memory'])
6465 i_mem_diff = beinfo[constants.BE_MEMORY] - i_used_mem
6466 remote_info['memory_free'] -= max(0, i_mem_diff)
6469 i_p_up_mem += beinfo[constants.BE_MEMORY]
6471 # compute memory used by instances
6473 "total_memory": remote_info['memory_total'],
6474 "reserved_memory": remote_info['memory_dom0'],
6475 "free_memory": remote_info['memory_free'],
6476 "total_disk": remote_info['vg_size'],
6477 "free_disk": remote_info['vg_free'],
6478 "total_cpus": remote_info['cpu_total'],
6479 "i_pri_memory": i_p_mem,
6480 "i_pri_up_memory": i_p_up_mem,
6484 node_results[nname] = pnr
6485 data["nodes"] = node_results
6489 for iinfo, beinfo in i_list:
6490 nic_data = [{"mac": n.mac, "ip": n.ip, "bridge": n.bridge}
6491 for n in iinfo.nics]
6493 "tags": list(iinfo.GetTags()),
6494 "admin_up": iinfo.admin_up,
6495 "vcpus": beinfo[constants.BE_VCPUS],
6496 "memory": beinfo[constants.BE_MEMORY],
6498 "nodes": [iinfo.primary_node] + list(iinfo.secondary_nodes),
6500 "disks": [{"size": dsk.size, "mode": dsk.mode} for dsk in iinfo.disks],
6501 "disk_template": iinfo.disk_template,
6502 "hypervisor": iinfo.hypervisor,
6504 instance_data[iinfo.name] = pir
6506 data["instances"] = instance_data
6510 def _AddNewInstance(self):
6511 """Add new instance data to allocator structure.
6513 This in combination with _AllocatorGetClusterData will create the
6514 correct structure needed as input for the allocator.
6516 The checks for the completeness of the opcode must have already been
6522 disk_space = _ComputeDiskSize(self.disk_template, self.disks)
6524 if self.disk_template in constants.DTS_NET_MIRROR:
6525 self.required_nodes = 2
6527 self.required_nodes = 1
6531 "disk_template": self.disk_template,
6534 "vcpus": self.vcpus,
6535 "memory": self.mem_size,
6536 "disks": self.disks,
6537 "disk_space_total": disk_space,
6539 "required_nodes": self.required_nodes,
6541 data["request"] = request
6543 def _AddRelocateInstance(self):
6544 """Add relocate instance data to allocator structure.
6546 This in combination with _IAllocatorGetClusterData will create the
6547 correct structure needed as input for the allocator.
6549 The checks for the completeness of the opcode must have already been
6553 instance = self.lu.cfg.GetInstanceInfo(self.name)
6554 if instance is None:
6555 raise errors.ProgrammerError("Unknown instance '%s' passed to"
6556 " IAllocator" % self.name)
6558 if instance.disk_template not in constants.DTS_NET_MIRROR:
6559 raise errors.OpPrereqError("Can't relocate non-mirrored instances")
6561 if len(instance.secondary_nodes) != 1:
6562 raise errors.OpPrereqError("Instance has not exactly one secondary node")
6564 self.required_nodes = 1
6565 disk_sizes = [{'size': disk.size} for disk in instance.disks]
6566 disk_space = _ComputeDiskSize(instance.disk_template, disk_sizes)
6571 "disk_space_total": disk_space,
6572 "required_nodes": self.required_nodes,
6573 "relocate_from": self.relocate_from,
6575 self.in_data["request"] = request
6577 def _BuildInputData(self):
6578 """Build input data structures.
6581 self._ComputeClusterData()
6583 if self.mode == constants.IALLOCATOR_MODE_ALLOC:
6584 self._AddNewInstance()
6586 self._AddRelocateInstance()
6588 self.in_text = serializer.Dump(self.in_data)
6590 def Run(self, name, validate=True, call_fn=None):
6591 """Run an instance allocator and return the results.
6595 call_fn = self.lu.rpc.call_iallocator_runner
6598 result = call_fn(self.lu.cfg.GetMasterNode(), name, self.in_text)
6601 if not isinstance(result.data, (list, tuple)) or len(result.data) != 4:
6602 raise errors.OpExecError("Invalid result from master iallocator runner")
6604 rcode, stdout, stderr, fail = result.data
6606 if rcode == constants.IARUN_NOTFOUND:
6607 raise errors.OpExecError("Can't find allocator '%s'" % name)
6608 elif rcode == constants.IARUN_FAILURE:
6609 raise errors.OpExecError("Instance allocator call failed: %s,"
6610 " output: %s" % (fail, stdout+stderr))
6611 self.out_text = stdout
6613 self._ValidateResult()
6615 def _ValidateResult(self):
6616 """Process the allocator results.
6618 This will process and if successful save the result in
6619 self.out_data and the other parameters.
6623 rdict = serializer.Load(self.out_text)
6624 except Exception, err:
6625 raise errors.OpExecError("Can't parse iallocator results: %s" % str(err))
6627 if not isinstance(rdict, dict):
6628 raise errors.OpExecError("Can't parse iallocator results: not a dict")
6630 for key in "success", "info", "nodes":
6631 if key not in rdict:
6632 raise errors.OpExecError("Can't parse iallocator results:"
6633 " missing key '%s'" % key)
6634 setattr(self, key, rdict[key])
6636 if not isinstance(rdict["nodes"], list):
6637 raise errors.OpExecError("Can't parse iallocator results: 'nodes' key"
6639 self.out_data = rdict
6642 class LUTestAllocator(NoHooksLU):
6643 """Run allocator tests.
6645 This LU runs the allocator tests
6648 _OP_REQP = ["direction", "mode", "name"]
6650 def CheckPrereq(self):
6651 """Check prerequisites.
6653 This checks the opcode parameters depending on the director and mode test.
6656 if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
6657 for attr in ["name", "mem_size", "disks", "disk_template",
6658 "os", "tags", "nics", "vcpus"]:
6659 if not hasattr(self.op, attr):
6660 raise errors.OpPrereqError("Missing attribute '%s' on opcode input" %
6662 iname = self.cfg.ExpandInstanceName(self.op.name)
6663 if iname is not None:
6664 raise errors.OpPrereqError("Instance '%s' already in the cluster" %
6666 if not isinstance(self.op.nics, list):
6667 raise errors.OpPrereqError("Invalid parameter 'nics'")
6668 for row in self.op.nics:
6669 if (not isinstance(row, dict) or
6672 "bridge" not in row):
6673 raise errors.OpPrereqError("Invalid contents of the"
6674 " 'nics' parameter")
6675 if not isinstance(self.op.disks, list):
6676 raise errors.OpPrereqError("Invalid parameter 'disks'")
6677 for row in self.op.disks:
6678 if (not isinstance(row, dict) or
6679 "size" not in row or
6680 not isinstance(row["size"], int) or
6681 "mode" not in row or
6682 row["mode"] not in ['r', 'w']):
6683 raise errors.OpPrereqError("Invalid contents of the"
6684 " 'disks' parameter")
6685 if not hasattr(self.op, "hypervisor") or self.op.hypervisor is None:
6686 self.op.hypervisor = self.cfg.GetHypervisorType()
6687 elif self.op.mode == constants.IALLOCATOR_MODE_RELOC:
6688 if not hasattr(self.op, "name"):
6689 raise errors.OpPrereqError("Missing attribute 'name' on opcode input")
6690 fname = self.cfg.ExpandInstanceName(self.op.name)
6692 raise errors.OpPrereqError("Instance '%s' not found for relocation" %
6694 self.op.name = fname
6695 self.relocate_from = self.cfg.GetInstanceInfo(fname).secondary_nodes
6697 raise errors.OpPrereqError("Invalid test allocator mode '%s'" %
6700 if self.op.direction == constants.IALLOCATOR_DIR_OUT:
6701 if not hasattr(self.op, "allocator") or self.op.allocator is None:
6702 raise errors.OpPrereqError("Missing allocator name")
6703 elif self.op.direction != constants.IALLOCATOR_DIR_IN:
6704 raise errors.OpPrereqError("Wrong allocator test '%s'" %
6707 def Exec(self, feedback_fn):
6708 """Run the allocator test.
6711 if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
6712 ial = IAllocator(self,
6715 mem_size=self.op.mem_size,
6716 disks=self.op.disks,
6717 disk_template=self.op.disk_template,
6721 vcpus=self.op.vcpus,
6722 hypervisor=self.op.hypervisor,
6725 ial = IAllocator(self,
6728 relocate_from=list(self.relocate_from),
6731 if self.op.direction == constants.IALLOCATOR_DIR_IN:
6732 result = ial.in_text
6734 ial.Run(self.op.allocator, validate=False)
6735 result = ial.out_text