config.py 10.3 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.

Markus Scheidgen's avatar
Markus Scheidgen committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
This module describes all configurable parameters for the nomad python code. The
configuration is used for all executed python code including API, worker, CLI, and other
scripts. To use the configuration in your own scripts or new modules, simply import
this module.

All parameters are structured into objects for two reasons. First, to have
categories. Second, to allow runtime manipulation that is not effected
by python import logic. The categories are choosen along infrastructure components:
``mongo``, ``elastic``, etc.

This module also provides utilities to read the configuration from environment variables
and .yaml files. This is done automatically on import. The precedence is env over .yaml
over defaults.

.. autoclass:: nomad.config.NomadConfig
.. autofunction:: nomad.config.apply
.. autofunction:: nomad.config.load_config
"""

Markus Scheidgen's avatar
Markus Scheidgen committed
35
import logging
36
37
38
import os
import os.path
import yaml
Markus Scheidgen's avatar
Markus Scheidgen committed
39
40
import warnings

Markus Scheidgen's avatar
Markus Scheidgen committed
41
42
43
from nomad import gitinfo


Markus Scheidgen's avatar
Markus Scheidgen committed
44
45
46
warnings.filterwarnings("ignore", message="numpy.dtype size changed")
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")

47

48
49
class NomadConfig(dict):
    """
Markus Scheidgen's avatar
Markus Scheidgen committed
50
51
    A class for configuration categories. It is a dict subclass that uses attributes as
    key/value pairs.
