def apply_pre_run_policies(lv_ac_db): LOG.debug('Applying pre-run policies for liveaction "%s".' % str(lv_ac_db.id)) policy_dbs = pc_db_access.Policy.query(resource_ref=lv_ac_db.action, enabled=True) LOG.debug('Identified %s policies for the action "%s".' % (len(policy_dbs), lv_ac_db.action)) for policy_db in policy_dbs: LOG.debug('Getting driver for policy "%s" (%s).' % (policy_db.ref, policy_db.policy_type)) driver = engine.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: message = 'Applying policy "%s" (%s) for liveaction "%s".' LOG.info(message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id))) lv_ac_db = driver.apply_before(lv_ac_db) except: message = 'An exception occurred while applying policy "%s" (%s) for liveaction "%s".' LOG.exception( message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id))) if lv_ac_db.status == ac_const.LIVEACTION_STATUS_POLICY_DELAYED: break return lv_ac_db
def apply_post_run_policies(lv_ac_db): LOG.debug('Applying post run policies for liveaction "%s".' % str(lv_ac_db.id)) policy_dbs = pc_db_access.Policy.query(resource_ref=lv_ac_db.action, enabled=True) LOG.debug('Identified %s policies for the action "%s".' % (len(policy_dbs), lv_ac_db.action)) for policy_db in policy_dbs: LOG.debug('Getting driver for policy "%s" (%s).' % (policy_db.ref, policy_db.policy_type)) driver = engine.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: message = 'Applying policy "%s" (%s) for liveaction "%s".' LOG.info(message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id))) lv_ac_db = driver.apply_after(lv_ac_db) except: message = 'An exception occurred while applying policy "%s" (%s) for liveaction "%s".' LOG.exception( message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id))) return lv_ac_db
def _apply_pre_run_policies(self, liveaction_db): # Apply policies defined for the action. policy_dbs = Policy.query(resource_ref=liveaction_db.action, enabled=True) LOG.debug('Applying %s pre_run policies' % (len(policy_dbs))) for policy_db in policy_dbs: driver = policies.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: LOG.debug( 'Applying pre_run policy "%s" (%s) for liveaction %s' % (policy_db.ref, policy_db.policy_type, str( liveaction_db.id))) liveaction_db = driver.apply_before(liveaction_db) except: LOG.exception( 'An exception occurred while applying policy "%s".', policy_db.ref) if liveaction_db.status == action_constants.LIVEACTION_STATUS_DELAYED: break return liveaction_db
def test_get_driver(self): policy_db = Policy.get_by_ref("wolfpack.action-1.concurrency") policy = get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) self.assertIsInstance(policy, ResourcePolicyApplicator) self.assertEqual(policy._policy_ref, policy_db.ref) self.assertEqual(policy._policy_type, policy_db.policy_type) self.assertTrue(hasattr(policy, "threshold")) self.assertEqual(policy.threshold, 3)
def test_get_driver(self): policy_db = Policy.get_by_ref('wolfpack.action-1.concurrency') policy = get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) self.assertIsInstance(policy, ResourcePolicyApplicator) self.assertEqual(policy._policy_ref, policy_db.ref) self.assertEqual(policy._policy_type, policy_db.policy_type) self.assertTrue(hasattr(policy, 'threshold')) self.assertEqual(policy.threshold, 3)
def _apply_post_run_policies(self, liveaction=None, execution_id=None): # Apply policies defined for the action. for policy_db in Policy.query(resource_ref=liveaction.action): driver = policies.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: liveaction = driver.apply_after(liveaction) except: LOG.exception('An exception occurred while applying policy "%s".', policy_db.ref)
def process(self, request): """Schedules the LiveAction and publishes the request to the appropriate action runner(s). LiveAction in statuses other than "requested" are ignored. :param request: Action execution request. :type request: ``st2common.models.db.liveaction.LiveActionDB`` """ if request.status != action_constants.LIVEACTION_STATUS_REQUESTED: LOG.info('%s is ignoring %s (id=%s) with "%s" status.', self.__class__.__name__, type(request), request.id, request.status) return try: liveaction_db = action_utils.get_liveaction_by_id(request.id) except StackStormDBObjectNotFoundError: LOG.exception('Failed to find liveaction %s in the database.', request.id) raise # Apply policies defined for the action. for policy_db in Policy.query(resource_ref=liveaction_db.action): driver = policies.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: liveaction_db = driver.apply_before(liveaction_db) except: LOG.exception('An exception occurred while applying policy "%s".', policy_db.ref) if liveaction_db.status == action_constants.LIVEACTION_STATUS_DELAYED: break # Exit if the status of the request is no longer runnable. # The status could have be changed by one of the policies. if liveaction_db.status not in [action_constants.LIVEACTION_STATUS_REQUESTED, action_constants.LIVEACTION_STATUS_SCHEDULED]: LOG.info('%s is ignoring %s (id=%s) with "%s" status after policies are applied.', self.__class__.__name__, type(request), request.id, liveaction_db.status) return # Update liveaction status to "scheduled". if liveaction_db.status == action_constants.LIVEACTION_STATUS_REQUESTED: liveaction_db = action_service.update_status( liveaction_db, action_constants.LIVEACTION_STATUS_SCHEDULED, publish=False) # Publish the "scheduled" status here manually. Otherwise, there could be a # race condition with the update of the action_execution_db if the execution # of the liveaction completes first. LiveAction.publish_status(liveaction_db)
def _apply_post_run_policies(self, liveaction=None, execution_id=None): # Apply policies defined for the action. policy_dbs = Policy.query(resource_ref=liveaction.action) LOG.debug("Applying %s post_run policies" % (len(policy_dbs))) for policy_db in policy_dbs: driver = policies.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: LOG.debug( 'Applying post_run policy "%s" (%s) for liveaction %s' % (policy_db.ref, policy_db.policy_type, str(liveaction.id)) ) liveaction = driver.apply_after(liveaction) except: LOG.exception('An exception occurred while applying policy "%s".', policy_db.ref)
def _apply_post_run_policies(self, liveaction=None): # Apply policies defined for the action. policy_dbs = Policy.query(resource_ref=liveaction.action) LOG.debug('Applying %s post_run policies' % (len(policy_dbs))) for policy_db in policy_dbs: driver = policies.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: LOG.debug('Applying post_run policy "%s" (%s) for liveaction %s' % (policy_db.ref, policy_db.policy_type, str(liveaction.id))) liveaction = driver.apply_after(liveaction) except: LOG.exception('An exception occurred while applying policy "%s".', policy_db.ref)
def apply_post_run_policies(lv_ac_db): LOG.debug('Applying post run policies for liveaction "%s".' % str(lv_ac_db.id)) policy_dbs = pc_db_access.Policy.query(resource_ref=lv_ac_db.action, enabled=True) LOG.debug('Identified %s policies for the action "%s".' % (len(policy_dbs), lv_ac_db.action)) for policy_db in policy_dbs: LOG.debug('Getting driver for policy "%s" (%s).' % (policy_db.ref, policy_db.policy_type)) driver = engine.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: message = 'Applying policy "%s" (%s) for liveaction "%s".' LOG.info(message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id))) lv_ac_db = driver.apply_after(lv_ac_db) except: message = 'An exception occurred while applying policy "%s" (%s) for liveaction "%s".' LOG.exception(message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id))) return lv_ac_db
def _apply_pre_run_policies(self, liveaction_db): # Apply policies defined for the action. policy_dbs = Policy.query(resource_ref=liveaction_db.action, enabled=True) LOG.debug('Applying %s pre_run policies' % (len(policy_dbs))) for policy_db in policy_dbs: driver = policies.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: LOG.debug('Applying pre_run policy "%s" (%s) for liveaction %s' % (policy_db.ref, policy_db.policy_type, str(liveaction_db.id))) liveaction_db = driver.apply_before(liveaction_db) except: LOG.exception('An exception occurred while applying policy "%s".', policy_db.ref) if liveaction_db.status == action_constants.LIVEACTION_STATUS_DELAYED: break return liveaction_db
def apply_pre_run_policies(lv_ac_db): LOG.debug('Applying pre-run policies for liveaction "%s".' % str(lv_ac_db.id)) policy_dbs = pc_db_access.Policy.query(resource_ref=lv_ac_db.action, enabled=True) LOG.debug('Identified %s policies for the action "%s".' % (len(policy_dbs), lv_ac_db.action)) for policy_db in policy_dbs: LOG.debug('Getting driver for policy "%s" (%s).' % (policy_db.ref, policy_db.policy_type)) driver = engine.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: message = 'Applying policy "%s" (%s) for liveaction "%s".' LOG.info(message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id))) lv_ac_db = driver.apply_before(lv_ac_db) except: message = 'An exception occurred while applying policy "%s" (%s) for liveaction "%s".' LOG.exception(message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id))) if lv_ac_db.status == ac_const.LIVEACTION_STATUS_POLICY_DELAYED: break return lv_ac_db
def process(self, request): """Schedules the LiveAction and publishes the request to the appropriate action runner(s). LiveAction in statuses other than "requested" are ignored. :param request: Action execution request. :type request: ``st2common.models.db.liveaction.LiveActionDB`` """ if request.status != action_constants.LIVEACTION_STATUS_REQUESTED: LOG.info('%s is ignoring %s (id=%s) with "%s" status.', self.__class__.__name__, type(request), request.id, request.status) return try: liveaction_db = action_utils.get_liveaction_by_id(request.id) except StackStormDBObjectNotFoundError: LOG.exception('Failed to find liveaction %s in the database.', request.id) raise # Apply policies defined for the action. for policy_db in Policy.query(resource_ref=liveaction_db.action): driver = policies.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters) try: liveaction_db = driver.apply_before(liveaction_db) except: LOG.exception( 'An exception occurred while applying policy "%s".', policy_db.ref) if liveaction_db.status == action_constants.LIVEACTION_STATUS_DELAYED: break # Exit if the status of the request is no longer runnable. # The status could have be changed by one of the policies. if liveaction_db.status not in [ action_constants.LIVEACTION_STATUS_REQUESTED, action_constants.LIVEACTION_STATUS_SCHEDULED ]: LOG.info( '%s is ignoring %s (id=%s) with "%s" status after policies are applied.', self.__class__.__name__, type(request), request.id, liveaction_db.status) return # Update liveaction status to "scheduled". if liveaction_db.status == action_constants.LIVEACTION_STATUS_REQUESTED: liveaction_db = action_service.update_status( liveaction_db, action_constants.LIVEACTION_STATUS_SCHEDULED, publish=False) # Publish the "scheduled" status here manually. Otherwise, there could be a # race condition with the update of the action_execution_db if the execution # of the liveaction completes first. LiveAction.publish_status(liveaction_db)