forked from omegaup/omegaup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-environment.py
executable file
·311 lines (277 loc) · 11.2 KB
/
bootstrap-environment.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
# pylint: disable=invalid-name
# This program is intended to be invoked from the console, not to be used as a
# module.
'''
A tool to run an import script to populate the database with objects.
'''
import argparse
import errno
import grp
import json
import logging
import os
import shutil
import subprocess
import time
from typing import Any, BinaryIO, Dict, Mapping, ItemsView, Optional
import requests
OMEGAUP_ROOT = os.path.abspath(os.path.join(__file__, '..', '..'))
OMEGAUP_RUNTIME_ROOT = '/var/lib/omegaup'
class ScopedFiles:
'''
A RAII wrapper over a map of POST names to filenames. Upon entering, it
creates a mapping from POST names to Python file objects, which are closed
on exit.
'''
def __init__(self, files: Optional[Mapping[str, str]]):
self.__files = files
self.files: Optional[Dict[str, BinaryIO]] = None
def __enter__(self) -> 'ScopedFiles':
if self.__files:
self.files = {}
for name, filename in self.__files.items():
self.files[name] = open(os.path.join(OMEGAUP_ROOT, filename),
'rb')
return self
def __exit__(self, *exc_info: Any) -> None:
if self.files:
for _, f in self.files.items():
f.close()
class Session:
'''
A context manager that represents an omegaUp user session.
Within the context, API requests can be performed as the user.
'''
def __init__(
self,
args: argparse.Namespace,
username: Optional[str],
password: Optional[str],
token: Optional[str],
):
# This is a false positive.
# pylint: disable=abstract-class-instantiated
self.jar = requests.cookies.RequestsCookieJar()
self.url = args.root_url.rstrip('/')
if token is not None:
self.token: Optional[str] = token
else:
self.token = None
request: Dict[str, Any] = {
'api': '/user/login',
'params': {
'usernameOrEmail': username,
'password': password,
}
}
result = self.request(request['api'], request['params'])
assert result and result['status'] == 'ok', (request, result)
def __enter__(self) -> 'Session':
return self
def __exit__(self, *exc_info: Any) -> None:
pass
def request(
self,
api: str,
data: Optional[Dict[str, str]] = None,
files: Optional[Mapping[str, str]] = None,
) -> Optional[Dict[str, Any]]:
'''Performs an API request.'''
logging.debug('Requesting %s: %s', api, data)
headers = {}
if self.token is not None:
headers['Authorization'] = f'token {self.token}'
if data:
with ScopedFiles(files) as f:
req = requests.post(f'{self.url}/api{api}',
files=f.files,
data=data,
cookies=self.jar,
headers=headers,
timeout=(3, 9))
else:
req = requests.get(f'{self.url}/api{api}',
cookies=self.jar,
headers=headers,
timeout=(3, 9))
cookies: ItemsView[str, str] = req.cookies.items() # type: ignore
for name, value in cookies:
self.jar[name] = value
if req.status_code == 404:
return None
try:
result: Dict[str, Any] = req.json()
except: # noqa: bare-except
logging.exception('Failed to parse json: %s', req.text)
raise
logging.debug('Result: %s', result)
return result
def _does_resource_exist(s: Session, request: Mapping[str, Any]) -> bool:
'''Returns whether a resource already exist.'''
api_endpoint = request['api'].lower()
if not api_endpoint.endswith('/'):
api_endpoint += '/'
if api_endpoint == '/problem/create/':
if s.request('/problem/details/',
{'problem_alias': request['params']['problem_alias']}):
logging.warning('Problem %s exists, skipping',
request['params']['problem_alias'])
return True
if api_endpoint == '/contest/create/':
if s.request('/contest/adminDetails/',
{'contest_alias': request['params']['alias']}):
logging.warning('Contest %s exists, skipping',
request['params']['alias'])
return True
if api_endpoint == '/course/create/':
if s.request('/course/adminDetails/',
{'alias': request['params']['alias']}):
logging.warning('Course %s exists, skipping',
request['params']['alias'])
return True
if api_endpoint == '/course/createassignment/':
if s.request(
'/course/assignmentDetails/', {
'course': request['params']['course_alias'],
'assignment': request['params']['alias']
}):
logging.warning('Assignment %s exists, skipping',
request['params']['alias'])
return True
if api_endpoint == '/user/create/':
if s.request('/user/profile/',
{'username': request['params']['username']}):
logging.warning('User %s exists, skipping',
request['params']['username'])
return True
return False
def _process_one_request(s: Session, request: Mapping[str, Any],
now: float) -> None:
'''Invokes a single request specified in |request|.'''
if _does_resource_exist(s, request):
return
# Date parameters need some special handling
for key, val in request['params'].items():
if isinstance(val, str) and val.startswith('$NOW$'):
# Replace $NOW$ with the current timestamp, adding an
# optional number of seconds.
tokens = val.split('+')
timestamp = now
if len(tokens) == 2:
timestamp += int(tokens[1])
val = int(timestamp)
request['params'][key] = val
logging.info('invoking one request %r', request)
result = s.request(
request['api'],
data=request['params'],
files=(request['files'] if 'files' in request else None))
fail_ok = 'fail_ok' in request and request['fail_ok']
status = 'error'
if result and 'status' in result:
status = result['status']
if status != 'ok':
if fail_ok:
logging.warning('Request %r failed, continuing. '
'Result is %r', request, result)
else:
assert status == 'ok', (request, result)
def _run_script(path: str, args: argparse.Namespace, now: float) -> None:
'''Runs a single script specified in |path|'''
with open(path, 'r', encoding='utf-8') as f:
script = json.load(f)
for session in script:
logging.info('running one session...')
with Session(args,
session.get('username'),
session.get('password'),
token=session.get('token')) as s:
for request in session['requests']:
_process_one_request(s, request, now)
def _purge_old_problems() -> None:
logging.info('Purging old problems')
# Removing directories requires the user to be in the 'www-data' group.
can_delete = 'www-data' in (grp.getgrgid(grid).gr_name
for grid in os.getgroups())
problems_root = os.path.join(OMEGAUP_RUNTIME_ROOT, 'problems.git')
if not os.path.isdir(OMEGAUP_RUNTIME_ROOT):
raise RuntimeError('Please run this script inside the VM / container'
) from FileNotFoundError(errno.ENOENT,
os.strerror(errno.ENOENT),
OMEGAUP_RUNTIME_ROOT)
if not os.path.isdir(problems_root):
return
for alias in os.listdir(problems_root):
path = os.path.join(problems_root, alias)
logging.debug('Removing %s', path)
if can_delete:
shutil.rmtree(path)
else:
subprocess.check_call(['/usr/bin/sudo', '/bin/rm', '-rf', path])
def _purge_old_submissions() -> None:
logging.info('Purging old submissions')
# Removing directories requires the user to be in the 'www-data' group.
can_delete = 'www-data' in (grp.getgrgid(grid).gr_name
for grid in os.getgroups())
submissions_root = os.path.join(OMEGAUP_RUNTIME_ROOT, 'submissions')
for root, _, filenames in os.walk(submissions_root):
for filename in filenames:
path = os.path.join(root, filename)
logging.debug('Removing %s', path)
if can_delete:
os.unlink(path)
else:
subprocess.check_call(
['/usr/bin/sudo', '/usr/bin/unlink', path])
def _main() -> None:
'''Main entrypoint.'''
parser = argparse.ArgumentParser()
parser.add_argument('--root-url',
type=str,
default='http://localhost:8001/')
parser.add_argument('--verbose', action='store_true')
parser.add_argument('--purge',
action='store_true',
help='Also purges and re-creates the database')
parser.add_argument('--mysql-config-file',
default=None,
help='.my.cnf file that stores credentials')
parser.add_argument('--username', default=None, help='MySQL username')
parser.add_argument('--password', default=None, help='MySQL password')
parser.add_argument(
'scripts',
metavar='SCRIPT',
type=str,
nargs='*',
default=[os.path.join(OMEGAUP_ROOT, 'stuff/bootstrap.json')],
help=('The JSON script with requests to '
'pre-populate the database'))
args = parser.parse_args()
now = time.time()
if args.verbose:
logging.getLogger().setLevel('DEBUG')
if args.purge:
_purge_old_problems()
_purge_old_submissions()
db_migrate_args = [
os.path.join(OMEGAUP_ROOT, 'stuff/db-migrate.py'),
'--kill-blocking-connections',
]
for name, value in [('--username', args.username),
('--password', args.password),
('--mysql-config-file', args.mysql_config_file)]:
if value is not None:
db_migrate_args.extend([name, value])
if args.verbose:
db_migrate_args.append('--verbose')
logging.info('Purging database...')
subprocess.check_call(db_migrate_args + ['purge'])
logging.info('Migrating database...')
subprocess.check_call(db_migrate_args +
['migrate', '--development-environment'])
for path in args.scripts:
logging.info('Running script %s...', path)
_run_script(path, args, now)
if __name__ == '__main__':
_main()