Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ in development
future, pack configs will be validated against the schema (if available). (new feature)
* Add data model and API changes for supporting user scoped variables. (new-feature, experimental)
* Add missing `pytz` dependency to ``st2client`` requirements file. (bug-fix)
* Set ``ST2_AUTH_TOKEN`` and ``ST2_API_URL`` env variables in actions to match sensors. (bug-fix)

1.4.0 - April 18, 2016
----------------------
Expand Down
2 changes: 1 addition & 1 deletion contrib/default/sensors/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# sensors

The sensors folder contains sensoros. See [Sensors](http://docs.stackstorm.com/sensors.html) for specifics on writing
The sensors folder contains sensors. See [Sensors](http://docs.stackstorm.com/sensors.html) for specifics on writing
sensors and registering TriggerTypes.
4 changes: 2 additions & 2 deletions contrib/examples/sensors/sample_polling_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class SamplePollingSensor(PollingSensor):
"""
* self._sensor_service
* self.sensor_service
- provides utilities like
get_logger() for writing to logs.
dispatch() for dispatching triggers into the system.
Expand All @@ -29,7 +29,7 @@ def poll(self):
# # _to_triggers is something you'd write to convert the data format you have
# # into a standard python dictionary. This should follow the payload schema
# # registered for the trigger.
# self._sensor_service.dispatch(trigger, payload)
# self.sensor_service.dispatch(trigger, payload)
# # You can refer to the trigger as dict
# # { "name": ${trigger_name}, "pack": ${trigger_pack} }
# # or just simply by reference as string.
Expand Down
4 changes: 2 additions & 2 deletions contrib/examples/sensors/sample_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class SampleSensor(Sensor):
"""
* self._sensor_service
* self.sensor_service
- provides utilities like
- get_logger() - returns logger instance specific to this sensor.
- dispatch() for dispatching triggers into the system.
Expand All @@ -24,7 +24,7 @@ def run(self):
# 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)
# 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.
Expand Down
8 changes: 4 additions & 4 deletions contrib/hello-st2/sensors/sensor1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class HelloSensor(Sensor):
def __init__(self, sensor_service, config):
super(HelloSensor, self).__init__(sensor_service=sensor_service, config=config)
self._logger = self._sensor_service.get_logger(name=self.__class__.__name__)
self._logger = self.sensor_service.get_logger(name=self.__class__.__name__)
self._stop = False

def setup(self):
Expand All @@ -15,10 +15,10 @@ def setup(self):
def run(self):
while not self._stop:
self._logger.debug('HelloSensor dispatching trigger...')
count = self._sensor_service.get_value('hello-st2.count') or 0
count = self.sensor_service.get_value('hello-st2.count') or 0
payload = {'greeting': 'Yo, StackStorm!', 'count': int(count) + 1}
self._sensor_service.dispatch(trigger='hello-st2.event1', payload=payload)
self._sensor_service.set_value('hello-st2.count', payload['count'])
self.sensor_service.dispatch(trigger='hello-st2.event1', payload=payload)
self.sensor_service.set_value('hello-st2.count', payload['count'])
eventlet.sleep(60)

def cleanup(self):
Expand Down
2 changes: 1 addition & 1 deletion contrib/linux/sensors/file_watch_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ def _handle_line(self, file_path, line):
'file_name': os.path.basename(file_path),
'line': line
}
self._sensor_service.dispatch(trigger=trigger, payload=payload)
self.sensor_service.dispatch(trigger=trigger, payload=payload)
22 changes: 21 additions & 1 deletion st2actions/st2actions/runners/pythonrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
from st2common.constants.action import LIVEACTION_STATUS_FAILED
from st2common.constants.action import LIVEACTION_STATUS_TIMED_OUT
from st2common.constants.error_messages import PACK_VIRTUALENV_DOESNT_EXIST
from st2common.constants.runners import PYTHON_RUNNER_DEFAULT_ACTION_TIMEOUT
from st2common.constants.system import API_URL_ENV_VARIABLE_NAME
from st2common.constants.system import AUTH_TOKEN_ENV_VARIABLE_NAME
from st2common.util.api import get_full_public_api_url
from st2common.util.sandboxing import get_sandbox_path
from st2common.util.sandboxing import get_sandbox_python_path
from st2common.util.sandboxing import get_sandbox_python_binary_path
from st2common.util.sandboxing import get_sandbox_virtualenv_path
from st2common.constants.runners import PYTHON_RUNNER_DEFAULT_ACTION_TIMEOUT


__all__ = [
'get_runner',
Expand Down Expand Up @@ -137,6 +141,8 @@ def run(self, action_parameters):
# Include common st2 environment variables
st2_env_vars = self._get_common_action_env_variables()
env.update(st2_env_vars)
datastore_env_vars = self._get_datastore_access_env_vars()
env.update(datastore_env_vars)

exit_code, stdout, stderr, timed_out = run_command(cmd=args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=False,
Expand Down Expand Up @@ -203,3 +209,17 @@ def _get_env_vars(self):
del env_vars[key]

return env_vars

def _get_datastore_access_env_vars(self):
"""
Return environment variables so datastore access using client (from st2client)
is possible with actions. This is done to be compatible with sensors.

:rtype: ``dict``
"""
env_vars = {}
if self.auth_token:
env_vars[AUTH_TOKEN_ENV_VARIABLE_NAME] = self.auth_token.token
env_vars[API_URL_ENV_VARIABLE_NAME] = get_full_public_api_url()

return env_vars
2 changes: 2 additions & 0 deletions st2actions/tests/unit/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from st2actions import scheduler
from st2actions.notifier import notifier
from st2common.constants import action as action_constants
from st2common.constants.system import AUTH_TOKEN_ENV_VARIABLE_NAME
from st2common.models.db.liveaction import LiveActionDB
from st2common.util.api import get_full_public_api_url
from st2common.constants.runners import COMMON_ACTION_ENV_VARIABLES
Expand All @@ -44,6 +45,7 @@ def assertCommonSt2EnvVarsAvailableInEnv(self, env):
self.assertTrue(var_name in env)

self.assertEqual(env['ST2_ACTION_API_URL'], get_full_public_api_url())
self.assertTrue(env[AUTH_TOKEN_ENV_VARIABLE_NAME] is not None)


class MockLiveActionPublisher(object):
Expand Down
6 changes: 4 additions & 2 deletions st2reactor/st2reactor/sensor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ def __init__(self, sensor_service, config=None):
:keyword config: Sensor config.
:type config: ``dict`` or None
"""
self._sensor_service = sensor_service
self._config = config or {}
self._sensor_service = sensor_service # Deprecate in the future
self.sensor_service = sensor_service
self._config = config or {} # Deprecate in the future
self.config = self._config

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally missed this PR (I blame thunderbird sync!) and that's how I've done it utilizing @Property - https://github.com/StackStorm/st2/pull/2698/files#diff-f120a0125bcd3ec86f1d85020ed530ffR26

@abc.abstractmethod
def setup(self):
Expand Down
12 changes: 12 additions & 0 deletions st2reactor/tests/unit/test_sensor_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ def setUpClass(cls):
super(SensorWrapperTestCase, cls).setUpClass()
tests_config.parse_args()

def test_sensor_instance_has_sensor_service(self):
file_path = os.path.join(RESOURCES_DIR, 'test_sensor.py')
trigger_types = ['trigger1', 'trigger2']
parent_args = ['--config-file', TESTS_CONFIG_PATH]

wrapper = SensorWrapper(pack='core', file_path=file_path,
class_name='TestSensor',
trigger_types=trigger_types,
parent_args=parent_args)
self.assertTrue(getattr(wrapper._sensor_instance, 'sensor_service', None) is not None)
self.assertTrue(getattr(wrapper._sensor_instance, 'config', None) is not None)

def test_trigger_cud_event_handlers(self):
file_path = os.path.join(RESOURCES_DIR, 'test_sensor.py')
trigger_types = ['trigger1', 'trigger2']
Expand Down