admin.py 31.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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
14
import typing
15
import click
16
import datetime
Markus Scheidgen's avatar
Markus Scheidgen committed
17
18
import elasticsearch_dsl
import elasticsearch
19
import sys
20
import threading
21

22
from nomad import processing as proc, search, datamodel, infrastructure, utils, config
23
from nomad.cli.cli import cli
24
25
26
27
from nomad.datamodel.material import Material, Calculation
from nomad.datamodel.encyclopedia import EncyclopediaMetadata
from nomad.search import material_document
from nomad.datamodel.material import Material, Calculation, Method, Properties, IdealizedStructure, Energies, Workflow, Bulk
28
29


Markus Scheidgen's avatar
Markus Scheidgen committed
30
31
def __run_parallel(
        uploads, parallel: int, callable, label: str):
32
33
34
35
36
37
38
39
    if isinstance(uploads, (tuple, list)):
        uploads_count = len(uploads)

    else:
        uploads_count = uploads.count()
        uploads = list(uploads)  # copy the whole mongo query set to avoid cursor timeouts

    cv = threading.Condition()
Markus Scheidgen's avatar
Markus Scheidgen committed
40
    threads: typing.List[threading.Thread] = []
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    state = dict(
        completed_count=0,
        skipped_count=0,
        available_threads_count=parallel)

    logger = utils.get_logger(__name__)

    print('%d uploads selected, %s ...' % (uploads_count, label))

    def process_upload(upload: proc.Upload):
        logger.info('%s started' % label, upload_id=upload.upload_id)

        completed = False
Markus Scheidgen's avatar
Markus Scheidgen committed
55
        if callable(upload, logger):
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
            completed = True

        with cv:
            state['completed_count'] += 1 if completed else 0
            state['skipped_count'] += 1 if not completed else 0
            state['available_threads_count'] += 1

            print(
                '   %s %s and skipped %s of %s uploads' %
                (label, state['completed_count'], state['skipped_count'], uploads_count))

            cv.notify()

    for upload in uploads:
        with cv:
            cv.wait_for(lambda: state['available_threads_count'] > 0)
            state['available_threads_count'] -= 1
            thread = threading.Thread(target=lambda: process_upload(upload))
            threads.append(thread)
            thread.start()

    for thread in threads:
        thread.join()


Markus Scheidgen's avatar
Markus Scheidgen committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
def __run_processing(
        uploads, parallel: int, process, label: str, reprocess_running: bool = False):

    def run_process(upload, logger):
        if upload.process_running and not reprocess_running:
            logger.warn(
                'cannot trigger %s, since the upload is already/still processing' % label,
                current_process=upload.current_process,
                current_task=upload.current_task, upload_id=upload.upload_id)
            return False
        else:
            upload.reset()
            process(upload)
            upload.block_until_complete(interval=.5)

            if upload.tasks_status == proc.FAILURE:
                logger.info('%s with failure' % label, upload_id=upload.upload_id)

            logger.info('%s complete' % label, upload_id=upload.upload_id)
            return True

    __run_parallel(uploads, parallel=parallel, callable=run_process, label=label)


105
106
107
108
109
@cli.group(help='''The nomad admin commands to do nasty stuff directly on the databases.
                     Remember: With great power comes great responsibility!''')
@click.pass_context
def admin(ctx):
    pass
110
111


112
113
114
115
116
117
118
119
120
121
122
123
124
125
@admin.command(help='Reset/remove all databases.')
@click.option('--remove', is_flag=True, help='Do not just reset all dbs, but also remove them.')
@click.option('--i-am-really-sure', is_flag=True, help='Must be set for the command to to anything.')
def reset(remove, i_am_really_sure):
    if not i_am_really_sure:
        print('You do not seem to be really sure about what you are doing.')
        sys.exit(1)

    infrastructure.setup_mongo()
    infrastructure.setup_elastic()

    infrastructure.reset(remove)


126
127
128
129
130
131
@admin.command(help='Reset all "stuck" in processing uploads and calc in low level mongodb operations.')
@click.option('--zero-complete-time', is_flag=True, help='Sets the complete time to epoch zero.')
def reset_processing(zero_complete_time):
    infrastructure.setup_mongo()

    def reset_collection(cls):
