-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.py
160 lines (126 loc) · 4.26 KB
/
dashboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
import socket
import logging
import logging.config
from logging import handlers
import sqlite3
import json
import requests
from cli.client import DrasticClient
from contextlib import closing
from flask import Flask, jsonify, request, session, g, redirect, url_for, render_template, Response
app = Flask(__name__)
app.config.from_object(__name__)
app.logger.setLevel(logging.DEBUG)
handler = handlers.RotatingFileHandler(
os.path.join(app.instance_path, 'dashboard.log'),
maxBytes=1024 * 1024 * 100,
backupCount=20)
app.logger.addHandler(handler)
app.config.update(dict(
DATABASE=os.path.join(app.instance_path, 'dashboard.db'),
DEBUG=True,
USERNAME='admin',
PASSWORD='default',
SECRET_KEY='INSECURE_DEVELOPMENT_KEY',
PROPAGATE_EXCEPTIONS=True
))
drastic_url = os.getenv('DRASTIC_URL', 'http://localhost')
drastic_user = os.getenv('DRASTIC_USER', 'worker')
drastic_password = os.getenv('DRASTIC_PASSWORD', 'password')
ciber_ftp_over_http_url = os.getenv('FTP_OVER_HTTP_URL', 'http://localhost')
__client = None
def get_client():
global __client
if __client is None:
myclient = DrasticClient(drastic_url)
res = myclient.authenticate(drastic_user, drastic_password)
if not res.ok():
raise IOError("Drastic authentication failed: {0}".format(res.msg()))
__client = myclient
return __client
@app.errorhandler(404)
def page_not_found(error):
return 'This route does not exist {}'.format(request.url), 404
def connect_db():
"""Connects to the specific database."""
logging.info(str(app.config['DATABASE']))
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv
@app.cli.command()
def init_db():
db = get_db()
with app.open_instance_resource('dashboard.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
db.close()
logging.warn('Initialized the database.')
def get_db():
"""Opens a new database connection if there is none yet for the
current application context.
"""
# db = getattr(g, '_database', None)
# if db is None:
# db = g._database = connect_db()
# return db
return connect_db()
# @app.teardown_appcontext
def close_db(error):
"""Closes the database again at the end of the request."""
db = getattr(g, '_database', None)
if db is not None:
db.close()
@app.route('/dashboard/')
def welcome():
return render_template('dashboard.html')
@app.route('/dashboard/httpftp_rgroups', methods=['GET'])
def get_httpftp_recordgroups():
res = requests.get('{0}/'.format(ciber_ftp_over_http_url))
res.raise_for_status()
return Response(res.text, mimetype='application/json')
@app.route('/dashboard/drastic_rgroups', methods=['GET'])
def get_drastic_rgroups():
return get_drastic_path('/NARA')
def get_drastic_path(path):
path = path[:-1] if path.endswith('?') else path
res = get_client().ls(path)
if res.code() in [404, 403]: # object probably deleted
logging.warn("Dropping task for an object that gives a 403/403: {0}".format(path))
return
if not res.ok():
raise IOError(str(res))
return jsonify(res.json())
@app.route('/dashboard/rgroup_status')
def collections():
return render_template('rgroup_status.html')
@app.route('/dashboard/drastic_rgroup_metadata', methods=['POST'])
def get_drastic_rgroup_metadata():
drastic_rgroup_name = request.form['rg']
res = get_client().get_cdmi('/NARA/'+drastic_rgroup_name)
if not res.ok():
raise IOError(str(res))
return jsonify(res.json())
@app.route('/dashboard/api/ingest_rgroup')
def ingest_rgroup():
task_id = uuid()
args = (2, 2)
kwargs = {}
message = json.dumps((args, kwargs, None),
application_headers={
'lang': 'py',
'task': 'proj.tasks.add',
'argsrepr': repr(args),
'kwargsrepr': repr(kwargs),
'origin': '@'.join([os.getpid(), socket.gethostname()])
},
properties={
'correlation_id': task_id,
'content_type': 'application/json',
'content_encoding': 'utf-8',
})
result = {}
result['jobId'] = 'placeholder'
return jsonify(result)
if __name__ == '__main__':
app.run("0.0.0.0", processes=5)