config.py 13.1 KB
Newer Older
Markus Scheidgen's avatar
Markus Scheidgen committed
1
2
3
4
#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
Markus Scheidgen's avatar
Markus Scheidgen committed
5
6
7
8
9
#
# 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
#
Markus Scheidgen's avatar
Markus Scheidgen committed
10
#     http://www.apache.org/licenses/LICENSE-2.0
Markus Scheidgen's avatar
Markus Scheidgen committed
11
12
#
# Unless required by applicable law or agreed to in writing, software
Markus Scheidgen's avatar
Markus Scheidgen committed
13
# distributed under the License is distributed on an "AS IS" BASIS,
Markus Scheidgen's avatar
Markus Scheidgen committed
14
15
16
# 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
17
#
Markus Scheidgen's avatar
Markus Scheidgen committed
18

19
'''
Markus Scheidgen's avatar
Markus Scheidgen committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
37
'''
Markus Scheidgen's avatar
Markus Scheidgen committed
38

Markus Scheidgen's avatar
Markus Scheidgen committed
39
import logging
40
41
42
import os
import os.path
import yaml
Markus Scheidgen's avatar
Markus Scheidgen committed
43
44
import warnings

45
46
47
48
49
50
51
52
53
54
try:
    from nomad import gitinfo
except ImportError:
    git_root = os.path.join(os.path.dirname(__file__), '..')
    cwd = os.getcwd()
    os.chdir(git_root)
    os.system('./gitinfo.sh')
    os.chdir(cwd)

    from nomad import gitinfo
Markus Scheidgen's avatar
Markus Scheidgen committed
55
56


Markus Scheidgen's avatar
Markus Scheidgen committed
57
58
warnings.filterwarnings('ignore', message='numpy.dtype size changed')
warnings.filterwarnings('ignore', message='numpy.ufunc size changed')
Markus Scheidgen's avatar
Markus Scheidgen committed
59

60

61
class NomadConfig(dict):
62
    '''
Markus Scheidgen's avatar
Markus Scheidgen committed
63
64
    A class for configuration categories. It is a dict subclass that uses attributes as
    key/value pairs.
65
    '''
66
67
68
69
70
71
72
73
74
75
76
    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
77

78
79
80
81
82
    def __delattr__(self, name):
        if name in self:
            del self[name]
        else:
            raise AttributeError("No such attribute: " + name)
83
84


85
86
CELERY_WORKER_ROUTING = 'worker'
CELERY_QUEUE_ROUTING = 'queue'
87

88
89
90
91
92
rabbitmq = NomadConfig(
    host='localhost',
    user='rabbitmq',
    password='rabbitmq'
)
93

94

95
96
def rabbitmq_url():
    return 'pyamqp://%s:%s@%s//' % (rabbitmq.user, rabbitmq.password, rabbitmq.host)
97

Lauri Himanen's avatar
Lauri Himanen committed
98

99
100
101
102
celery = NomadConfig(
    max_memory=64e6,  # 64 GB
    timeout=1800,  # 1/2 h
    acks_late=True,
103
    routing=CELERY_QUEUE_ROUTING,
104
105
106
107
108
    priorities={
        'Upload.process_upload': 5,
        'Upload.delete_upload': 9,
        'Upload.publish_upload': 10
    }
109
)
110

111
112
113
114
fs = NomadConfig(
    tmp='.volumes/fs/tmp',
    staging='.volumes/fs/staging',
    public='.volumes/fs/public',
115
    local_tmp='/tmp',
116
117
    prefix_size=2,
    working_directory=os.getcwd()
118
)
119

120
121
122
elastic = NomadConfig(
    host='localhost',
    port=9200,
123
124
    index_name='nomad_fairdi_calcs',
    materials_index_name='nomad_fairdi_materials'
125
)
126

127
keycloak = NomadConfig(
128
    server_url='https://nomad-lab.eu/fairdi/keycloak/auth/',
129
130
131
    realm_name='fairdi_nomad_test',
    username='admin',
    password='password',
132
133
134
    client_id='nomad_public',
    client_secret=None,
    oasis=False)
135

136
137
138
139
140
mongo = NomadConfig(
    host='localhost',
    port=27017,
    db_name='nomad_fairdi'
)
141

142
logstash = NomadConfig(
143
    enabled=False,
144
145
146
147
    host='localhost',
    tcp_port='5000',
    level=logging.DEBUG
)
Markus Scheidgen's avatar
Markus Scheidgen committed
148