132
        in_processing = cls.objects(process_status__in=[proc.PROCESS_RUNNING, proc.base.PROCESS_CALLED])
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
        print('%d %s processes need to be reset due to incomplete process' % (in_processing.count(), cls.__name__))
        in_processing.update(
            process_status=None,
            current_process=None,
            worker_hostname=None,
            celery_task_id=None,
            errors=[], warnings=[],
            complete_time=datetime.datetime.fromtimestamp(0) if zero_complete_time else datetime.datetime.now(),
            current_task=None,
            tasks_status=proc.base.CREATED)

        in_tasks = cls.objects(tasks_status__in=[proc.PENDING, proc.RUNNING])
        print('%d %s processes need to be reset due to incomplete tasks' % (in_tasks.count(), cls.__name__))
        in_tasks.update(
            current_task=None,
            tasks_status=proc.base.CREATED,
            errors=[], warnings=[],
            complete_time=datetime.datetime.fromtimestamp(0) if zero_complete_time else datetime.datetime.now())

    reset_collection(proc.Calc)
    reset_collection(proc.Upload)


156
157
158
159
160
161
162
163
@admin.command(help='Check and lift embargo of data with expired embargo period.')
@click.option('--dry', is_flag=True, help='Do not lift the embargo, just show what needs to be done.')
@click.option('--parallel', default=1, type=int, help='Use the given amount of parallel processes. Default is 1.')
def lift_embargo(dry, parallel):
    infrastructure.setup_mongo()
    infrastructure.setup_elastic()

    request = search.SearchRequest()
Markus Scheidgen's avatar
Markus Scheidgen committed
164
    request.q = elasticsearch_dsl.Q('term', with_embargo=True) & elasticsearch_dsl.Q('term', published=True)
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    request.quantity('upload_id', 1000)
    result = request.execute()

    uploads_to_repack = []
    for upload_id in result['quantities']['upload_id']['values']:
        upload = proc.Upload.get(upload_id)
        embargo_length = upload.embargo_length
        if embargo_length is None:
            embargo_length = 36
            upload.embargo_length = 36

        if upload.upload_time + datetime.timedelta(days=int(embargo_length * 365 / 12)) < datetime.datetime.now():
            print('need to lift the embargo of %s (upload_time=%s, embargo=%d)' % (
                upload.upload_id, upload.upload_time, embargo_length))

            if not dry:
                proc.Calc._get_collection().update_many(
                    {'upload_id': upload_id},
                    {'$set': {'metadata.with_embargo': False}})
                uploads_to_repack.append(upload)
                upload.save()

187
188
                with upload.entries_metadata() as entries:
                    search.index_all(entries)
189
190
191
192
193

    if not dry:
        __run_processing(uploads_to_repack, parallel, lambda upload: upload.re_pack(), 're-packing')


194
@admin.command(help='(Re-)index all calcs.')
195
@click.option('--threads', type=int, default=1, help='Number of threads to use.')
196
197
@click.option('--dry', is_flag=True, help='Do not index, just compute entries.')
def index(threads, dry):
198
199
200
201
202
203
    infrastructure.setup_mongo()
    infrastructure.setup_elastic()

    all_calcs = proc.Calc.objects().count()
    print('indexing %d ...' % all_calcs)

204
    def elastic_updates():
205
206
207
        with utils.ETA(all_calcs, '   index %10d or %10d calcs, ETA %s') as eta:
            for calc in proc.Calc.objects():
                eta.add()
208
209
                entry_metadata = datamodel.EntryMetadata.m_from_dict(calc.metadata)
                entry = entry_metadata.a_elastic.create_index_entry().to_dict(include_meta=True)
210
211
                entry['_op_type'] = 'index'
                yield entry
Markus Scheidgen's avatar
Markus Scheidgen committed
212

