admin.py 3.9 KB
Newer Older
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
import click
16
import datetime
17

18
from nomad import processing as proc, search, datamodel, infrastructure, utils, config
19
20
21
22
23
24
25
26
27

from nomad.cli.cli import cli


@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
28
29
30


@admin.command(help='(Re-)index all calcs.')
31
32
@click.option('--dry', help='Doe not index, only compute entries.', is_flag=True)
def index(dry):
33
34
35
36
37
38
39
40
41
    infrastructure.setup_logging()
    infrastructure.setup_mongo()
    infrastructure.setup_elastic()
    infrastructure.setup_repository_db()

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

    def calc_generator():
42
43
44
45
        with utils.ETA(all_calcs, '   index %10d or %10d calcs, ETA %s') as eta:
            for calc in proc.Calc.objects():
                eta.add()
                yield datamodel.CalcWithMetadata(**calc.metadata)
46

47
48
49
50
51
52
53
    if dry:
        for _ in calc_generator():
            pass
        failed = 0
    else:
        failed = search.index_all(calc_generator())

54
55
    print('')
    print('indexing completed, %d failed entries' % failed)
56
57


58
@admin.group(help='Generate scripts and commands for nomad operation.')
59
60
61
62
def ops():
    pass


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@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))


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
@ops.command(help=(
    'Generate a proxy pass config for apache2 reverse proxy servers. This can be used to '
    'generate partial apache2 configs, e.g. via ... > /etc/https/conf.d/nomad.conf.'))
@click.option('--nginx', is_flag=True, help='Provide config for apache2, default is apache.')
@click.option('--prefix', type=str, default='uploads', help='The path prefix under which everything is proxy passed.')
@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.')
def proxy_pass(nginx, prefix, host, port):
    if not nginx:
        print('''\
ProxyPass "/{0}" "http://{1}:{2}/{0}"
ProxyPassReverse "/{0}" "http://{1}:{2}/{0}"
<Proxy http://{1}:{2}/{0}>
    ProxyPreserveHost On
    Order deny,allow
    Allow from all
</Proxy>'''.format(prefix, host, port))
    else:
        print('''\
    location /{0} {{
        client_max_body_size 35g;
        proxy_pass http://{1}:{2};
        proxy_set_header Host $host;
        proxy_pass_request_headers on;
        proxy_buffering off;
        proxy_request_buffering off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 600;
        proxy_send_timeout 3600;
        proxy_read_timeout 600;
        send_timeout 3600;
    }}'''.format(prefix, host, port))