52
53
54
55
56
57
58
59
60
61
62
63
    """
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def __getattr__(self, name):
        if name in self:
            return self[name]
        else:
            raise AttributeError("No such attribute: " + name)

    def __setattr__(self, name, value):
        self[name] = value
64

65
66
67
68
69
    def __delattr__(self, name):
        if name in self:
            del self[name]
        else:
            raise AttributeError("No such attribute: " + name)
70
71


72
73
CELERY_WORKER_ROUTING = 'worker'
CELERY_QUEUE_ROUTING = 'queue'
74

75
76
77
78
79
rabbitmq = NomadConfig(
    host='localhost',
    user='rabbitmq',
    password='rabbitmq'
)
80

81

82
83
def rabbitmq_url():
    return 'pyamqp://%s:%s@%s//' % (rabbitmq.user, rabbitmq.password, rabbitmq.host)
84

85

86
87
88
89
celery = NomadConfig(
    max_memory=64e6,  # 64 GB
    timeout=1800,  # 1/2 h
    acks_late=True,
90
    routing=CELERY_QUEUE_ROUTING,
91
92
93
94
95
    priorities={
        'Upload.process_upload': 5,
        'Upload.delete_upload': 9,
        'Upload.publish_upload': 10
    }
96
)
97

98
99
100
101
fs = NomadConfig(
    tmp='.volumes/fs/tmp',
    staging='.volumes/fs/staging',
    public='.volumes/fs/public',
102
    migration_packages='.volumes/fs/migration_packages',
103
    local_tmp='/tmp',
104
105
    prefix_size=2,
    working_directory=os.getcwd()
106
)
107

108
109
110
111
112
elastic = NomadConfig(
    host='localhost',
    port=9200,
    index_name='nomad_fairdi_calcs'
)
113

114
keycloak = NomadConfig(
115
    server_url='https://repository.nomad-coe.eu/fairdi/keycloak/auth/',
116
117
118
    realm_name='fairdi_nomad_test',
    username='admin',
    password='password',
119
    client_id='nomad_api_dev',
120
    client_secret='**********'
121
122
)

123
124
125
126
127
mongo = NomadConfig(
    host='localhost',
    port=27017,
    db_name='nomad_fairdi'
)
128

129
130
131
132
133
134
logstash = NomadConfig(
    enabled=True,
    host='localhost',
    tcp_port='5000',
    level=logging.DEBUG
)
Markus Scheidgen's avatar
Markus Scheidgen committed
135

136
137
138
services = NomadConfig(
    api_host='localhost',
    api_port=8000,
Markus Scheidgen's avatar
Markus Scheidgen committed
139
    api_base_path='/fairdi/nomad/latest',
140
    api_secret='defaultApiSecret',
141
    api_chaos=0,
142
    admin_user_id='00000000-0000-0000-0000-000000000000',
143
    not_processed_value='not processed',
144
    unavailable_value='unavailable',
145
    https=False,
146
147
    upload_limit=10,
    force_raw_file_decoding=False
148
149
)

Markus Scheidgen's avatar
Markus Scheidgen committed
150
151
152
153
tests = NomadConfig(
    default_timeout=30
)

154

155
def api_url(ssl: bool = True):
156
    return '%s://%s/%s/api' % (
157
        'https' if services.https and ssl else 'http',
158
159
        services.api_host.strip('/'),
        services.api_base_path.strip('/'))
160
161


162
163
164
165
166
167
def gui_url():
    base = api_url(True)[:-3]
    if base.endswith('/'):
        base = base[:-1]
    return '%s/gui' % base

168
169
170
171
172

mail = NomadConfig(
    enabled=False,
    with_login=False,
    host='',
Markus Scheidgen's avatar
Markus Scheidgen committed
173
    port=8995,
174
175
    user='',
    password='',
176
177
    from_address='webmaster@nomad-coe.eu',
    cc_address='webmaster@nomad-coe.eu'
178
179
180
)

normalize = NomadConfig(
181
182
183
    # The system size limit for running the dimensionality analysis. For very
    # large systems the dimensionality analysis will get too expensive.
    system_classification_with_clusters_threshold=50,
184
    # Symmetry tolerance controls the precision used by spglib in order to find
185
186
187
    # symmetries. The atoms are allowed to move 1/2*symmetry_tolerance from
    # their symmetry positions in order for spglib to still detect symmetries.
    # The unit is angstroms.
188
189
190
191
192
193
194
    symmetry_tolerance=0.1,
    # Maximum number of atoms in the single cell of a 2D material for it to be
    # considered valid.
    max_2d_single_cell_size=7,
    # The distance tolerance between atoms for grouping them into the same
    # cluster. Used in detecting system type.
    cluster_threshold=3.1,
195
    # Defines the "bin size" for rounding cell angles for the material hash
Lauri Himanen's avatar
Lauri Himanen committed
196
    angle_rounding=float(10.0)  # unit: degree
197
198
199
200
201
)

client = NomadConfig(
    user='leonard.hofstadter@nomad-fairdi.tests.de',
    password='password',
202
    url='http://localhost:8000/fairdi/nomad/latest/api'
203
204
)

205
206
207
208
209
210
211
212
datacite = NomadConfig(
    mds_host='https://mds.datacite.org',
    enabled=False,
    prefix='10.17172',
    user='*',
    password='*'
)

213
version = '0.7.2'
214
commit = gitinfo.commit
215
release = 'devel'
216
217
domain = 'DFT'
service = 'unknown nomad service'
218
auxfile_cutoff = 100
219
parser_matching_size = 9128
220
console_log_level = logging.WARNING
221
max_upload_size = 32 * (1024 ** 3)
222
raw_file_strip_cutoff = 1000
223
use_empty_parsers = False
224

225

226
227
228
springer_db_relative_path = 'normalizing/data/SM_all08.db'
springer_db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), springer_db_relative_path)

229

230
231
def normalize_loglevel(value, default_level=logging.INFO):
    plain_value = value
232
233
234
235
236
237
    if plain_value is None:
        return default_level
    else:
        try:
            return int(plain_value)
        except ValueError:
238
            return getattr(logging, plain_value)
239
240


241
transformations = {
242
243
    'console_log_level': normalize_loglevel,
    'logstash_level': normalize_loglevel
244
}
Markus Scheidgen's avatar
Markus Scheidgen committed
245

Markus Scheidgen's avatar
Markus Scheidgen committed
246

247
248
249
250
# use std python logger, since logging is not configured while loading configuration
logger = logging.getLogger(__name__)


251
def apply(key, value) -> None:
252
253
254
255
256
257
258
259
    """
    Changes the config according to given key and value. The keys are interpreted as paths
    to config values with ``_`` as a separator. E.g. ``fs_staging`` leading to
    ``config.fs.staging``
    """
    path = list(reversed(key.split('_')))
    child_segment = None
    current_value = None
260
    child_config = globals()
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
    child_key = None

    try:
        while len(path) > 0:
            if child_segment is None:
                child_segment = path.pop()
            else:
                child_segment += '_' + path.pop()

            if child_segment in child_config:
                current_value = child_config[child_segment]

            if current_value is None:
                if len(path) == 0:
                    raise KeyError
276
277

                continue
278
279
            if isinstance(current_value, NomadConfig):
                child_config = current_value
280
                current_value = None
281
282
283
284
                child_segment = None
            else:
                if len(path) > 0:
                    raise KeyError()
Markus Scheidgen's avatar
Markus Scheidgen committed
285
286
287

                child_key = child_segment
                break
288
289
290
291

        if child_key is None or current_value is None:
            raise KeyError()
    except KeyError:
Markus Scheidgen's avatar
Markus Scheidgen committed
292
        return
293
294
295
296
297
298
299
300
301
302
303

    if not isinstance(value, type(current_value)):
        try:
            value = transformations.get(key, type(current_value))(value)
        except Exception as e:
            logger.error(
                'config key %s value %s has wrong type: %s' % (key, str(value), str(e)))

    child_config[child_key] = value


Markus Scheidgen's avatar
Markus Scheidgen committed
304
def load_config(config_file: str = os.environ.get('NOMAD_CONFIG', 'nomad.yaml')) -> None:
Markus Scheidgen's avatar
Markus Scheidgen committed
305
306
307
308
309
310
311
    """
    Loads the configuration from the ``config_file`` and environment.

    Arguments:
        config_file: Override the configfile, default is file stored in env variable
            NOMAD_CONFIG or ``nomad.yaml``.
    """
Markus Scheidgen's avatar
Markus Scheidgen committed
312
    # load yaml and override defaults
313
314
315
    if os.path.exists(config_file):
        with open(config_file, 'r') as stream:
            try:
316
                config_data = yaml.load(stream, Loader=getattr(yaml, 'FullLoader'))
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
            except yaml.YAMLError as e:
                logger.error('cannot read nomad config', exc_info=e)

        def adapt(config, new_config, child_key=None):
            for key, value in new_config.items():
                if key in config:
                    if child_key is None:
                        qualified_key = key
                    else:
                        qualified_key = '%s_%s' % (child_key, key)

                    current_value = config[key]
                    if isinstance(value, dict) and isinstance(current_value, NomadConfig):
                        adapt(current_value, value, qualified_key)
                    else:
                        if not isinstance(value, type(current_value)):
                            try:
                                value = transformations.get(qualified_key, type(current_value))(value)
                            except Exception as e:
                                logger.error(
                                    'config key %s value %s has wrong type: %s' % (key, str(value), str(e)))
                        else:
                            config[key] = value
340
                            logger.info('override config key %s with value %s' % (key, str(value)))
341
342
343
344
345
                else:
                    logger.error('config key %s does not exist' % key)

        adapt(globals(), config_data)

Markus Scheidgen's avatar
Markus Scheidgen committed
346
    # load env and override yaml and defaults
347
348
349
350
351
    kwargs = {
        key[len('NOMAD_'):].lower(): value
        for key, value in os.environ.items()
        if key.startswith('NOMAD_')
    }
352

353
    for key, value in kwargs.items():
354
        apply(key, value)
355
356
357


load_config()