| 1 |  |  | # Licensed to the StackStorm, Inc ('StackStorm') under one or more | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # contributor license agreements.  See the NOTICE file distributed with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # this work for additional information regarding copyright ownership. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # The ASF licenses this file to You under the Apache License, Version 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | # (the "License"); you may not use this file except in compliance with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | # the License.  You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | #     http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | # Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | # See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | # limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | import sys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | import traceback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | from kombu import Connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | from st2actions.container.base import RunnerContainer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | from st2common import log as logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | from st2common.constants import action as action_constants | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | from st2common.exceptions.actionrunner import ActionRunnerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | from st2common.exceptions.db import StackStormDBObjectNotFoundError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | from st2common.models.db.liveaction import LiveActionDB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | from st2common.persistence.execution import ActionExecution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | from st2common.services import executions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | from st2common.transport import consumers, liveaction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | from st2common.transport import utils as transport_utils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | from st2common.util import action_db as action_utils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | from st2common.util import system_info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | LOG = logging.getLogger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | ACTIONRUNNER_WORK_Q = liveaction.get_status_management_queue( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     'st2.actionrunner.work', routing_key=action_constants.LIVEACTION_STATUS_SCHEDULED) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | ACTIONRUNNER_CANCEL_Q = liveaction.get_status_management_queue( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     'st2.actionrunner.canel', routing_key=action_constants.LIVEACTION_STATUS_CANCELING) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | class ActionExecutionDispatcher(consumers.MessageHandler): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     message_type = LiveActionDB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     def __init__(self, connection, queues): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         super(ActionExecutionDispatcher, self).__init__(connection, queues) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         self.container = RunnerContainer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         self._running_liveactions = set() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     def process(self, liveaction): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         """Dispatches the LiveAction to appropriate action runner. | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         LiveAction in statuses other than "scheduled" and "canceling" are ignored. If | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         LiveAction is already canceled and result is empty, the LiveAction | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         is updated with a generic exception message. | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         :param liveaction: Action execution request. | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         :type liveaction: ``st2common.models.db.liveaction.LiveActionDB`` | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         :rtype: ``dict`` | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         if liveaction.status == action_constants.LIVEACTION_STATUS_CANCELED: | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             LOG.info('%s is not executing %s (id=%s) with "%s" status.', | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                      self.__class__.__name__, type(liveaction), liveaction.id, liveaction.status) | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             if not liveaction.result: | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 updated_liveaction = action_utils.update_liveaction_status( | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                     status=liveaction.status, | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                     result={'message': 'Action execution canceled by user.'}, | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                     liveaction_id=liveaction.id) | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                 executions.update_execution(updated_liveaction) | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             return | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         if liveaction.status not in [action_constants.LIVEACTION_STATUS_SCHEDULED, | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                                      action_constants.LIVEACTION_STATUS_CANCELING]: | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             LOG.info('%s is not dispatching %s (id=%s) with "%s" status.', | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                      self.__class__.__name__, type(liveaction), liveaction.id, liveaction.status) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             return | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         try: | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             liveaction_db = action_utils.get_liveaction_by_id(liveaction.id) | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         except StackStormDBObjectNotFoundError: | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             LOG.exception('Failed to find liveaction %s in the database.', liveaction.id) | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             raise | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         return (self._run_action(liveaction_db) | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                 if liveaction.status == action_constants.LIVEACTION_STATUS_SCHEDULED | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                 else self._cancel_action(liveaction_db)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     def shutdown(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         super(ActionExecutionDispatcher, self).shutdown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         # Abandon running executions if incomplete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         for liveaction_id in self._running_liveactions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             executions.abandon_execution_if_incomplete(liveaction_id=liveaction_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     def _run_action(self, liveaction_db): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         # stamp liveaction with process_info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         runner_info = system_info.get_process_info() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         # Update liveaction status to "running" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         liveaction_db = action_utils.update_liveaction_status( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             status=action_constants.LIVEACTION_STATUS_RUNNING, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             runner_info=runner_info, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             liveaction_id=liveaction_db.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         self._running_liveactions.add(liveaction_db.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         action_execution_db = executions.update_execution(liveaction_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         # Launch action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         extra = {'action_execution_db': action_execution_db, 'liveaction_db': liveaction_db} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         LOG.audit('Launching action execution.', extra=extra) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         # the extra field will not be shown in non-audit logs so temporarily log at info. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         LOG.info('Dispatched {~}action_execution: %s / {~}live_action: %s with "%s" status.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                  action_execution_db.id, liveaction_db.id, liveaction_db.status) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         extra = {'liveaction_db': liveaction_db} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             result = self.container.dispatch(liveaction_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             LOG.debug('Runner dispatch produced result: %s', result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             if not result: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 raise ActionRunnerException('Failed to execute action.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         except: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             _, ex, tb = sys.exc_info() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             extra['error'] = str(ex) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             LOG.info('Action "%s" failed: %s' % (liveaction_db.action, str(ex)), extra=extra) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             liveaction_db = action_utils.update_liveaction_status( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 status=action_constants.LIVEACTION_STATUS_FAILED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 liveaction_id=liveaction_db.id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 result={'error': str(ex), 'traceback': ''.join(traceback.format_tb(tb, 20))}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             executions.update_execution(liveaction_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             raise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         finally: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             self._running_liveactions.remove(liveaction_db.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         return result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     def _cancel_action(self, liveaction_db): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         action_execution_db = ActionExecution.get(liveaction__id=str(liveaction_db.id)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         extra = {'action_execution_db': action_execution_db, 'liveaction_db': liveaction_db} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         LOG.audit('Canceling action execution.', extra=extra) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         # the extra field will not be shown in non-audit logs so temporarily log at info. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         LOG.info('Dispatched {~}action_execution: %s / {~}live_action: %s with "%s" status.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                  action_execution_db.id, liveaction_db.id, liveaction_db.status) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             result = self.container.dispatch(liveaction_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             LOG.debug('Runner dispatch produced result: %s', result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         except: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             _, ex, tb = sys.exc_info() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             extra['error'] = str(ex) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             LOG.info('Failed to cancel action execution %s.' % (liveaction_db.id), extra=extra) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             raise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         return result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | def get_worker(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     with Connection(transport_utils.get_messaging_urls()) as conn: | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  |         return ActionExecutionDispatcher(conn, [ACTIONRUNNER_WORK_Q, ACTIONRUNNER_CANCEL_Q]) | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |  | 
            
                        
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: