admin.py 2.85 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
from flask import request
16
from flask_restplus import abort, Resource, fields
17
18
19
20

from nomad import infrastructure, config

from .app import api
21
from .auth import admin_login_required
22
23
24
25
26


ns = api.namespace('admin', description='Administrative operations')


Markus Scheidgen's avatar
Markus Scheidgen committed
27
@ns.route('/reset')
Markus Scheidgen's avatar
Markus Scheidgen committed
28
class AdminResetResource(Resource):
Markus Scheidgen's avatar
Markus Scheidgen committed
29
30
31
    @api.doc('exec_reset_command')
    @api.response(200, 'Reset performed')
    @api.response(400, 'Reset not available/disabled')
32
    @admin_login_required
Markus Scheidgen's avatar
Markus Scheidgen committed
33
    def post(self):
34
        """
35
        The ``reset`` command will attempt to clear the contents of all databased and
36
37
        indices.

38
        Nomad can be configured to disable reset and the command might not be available.
Markus Scheidgen's avatar
Markus Scheidgen committed
39
40
41
42
        """
        if config.services.disable_reset:
            abort(400, message='Operation is disabled')

Markus Scheidgen's avatar
Markus Scheidgen committed
43
        infrastructure.reset(repo_content_only=True)
Markus Scheidgen's avatar
Markus Scheidgen committed
44
45
46
47
48

        return dict(messager='Reset performed.'), 200


@ns.route('/remove')
Markus Scheidgen's avatar
Markus Scheidgen committed
49
class AdminRemoveResource(Resource):
Markus Scheidgen's avatar
Markus Scheidgen committed
50
51
52
    @api.doc('exec_remove_command')
    @api.response(200, 'Remove performed')
    @api.response(400, 'Remove not available/disabled')
53
    @admin_login_required
Markus Scheidgen's avatar
Markus Scheidgen committed
54
55
    def post(self):
        """
56
        The ``remove``command will attempt to remove all databases. Expect the
57
58
        api to stop functioning after this request.

59
        Nomad can be configured to disable remove and the command might not be available.
60
        """
Markus Scheidgen's avatar
Markus Scheidgen committed
61
62
63
64
65
66
67

        if config.services.disable_reset:
            abort(400, message='Operation is disabled')

        infrastructure.remove()

        return dict(messager='Remove performed.'), 200
68
69
70
71
72
73
74


pidprefix_model = api.model('PidPrefix', {
    'prefix': fields.Integer(description='The prefix. All new calculations will get an id that is greater.', required=True)
})


75
# TODO remove after migration
76
77
78
79
80
81
@ns.route('/pidprefix')
class AdminPidPrefixResource(Resource):
    @api.doc('exec_pidprefix_command')
    @api.response(200, 'Pid prefix set')
    @api.response(400, 'Bad pid prefix data')
    @api.expect(pidprefix_model)
82
    @admin_login_required
83
84
85
86
87
88
89
90
91
92
    def post(self):
        """
        The ``pidprefix``command will set the pid counter to the given value.

        This might be useful while migrating data with old pids.
        """

        infrastructure.set_pid_prefix(**request.get_json())

        return dict(messager='PID prefix set.'), 200