213
214
215
    if dry:
        for _ in elastic_updates():
            pass
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
    else:
        if threads > 1:
            print('  use %d threads' % threads)
            for _ in elasticsearch.helpers.parallel_bulk(
                    infrastructure.elastic_client, elastic_updates(), chunk_size=500,
                    thread_count=threads):
                pass
        else:
            elasticsearch.helpers.bulk(
                infrastructure.elastic_client, elastic_updates())
        search.refresh()

    print('')
    print('indexing completed')


@admin.command()
@click.option('--threads', type=int, default=1, help='Number of threads to use.')
@click.option('--code', multiple=True, type=str, help='Index only calculcations of given codes.')
@click.option('--dry', is_flag=True, help='Do not index, just compute entries.')
@click.option('--in-place', is_flag=True, default=False, help='Perform indexing in the current elastic search index. Meant only for small reindex operations.')
@click.option('-n', type=int, default=None, help='Number of calculations to process. Leave undefined to process all calculations.')
@click.option('--source',
              type=click.Choice(['mongo', 'es'], case_sensitive=True))
def index_materials(threads, code, dry, in_place, n, source):
    """(Re-)index all materials.

    This command will completely rebuild the materials index. The index is
    built from the material metainfo stored in MongoDB. The materials index can
    be used normally during the reindexing.
    """
    chunk_size = 500
    infrastructure.setup_mongo()
    client = infrastructure.setup_elastic()

    # In order to do the reindexing with zero downtime, two different indices
    # are rotated and an alias is used
    old_index_name = list(client.indices.get(config.elastic.materials_index_name).keys())[0]
    if in_place:
        target_index_name = old_index_name
    else:
        if old_index_name == config.elastic.materials_index_name + "_a":
            target_index_name = config.elastic.materials_index_name + "_b"
        elif old_index_name == config.elastic.materials_index_name + "_b":
            target_index_name = config.elastic.materials_index_name + "_a"
        else:
            raise ValueError(
                "Unrecognized index name accociated with the alias {}"
                .format(config.elastic.materials_index_name)
            )

    if source == "mongo":
        all_calcs = proc.Calc.objects().count()
        print('indexing materials from %d calculations ...' % all_calcs)

        # Bulk update
        def elastic_updates():
            with utils.ETA(all_calcs, '   index %10d of %10d calcs, ETA %s') as eta:
                mongo_db = infrastructure.mongo_client[config.mongo.db_name]
                mongo_collection = mongo_db['archive']
                i_calc = 0
                for mongo_archive in mongo_collection.find():
                    i_calc += 1
                    if n is not None:
                        if i_calc > n:
                            return
                    eta.add()

                    # Do not process entries that do not have the material
                    # information
                    try:
                        status = mongo_archive["section_metadata"]["encyclopedia"]["status"]
                        if status != EncyclopediaMetadata.status.type.success:
                            raise AttributeError
                    except (KeyError, AttributeError, IndexError):
                        continue

                    # Create material information
                    metadata = mongo_archive["section_metadata"]
                    encyclopedia = EncyclopediaMetadata.m_from_dict(metadata["encyclopedia"])
                    dft = metadata["dft"]
                    material: Material = Material()
                    material.material_id = encyclopedia.material.material_id
                    material.material_type = encyclopedia.material.material_type
                    material.material_name = encyclopedia.material.material_name
                    material.material_classification = encyclopedia.material.material_classification
                    material.formula = encyclopedia.material.formula
                    material.formula_reduced = encyclopedia.material.formula_reduced
                    material.species_and_counts = encyclopedia.material.species_and_counts
                    material.species = encyclopedia.material.species
                    enc_bulk = encyclopedia.material.bulk
                    if enc_bulk:
                        bulk = Bulk.m_from_dict(enc_bulk.m_to_dict())
                        material.m_add_sub_section(Material.bulk, bulk)

                    # Create calculation info for this entry
                    calc = Calculation()
                    calc.calc_id = metadata["calc_id"]
                    calc.upload_id = metadata["upload_id"]
                    mongo_calc = proc.Calc.get(calc.calc_id)
                    calc.published = mongo_calc["metadata"]["published"]
                    calc.with_embargo = mongo_calc["metadata"]["with_embargo"]
                    calc.owners = [mongo_calc["metadata"]["uploader"]] + mongo_calc["metadata"]["shared_with"]
                    enc_idealized_structure = encyclopedia.material.idealized_structure
                    idealized_structure = IdealizedStructure()
                    cell_volume = enc_idealized_structure.cell_volume
                    if cell_volume is not None:
                        idealized_structure.cell_volume = cell_volume
                    idealized_structure.lattice_parameters = enc_idealized_structure.lattice_parameters
                    calc.m_add_sub_section(Calculation.idealized_structure, idealized_structure)
                    enc_method = encyclopedia.method
                    method = Method.m_from_dict(enc_method.m_to_dict())
                    method.program_name = dft["code_name"]
                    method.program_version = dft["code_version"]
                    method.basis_set = dft["basis_set"]
                    calc.m_add_sub_section(Calculation.method, method)
                    enc_props = encyclopedia.properties

                    # Properties may not exist at all
                    if enc_props is not None:
                        properties = Properties()

                        # Energies may not be present in all calculations
                        try:
                            energies = Energies.m_from_dict(enc_props.energies.m_to_dict())
                            properties.m_add_sub_section(Properties.energies, energies)
                        except AttributeError:
                            pass

                        properties.has_electronic_dos = enc_props.electronic_dos is not None
                        properties.has_electronic_band_structure = enc_props.electronic_band_structure is not None
                        properties.has_thermodynamical_properties = enc_props.thermodynamical_properties is not None
                        atomic_density = enc_props.atomic_density
                        if atomic_density is not None:
                            properties.atomic_density = atomic_density
                        mass_density = enc_props.mass_density
                        if mass_density is not None:
                            properties.mass_density = mass_density
                        band_gap = enc_props.band_gap
                        if band_gap is not None:
                            properties.band_gap = band_gap
                        band_gap_direct = enc_props.band_gap_direct
                        if band_gap_direct is not None:
                            properties.band_gap_direct = band_gap_direct
                        calc.m_add_sub_section(Calculation.properties, properties)

                    workflow = Workflow()
                    workflow.workflow_type = encyclopedia.calculation.calculation_type
                    calc.m_add_sub_section(Calculation.workflow, workflow)
                    material.m_add_sub_section(Material.calculations, calc)

                    # Update entry that inserts the full material info if entry
                    # does not exists, otherwise only adds the calculation into the
                    # nested subdocument
                    entry = {}
                    entry['_op_type'] = 'update'
                    entry['_index'] = target_index_name
                    entry['_id'] = material.material_id
                    entry['_type'] = 'doc'
                    entry['_source'] = {
                        "upsert": material.m_to_dict(include_defaults=False, partial="es"),
                        "doc_as_upsert": False,
                        "script": {
                            "source": "ctx._source.calculations.add(params.calc)",
                            "params": {
                                "calc": calc.m_to_dict(include_defaults=False, partial="es")
                            },
                        }
                    }
                    yield entry
    elif source == "es":
        s = elasticsearch_dsl.Search(index=config.elastic.index_name)
        filters = [elasticsearch_dsl.Q("term", encyclopedia__status="success")]
        if code:
            filters.append(elasticsearch_dsl.Q("terms", dft__code_name=code))
        query = elasticsearch_dsl.Q(
            "bool",
            filter=filters,
        )
        s = s.query(query)
        s = s.extra(**{
            "size": 0,
        })
        all_calcs = s.execute().hits.total
        print('indexing materials from %d calculations ...' % all_calcs)

        def elastic_updates():
            with utils.ETA(all_calcs, '   index %10d of %10d calcs, ETA %s', chunk_size) as eta:

                s = elasticsearch_dsl.Search(index=config.elastic.index_name)
                filters = [elasticsearch_dsl.Q("term", encyclopedia__status="success")]
                if code:
                    filters.append(elasticsearch_dsl.Q("terms", dft__code_name=code))
                query = elasticsearch_dsl.Q(
                    "bool",
                    filter=filters,
                )
                s = s.query(query)
                s = s.extra(**{
                    "size": chunk_size,
                })
                i_calc = 0
                for hit in s.scan():
                    i_calc += 1
                    if n is not None:
                        if i_calc > n:
                            return
                    eta.add()

                    material: Material = Material()
                    calc = Calculation()

                    # Check that all required information exists. If not, the
                    # calculation is skipped.
                    try:
                        material.material_id = hit.encyclopedia.material.material_id
                        material.material_type = hit.encyclopedia.material.material_type
                        material.formula = hit.encyclopedia.material.formula
                        material.formula_reduced = hit.encyclopedia.material.formula_reduced
                        material.species_and_counts = hit.encyclopedia.material.species_and_counts
                        material.species = hit.encyclopedia.material.species
                        calc.calc_id = hit.calc_id
                        calc.upload_id = hit.upload_id
                        calc.published = hit.published
                        calc.with_embargo = hit.with_embargo
                        calc.owners = [x.user_id for x in hit.owners]
                        idealized_structure = IdealizedStructure.m_from_dict(hit.encyclopedia.material.idealized_structure.to_dict())
                        calc.m_add_sub_section(Calculation.idealized_structure, idealized_structure)

                        method = Method.m_from_dict(hit.encyclopedia.method.to_dict())
                        method.program_name = hit.dft.code_name
                        method.program_version = hit.dft.code_version
                        method.basis_set = hit.dft.basis_set
                        calc.m_add_sub_section(Calculation.method, method)

                        workflow = Workflow()
                        workflow.workflow_type = hit.encyclopedia.calculation.calculation_type
                        calc.m_add_sub_section(Calculation.workflow, workflow)
                    except AttributeError:
                        continue

                    # Not all materials have a name
                    try:
                        material.material_name = hit.encyclopedia.material.material_name
                    except AttributeError:
                        pass

                    # Not all materials have a bulk section
                    try:
                        bulk = Bulk.m_from_dict(hit.encyclopedia.material.bulk)
                        material.m_add_sub_section(Material.bulk, bulk)
                    except AttributeError:
                        pass

                    # Properties may not exist at all
                    try:
                        enc_properties = hit.encyclopedia.properties
                    except AttributeError:
                        pass
                    else:
                        properties = Properties()

                        # Energies may not be present in all calculations
                        try:
                            energies = Energies.m_from_dict(enc_properties.energies.to_dict())
                            properties.m_add_sub_section(Properties.energies, energies)
                        except AttributeError:
                            pass

                        # Gather the boolean flags that indicate the presence of
                        # certain properties
                        try:
                            properties.has_electronic_dos = enc_properties.electronic_dos is not None
                        except AttributeError:
                            properties.has_electronic_dos = False
                        try:
                            properties.has_electronic_band_structure = enc_properties.electronic_band_structure is not None
                        except AttributeError:
                            properties.has_electronic_band_structure = False
                        try:
                            properties.has_thermodynamical_properties = enc_properties.thermodynamical_properties is not None
                        except AttributeError:
                            properties.has_thermodynamical_properties = False

                        # Not all materials have an atomic density
                        try:
                            properties.atomic_density = enc_properties.atomic_density
                        except AttributeError:
                            pass

                        # Not all materials have a mass density
                        try:
                            properties.mass_density = enc_properties.mass_density
                        except AttributeError:
                            pass

                        # Not all materials have band gaps
                        try:
                            properties.band_gap = enc_properties.band_gap
                        except AttributeError:
                            pass

                        # Not all materials have band gap type
                        try:
                            properties.band_gap_direct = enc_properties.band_gap_direct
                        except AttributeError:
                            pass

                        calc.m_add_sub_section(Calculation.properties, properties)

                    material.m_add_sub_section(Material.calculations, calc)

                    # Update entry that inserts the full material info if entry
                    # does not exists, otherwise only adds the calculation into
                    # the nested subdocument
                    entry = {}
                    entry['_op_type'] = 'update'
                    entry['_index'] = target_index_name
                    entry['_id'] = material.material_id
                    entry['_type'] = 'doc'
                    entry['_source'] = {
                        "upsert": material.m_to_dict(include_defaults=False, partial="es"),
                        "doc_as_upsert": False,
                        "script": {
                            "params": {
                                "calc": calc.m_to_dict(include_defaults=False, partial="es")
                            },
                        }
                    }
                    if in_place:
                        entry['_source']["script"]["source"] = "ctx._source.calculations.removeIf(x -> x.calc_id == params.calc.calc_id); ctx._source.calculations.add(params.calc)"
                    else:
                        entry['_source']["script"]["source"] = "ctx._source.calculations.add(params.calc)"
                    yield entry

    if dry:
        for _ in elastic_updates():
