StackStorm介绍
StackStorm是一个强大的自动化平台,结合DevOps和ChatOps,提供可扩展、灵活和健壮的工具链用于应用、服务和工作流的自动化能力。
Sensor
Sensor是一系列的感应器用于接受或者监测事件,当事件发生的时候,Sensor将会通知Trigger提交事件到StackStorm。
StackStorm定义了Sensor的接口,第三方系统可以方便的和StackStorm对接。
我们现在创建一个Sensor, 首先需要实现Python脚本sample_sensor.py:
from st2reactor.sensor.base import Sensor class SampleSensor(Sensor): """ * self.sensor_service - provides utilities like - get_logger() - returns logger instance specific to this sensor. - dispatch() for dispatching triggers into the system. * self._config - contains parsed configuration that was specified as config.yaml in the pack. """ def setup(self): # Setup stuff goes here. For example, you might establish connections # to external system once and reuse it. This is called only once by the system. pass def run(self): # This is where the crux of the sensor work goes. # This is called once by the system. # (If you want to sleep for regular intervals and keep # interacting with your external system, you'd inherit from PollingSensor.) # For example, let's consider a simple flask app. You'd run the flask app here. # You can dispatch triggers using sensor_service like so: # self.sensor_service(trigger, payload, trace_tag) # # You can refer to the trigger as dict # # { "name": ${trigger_name}, "pack": ${trigger_pack} } # # or just simply by reference as string. # # i.e. dispatch(${trigger_pack}.${trigger_name}, payload) # # E.g.: dispatch('examples.foo_sensor', {'k1': 'stuff', 'k2': 'foo'}) # # trace_tag is a tag you would like to associate with the dispatched TriggerInstance # # Typically the trace_tag is unique and a reference to an external event. pass def cleanup(self): # This is called when the st2 system goes down. You can perform cleanup operations like # closing the connections to external system here. pass def add_trigger(self, trigger): # This method is called when trigger is created |