def test_run(self): pack = 'dummy_pack_1' # Verify all the resources are there pack_dbs = Pack.query(ref=pack) action_dbs = Action.query(pack=pack) alias_dbs = ActionAlias.query(pack=pack) rule_dbs = Rule.query(pack=pack) sensor_dbs = Sensor.query(pack=pack) trigger_type_dbs = TriggerType.query(pack=pack) policy_dbs = Policy.query(pack=pack) config_schema_dbs = ConfigSchema.query(pack=pack) config_dbs = Config.query(pack=pack) self.assertEqual(len(pack_dbs), 1) self.assertEqual(len(action_dbs), 1) self.assertEqual(len(alias_dbs), 2) self.assertEqual(len(rule_dbs), 1) self.assertEqual(len(sensor_dbs), 3) self.assertEqual(len(trigger_type_dbs), 4) self.assertEqual(len(policy_dbs), 2) self.assertEqual(len(config_schema_dbs), 1) self.assertEqual(len(config_dbs), 1) # Run action action = self.get_action_instance() action.run(packs=[pack]) # Make sure all resources have been removed from the db pack_dbs = Pack.query(ref=pack) action_dbs = Action.query(pack=pack) alias_dbs = ActionAlias.query(pack=pack) rule_dbs = Rule.query(pack=pack) sensor_dbs = Sensor.query(pack=pack) trigger_type_dbs = TriggerType.query(pack=pack) policy_dbs = Policy.query(pack=pack) config_schema_dbs = ConfigSchema.query(pack=pack) config_dbs = Config.query(pack=pack) self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(action_dbs), 0) self.assertEqual(len(alias_dbs), 0) self.assertEqual(len(rule_dbs), 0) self.assertEqual(len(sensor_dbs), 0) self.assertEqual(len(trigger_type_dbs), 0) self.assertEqual(len(policy_dbs), 0) self.assertEqual(len(config_schema_dbs), 0) self.assertEqual(len(config_dbs), 0)
def test_run(self): pack = 'dummy_pack_1' # Verify all the resources are there pack_dbs = Pack.query(ref=pack) action_dbs = Action.query(pack=pack) alias_dbs = ActionAlias.query(pack=pack) rule_dbs = Rule.query(pack=pack) sensor_dbs = Sensor.query(pack=pack) trigger_type_dbs = TriggerType.query(pack=pack) policy_dbs = Policy.query(pack=pack) config_schema_dbs = ConfigSchema.query(pack=pack) config_dbs = Config.query(pack=pack) self.assertEqual(len(pack_dbs), 1) self.assertEqual(len(action_dbs), 1) self.assertEqual(len(alias_dbs), 3) self.assertEqual(len(rule_dbs), 1) self.assertEqual(len(sensor_dbs), 3) self.assertEqual(len(trigger_type_dbs), 4) self.assertEqual(len(policy_dbs), 2) self.assertEqual(len(config_schema_dbs), 1) self.assertEqual(len(config_dbs), 1) # Run action action = self.get_action_instance() action.run(packs=[pack]) # Make sure all resources have been removed from the db pack_dbs = Pack.query(ref=pack) action_dbs = Action.query(pack=pack) alias_dbs = ActionAlias.query(pack=pack) rule_dbs = Rule.query(pack=pack) sensor_dbs = Sensor.query(pack=pack) trigger_type_dbs = TriggerType.query(pack=pack) policy_dbs = Policy.query(pack=pack) config_schema_dbs = ConfigSchema.query(pack=pack) config_dbs = Config.query(pack=pack) self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(action_dbs), 0) self.assertEqual(len(alias_dbs), 0) self.assertEqual(len(rule_dbs), 0) self.assertEqual(len(sensor_dbs), 0) self.assertEqual(len(trigger_type_dbs), 0) self.assertEqual(len(policy_dbs), 0) self.assertEqual(len(config_schema_dbs), 0) self.assertEqual(len(config_dbs), 0)
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_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_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 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)