-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeploy.py
More file actions
executable file
·348 lines (280 loc) · 10.4 KB
/
deploy.py
File metadata and controls
executable file
·348 lines (280 loc) · 10.4 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python2
import argparse
import os, errno
import stat
import sys
import shutil
from shutil import Error, copy2, copystat
from zipfile import ZipFile, ZIP_DEFLATED
from contextlib import closing
import ConfigParser
SVN_DIR_NAME = '.svn' # Directory to be excluded till import
CHECKERS_ORDER = ('xml', 'py', 'ext')
# Subdirs for export
LIBQUEST_DIRNAME = 'libquest'
STATIC_DIRNAME = 'static'
# Config file to create
CFG_FILENAME = 'questserver.cfg'
CFG_TEMPLATE = 'questserver.cfg.template'
TASK_CFG_FILENAME = 'tasks.cfg'
### Variables to for config file
srv_name = "http://quals2011.ructf.org"
base_path = "/"
base_url = "%(srv_name)s%(base_path)s"
log_dir = "/var/log/qserver"
workers = 10
open_all = "yes"
backup_dir = "backup"
backup_savefile = "state.bin"
backup_savecount = 50
class Task(object):
def __init__(self, full_path,
name=None,
category=None,
score=None,
relative_path=None,
checker=None,
checker_type=None):
self.full_path = full_path
self.name = name
self.category = category
self.score = score
self.relative_path = relative_path
self.checker = checker
self.checker_type = checker_type
def __str__(self):
return ", ".join(["{0}: '{1}'".format(k.capitalize(), v) for k, v in self.__dict__.items() if v])
class QuestServerCheckers(object):
def _scan_path(self, path):
tasks = []
for task_name in os.listdir(path):
full_task_path = os.path.join(path, task_name)
# Skipping all garbage in directory
if not os.path.isdir(full_task_path):
continue
tasks.append(Task(full_task_path, name=task_name, relative_path=task_name))
return tasks
def _script_quest_finder(self, checker_type, task):
full_checker_path = os.path.join(task.full_path, task.name)
if os.path.exists(full_checker_path):
return (checker_type, task.name)
return None
def _extention_finder(self, checker_type, task):
checker_file = task.name + '.' + checker_type
full_checker_path = os.path.join(task.full_path, checker_file)
if os.path.exists(full_checker_path):
return (checker_type, checker_file)
return None
def scan_for_checkers(self):
self.tasks = self._scan_path(self.path)
tasks_with_checkers = []
for task in self.tasks:
found = False
for checker_type in CHECKERS_ORDER:
checker = self.checkers_finders[checker_type]
checkertype_checkerfile = checker(checker_type, task)
if checkertype_checkerfile:
(task.checker_type, task.checker) = checkertype_checkerfile
tasks_with_checkers.append(task)
found = True
break
if not found:
print ">>>> Can't find checker in '{0}', skipping".format(task.full_path)
self.tasks = tasks_with_checkers
def __init__(self, path):
self.path = path
self.checkers_finders = {
'xml': self._extention_finder,
'py': self._extention_finder,
'ext': self._script_quest_finder,
}
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST:
pass
else:
raise
def copytree(src, dst, symlinks=False, ignore=None):
names = os.listdir(src)
if ignore is not None:
ignored_names = ignore(src, names)
else:
ignored_names = set()
mkdir_p(dst)
errors = []
for name in names:
if name in ignored_names:
continue
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks, ignore)
else:
copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
print "!!!!1"
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error, err:
errors.extend(err.args[0])
try:
copystat(src, dst)
except OSError, why:
if WindowsError is not None and isinstance(why, WindowsError):
# Copying file access times may fail on Windows
pass
else:
errors.extend((src, dst, str(why)))
if errors:
raise Error, errors
def parse_args():
parser = argparse.ArgumentParser(description='Deploy RuCTF Quals')
parser.add_argument('src_dir', metavar='src_dir', type=str, help='Source dir to deploy')
parser.add_argument('dst_dir', metavar='dst_dir', type=str, help='Destination dir to deploy')
args = parser.parse_args()
return args
def svn_export_ignore_svn_path(path, names):
if SVN_DIR_NAME not in names:
return []
for name in names:
if name != SVN_DIR_NAME:
continue
full_path = os.path.join(path, name)
if os.path.isdir(full_path):
return SVN_DIR_NAME
def svn_export(src_dir, dst_dir):
print "== Copy '%s' -> '%s'" % (src_dir, dst_dir)
shutil.copytree(src_dir, dst_dir, ignore=svn_export_ignore_svn_path)
def make_scripts_executable(qac):
files_x = []
for task in qac.tasks:
if task.checker_type != 'ext':
continue
full_filename = os.path.join(task.full_path, task.checker)
mode = os.stat(full_filename)[0]
print mode
mode = (mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
print mode
os.chmod(full_filename, mode)
files_x.append(full_filename)
return files_x
def compress_zip(path):
(path_to_zip, zip_filename) = os.path.split(path)
with closing(ZipFile(zip_filename, 'w', ZIP_DEFLATED)) as zip_file_fn:
for root, dirs, files in os.walk(path):
for fn in files:
full_fn = os.path.join(root, fn)
zfn = full_fn[len(path) + len(os.sep):]
zip_file_fn.write(full_fn, zfn)
shutil.rmtree(path)
shutil.move(zip_filename, path_to_zip)
def compress_zips(dst_dir):
dirs_to_compress = []
for root, dirs, files in os.walk(dst_dir):
for one_dir in dirs:
if one_dir.endswith('.zip'):
full_path = os.path.join(root, one_dir)
dirs_to_compress.append(full_path)
for dtc in dirs_to_compress:
compress_zip(dtc)
return dirs_to_compress
def make_cfgfile(qac, cfg_filename):
cp = ConfigParser.RawConfigParser()
#cp.add_section('DEFAULT')
if os.path.exists(CFG_TEMPLATE):
cp.read(CFG_TEMPLATE)
else:
cp.set('DEFAULT', 'srv_name', srv_name)
cp.set('DEFAULT', 'base_path', base_path)
cp.set('DEFAULT', 'base_url', base_url)
cp.set('DEFAULT', 'log_dir', log_dir)
cp.set('DEFAULT', 'workers', workers)
cp.set('DEFAULT', 'open_all', open_all)
cp.add_section('backup')
cp.set('backup', 'dir', backup_dir)
cp.set('backup', 'savefile', backup_savefile)
cp.set('backup', 'savecount', backup_savecount)
with open(cfg_filename, 'w') as out_fn:
cp.write(out_fn)
def make_task_config(qac, tasks_filename, libquest_dir):
cp = ConfigParser.RawConfigParser()
categories = set(t.category for t in qac.tasks)
cp.set('DEFAULT', 'categories', ':'.join(map(str, categories)))
for category in categories:
cp.add_section(str(category))
cp.set(category, 'name', str(category))
category_dir = libquest_dir
if category is not None:
category_dir = os.path.join(category_dir, str(category))
cp.set(category, 'dir', category_dir)
pseudo_price = 1
for task in qac.tasks:
if task.category is None:
print "!!!! Task {0} at '{1}' has no category".format(task.name, task.full_path)
task_id = "q{0}".format(pseudo_price)
checker_path = os.path.join(task.relative_path, task.checker)
value = "{0}:{1}".format(task.checker_type, checker_path)
cp.set(str(task.category), task_id, value)
pseudo_price += 1
with open(tasks_filename, 'w') as out_fn:
cp.write(out_fn)
def make_static_dir(qac, static_dir):
static_dirs = []
static_dir = os.path.join(static_dir, '')
os.mkdir(static_dir)
for task in qac.tasks:
full_path = os.path.join(task.full_path, STATIC_DIRNAME)
if not os.path.isdir(full_path):
continue
copytree(full_path, static_dir)
static_dirs.append(full_path)
return static_dirs
def main():
args = parse_args()
src_dir = args.src_dir
dst_dir = args.dst_dir
dst_dir_libquest = os.path.join(dst_dir, LIBQUEST_DIRNAME)
if not os.path.isdir(src_dir):
print >> sys.stderr, "%s does not exists or not directory" % src_dir
sys.exit(1)
if os.path.exists(dst_dir):
print >> sys.stderr, "%s already exists" % dst_dir
sys.exit(2)
print "== Deployng '%s' to '%s'" % (src_dir, dst_dir)
svn_export(src_dir, dst_dir_libquest)
print "== Scaning for checkers"
qac = QuestServerCheckers(dst_dir_libquest)
qac.scan_for_checkers()
print "== Found tasks:"
for task in qac.tasks:
print str(task)
print "== Making executable:"
if os.name != 'posix':
print "(Skipped because not POSIX OS)"
else:
files_x = make_scripts_executable(qac)
print "\n".join(files_x)
print "== Compress zips:"
zips = compress_zips(dst_dir_libquest)
print "\n".join(zips)
print "== Make static dir:"
static_dir = os.path.join(dst_dir, STATIC_DIRNAME)
static_dirs = make_static_dir(qac, static_dir)
print "\n".join(static_dirs)
print "== Make cfg_file"
cfg_path = os.path.join(dst_dir, CFG_FILENAME)
make_cfgfile(qac, cfg_path)
print "== Make tasks config file"
task_config_path = os.path.join(dst_dir, TASK_CFG_FILENAME)
make_task_config(qac, task_config_path, LIBQUEST_DIRNAME)
print cfg_path
if __name__ == "__main__":
main()