149
150
151
services = NomadConfig(
    api_host='localhost',
    api_port=8000,
Markus Scheidgen's avatar
Markus Scheidgen committed
152
    api_base_path='/fairdi/nomad/latest',
153
    api_secret='defaultApiSecret',
154
    api_chaos=0,
155
    admin_user_id='00000000-0000-0000-0000-000000000000',
156
    not_processed_value='not processed',
157
    unavailable_value='unavailable',
158
    https=False,
159
    upload_limit=10,
160
161
    force_raw_file_decoding=False,
    download_scan_size=500,
162
163
    download_scan_timeout=u'30m',
    central_nomad_api_url='https://nomad-lab.eu/prod/rae/api'
164
165
)

Markus Scheidgen's avatar
Markus Scheidgen committed
166
167
168
169
tests = NomadConfig(
    default_timeout=30
)

170

171
def api_url(ssl: bool = True):
172
    return '%s://%s/%s/api' % (
173
        'https' if services.https and ssl else 'http',
174
175
        services.api_host.strip('/'),
        services.api_base_path.strip('/'))
176
177


178
def gui_url(page: str = None):
179
180
181
    base = api_url(True)[:-3]
    if base.endswith('/'):
        base = base[:-1]
182
183
184
185

    if page is not None:
        return '%s/gui/%s' % (base, page)

186
187
    return '%s/gui' % base

188

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def check_config():
    """Used to check that the current configuration is valid. Should only be
    called once after the final config is loaded.

    Raises:
        AssertionError: if there is a contradiction or invalid values in the
            config file settings.
    """
    # The AFLOW symmetry information is checked once on import
    proto_symmetry_tolerance = normalize.prototype_symmetry_tolerance
    symmetry_tolerance = normalize.symmetry_tolerance
    if proto_symmetry_tolerance != symmetry_tolerance:
        raise AssertionError(
            "The AFLOW prototype information is outdated due to changed tolerance "
            "for symmetry detection. Please update the AFLOW prototype information "
            "by running the CLI command 'nomad admin ops prototype-update "
            "--matches-only'."
        )

208
209
210
    if not os.path.exists(normalize.springer_db_path):
        normalize.springer_db_path = None

211

212
mail = NomadConfig(
Lauri Himanen's avatar
Lauri Himanen committed
213
    enabled=False,
214
    with_login=False,
Lauri Himanen's avatar
Lauri Himanen committed
215
216
    host='',
    port=8995,
217
218
    user='',
    password='',
219
220
    from_address='support@nomad-lab.eu',
    cc_address='support@nomad-lab.eu'
221
222
223
)

normalize = NomadConfig(
224
225
    # The system size limit for running the dimensionality analysis. For very
    # large systems the dimensionality analysis will get too expensive.
226
    system_classification_with_clusters_threshold=64,
227
    # Symmetry tolerance controls the precision used by spglib in order to find
228
229
    # symmetries. The atoms are allowed to move 1/2*symmetry_tolerance from
    # their symmetry positions in order for spglib to still detect symmetries.
Lauri Himanen's avatar
Merged.    
Lauri Himanen committed
230
231
232
    # The unit is angstroms. The value of 0.1 is used e.g. by Materials Project
    # according to
    # https://pymatgen.org/pymatgen.symmetry.analyzer.html#pymatgen.symmetry.analyzer.SpacegroupAnalyzer
233
    symmetry_tolerance=0.1,
Lauri Himanen's avatar
Merged.    
Lauri Himanen committed
234
235
    # The symmetry tolerance used in aflow prototype matching. Should only be
    # changed before re-running the prototype detection.
236
    prototype_symmetry_tolerance=0.1,
237
238
239
240
241
    # 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.
242
    cluster_threshold=2.5,
243
    # Defines the "bin size" for rounding cell angles for the material hash
244
245
246
247
248
    angle_rounding=float(10.0),  # unit: degree
    # The threshold for a system to be considered "flat". Used e.g. when
    # determining if a 2D structure is purely 2-dimensional to allow extra rigid
    # transformations that are improper in 3D but proper in 2D.
    flat_dim_threshold=0.1,
249
250
251
    # The threshold for point equality in k-space. Unit: 1/m.
    k_space_precision=150e6,
    # The energy threshold for how much a band can be on top or below the fermi
252
253
    # level in order to detect a gap. Unit: Joule.
    band_structure_energy_tolerance=1.6022e-20,  # 0.1 eV
254
255
    springer_db_path=os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
256
257
        'normalizing/data/springer.msg'
    )