553
            pass
554
    else:
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
        # Create new index into which the data will be inserted. The old index will
        # keep working while the new index is being built
        material_document.init(index=target_index_name)

        if threads > 1:
            print('  use %d threads' % threads)
            for _ in elasticsearch.helpers.parallel_bulk(
                    infrastructure.elastic_client, elastic_updates(), chunk_size=chunk_size,
                    thread_count=threads):
                pass
        else:
            elasticsearch.helpers.bulk(
                infrastructure.elastic_client, elastic_updates())
            search.refresh()

        # Changes materials index alias to point to the new index and remove the
        # old index.
        if not in_place:
            new_index = elasticsearch_dsl.Index(target_index_name)
            new_index.put_alias(name=config.elastic.materials_index_name)
            old_index = elasticsearch_dsl.Index(old_index_name)
            old_index.delete()
577

578
    print('')
579
    print('indexing completed')
580
581


582
@admin.group(help='Generate scripts and commands for nomad operation.')
583
584
585
586
def ops():
    pass


587
588
589
590
591
@ops.group(help='Tools for managing the DOS similarity data.')
def similarity():
    pass


592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
@ops.command(help=('Dump the mongo (calculation metadata) db.'))
@click.option('--restore', is_flag=True, help='Do not dump, but restore.')
def dump(restore: bool):
    date_str = datetime.datetime.utcnow().strftime('%Y_%m_%d')
    print('mongodump --host {} --port {} --db {} -o /backup/fairdi/mongo/{}'.format(
        config.mongo.host, config.mongo.port, config.mongo.db_name, date_str))


