Skip to content

Commit

Permalink
Support reading environment variables into config. (#246)
Browse files Browse the repository at this point in the history
Implemented by enabling interpolation in the "template" style (e.g., run_as_user='$USER'). When loading the config, the webapp.py copies the os.environ dictionary into the DEFAULT section of the config object.

Consolidated the two places the ConfigObj is created into one read_config method.
  • Loading branch information
peichman-umd authored and Jon Stroop committed Oct 28, 2016
1 parent 77aaee7 commit 9a397c4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
13 changes: 13 additions & 0 deletions doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ In addition to a bunch of directory paths (items that end with `_dp`) which shou

**Quick Start:** Nearly everything should fine with the default settings. As long as you're pointing to `kdu_expand` and the Kakadu libs (as discussed in the [previous](dependencies.md) section) you can proceed to [Cache Maintenance](cache_maintenance.md) and come back later.

Environment variables are loaded into the `[DEFAULT]` section of the config file in memory, and `Template`-style [string interpolation] is on. This means you can pull in config information from the environment instead of hardcoding it in the config file. For example:

```
...
# get user and group from the environment
run_as_user='$USER'
run_as_group='$GROUP'
...
```


### `[loris.Loris]`

Expand All @@ -19,6 +29,7 @@ In addition to a bunch of directory paths (items that end with `_dp`) which shou
* `max_size_above_full` A numerical value which restricts the maximum image size to `max_size_above_full` percent of
the original image size. Setting this value to 100 disables server side interpolation of images. Default value is 200 (maximum double width or height allowed). To allow any size, set this value to 0.
* `proxy_path` The path you would like loris to proxy to. This will override the default path to your info.json file. proxy_path defaults to None if not explicitly set.

### `[logging]`

Each module has its own logger and is chock-full of debug statements, so setting the level to to `INFO` or higher is highly recommended.
Expand Down Expand Up @@ -56,3 +67,5 @@ Then Loris will, as the name of the option suggests, map the color profile that
* * *

Proceed to [Cache Maintenance](cache_maintenance.md) or go [Back to README](../README.md)

[string interpolation]: http://www.voidspace.org.uk/python/configobj.html#string-interpolation
5 changes: 4 additions & 1 deletion etc/loris2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
#
# <http://www.voidspace.org.uk/python/configobj.html#unrepr-mode>
#
# String interpolation is disabled.
# String interpolation is enabled using the "template" style. OS environment
# variables are available for interpolation, e.g., run_as_user='$USER'
#
# <http://www.voidspace.org.uk/python/configobj.html#string-interpolation>
#

[loris.Loris]
Expand Down
19 changes: 15 additions & 4 deletions loris/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@
import re
import string
import transforms
import os

getcontext().prec = 25 # Decimal precision. This should be plenty.

def create_app(debug=False, debug_jp2_transformer='kdu', config_file_path=''):
global logger
if debug:
project_dp = path.dirname(path.dirname(path.realpath(__file__)))

# change a few things, read the config and set up logging
project_dp = path.dirname(path.dirname(path.realpath(__file__)))
config_file_path = path.join(project_dp, 'etc', 'loris2.conf')
config = ConfigObj(config_file_path, unrepr=True, interpolation=False)

config = read_config(config_file_path)

config['logging']['log_to'] = 'console'
config['logging']['log_level'] = 'DEBUG'
__configure_logging(config['logging'])
Expand Down Expand Up @@ -71,7 +73,7 @@ def create_app(debug=False, debug_jp2_transformer='kdu', config_file_path=''):
config['transforms']['jp2']['kdu_libs'] = path.join(project_dp, libkdu_dir)

else:
config = ConfigObj(config_file_path, unrepr=True, interpolation=False)
config = read_config(config_file_path)
__configure_logging(config['logging'])
logger = logging.getLogger(__name__)
logger.debug('Running in production mode.')
Expand All @@ -97,6 +99,15 @@ def create_app(debug=False, debug_jp2_transformer='kdu', config_file_path=''):
else:
return Loris(config, debug)

def read_config(config_file_path):
config = ConfigObj(config_file_path, unrepr=True, interpolation='template')
# add the OS environment variables as the DEFAULT section to support
# interpolating their values into other keys
# make a copy of the os.environ dictionary so that the config object can't
# inadvertently modify the environment
config['DEFAULT'] = dict(os.environ)
return config

def __configure_logging(config):
logger = logging.getLogger()

Expand Down

0 comments on commit 9a397c4

Please sign in to comment.