config.py 4.38 KB
Newer Older
Markus Scheidgen's avatar
Markus Scheidgen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
16
17
18
19
"""
This module is used to store all configuration values. It makes use of
*namedtuples* to create key sensitive configuration objects.
"""

20
import os
Markus Scheidgen's avatar
Markus Scheidgen committed
21
import logging
22
23
from collections import namedtuple

24
FilesConfig = namedtuple(
25
    'FilesConfig', ['uploads_bucket', 'raw_bucket', 'archive_bucket', 'compress_archive', 'staging_bucket', 'public_bucket'])
26
27
""" API independent configuration for the object storage. """

Markus Scheidgen's avatar
Markus Scheidgen committed
28
CeleryConfig = namedtuple('Celery', ['broker_url'])
Markus Scheidgen's avatar
Markus Scheidgen committed
29
""" Used to configure the RabbitMQ for celery. """
30

Markus Scheidgen's avatar
Markus Scheidgen committed
31
FSConfig = namedtuple('FSConfig', ['tmp', 'objects'])
32
33
""" Used to configure file stystem access. """

34
35
36
RepositoryDBConfig = namedtuple('RepositoryDBConfig', ['host', 'port', 'dbname', 'user', 'password'])
""" Used to configure access to NOMAD-coe repository db. """

37
ElasticConfig = namedtuple('ElasticConfig', ['host', 'port', 'index_name'])
38
39
""" Used to configure elastic search. """

40
MongoConfig = namedtuple('MongoConfig', ['host', 'port', 'db_name'])
41
42
""" Used to configure mongo db. """

Markus Scheidgen's avatar
Markus Scheidgen committed
43
LogstashConfig = namedtuple('LogstashConfig', ['enabled', 'host', 'tcp_port', 'level'])
44
""" Used to configure and enable/disable the ELK based centralized logging. """
45

46
NomadServicesConfig = namedtuple('NomadServicesConfig', ['api_host', 'api_port', 'api_base_path', 'api_secret'])
Markus Scheidgen's avatar
Markus Scheidgen committed
47
48
""" Used to configure nomad services: worker, handler, api """

49
files = FilesConfig(
50
    uploads_bucket='uploads',
51
    raw_bucket=os.environ.get('NOMAD_FILES_RAW_BUCKET', 'raw'),
52
    archive_bucket='archive',
53
54
55
    compress_archive=True,
    staging_bucket='staging',
    public_bucket='public'
56
)
57
58
59
60
61
62
63

rabbit_host = os.environ.get('NOMAD_RABBITMQ_HOST', 'localhost')
rabbit_port = os.environ.get('NOMAD_RABBITMQ_PORT', None)
rabbit_user = 'rabbitmq'
rabbit_password = 'rabbitmq'
rabbit_url = 'pyamqp://%s:%s@%s//' % (rabbit_user, rabbit_password, rabbit_host)

64
65
66
67
68
69
70
71
72
73
74
75

def get_loglevel_from_env(key, default_level=logging.INFO):
    plain_value = os.environ.get(key, None)
    if plain_value is None:
        return default_level
    else:
        try:
            return int(plain_value)
        except ValueError:
            return getattr(logging, plain_value, default_level)


76
celery = CeleryConfig(
Markus Scheidgen's avatar
Markus Scheidgen committed
77
    broker_url=rabbit_url
78
)
79

80
fs = FSConfig(
81
82
    tmp=os.environ.get('NOMAD_FILES_TMP_DIR', '.volumes/fs/tmp'),
    objects=os.environ.get('NOMAD_FILES_OBJECTS_DIR', '.volumes/fs/objects')
83
)
84
elastic = ElasticConfig(
85
    host=os.environ.get('NOMAD_ELASTIC_HOST', 'localhost'),
86
    port=int(os.environ.get('NOMAD_ELASTIC_PORT', 9200)),
87
    index_name=os.environ.get('NOMAD_ELASTIC_INDEX_NAME', 'calcs')
88
)
89
repository_db = RepositoryDBConfig(
90
91
92
93
94
    host=os.environ.get('NOMAD_COE_REPO_DB_HOST', 'localhost'),
    port=int(os.environ.get('NOMAD_COE_REPO_DB_PORT', 5432)),
    dbname=os.environ.get('NOMAD_COE_REPO_DB_NAME', 'nomad'),
    user=os.environ.get('NOMAD_COE_REPO_DB_USER', 'postgres'),
    password=os.environ.get('NOMAD_COE_REPO_PASSWORD', 'nomad')
95
)
96
mongo = MongoConfig(
97
    host=os.environ.get('NOMAD_MONGO_HOST', 'localhost'),
Markus Scheidgen's avatar
Markus Scheidgen committed
98
    port=int(os.environ.get('NOMAD_MONGO_PORT', 27017)),
99
    db_name=os.environ.get('NOMAD_MONGO_DB_NAME', 'users')
100
)
101
logstash = LogstashConfig(
102
    enabled=True,
103
    host=os.environ.get('NOMAD_LOGSTASH_HOST', 'localhost'),
Markus Scheidgen's avatar
Markus Scheidgen committed
104
    tcp_port=int(os.environ.get('NOMAD_LOGSTASH_TCPPORT', '5000')),
105
    level=get_loglevel_from_env('NOMAD_LOGSTASH_LEVEL', default_level=logging.DEBUG)
106
)
Markus Scheidgen's avatar
Markus Scheidgen committed
107
services = NomadServicesConfig(
108
109
    api_host=os.environ.get('NOMAD_API_HOST', 'localhost'),
    api_port=int(os.environ.get('NOMAD_API_PORT', 8000)),
110
    api_base_path=os.environ.get('NOMAD_API_BASE_PATH', '/nomad/api'),
111
    api_secret=os.environ.get('NOMAD_API_SECRET', 'defaultApiSecret')
Markus Scheidgen's avatar
Markus Scheidgen committed
112
)
Markus Scheidgen's avatar
Markus Scheidgen committed
113

114
console_log_level = get_loglevel_from_env('NOMAD_CONSOLE_LOGLEVEL', default_level=logging.INFO)
115
service = os.environ.get('NOMAD_SERVICE', 'unknown nomad service')
116
release = os.environ.get('NOMAD_RELEASE', 'devel')