@ops.command(help=('Restore the mongo (calculation metadata) db.'))
@click.argument('PATH_TO_DUMP', type=str, nargs=1)
def restore(path_to_dump):
    print('mongorestore --host {} --port {} --db {} {}'.format(
        config.mongo.host, config.mongo.port, config.mongo.db_name, path_to_dump))


607
608
609
610
611
612
613
614
615
616
@ops.command(help=('Generate an nginx.conf to serve the GUI and proxy pass to API container.'))
@click.option('--prefix', type=str, default='/example_nomad', help='Url path prefix. Default is /example_nomd, can be empty str.')
def nginx_conf(prefix):
    prefix = prefix.rstrip('/')
    prefix = '/%s' % prefix.lstrip('/')

    print('''\
server {{
    listen        80;
    server_name   www.example.com;
617
    proxy_set_header Host $host;
618

619
620
    location / {{
        proxy_pass http://app:8000;
621
622
    }}

623
624
    location ~ {1}\\/?(gui)?$ {{
        rewrite ^ {1}/gui/ permanent;
625
626
    }}

627
628
629
630
631
632
633
634
635
    location {1}/gui/ {{
        proxy_intercept_errors on;
        error_page 404 = @redirect_to_index;
        proxy_pass http://app:8000;
    }}

    location @redirect_to_index {{
        rewrite ^ {1}/gui/index.html break;
        proxy_pass http://app:8000;
636
637
    }}

638
    location ~ \\/gui\\/(service-worker\\.js|meta\\.json)$ {{
639
640
641
642
643
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;
644
        proxy_pass http://app:8000;
645
646
    }}

647
    location ~ \\/api\\/uploads\\/?$ {{
648
649
        client_max_body_size 35g;
        proxy_request_buffering off;
650
        proxy_pass http://app:8000;
651
652
    }}

653
    location ~ \\/api\\/(raw|archive) {{
654
        proxy_buffering off;
655
        proxy_pass http://app:8000;
656
657
    }}

658
    location ~ \\/api\\/mirror {{
659
        proxy_buffering off;
660
661
        proxy_read_timeout 600;
        proxy_pass http://app:8000;
662
    }}
663
}}'''.format(prefix))
664
665
666


