예제 #1
0
    def ExecOpCode(self, op, cbs, timeout=None):
        """Execute an opcode.

    @type op: an OpCode instance
    @param op: the opcode to be executed
    @type cbs: L{OpExecCbBase}
    @param cbs: Runtime callbacks
    @type timeout: float or None
    @param timeout: Maximum time to acquire all locks, None for no timeout
    @raise LockAcquireTimeout: In case locks couldn't be acquired in specified
        amount of time

    """
        if not isinstance(op, opcodes.OpCode):
            raise errors.ProgrammerError("Non-opcode instance passed"
                                         " to ExecOpcode (%s)" % type(op))

        lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
        if lu_class is None:
            raise errors.OpCodeUnknown("Unknown opcode")

        if timeout is None:
            calc_timeout = lambda: None
        else:
            calc_timeout = utils.RunningTimeout(timeout, False).Remaining

        self._cbs = cbs
        try:
            result = self._PrepareLockListsAndExecLU(op, lu_class,
                                                     calc_timeout)

            # The post hooks below are always executed with a SUCCESS status because
            # all the possible errors during pre hooks and LU execution cause
            # exception and therefore the statement below will be skipped.
            if self._hm is not None:
                self._hm.RunPhase(
                    constants.HOOKS_PHASE_POST,
                    is_global=True,
                    post_status=constants.POST_HOOKS_STATUS_SUCCESS)
        except:
            # execute global post hooks with the failed status on any exception
            hooksmaster.ExecGlobalPostHooks(op.OP_ID,
                                            self.cfg.GetMasterNodeName(),
                                            self.rpc.call_hooks_runner,
                                            logging.warning,
                                            self.cfg.GetClusterName(),
                                            self.cfg.GetMasterNode(),
                                            self.GetECId(),
                                            constants.POST_HOOKS_STATUS_ERROR)
            raise

        self._CheckLUResult(op, result)
        return result
예제 #2
0
    def ExecOpCode(self, op, cbs, timeout=None):
        """Execute an opcode.

    @type op: an OpCode instance
    @param op: the opcode to be executed
    @type cbs: L{OpExecCbBase}
    @param cbs: Runtime callbacks
    @type timeout: float or None
    @param timeout: Maximum time to acquire all locks, None for no timeout
    @raise LockAcquireTimeout: In case locks couldn't be acquired in specified
        amount of time

    """
        if not isinstance(op, opcodes.OpCode):
            raise errors.ProgrammerError("Non-opcode instance passed"
                                         " to ExecOpcode (%s)" % type(op))

        lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
        if lu_class is None:
            raise errors.OpCodeUnknown("Unknown opcode")

        if timeout is None:
            calc_timeout = lambda: None
        else:
            calc_timeout = utils.RunningTimeout(timeout, False).Remaining

        self._cbs = cbs
        try:
            if self._enable_locks:
                # Acquire the Big Ganeti Lock exclusively if this LU requires it,
                # and in a shared fashion otherwise (to prevent concurrent run with
                # an exclusive LU.
                self._AcquireLocks(locking.LEVEL_CLUSTER, locking.BGL,
                                   not lu_class.REQ_BGL, False, calc_timeout())
            elif lu_class.REQ_BGL:
                raise errors.ProgrammerError(
                    "Opcode '%s' requires BGL, but locks are"
                    " disabled" % op.OP_ID)

            lu = lu_class(self, op, self.cfg, self.rpc, self._wconfdcontext,
                          self.wconfd)
            lu.wconfdlocks = self.wconfd.Client().ListLocks(
                self._wconfdcontext)
            _CheckSecretParameters(op)
            lu.ExpandNames()
            assert lu.needed_locks is not None, "needed_locks not set by LU"

            try:
                result = self._LockAndExecLU(lu, locking.LEVEL_CLUSTER + 1,
                                             calc_timeout)
            finally:
                if self._ec_id:
                    self.cfg.DropECReservations(self._ec_id)
        finally:
            self.wconfd.Client().FreeLocksLevel(
                self._wconfdcontext,
                locking.LEVEL_NAMES[locking.LEVEL_CLUSTER])
            self._cbs = None

        self._CheckLUResult(op, result)

        return result