-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
executable file
·279 lines (206 loc) · 7.93 KB
/
setup.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
#!/usr/bin/env python3
import os
import sys
import shutil
import fnmatch
import subprocess
import argparse
DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(DIR, 'config'))
sys.path.append(os.path.join(DIR, 'lib'))
from config import CONFIG as KEYS, load_executables
from conf import insert_conf
parser = argparse.ArgumentParser(description='An install script for epsilon.')
parser.add_argument('--prefix', default='/opt/epsilon', help='the prefix that epsilon should be installed under')
parser.add_argument('--noserver', default=False, action='store_true', help='don\'t install server')
parser.add_argument('--nojudge', default=False, action='store_true', help='don\'t install judge')
parser.add_argument('--nojail', default=False, action='store_true', help='don\'t build judge jail')
parser.add_argument('--nomanualjudge', default=False, action='store_true', help='don\'t install the manual judge')
parser.add_argument('action', help='"install" or "uninstall"')
opts = parser.parse_args()
opts.prefix = os.path.abspath(opts.prefix).rstrip('/')
BIN_PATH = '/usr/local/bin'
PERMS = [
('*', (644, 755, 'root')),
('./judge/execute-submission.sh', (755, 755, 'root')),
('./judge/jail-setup.sh', (744, 744, 'root')),
('./judge/jail-destroy.sh', (744, 744, 'root')),
('./judge/submissions', (777, 777, 'root')),
('./judge/scripts/*.sh', (755, 755, 'root')),
('./server/db/setup_db.sh', (755, 755, 'root')),
('./bin/epsilon-judge', (755, 755, 'root')),
('./bin/epsilon-server', (755, 755, 'root')),
('./bin/epsilon-manual-judge', (755, 755, 'root')),
]
PROG_LANGS = {
('js', 'JS'),
('python2', 'PYTHON2'),
('python3', 'PYTHON3'),
('ruby', 'RUBY'),
('perl', 'PERL'),
('java', 'JAVA'),
('mono', 'MONO'),
('octave', 'OCTAVE'),
}
KEY_EXPAND = [
'./config/config.ini',
'./bin/epsilon-*',
]
def log(txt):
sys.stdout.write("%s\n" % txt)
def fatal(error):
sys.stderr.write('error: %s\n' % error)
sys.exit(1)
def sh(cmd, cwd=None, die=True):
log(' '.join(cmd))
global opts
if cwd is None:
cwd = opts.prefix
sub = subprocess.Popen(cmd, cwd=cwd)
if sub.wait() != 0 and die:
fatal('command failed: %s' % ' '.join(cmd))
def sh_com(cmd, stdin, cwd=None, die=True):
log(' '.join(cmd))
global opts
if cwd is None:
cwd = opts.prefix
sub = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=cwd)
stdout, stderr = sub.communicate(stdin)
if sub.returncode != 0 and die:
fatal('command failed: %s' % ' '.join(cmd))
return stdout.decode('utf-8')
def updateperms(path):
global opts
for glob, (f, d, u) in reversed(PERMS):
if fnmatch.fnmatch(path, glob):
if os.path.isfile(path):
sh(['chmod', str(f), path])
else:
sh(['chmod', str(d), path])
sh(['chown', u, path])
break
def copy(path):
global opts
src = os.path.abspath(os.path.join(DIR, path))
dest = os.path.abspath(os.path.join(opts.prefix, path))
log('copying %s' % path)
found = False
for glob in KEY_EXPAND:
if fnmatch.fnmatch(path, glob):
found = True
break
if found:
with open(src, 'r', encoding='utf-8') as f:
txt = f.read()
res = insert_conf(txt, KEYS, filename=path)
with open(dest, 'w', encoding='utf-8') as f:
f.write(res)
else:
shutil.copyfile(src, dest)
def update(path):
global opts
src = os.path.abspath(os.path.join(DIR, path))
dest = os.path.abspath(os.path.join(opts.prefix, path))
log('updating %s' % dest)
if os.path.isfile(dest):
os.unlink(dest)
if not os.path.exists(dest):
os.mkdir(dest)
updateperms(path)
for f in os.listdir(src):
if os.path.isdir(os.path.join(src, f)):
update(os.path.join(path, f))
else:
if os.path.isdir(os.path.join(dest, f)):
shutil.rmtree(os.path.join(dest, f))
if os.path.isfile(os.path.join(dest, f)):
os.unlink(os.path.join(dest, f))
copy(os.path.join(path, f))
updateperms(os.path.join(path, f))
def setup_virtualenv(path):
global opts
path = os.path.abspath(os.path.join(opts.prefix, path))
if not os.path.exists(os.path.join(path, 'venv')):
log('setting up virtualenv at %s' % path)
sh(['virtualenv', '--no-site-packages', '-p', 'python3', 'venv'], cwd=path)
log('updating virtualenv requirements at %s' % path)
sh(['bash', '-c', '''
source ./venv/bin/activate
pip install -r requirements.txt
'''], cwd=path)
def install():
global opts
KEYS['PREFIX'] = opts.prefix
# TODO: make this optional if not installing judge
log('installing safeexec')
sh(['make'], cwd=os.path.join(DIR, 'judge/SafeExec'))
sh(['make', 'install'], cwd=os.path.join(DIR, 'judge/SafeExec'))
sh(['make', 'clean'], cwd=os.path.join(DIR, 'judge/SafeExec'))
if not os.path.isdir(opts.prefix):
os.makedirs(opts.prefix)
load_executables()
log('installing necessary files')
updateperms('.')
log('config files')
update('./config')
log('files for the library')
update('./lib')
log('files for the virtualenv')
copy('./requirements.txt')
updateperms('./requirements.txt')
setup_virtualenv('.')
log('executables')
update('./bin')
if not opts.noserver:
log('files for the server')
update('./server')
sh(['ln', '-sf', os.path.join(opts.prefix, 'bin/epsilon-server'), os.path.join(BIN_PATH, 'epsilon-server')])
if not opts.nojudge:
log('files for the judge')
update('./judge')
sh(['ln', '-sf', os.path.join(opts.prefix, 'bin/epsilon-judge'), os.path.join(BIN_PATH, 'epsilon-judge')])
if not opts.nojail:
log('destroying the jail, if it exists')
print(os.path.join(opts.prefix, 'judge'))
sh(['./jail-destroy.sh'], cwd=os.path.join(opts.prefix, 'judge'))
log('creating the jail')
sh(['./jail-setup.sh'], cwd=os.path.join(opts.prefix, 'judge'))
log('creating symlinks for programming languages')
sh(['mkdir', os.path.join(opts.prefix, 'judge/jail/bin/lang/')])
for lang, key_name in PROG_LANGS:
sh(['ln', '-sf', KEYS['EXE_' + key_name], os.path.join(opts.prefix, 'judge/jail/bin/lang/' + lang)])
if not opts.nomanualjudge:
log('files for the manual judge')
update('./manual_judge')
sh(['ln', '-sf', os.path.join(opts.prefix, 'bin/epsilon-manual-judge'), os.path.join(BIN_PATH, 'epsilon-manual-judge')])
log('')
log('')
log('Installation succeeded.')
if not opts.nojudge:
log('Please do the following:')
log('')
log(' - append the following to /etc/sudoers (where your_username is the user who will be running epsilon judge):')
log(' your_username ALL=(root) NOPASSWD: %s/judge/execute-submission.sh' % opts.prefix)
log('')
log('')
def uninstall():
global opts
if os.path.exists(os.path.join(opts.prefix, 'judge/jail-destroy.sh')):
log('destroying the jail')
sh(['./jail-destroy.sh'], cwd=os.path.join(opts.prefix, 'judge'))
log('removing binaries')
sh(['rm', '-f', os.path.join(BIN_PATH, 'epsilon-server')], cwd='/')
sh(['rm', '-f', os.path.join(BIN_PATH, 'epsilon-judge')], cwd='/')
sh(['rm', '-f', os.path.join(BIN_PATH, 'epsilon-manual-judge')], cwd='/')
log('erasing the whole prefix directory')
sh(['rm', '-rf', opts.prefix], cwd='/')
log('uninstalling safeexec')
sh(['make', 'uninstall'], cwd=os.path.join(DIR, 'judge/SafeExec'))
if opts.action == 'install':
install()
elif opts.action == 'uninstall':
uninstall()
else:
sys.stderr.write('error: action must be either "install" or "uninstall"\n')
parser.print_help()
sys.exit(1)