@ops.command(help=('Generate a proxy pass config for apache2 reverse proxy servers.'))
667
@click.option('--prefix', type=str, default='app', help='The path prefix under which everything is proxy passed.')
668
669
@click.option('--host', type=str, default='130.183.207.104', help='The host to proxy to.')
@click.option('--port', type=str, default='30001', help='The port to proxy to.')
670
671
def apache_conf(prefix, host, port):
    print('''\
672
673
674
675
ProxyPass "/{0}" "http://{1}:{2}/{0}"
ProxyPassReverse "/{0}" "http://{1}:{2}/{0}"
<Proxy http://{1}:{2}/{0}>
    ProxyPreserveHost On
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
    <IfModule !mod_access_compat.c>
         Require all granted
     </IfModule>
     <IfModule mod_access_compat.c>
         Order allow,deny
         Allow from all
     </IfModule>
</Proxy>

RequestHeader set "X-Forwarded-Proto" expr=%{{REQUEST_SCHEME}}
RequestHeader set "X-Forwarded-SSL" expr=%{{HTTPS}}

ProxyPass /fairdi/keycloak http://{1}:8002/fairdi/keycloak
ProxyPassReverse /fairdi/keycloak http://{1}:8002/fairdi/keycloak
<Proxy http://{1}:8002/app>
     ProxyPreserveHost On
     <IfModule !mod_access_compat.c>
         Require all granted
     </IfModule>
     <IfModule mod_access_compat.c>
         Order allow,deny
         Allow from all
     </IfModule>
</Proxy>

RewriteEngine on
RewriteCond %{QUERY_STRING} ^pid=([^&]+)$
RewriteRule ^/NomadRepository-1.1/views/calculation.zul$ /{0}/gui/entry/pid/%1? [R=301]

AllowEncodedSlashes On
706
'''.format(prefix, host, port))  # type: ignore
707
708
709
710
711
712
713