258
259
)

260
261
262
263
paths = NomadConfig(
    similarity="",
)

264
265
266
client = NomadConfig(
    user='leonard.hofstadter@nomad-fairdi.tests.de',
    password='password',
267
    url='http://nomad-lab.eu/prod/rae/api'
268
269
)

270
271
272
273
274
275
276
277
datacite = NomadConfig(
    mds_host='https://mds.datacite.org',
    enabled=False,
    prefix='10.17172',
    user='*',
    password='*'
)

278
meta = NomadConfig(
Markus Scheidgen's avatar
Markus Scheidgen committed
279
    version='0.9.8',
280
281
282
283
284
285
    commit=gitinfo.commit,
    release='devel',
    default_domain='dft',
    service='unknown nomad service',
    name='novel materials discovery (NOMAD)',
    description='A FAIR data sharing platform for materials science data',
286
    homepage='https://nomad-lab.eu',
287
288
    source_url='https://gitlab.mpcdf.mpg.de/nomad-lab/nomad-FAIR',
    maintainer_email='markus.scheidgen@physik.hu-berlin.de'
289
290
)

291
auxfile_cutoff = 100
292
parser_matching_size = 9128
293
console_log_level = logging.WARNING
294
max_upload_size = 32 * (1024 ** 3)
295
raw_file_strip_cutoff = 1000
296
use_empty_parsers = False
297
reprocess_unmatched = True
298
299
metadata_file_name = 'nomad'
metadata_file_extensions = ('json', 'yaml', 'yml')
Lauri Himanen's avatar
Merged.    
Lauri Himanen committed
300

301

302
303
def normalize_loglevel(value, default_level=logging.INFO):
    plain_value = value
304
305
306
307
308
309
    if plain_value is None:
        return default_level
    else:
        try:
            return int(plain_value)
        except ValueError:
310
            return getattr(logging, plain_value)
311
312


313
transformations = {
314
315
    'console_log_level': normalize_loglevel,
    'logstash_level': normalize_loglevel
316
}
Markus Scheidgen's avatar
Markus Scheidgen committed
317

Markus Scheidgen's avatar
Markus Scheidgen committed
318

319
320
321
322
# use std python logger, since logging is not configured while loading configuration
logger = logging.getLogger(__name__)


323
def apply(key, value) -> None:
324
    '''
325
326
327
    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``
328
    '''
329
330
331
    path = list(reversed(key.split('_')))
    child_segment = None
    current_value = None
332
    child_config = globals()
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
    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
348
349

                continue
350
351
            if isinstance(current_value, NomadConfig):
                child_config = current_value
352
                current_value = None
353
354
355
356
                child_segment = None
            else:
                if len(path) > 0:
                    raise KeyError()
Markus Scheidgen's avatar
Markus Scheidgen committed
357
358
359

                child_key = child_segment
                break
360
361
362
363

        if child_key is None or current_value is None:
            raise KeyError()
    except KeyError:
Markus Scheidgen's avatar
Markus Scheidgen committed
364
        return
365
366
367
368
369
370
371
372
373
374
375

    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
376
def load_config(config_file: str = os.environ.get('NOMAD_CONFIG', 'nomad.yaml')) -> None:
377
    '''
Markus Scheidgen's avatar
Markus Scheidgen committed
378
379
380
381
382
    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``.
383
    '''
384
    # load yaml and override defaults (only when not in test)
385
386
387
    if os.path.exists(config_file):
        with open(config_file, 'r') as stream:
            try:
388
                config_data = yaml.load(stream, Loader=getattr(yaml, 'FullLoader'))
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
            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
Markus Scheidgen's avatar
Markus Scheidgen committed
412
                            logger.debug('override config key %s with value %s' % (key, str(value)))
413
414
415
                else:
                    logger.error('config key %s does not exist' % key)

Markus Scheidgen's avatar
Markus Scheidgen committed
416
417
        if config_data is not None:
            adapt(globals(), config_data)
418

Markus Scheidgen's avatar
Markus Scheidgen committed
419
    # load env and override yaml and defaults
420
421
422
423
424
    kwargs = {
        key[len('NOMAD_'):].lower(): value
        for key, value in os.environ.items()
        if key.startswith('NOMAD_')
    }
425

426
    for key, value in kwargs.items():
427
        apply(key, value)
428

429
430
    check_config()

431
432

load_config()