7
7
import sys
8
8
import importlib .util
9
9
10
+ from collections import OrderedDict
10
11
from configparser import MissingSectionHeaderError , NoOptionError
11
12
from flask import Flask , request
12
13
from importlib import import_module
@@ -28,6 +29,32 @@ def read_config_section(parser: configparser.ConfigParser, section: str) -> Dict
28
29
}
29
30
return section_info
30
31
32
+ def read_config_from_env_vars (bot_name : Optional [str ] = None ) -> Dict [str , Dict [str , str ]]:
33
+ bots_config = {} # type: Dict[str, Dict[str, str]]
34
+ json_config = os .environ .get ('ZULIP_BOTSERVER_CONFIG' )
35
+
36
+ if json_config is None :
37
+ raise OSError ("Could not read environment variable 'ZULIP_BOTSERVER_CONFIG': Variable not set." )
38
+
39
+ # Load JSON-formatted environment variable; use OrderedDict to
40
+ # preserve ordering on Python 3.6 and below.
41
+ env_config = json .loads (json_config , object_pairs_hook = OrderedDict )
42
+ if bot_name is not None :
43
+ if bot_name in env_config :
44
+ bots_config [bot_name ] = env_config [bot_name ]
45
+ else :
46
+ # If the bot name provided via the command line does not
47
+ # exist in the configuration provided via the environment
48
+ # variable, use the first bot in the environment variable,
49
+ # with name updated to match, along with a warning.
50
+ first_bot_name = list (env_config .keys ())[0 ]
51
+ bots_config [bot_name ] = env_config [first_bot_name ]
52
+ logging .warning (
53
+ "First bot name in the config list was changed from '{}' to '{}'" .format (first_bot_name , bot_name )
54
+ )
55
+ else :
56
+ bots_config = dict (env_config )
57
+ return bots_config
31
58
32
59
def read_config_file (config_file_path : str , bot_name : Optional [str ] = None ) -> Dict [str , Dict [str , str ]]:
33
60
parser = parse_config_file (config_file_path )
@@ -178,16 +205,20 @@ def handle_bot() -> str:
178
205
def main () -> None :
179
206
options = parse_args ()
180
207
global bots_config
181
- try :
182
- bots_config = read_config_file (options .config_file , options .bot_name )
183
- except MissingSectionHeaderError :
184
- sys .exit ("Error: Your Botserver config file `{0}` contains an empty section header!\n "
185
- "You need to write the names of the bots you want to run in the "
186
- "section headers of `{0}`." .format (options .config_file ))
187
- except NoOptionError as e :
188
- sys .exit ("Error: Your Botserver config file `{0}` has a missing option `{1}` in section `{2}`!\n "
189
- "You need to add option `{1}` with appropriate value in section `{2}` of `{0}`"
190
- .format (options .config_file , e .option , e .section ))
208
+
209
+ if options .use_env_vars :
210
+ bots_config = read_config_from_env_vars (options .bot_name )
211
+ elif options .config_file :
212
+ try :
213
+ bots_config = read_config_file (options .config_file , options .bot_name )
214
+ except MissingSectionHeaderError :
215
+ sys .exit ("Error: Your Botserver config file `{0}` contains an empty section header!\n "
216
+ "You need to write the names of the bots you want to run in the "
217
+ "section headers of `{0}`." .format (options .config_file ))
218
+ except NoOptionError as e :
219
+ sys .exit ("Error: Your Botserver config file `{0}` has a missing option `{1}` in section `{2}`!\n "
220
+ "You need to add option `{1}` with appropriate value in section `{2}` of `{0}`"
221
+ .format (options .config_file , e .option , e .section ))
191
222
192
223
available_bots = list (bots_config .keys ())
193
224
bots_lib_modules = load_lib_modules (available_bots )
0 commit comments