From 27c0bcd2ebe1431f5c0d2802c6fa2ed01e6a4ca6 Mon Sep 17 00:00:00 2001 From: Kari Palenius Date: Fri, 11 May 2018 22:23:35 +0300 Subject: [PATCH 1/4] Implement overriding configuration from environment variables. --- rtmbot/bin/run_rtmbot.py | 52 ++++++++++++++++++++++++++++++++ tests/test_rtmbot_runner.py | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 tests/test_rtmbot_runner.py diff --git a/rtmbot/bin/run_rtmbot.py b/rtmbot/bin/run_rtmbot.py index 7676e7f..39ae6f7 100755 --- a/rtmbot/bin/run_rtmbot.py +++ b/rtmbot/bin/run_rtmbot.py @@ -3,6 +3,7 @@ import sys import os import yaml +import re from rtmbot import RtmBot @@ -20,12 +21,63 @@ def parse_args(): return parser.parse_args() +def prefix_from_plugin_name(name): + basename = name.split('.')[-1] + # Attribution: https://stackoverflow.com/a/1176023 + splitted = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', basename) + return re.sub('([a-z0-9])([A-Z])', r'\1_\2', splitted).upper() + '_' + + +def load_overrides_from_env(config): + ''' + Read and apply overrides from environment variables. For plugins, name of the class + is turned into a prefix. For example: + + YourAwesomePlugin.foo_bar => YOUR_AWESOME_PLUGIN_FOO_BAR + ''' + def boolish(v): + return v == '1' or v == 'true' # bool('false') == True, so do it the hard way + + params = [ + {'name': 'SLACK_TOKEN', 'type': str}, + {'name': 'BASE_PATH', 'type': str}, + {'name': 'LOGFILE', 'type': str}, + {'name': 'DEBUG', 'type': boolish}, + {'name': 'DAEMON', 'type': boolish} + ] + + # Override the rtmbot-specific variables. Here we can take advantage + # of the fact that we know what type they are supposed to be in. + config.update({ + param['name']: param['type'](os.environ[param['name']]) + for param in params + if param['name'] in os.environ}) + + + # Override plugin-specific variables. Since we don't know a schema, + # treat values as string. Leave type conversion for plugins themselves. + for plugin in config.get('ACTIVE_PLUGINS', []): + prefix = prefix_from_plugin_name(plugin) + plugin_configs = [ + var for var in os.environ + if var.startswith(prefix)] + + # Create if necessary + if plugin not in config: + config[plugin] = {} + + config[plugin].update({ + param.split(prefix)[-1].lower(): os.environ[param] + for param in plugin_configs}) + + def main(args=None): # load args with config path if not specified if not args: args = parse_args() config = yaml.load(open(args.config or 'rtmbot.conf', 'r')) + load_overrides_from_env(config) bot = RtmBot(config) try: bot.start() diff --git a/tests/test_rtmbot_runner.py b/tests/test_rtmbot_runner.py new file mode 100644 index 0000000..1d5b649 --- /dev/null +++ b/tests/test_rtmbot_runner.py @@ -0,0 +1,59 @@ +import os + +from contextlib import contextmanager +from rtmbot.bin.run_rtmbot import load_overrides_from_env + +@contextmanager +def override_env(env): + _environ = dict(os.environ) + try: + os.environ.clear() + os.environ.update(env) + yield + finally: + os.environ.clear() + os.environ.update(_environ) + +def test_normal_overrides(): + config = { + 'SLACK_TOKEN': 'xoxb-foo-bar', + 'BASE_PATH': os.getcwd(), + 'LOGFILE': 'foobar.log', + 'DEBUG': True, + 'DAEMON': True + } + + override = { + 'SLACK_TOKEN': 'xoxb-this-should-change', + 'BASE_PATH': '/opt/nope', + 'LOGFILE': 'awesome.log', + 'DEBUG': 'false', + 'DAEMON': 'false' + } + + with override_env(override): + load_overrides_from_env(config) + expected = { + 'SLACK_TOKEN': 'xoxb-this-should-change', + 'BASE_PATH': '/opt/nope', + 'LOGFILE': 'awesome.log', + 'DEBUG': False, + 'DAEMON': False + } + assert config == expected + +def test_plugin_overrides(): + config = {'ACTIVE_PLUGINS': ['plugins.AwesomePlugin']} + + override = { + 'AWESOME_PLUGIN_FOO': '1', + 'AWESOME_PLUGIN_BAR_BAR': 'some_awesome_value' + } + + with override_env(override): + load_overrides_from_env(config) + expected = { + 'foo': '1', + 'bar_bar': 'some_awesome_value' + } + assert config['plugins.AwesomePlugin'] == expected From 0aee263eac8adf8351c50e2be8b2bb39fe519a2a Mon Sep 17 00:00:00 2001 From: Kari Palenius Date: Fri, 11 May 2018 22:29:18 +0300 Subject: [PATCH 2/4] Test override case. --- tests/test_rtmbot_runner.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/test_rtmbot_runner.py b/tests/test_rtmbot_runner.py index 1d5b649..55b3ea6 100644 --- a/tests/test_rtmbot_runner.py +++ b/tests/test_rtmbot_runner.py @@ -43,7 +43,8 @@ def test_normal_overrides(): assert config == expected def test_plugin_overrides(): - config = {'ACTIVE_PLUGINS': ['plugins.AwesomePlugin']} + config = { + 'ACTIVE_PLUGINS': ['plugins.AwesomePlugin']} override = { 'AWESOME_PLUGIN_FOO': '1', @@ -57,3 +58,22 @@ def test_plugin_overrides(): 'bar_bar': 'some_awesome_value' } assert config['plugins.AwesomePlugin'] == expected + +def test_plugin_config_already_exists(): + config = { + 'ACTIVE_PLUGINS': ['plugins.AwesomePlugin'], + 'plugins.AwesomePlugin': { + 'some_credential': 'foo' + } + } + + override = { + 'AWESOME_PLUGIN_SOME_CREDENTIAL': 'bar' + } + + with override_env(override): + load_overrides_from_env(config) + expected = { + 'some_credential': 'bar' + } + assert config['plugins.AwesomePlugin'] == expected From 03b7c87f7271e8716d54ee0b9ef52f1289093c39 Mon Sep 17 00:00:00 2001 From: Kari Palenius Date: Fri, 11 May 2018 22:51:12 +0300 Subject: [PATCH 3/4] Add documentation. --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 0e8a7ad..eba53ce 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,15 @@ Installation For example, if your python path includes '/path/to/myproject' and you include `plugins.repeat.RepeatPlugin` in ACTIVE_PLUGINS, it will find the RepeatPlugin class within /path/to/myproject/plugins/repeat.py and instantiate it, then attach it to your running RTMBot. +### Environment variables + +All of the configurations variables mentioned can be overriden using environment +variables. For example, the following will override slack token. + + $ SLACK_TOKEN="xoxb-foo-bar" rtmbot + +This is especially useful when you want or need to hide credentials. + A Word on Structure ------- To give you a quick sense of how this library is structured, there is a RtmBot class which does the setup and handles input and outputs of messages. It will also search for and register Plugins within the specified directory(ies). These Plugins handle different message types with various methods and can also register periodic Jobs which will be executed by the Plugins. @@ -127,6 +136,29 @@ The repeat plugin will now be loaded by the bot on startup. Run `rtmbot` from co rtmbot ``` +### Configuration + +You can configure your plugins using `rtmbot.conf` and environment variables. +For example, running the following + + $ REPEAT_PLUGIN_DUMMY_VARIABLE="true" rtmbot + +will result in effectively same as the following `rtmbot.conf`: + +``` + plugins.repeat.RepeatPlugin: + dummy_variable: "true" +``` + +Note that environment variable values are treated as strings. If you need to +override for example lists, booleans or numbers, handle it in your plugin. Plugin +configuration can be accessed in the plugin from `self.plugin_config`. + +```python + def process_message(self, data): + print(self.plugin_config['dummy_variable']) # prints "true" +``` + Create Plugins -------- From bd0d2558588b4769c79151759ea36054570c2cf5 Mon Sep 17 00:00:00 2001 From: Kari Palenius Date: Fri, 11 May 2018 23:16:40 +0300 Subject: [PATCH 4/4] Fix flake8 warnings. --- rtmbot/bin/run_rtmbot.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/rtmbot/bin/run_rtmbot.py b/rtmbot/bin/run_rtmbot.py index 39ae6f7..3255bda 100755 --- a/rtmbot/bin/run_rtmbot.py +++ b/rtmbot/bin/run_rtmbot.py @@ -32,7 +32,6 @@ def load_overrides_from_env(config): ''' Read and apply overrides from environment variables. For plugins, name of the class is turned into a prefix. For example: - YourAwesomePlugin.foo_bar => YOUR_AWESOME_PLUGIN_FOO_BAR ''' def boolish(v): @@ -53,7 +52,6 @@ def boolish(v): for param in params if param['name'] in os.environ}) - # Override plugin-specific variables. Since we don't know a schema, # treat values as string. Leave type conversion for plugins themselves. for plugin in config.get('ACTIVE_PLUGINS', []):