@ops.command(help='Updates the AFLOW prototype information using the latest online version and writes the results to a python module in the given FILEPATH.')
@click.argument('FILEPATH', nargs=1, type=str)
@click.option('--matches-only', is_flag=True, help='Only update the match information that depends on the symmetry analysis settings. Will not perform and online update.')
@click.pass_context
def prototypes_update(ctx, filepath, matches_only):
714
715
    from nomad.cli.admin import prototypes
    prototypes.update_prototypes(ctx, filepath, matches_only)
716

717
718

@ops.command(help='Updates the springer database in nomad.config.normalize.springer_db_path.')
719
720
721
@click.option('--max-n-query', default=10, type=int, help='Number of unsuccessful springer request before returning an error. Default is 10.')
@click.option('--retry-time', default=120, type=int, help='Time in seconds to retry after unsuccessful request. Default is 120.')
def springer_update(max_n_query, retry_time):
722
    from nomad.cli.admin import springer
723
    springer.update_springer(max_n_query, retry_time)
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741


@similarity.command(help='Updates the msgpack file containing the similarity information.')
@click.option('--dir', "-d", "input_dir", type=str, help='Path of the folder containing the raw similarity information files')
@click.option('--out', "-o", type=str, help='Path of the output msgpack file.')
@click.option('--verbose', is_flag=True, help='Enable verbose output.')
def update(input_dir, out, verbose):
    from nomad.cli.admin import similarity
    similarity.update(input_dir, out, verbose)


@similarity.command(help='Ingests the given similarity information from an msgpack file into MongoDB.')
@click.option('--in', "-i", "input_path", type=str, help='Path of the ingested msgpack file.')
@click.option('--batch_size', type=int, default=10000, help='Batch size for MongoDB bulk ingestion.')
@click.option('--verbose', is_flag=True, help='Enable verbose output.')
def ingest(input_path, batch_size, verbose):
    from nomad.cli.admin import similarity
    similarity.ingest(input_path, batch_size, verbose)