-
Notifications
You must be signed in to change notification settings - Fork 18
/
config.py
292 lines (257 loc) · 10 KB
/
config.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
import os
import copy
import glob
import logging
import pymongo
import datetime
import elasticsearch
from . import util
logging.basicConfig(
format='%(asctime)s %(name)16.16s %(filename)24.24s %(lineno)5d:%(levelname)4.4s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG,
)
log = logging.getLogger('scitran.api')
logging.getLogger('MARKDOWN').setLevel(logging.WARNING) # silence Markdown library
logging.getLogger('requests').setLevel(logging.WARNING) # silence Requests library
logging.getLogger('paste.httpserver').setLevel(logging.WARNING) # silence Paste library
# NOTE: Keep in sync with environment variables in sample.config file.
DEFAULT_CONFIG = {
'core': {
'debug': False,
'log_level': 'info',
'access_log_enabled': False,
'newrelic': None,
'drone_secret': None,
},
'site': {
'id': 'local',
'name': 'Local',
'api_url': 'https://localhost/api',
'central_url': 'https://sdmc.scitran.io/api',
'registered': False,
'ssl_cert': None
},
'queue': {
'max_retries': 3,
'retry_on_fail': False,
'prefetch': False
},
'auth': {
'auth_type': 'google',
'client_id': '1052740023071-n20pk8h5uepdua3r8971pc6jrf25lvee.apps.googleusercontent.com',
'id_endpoint': 'https://www.googleapis.com/plus/v1/people/me/openIdConnect',
'auth_endpoint': 'https://accounts.google.com/o/oauth2/auth',
'verify_endpoint': 'https://www.googleapis.com/oauth2/v1/tokeninfo',
},
'persistent': {
'db_uri': 'mongodb://localhost:9001/scitran',
'db_log_uri': 'mongodb://localhost:9001/logs',
'db_connect_timeout': '2000',
'db_server_selection_timeout': '3000',
'data_path': os.path.join(os.path.dirname(__file__), '../persistent/data'),
'elasticsearch_host': 'localhost:9200',
},
}
def apply_env_variables(config):
# Overwrite default config values with SCITRAN env variables if available
for outer_key, scoped_config in config.iteritems():
try:
for inner_key in scoped_config:
key = 'SCITRAN_' + outer_key.upper() + '_' + inner_key.upper()
if key in os.environ:
value = os.environ[key]
if value.lower() == 'true':
value = True
elif value.lower() == 'false':
value = False
elif value.lower() == 'none':
value = None
config[outer_key][inner_key] = value
except Exception: # pylint: disable=broad-except
# ignore uniterable keys like `created` and `modified`
pass
return config
# Create config for startup, will be merged with db config when db is available
__config = apply_env_variables(copy.deepcopy(DEFAULT_CONFIG))
__config_persisted = False
__last_update = datetime.datetime.utcfromtimestamp(0)
if not os.path.exists(__config['persistent']['data_path']):
os.makedirs(__config['persistent']['data_path'])
log.setLevel(getattr(logging, __config['core']['log_level'].upper()))
db = pymongo.MongoClient(
__config['persistent']['db_uri'],
j=True, # Requests only return once write has hit the DB journal
connectTimeoutMS=__config['persistent']['db_connect_timeout'],
serverSelectionTimeoutMS=__config['persistent']['db_server_selection_timeout'],
connect=False, # Connect on first operation to avoid multi-threading related errors
).get_default_database()
log.debug(str(db))
log_db = pymongo.MongoClient(
__config['persistent']['db_log_uri'],
j=True, # Requests only return once write has hit the DB journal
connectTimeoutMS=__config['persistent']['db_connect_timeout'],
serverSelectionTimeoutMS=__config['persistent']['db_server_selection_timeout'],
connect=False, # Connect on first operation to avoid multi-threading related errors
).get_default_database()
log.debug(str(log_db))
es = elasticsearch.Elasticsearch([__config['persistent']['elasticsearch_host']])
# validate the lists of json schemas
schema_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../raml/schemas')
expected_mongo_schemas = set([
'acquisition.json',
'analysis.json',
'collection.json',
'container.json',
'file.json',
'group.json',
'note.json',
'permission.json',
'project.json',
'session.json',
'subject.json',
'user.json',
'avatars.json',
'tag.json'
])
expected_input_schemas = set([
'acquisition.json',
'acquisition-update.json',
'analysis.json',
'analysis-job.json',
'avatars.json',
'collection.json',
'collection-update.json',
'container.json',
'device.json',
'file.json',
'group-new.json',
'group-update.json',
'modality.json',
'note.json',
'packfile.json',
'permission.json',
'project.json',
'project-template.json',
'project-update.json',
'session.json',
'session-update.json',
'subject.json',
'user-new.json',
'user-update.json',
'download.json',
'tag.json',
'enginemetadata.json',
'labelupload.json',
'uidupload.json',
'uidmatchupload.json',
'search.json'
])
mongo_schemas = set()
input_schemas = set()
# check that the lists of schemas are correct
for schema_filepath in glob.glob(schema_path + '/mongo/*.json'):
schema_file = os.path.basename(schema_filepath)
mongo_schemas.add(schema_file)
with open(schema_filepath, 'rU') as f:
pass
assert mongo_schemas == expected_mongo_schemas, '{} is different from {}'.format(mongo_schemas, expected_mongo_schemas)
for schema_filepath in glob.glob(schema_path + '/input/*.json'):
schema_file = os.path.basename(schema_filepath)
input_schemas.add(schema_file)
with open(schema_filepath, 'rU') as f:
pass
assert input_schemas == expected_input_schemas, '{} is different from {}'.format(input_schemas, expected_input_schemas)
def create_or_recreate_ttl_index(coll_name, index_name, ttl):
if coll_name in db.collection_names():
index_list = db[coll_name].index_information()
if index_list:
for index in index_list:
# search for index by given name
# example: "timestamp_1": {"key": [["timestamp", 1]], ...}
if index_list[index]['key'][0][0] == index_name:
if index_list[index].get('expireAfterSeconds', None) != ttl:
# drop existing, recreate below
db[coll_name].drop_index(index)
break
else:
# index exists with proper ttl, bail
return
db[coll_name].create_index(index_name, expireAfterSeconds=ttl)
def initialize_db():
log.info('Initializing database, creating indexes')
# TODO review all indexes
db.users.create_index('api_key.key')
db.projects.create_index([('gid', 1), ('name', 1)])
db.sessions.create_index('project')
db.sessions.create_index('uid')
db.sessions.create_index('created')
db.acquisitions.create_index('session')
db.acquisitions.create_index('uid')
db.acquisitions.create_index('collections')
db.jobs.create_index([('inputs.id',1), ('inputs.type', 1)])
db.jobs.create_index([('state', 1),('now', 1), ('modified', 1)])
db.gears.create_index('name')
db.batch.create_index('jobs')
if __config['core']['access_log_enabled']:
log_db.access_log.create_index('context.ticket_id')
# Must be kept in sync with jobs/rules.py
db.singletons.update({"_id" : "rules"}, {
'$setOnInsert': {
'rule_list': []
}
},
upsert=True
)
create_or_recreate_ttl_index('authtokens', 'timestamp', 604800)
create_or_recreate_ttl_index('uploads', 'timestamp', 60)
create_or_recreate_ttl_index('downloads', 'timestamp', 60)
now = datetime.datetime.utcnow()
db.groups.update_one({'_id': 'unknown'}, {'$setOnInsert': { 'created': now, 'modified': now, 'name': 'Unknown', 'roles': []}}, upsert=True)
db.sites.replace_one({'_id': __config['site']['id']}, {'name': __config['site']['name'], 'site_url': __config['site']['api_url']}, upsert=True)
def get_config():
global __last_update, __config, __config_persisted #pylint: disable=global-statement
now = datetime.datetime.utcnow()
if not __config_persisted:
initialize_db()
log.info('Persisting configuration')
db_config = db.singletons.find_one({'_id': 'config'})
if db_config is not None:
startup_config = copy.deepcopy(__config)
startup_config = util.deep_update(startup_config, db_config)
# Precedence order for config is env vars -> db values -> default
__config = apply_env_variables(startup_config)
else:
__config['created'] = now
__config['modified'] = now
db.singletons.replace_one({'_id': 'config'}, __config, upsert=True)
__config_persisted = True
__last_update = now
elif now - __last_update > datetime.timedelta(seconds=120):
log.debug('Refreshing configuration from database')
__config = db.singletons.find_one({'_id': 'config'})
__last_update = now
log.setLevel(getattr(logging, __config['core']['log_level'].upper()))
return __config
def get_public_config():
return {
'created': __config.get('created'),
'modified': __config.get('modified'),
'site': __config.get('site'),
'auth': __config.get('auth'),
}
def get_version():
return db.singletons.find_one({'_id': 'version'})
def get_item(outer, inner):
return get_config()[outer][inner]
def mongo_pipeline(table, pipeline):
"""
Temporary philosophical dupe with reporthandler.py.
Execute a mongo pipeline, check status, return results.
A workaround for wonky pymongo aggregation behavior.
"""
output = db.command('aggregate', table, pipeline=pipeline)
result = output.get('result')
if output.get('ok') != 1.0 or result is None:
raise Exception()
return result