23
23
# This is a command-line application, for use by the administrator.
24
# This program is a frontend for the modules in the setup packages that
25
# configure, build and install IVLE in three separate steps.
24
# This program configures, builds and installs IVLE in three separate steps.
26
25
# It is called with at least one argument, which specifies which operation to
28
# setup.py listmake (for developer use only)
29
# Recurses through the source tree and builds a list of all files which should
30
# be copied upon installation. This should be run by the developer before
31
# cutting a distribution, and the listfile it generates should be included in
32
# the distribution, avoiding the administrator having to run it.
34
# setup.py config [args]
35
# Configures IVLE with machine-specific details, most notably, various paths.
36
# Either prompts the administrator for these details or accepts them as
38
# Creates lib/conf/conf.py and trampoline/conf.h.
41
# Compiles all files and sets up a jail template in the source directory.
43
# Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
45
# Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
46
# Copies console/ to a location within the jail.
47
# Copies OS programs and files to corresponding locations within the jail
48
# (eg. python and Python libs, ld.so, etc).
49
# Generates .pyc files for all the IVLE .py files.
51
# setup.py install [--nojail] [--dry|n]
53
# Create target install directory ($target).
55
# Copy trampoline/trampoline to $target/bin.
56
# chown and chmod the installed trampoline.
57
# Copy www/ to $target.
58
# Copy jail/ to jails template directory (unless --nojail specified).
30
import setup.configure
74
# Import modules from the website is tricky since they're in the www
76
sys.path.append(os.path.join(os.getcwd(), 'lib'))
78
import common.makeuser
80
# Determine which Python version (2.4 or 2.5, for example) we are running,
81
# and use that as the filename to the Python directory.
82
# Just get the first 3 characters of sys.version.
83
PYTHON_VERSION = sys.version[0:3]
85
# Operating system files to copy over into the jail.
86
# These will be copied from the given place on the OS file system into the
87
# same place within the jail.
90
'/lib/tls/i686/cmov/libc.so.6',
91
'/lib/tls/i686/cmov/libdl.so.2',
92
'/lib/tls/i686/cmov/libm.so.6',
93
'/lib/tls/i686/cmov/libpthread.so.0',
94
'/lib/tls/i686/cmov/libutil.so.1',
97
# These 2 files do not exist in Ubuntu
98
#'/etc/ld.so.preload',
99
#'/etc/ld.so.nohwcap',
105
'/usr/bin/python%s' % PYTHON_VERSION,
106
# Needed by fileservice
107
'/lib/libcom_err.so.2',
108
'/lib/libcrypt.so.1',
109
'/lib/libkeyutils.so.1',
110
'/lib/libresolv.so.2',
113
'/usr/lib/libapr-1.so.0',
114
'/usr/lib/libaprutil-1.so.0',
115
'/usr/lib/libdb-4.4.so',
116
'/usr/lib/libexpat.so.1',
117
'/usr/lib/libgcrypt.so.11',
118
'/usr/lib/libgnutls.so.13',
119
'/usr/lib/libgpg-error.so.0',
120
'/usr/lib/libgssapi_krb5.so.2',
121
'/usr/lib/libk5crypto.so.3',
122
'/usr/lib/libkrb5.so.3',
123
'/usr/lib/libkrb5support.so.0',
124
'/usr/lib/liblber.so.2',
125
'/usr/lib/libldap_r.so.2',
126
'/usr/lib/libneon.so.26',
127
'/usr/lib/libpq.so.5',
128
'/usr/lib/libsasl2.so.2',
129
'/usr/lib/libsqlite3.so.0',
130
'/usr/lib/libsvn_client-1.so.1',
131
'/usr/lib/libsvn_delta-1.so.1',
132
'/usr/lib/libsvn_diff-1.so.1',
133
'/usr/lib/libsvn_fs-1.so.1',
134
'/usr/lib/libsvn_fs_base-1.so.1',
135
'/usr/lib/libsvn_fs_fs-1.so.1',
136
'/usr/lib/libsvn_ra-1.so.1',
137
'/usr/lib/libsvn_ra_dav-1.so.1',
138
'/usr/lib/libsvn_ra_local-1.so.1',
139
'/usr/lib/libsvn_ra_svn-1.so.1',
140
'/usr/lib/libsvn_repos-1.so.1',
141
'/usr/lib/libsvn_subr-1.so.1',
142
'/usr/lib/libsvn_wc-1.so.1',
143
'/usr/lib/libtasn1.so.3',
144
'/usr/lib/libxml2.so.2',
145
# Needed by matplotlib
146
'/usr/lib/i686/cmov/libssl.so.0.9.8',
147
'/usr/lib/i686/cmov/libcrypto.so.0.9.8',
148
'/lib/tls/i686/cmov/libnsl.so.1',
149
'/usr/lib/libz.so.1',
150
'/usr/lib/atlas/liblapack.so.3',
151
'/usr/lib/atlas/libblas.so.3',
152
'/usr/lib/libg2c.so.0',
153
'/usr/lib/libstdc++.so.6',
154
'/usr/lib/libfreetype.so.6',
155
'/usr/lib/libpng12.so.0',
156
'/usr/lib/libBLT.2.4.so.8.4',
157
'/usr/lib/libtk8.4.so.0',
158
'/usr/lib/libtcl8.4.so.0',
159
'/usr/lib/tcl8.4/init.tcl',
160
'/usr/lib/libX11.so.6',
161
'/usr/lib/libXau.so.6',
162
'/usr/lib/libXdmcp.so.6',
163
'/lib/libgcc_s.so.1',
166
'/lib/libnss_dns.so.2',
167
'/lib/libnss_mdns4_minimal.so.2',
172
'/etc/nsswitch.conf',
173
'/lib/libnss_files.so.2',
175
'/usr/lib/libjpeg.so.62',
177
'/usr/lib/libxslt.so.1',
178
'/usr/lib/libexslt.so.0',
179
# Needed by elementtree
180
'/usr/lib/libtidy-0.99.so.0',
182
# Symlinks to make within the jail. Src mapped to dst.
184
'python%s' % PYTHON_VERSION: 'jail/usr/bin/python',
186
# Trees to copy. Src mapped to dst (these will be passed to action_copytree).
188
'/usr/lib/python%s' % PYTHON_VERSION:
189
'jail/usr/lib/python%s' % PYTHON_VERSION,
190
'/var/lib/python-support/python%s' % PYTHON_VERSION:
191
'jail/var/lib/python-support/python%s' %PYTHON_VERSION,
192
'/usr/share/matplotlib': 'jail/usr/share/matplotlib',
193
'/etc/ld.so.conf.d': 'jail/etc/ld.so.conf.d',
194
'/usr/share/pycentral': 'jail/usr/share/pycentral',
195
'/usr/share/pycentral-data': 'jail/usr/share/pycentral-data',
196
'/usr/share/nltk': 'jail/usr/share/nltk',
200
"""A configuration option; one of the things written to conf.py."""
201
def __init__(self, option_name, default, prompt, comment):
202
"""Creates a configuration option.
203
option_name: Name of the variable in conf.py. Also name of the
204
command-line argument to setup.py conf.
205
default: Default value for this variable.
206
prompt: (Short) string presented during the interactive prompt in
208
comment: (Long) comment string stored in conf.py. Each line of this
209
string should begin with a '#'.
211
self.option_name = option_name
212
self.default = default
214
self.comment = comment
216
# Configuration options, defaults and descriptions
218
config_options.append(ConfigOption("root_dir", "/",
219
"""Root directory where IVLE is located (in URL space):""",
221
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
223
# eg. "/" or "/ivle"."""))
224
config_options.append(ConfigOption("ivle_install_dir", "/opt/ivle",
225
'Root directory where IVLE will be installed (on the local file '
228
# In the local file system, where IVLE is actually installed.
229
# This directory should contain the "www" and "bin" directories."""))
230
config_options.append(ConfigOption("jail_base", "/home/informatics/jails",
231
"""Location of Directories
232
=======================
233
Root directory where the jails (containing user files) are stored
234
(on the local file system):""",
236
# In the local file system, where are the student/user file spaces located.
237
# The user jails are expected to be located immediately in subdirectories of
238
# this location."""))
239
config_options.append(ConfigOption("subjects_base",
240
"/home/informatics/subjects",
241
"""Root directory where the subject directories (containing worksheets
242
and other per-subject files) are stored (on the local file system):""",
244
# In the local file system, where are the per-subject file spaces located.
245
# The individual subject directories are expected to be located immediately
246
# in subdirectories of this location."""))
247
config_options.append(ConfigOption("exercises_base",
248
"/home/informatics/exercises",
249
"""Root directory where the exercise directories (containing
250
subject-independent exercise sheets) are stored (on the local file
253
# In the local file system, where are the subject-independent exercise sheet
254
# file spaces located."""))
255
config_options.append(ConfigOption("tos_path",
256
"/home/informatics/tos.html",
257
"""Location where the Terms of Service document is stored (on the local
260
# In the local file system, where is the Terms of Service document located."""))
261
config_options.append(ConfigOption("motd_path",
262
"/home/informatics/motd.html",
263
"""Location where the Message of the Day document is stored (on the local
266
# In the local file system, where is the Message of the Day document
267
# located. This is an HTML file (just the body fragment), which will
268
# be displayed on the login page. It is optional."""))
269
config_options.append(ConfigOption("public_host", "public.localhost",
270
"""Hostname which will cause the server to go into "public mode",
271
providing login-free access to student's published work:""",
273
# The server goes into "public mode" if the browser sends a request with this
274
# host. This is for security reasons - we only serve public student files on a
275
# separate domain to the main IVLE site.
276
# Public mode does not use cookies, and serves only public content.
277
# Private mode (normal mode) requires login, and only serves files relevant to
278
# the logged-in user."""))
279
config_options.append(ConfigOption("allowed_uids", "33",
280
"""UID of the web server process which will run IVLE.
281
Only this user may execute the trampoline. May specify multiple users as
282
a comma-separated list.
285
# The User-ID of the web server process which will run IVLE, and any other
286
# users who are allowed to run the trampoline. This is stores as a string of
287
# comma-separated integers, simply because it is not used within Python, only
288
# used by the setup program to write to conf.h (see setup.py config)."""))
289
config_options.append(ConfigOption("db_host", "localhost",
290
"""PostgreSQL Database config
291
==========================
292
Hostname of the DB server:""",
294
### PostgreSQL Database config ###
295
# Database server hostname"""))
296
config_options.append(ConfigOption("db_port", "5432",
297
"""Port of the DB server:""",
299
# Database server port"""))
300
config_options.append(ConfigOption("db_dbname", "ivle",
301
"""Database name:""",
304
config_options.append(ConfigOption("db_forumdbname", "ivle_forum",
305
"""Forum Database name:""",
307
# Forum Database name"""))
308
config_options.append(ConfigOption("db_user", "postgres",
309
"""Username for DB server login:""",
311
# Database username"""))
312
config_options.append(ConfigOption("db_password", "",
313
"""Password for DB server login:
314
(Caution: This password is stored in plaintext in lib/conf/conf.py)""",
316
# Database password"""))
317
config_options.append(ConfigOption("auth_modules", "ldap_auth",
318
"""Authentication config
319
=====================
320
Comma-separated list of authentication modules. Only "ldap" is available
323
# Comma-separated list of authentication modules.
324
# These refer to importable Python modules in the www/auth directory.
325
# Modules "ldap" and "guest" are available in the source tree, but
326
# other modules may be plugged in to auth against organisation-specific
327
# auth backends."""))
328
config_options.append(ConfigOption("ldap_url", "ldaps://www.example.com",
329
"""(LDAP options are only relevant if "ldap" is included in the list of
331
URL for LDAP authentication server:""",
333
# URL for LDAP authentication server"""))
334
config_options.append(ConfigOption("ldap_format_string",
335
"uid=%s,ou=users,o=example",
336
"""Format string for LDAP auth request:
337
(Must contain a single "%s" for the user's login name)""",
339
# Format string for LDAP auth request
340
# (Must contain a single "%s" for the user's login name)"""))
341
config_options.append(ConfigOption("svn_addr", "http://svn.localhost/",
344
The base url for accessing subversion repositories:""",
346
# The base url for accessing subversion repositories."""))
347
config_options.append(ConfigOption("svn_conf", "/opt/ivle/svn/svn.conf",
348
"""The location of the subversion configuration file used by apache
349
to host the user repositories:""",
351
# The location of the subversion configuration file used by
352
# apache to host the user repositories."""))
353
config_options.append(ConfigOption("svn_repo_path", "/home/informatics/repositories",
354
"""The root directory for the subversion repositories:""",
356
# The root directory for the subversion repositories."""))
357
config_options.append(ConfigOption("svn_auth_ivle", "/opt/ivle/svn/ivle.auth",
358
"""The location of the password file used to authenticate users
359
of the subversion repository from the ivle server:""",
361
# The location of the password file used to authenticate users
362
# of the subversion repository from the ivle server."""))
363
config_options.append(ConfigOption("svn_auth_local", "/opt/ivle/svn/local.auth",
364
"""The location of the password file used to authenticate local users
365
of the subversion repository:""",
367
# The location of the password file used to authenticate local users
368
# of the subversion repository."""))
369
config_options.append(ConfigOption("usrmgt_host", "localhost",
370
"""User Management Server config
371
============================
372
The hostname where the usrmgt-server runs:""",
374
# The hostname where the usrmgt-server runs."""))
375
config_options.append(ConfigOption("usrmgt_port", "2178",
376
"""The port where the usrmgt-server runs:""",
378
# The port where the usrmgt-server runs."""))
379
config_options.append(ConfigOption("usrmgt_magic", "",
380
"""The password for the usrmgt-server:""",
382
# The password for the usrmgt-server."""))
384
# Try importing existing conf, but if we can't just set up defaults
385
# The reason for this is that these settings are used by other phases
386
# of setup besides conf, so we need to know them.
387
# Also this allows you to hit Return to accept the existing value.
389
confmodule = __import__("lib/conf/conf")
390
for opt in config_options:
392
globals()[opt.option_name] = confmodule.__dict__[opt.option_name]
394
globals()[opt.option_name] = opt.default
396
# Just set reasonable defaults
397
for opt in config_options:
398
globals()[opt.option_name] = opt.default
400
# Try importing install_list, but don't fail if we can't, because listmake can
401
# function without it.
407
# Mime types which will automatically be placed in the list by listmake.
408
# Note that listmake is not intended to be run by the final user (the system
409
# administrator who installs this), so the developers can customize the list
410
# as necessary, and include it in the distribution.
411
listmake_mimetypes = ['text/x-python', 'text/html',
412
'application/x-javascript', 'application/javascript',
413
'text/css', 'image/png', 'image/gif', 'application/xml']
415
# Main function skeleton from Guido van Rossum
416
# http://www.artima.com/weblogs/viewpost.jsp?thread=4829
36
418
def main(argv=None):
59
oper_func = call_operator(operation)
441
# Disallow run as root unless installing
442
if (operation != 'install' and operation != 'updatejails'
443
and os.geteuid() == 0):
444
print >>sys.stderr, "I do not want to run this stage as root."
445
print >>sys.stderr, "Please run as a normal user."
447
# Call the requested operation's function
453
'listmake' : listmake,
455
'updatejails' : updatejails,
458
print >>sys.stderr, (
459
"""Invalid operation '%s'. Try python setup.py help."""
60
462
return oper_func(argv[2:])
464
# Operation functions
64
print """Usage: python setup.py operation [options]
468
print """Usage: python setup.py operation [args]
469
Operation (and args) can be:
67
471
listmake (developer use only)
72
For help and options for a specific operation use 'help [operation]'."""
75
oper_func = call_operator(operator)
76
oper_func(['operator','--help'])
78
def call_operator(operation):
79
# Call the requested operation's function
83
'config' : setup.configure.configure,
84
'build' : setup.build.build,
85
'listmake' : setup.listmake.listmake,
86
'install' : setup.install.install,
87
#'updatejails' : None,
474
install [--nojail] [--nosubjects] [-n|--dry]
478
print """Usage: python setup.py help [operation]"""
483
if operation == 'help':
484
print """python setup.py help [operation]
485
Prints the usage message or detailed help on an operation, then exits."""
486
elif operation == 'listmake':
487
print """python setup.py listmake
488
(For developer use only)
489
Recurses through the source tree and builds a list of all files which should
490
be copied upon installation. This should be run by the developer before
491
cutting a distribution, and the listfile it generates should be included in
492
the distribution, avoiding the administrator having to run it."""
493
elif operation == 'config':
494
print """python setup.py config [args]
495
Configures IVLE with machine-specific details, most notably, various paths.
496
Either prompts the administrator for these details or accepts them as
497
command-line args. Will be interactive only if there are no arguments given.
498
Takes defaults from existing conf file if it exists.
500
To run IVLE out of the source directory (allowing development without having
501
to rebuild/install), just provide ivle_install_dir as the IVLE trunk
502
directory, and run build/install one time.
504
Creates lib/conf/conf.py and trampoline/conf.h.
507
for opt in config_options:
508
print " --" + opt.option_name
509
print """As explained in the interactive prompt or conf.py.
511
elif operation == 'build':
512
print """python -O setup.py build [--dry|-n]
513
Compiles all files and sets up a jail template in the source directory.
514
-O is recommended to cause compilation to be optimised.
516
Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
518
Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
519
Copies console/ to a location within the jail.
520
Copies OS programs and files to corresponding locations within the jail
521
(eg. python and Python libs, ld.so, etc).
522
Generates .pyc or .pyo files for all the IVLE .py files.
524
--dry | -n Print out the actions but don't do anything."""
525
elif operation == 'install':
526
print """sudo python setup.py install [--nojail] [--nosubjects][--dry|-n]
528
Create target install directory ($target).
530
Copy trampoline/trampoline to $target/bin.
531
chown and chmod the installed trampoline.
532
Copy www/ to $target.
533
Copy jail/ to jails template directory (unless --nojail specified).
534
Copy subjects/ to subjects directory (unless --nosubjects specified).
536
--nojail Do not copy the jail.
537
--nosubjects Do not copy the subjects and exercises directories.
538
--dry | -n Print out the actions but don't do anything."""
539
elif operation == 'updatejails':
540
print """sudo python setup.py updatejails [--dry|-n]
542
Copy jail/ to each subdirectory in jails directory.
544
--dry | -n Print out the actions but don't do anything."""
90
546
print >>sys.stderr, (
91
547
"""Invalid operation '%s'. Try python setup.py help."""
552
# We build two separate lists, by walking www and console
553
list_www = build_list_py_files('www')
554
list_lib = build_list_py_files('lib')
555
list_subjects = build_list_py_files('subjects', no_top_level=True)
556
list_exercises = build_list_py_files('exercises', no_top_level=True)
558
"scripts/python-console",
559
"scripts/fileservice",
560
"scripts/serveservice",
561
"scripts/usrmgt-server",
562
"scripts/diffservice",
564
# Make sure that the files generated by conf are in the list
565
# (since listmake is typically run before conf)
566
if "lib/conf/conf.py" not in list_lib:
567
list_lib.append("lib/conf/conf.py")
568
# Write these out to a file
570
# the files that will be created/overwritten
571
listfile = os.path.join(cwd, "install_list.py")
574
file = open(listfile, "w")
576
file.write("""# IVLE Configuration File
578
# Provides lists of all files to be installed by `setup.py install' from
579
# certain directories.
580
# Note that any files with the given filename plus 'c' or 'o' (that is,
581
# compiled .pyc or .pyo files) will be copied as well.
583
# List of all installable files in www directory.
585
writelist_pretty(file, list_www)
587
# List of all installable files in lib directory.
589
writelist_pretty(file, list_lib)
591
# List of all installable files in scripts directory.
593
writelist_pretty(file, list_scripts)
595
# List of all installable files in subjects directory.
596
# This is to install sample subjects and material.
598
writelist_pretty(file, list_subjects)
600
# List of all installable files in exercises directory.
601
# This is to install sample exercise material.
602
list_exercises = """)
603
writelist_pretty(file, list_exercises)
606
except IOError, (errno, strerror):
607
print "IO error(%s): %s" % (errno, strerror)
610
print "Successfully wrote install_list.py"
613
print ("You may modify the set of installable files before cutting the "
620
def build_list_py_files(dir, no_top_level=False):
621
"""Builds a list of all py files found in a directory and its
622
subdirectories. Returns this as a list of strings.
623
no_top_level=True means the file paths will not include the top-level
627
for (dirpath, dirnames, filenames) in os.walk(dir):
628
# Exclude directories beginning with a '.' (such as '.svn')
629
filter_mutate(lambda x: x[0] != '.', dirnames)
630
# All *.py files are added to the list
631
pylist += [os.path.join(dirpath, item) for item in filenames
632
if mimetypes.guess_type(item)[0] in listmake_mimetypes]
634
for i in range(0, len(pylist)):
635
_, pylist[i] = pylist[i].split(os.sep, 1)
638
def writelist_pretty(file, list):
639
"""Writes a list one element per line, to a file."""
645
file.write(' %s,\n' % repr(elem))
649
global db_port, usrmgt_port
650
# Set up some variables
653
# the files that will be created/overwritten
654
conffile = os.path.join(cwd, "lib/conf/conf.py")
655
jailconffile = os.path.join(cwd, "lib/conf/jailconf.py")
656
conf_hfile = os.path.join(cwd, "trampoline/conf.h")
657
phpBBconffile = os.path.join(cwd, "www/php/phpBB3/config.php")
658
usrmgtserver_initdfile = os.path.join(cwd, "doc/setup/usrmgt-server.init")
660
# Get command-line arguments to avoid asking questions.
663
for opt in config_options:
664
optnames.append(opt.option_name + "=")
665
(opts, args) = getopt.gnu_getopt(args, "", optnames)
668
print >>sys.stderr, "Invalid arguments:", string.join(args, ' ')
672
# Interactive mode. Prompt the user for all the values.
674
print """This tool will create the following files:
680
prompting you for details about your configuration. The file will be
681
overwritten if it already exists. It will *not* install or deploy IVLE.
683
Please hit Ctrl+C now if you do not wish to do this.
684
""" % (conffile, jailconffile, conf_hfile, phpBBconffile, usrmgtserver_initdfile)
686
# Get information from the administrator
687
# If EOF is encountered at any time during the questioning, just exit
690
for opt in config_options:
691
globals()[opt.option_name] = \
692
query_user(globals()[opt.option_name], opt.prompt)
695
# Non-interactive mode. Parse the options.
696
for opt in config_options:
697
if '--' + opt.option_name in opts:
698
globals()[opt.option_name] = opts['--' + opt.option_name]
700
# Error handling on input values
702
allowed_uids_list = map(int, allowed_uids.split(','))
704
print >>sys.stderr, (
705
"Invalid UID list (%s).\n"
706
"Must be a comma-separated list of integers." % allowed_uids)
709
db_port = int(db_port)
710
if db_port < 0 or db_port >= 65536: raise ValueError()
712
print >>sys.stderr, (
713
"Invalid DB port (%s).\n"
714
"Must be an integer between 0 and 65535." % repr(db_port))
717
usrmgt_port = int(usrmgt_port)
718
if usrmgt_port < 0 or usrmgt_port >= 65536: raise ValueError()
720
print >>sys.stderr, (
721
"Invalid user management port (%s).\n"
722
"Must be an integer between 0 and 65535." % repr(usrmgt_port))
725
# Generate the forum secret
726
forum_secret = hashlib.md5(uuid.uuid4().bytes).hexdigest()
728
# Write lib/conf/conf.py
731
conf = open(conffile, "w")
733
conf.write("""# IVLE Configuration File
735
# Miscellaneous application settings
738
for opt in config_options:
739
conf.write('%s\n%s = %s\n' % (opt.comment, opt.option_name,
740
repr(globals()[opt.option_name])))
742
# Add the forum secret to the config file (regenerated each config)
743
conf.write('forum_secret = "%s"\n' % (forum_secret))
746
except IOError, (errno, strerror):
747
print "IO error(%s): %s" % (errno, strerror)
750
print "Successfully wrote lib/conf/conf.py"
752
# Write conf/jailconf.py
755
conf = open(jailconffile, "w")
757
# In the "in-jail" version of conf, we don't need MOST of the details
758
# (it would be a security risk to have them here).
759
# So we just write root_dir, and jail_base is "/".
760
# (jail_base being "/" means "jail-relative" paths are relative to "/"
761
# when inside the jail.)
762
conf.write("""# IVLE Configuration File
764
# Miscellaneous application settings
765
# (User jail version)
768
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
770
# eg. "/" or "/ivle".
773
# In the local file system, where are the student/user file spaces located.
774
# The user jails are expected to be located immediately in subdirectories of
778
# The hostname for serving publicly accessible pages
780
""" % (repr(root_dir),repr(public_host)))
783
except IOError, (errno, strerror):
784
print "IO error(%s): %s" % (errno, strerror)
787
print "Successfully wrote lib/conf/jailconf.py"
789
# Write trampoline/conf.h
792
conf = open(conf_hfile, "w")
794
conf.write("""/* IVLE Configuration File
796
* Administrator settings required by trampoline.
797
* Note: trampoline will have to be rebuilt in order for changes to this file
801
/* In the local file system, where are the jails located.
802
* The trampoline does not allow the creation of a jail anywhere besides
803
* jail_base or a subdirectory of jail_base.
805
static const char* jail_base = "%s";
807
/* Which user IDs are allowed to run the trampoline.
808
* This list should be limited to the web server user.
809
* (Note that root is an implicit member of this list).
811
static const int allowed_uids[] = { %s };
812
""" % (repr(jail_base)[1:-1], repr(allowed_uids_list)[1:-1]))
813
# Note: The above uses PYTHON reprs, not C reprs
814
# However they should be the same with the exception of the outer
815
# characters, which are stripped off and replaced
818
except IOError, (errno, strerror):
819
print "IO error(%s): %s" % (errno, strerror)
822
print "Successfully wrote trampoline/conf.h"
824
# Write www/php/phpBB3/config.php
827
conf = open(phpBBconffile, "w")
830
if db_host == 'localhost':
831
forumdb_host = '127.0.0.1'
833
forumdb_host = db_host
836
// phpBB 3.0.x auto-generated configuration file
837
// Do not change anything in this file!
839
$dbhost = '""" + forumdb_host + """';
840
$dbport = '""" + str(db_port) + """';
841
$dbname = '""" + db_forumdbname + """';
842
$dbuser = '""" + db_user + """';
843
$dbpasswd = '""" + db_password + """';
845
$table_prefix = 'phpbb_';
847
$load_extensions = '';
848
@define('PHPBB_INSTALLED', true);
849
// @define('DEBUG', true);
850
//@define('DEBUG_EXTRA', true);
852
$forum_secret = '""" + forum_secret +"""';
856
except IOError, (errno, strerror):
857
print "IO error(%s): %s" % (errno, strerror)
860
print "Successfully wrote www/php/phpBB3/config.php"
862
# Write lib/conf/usrmgt-server.init
865
conf = open(usrmgtserver_initdfile, "w")
867
conf.write( '''#! /bin/sh
869
# Works for Ubuntu. Check before using on other distributions
872
# Provides: usrmgt-server
873
# Required-Start: $syslog $networking $urandom
874
# Required-Stop: $syslog
875
# Default-Start: 2 3 4 5
877
# Short-Description: IVLE user management server
878
# Description: Daemon connecting to the IVLE user management database.
881
PATH=/sbin:/bin:/usr/sbin:/usr/bin
882
DESC="IVLE user management server"
884
DAEMON=/opt/ivle/scripts/$NAME
885
DAEMON_ARGS="''' + str(usrmgt_port) + ''' ''' + usrmgt_magic + '''"
886
PIDFILE=/var/run/$NAME.pid
887
SCRIPTNAME=/etc/init.d/usrmgt-server
889
# Exit if the daemon does not exist
890
test -f $DAEMON || exit 0
892
# Load the VERBOSE setting and other rcS variables
893
[ -f /etc/default/rcS ] && . /etc/default/rcS
895
# Define LSB log_* functions.
896
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
897
. /lib/lsb/init-functions
900
# Function that starts the daemon/service
905
# 0 if daemon has been started
906
# 1 if daemon was already running
907
# 2 if daemon could not be started
908
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
910
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
913
# Add code here, if necessary, that waits for the process to be ready
914
# to handle requests from services started subsequently which depend
915
# on this one. As a last resort, sleep for some time.
919
# Function that stops the daemon/service
924
# 0 if daemon has been stopped
925
# 1 if daemon was already stopped
926
# 2 if daemon could not be stopped
927
# other if a failure occurred
928
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
930
[ "$RETVAL" = 2 ] && return 2
931
# Wait for children to finish too if this is a daemon that forks
932
# and if the daemon is only ever run from this initscript.
933
# If the above conditions are not satisfied then add some other code
934
# that waits for the process to drop all resources that could be
935
# needed by services started subsequently. A last resort is to
936
# sleep for some time.
937
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
938
[ "$?" = 2 ] && return 2
939
# Many daemons don't delete their pidfiles when they exit.
945
# Function that sends a SIGHUP to the daemon/service
949
# If the daemon can reload its configuration without
950
# restarting (for example, when it is sent a SIGHUP),
951
# then implement that here.
953
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
959
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
962
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
963
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
967
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
970
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
971
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
974
#reload|force-reload)
976
# If do_reload() is not implemented then leave this commented out
977
# and leave 'force-reload' as an alias for 'restart'.
979
#log_daemon_msg "Reloading $DESC" "$NAME"
983
restart|force-reload)
985
# If the "reload" option is implemented then remove the
986
# 'force-reload' alias
988
log_daemon_msg "Restarting $DESC" "$NAME"
995
1) log_end_msg 1 ;; # Old process is still running
996
*) log_end_msg 1 ;; # Failed to start
1006
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
1007
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
1016
except IOError, (errno, strerror):
1017
print "IO error(%s): %s" % (errno, strerror)
1020
# fix permissions as the file contains the database password
1022
os.chmod('doc/setup/usrmgt-server.init', 0600)
1023
except OSError, (errno, strerror):
1024
print "WARNING: Couldn't chmod doc/setup/usrmgt-server.init:"
1025
print "OS error(%s): %s" % (errno, strerror)
1027
print "Successfully wrote lib/conf/usrmgt-server.init"
1030
print "You may modify the configuration at any time by editing"
1035
print usrmgtserver_initdfile
1040
# Get "dry" variable from command line
1041
(opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
1043
dry = '-n' in opts or '--dry' in opts
1046
print "Dry run (no actions will be executed\n"
1048
# Find out the revison number
1049
revnum = get_svn_revision()
1050
print "Building Revision %s"%str(revnum)
1052
vfile = open('BUILD-VERSION','w')
1053
vfile.write(str(revnum) + '\n')
1056
# Compile the trampoline
1057
curdir = os.getcwd()
1058
os.chdir('trampoline')
1059
action_runprog('make', [], dry)
1062
# Create the jail and its subdirectories
1063
# Note: Other subdirs will be made by copying files
1064
action_mkdir('jail', dry)
1065
action_mkdir('jail/home', dry)
1066
action_mkdir('jail/tmp', dry)
1068
# Chmod the tmp directory to world writable
1069
action_chmod_w('jail/tmp', dry)
1071
# Copy all console and operating system files into the jail
1072
action_copylist(install_list.list_scripts, 'jail/opt/ivle', dry)
1073
copy_os_files_jail(dry)
1074
# Chmod the python console
1075
action_chmod_x('jail/opt/ivle/scripts/python-console', dry)
1076
action_chmod_x('jail/opt/ivle/scripts/fileservice', dry)
1077
action_chmod_x('jail/opt/ivle/scripts/serveservice', dry)
1079
# Also copy the IVLE lib directory into the jail
1080
# This is necessary for running certain scripts
1081
action_copylist(install_list.list_lib, 'jail/opt/ivle', dry)
1082
# IMPORTANT: The file jail/opt/ivle/lib/conf/conf.py contains details
1083
# which could compromise security if left in the jail (such as the DB
1085
# The "safe" version is in jailconf.py. Delete conf.py and replace it with
1087
action_copyfile('lib/conf/jailconf.py',
1088
'jail/opt/ivle/lib/conf/conf.py', dry)
1090
# Compile .py files into .pyc or .pyo files
1091
compileall.compile_dir('www', quiet=True)
1092
compileall.compile_dir('lib', quiet=True)
1093
compileall.compile_dir('scripts', quiet=True)
1094
compileall.compile_dir('jail/opt/ivle/lib', quiet=True)
1096
# Set up ivle.pth inside the jail
1097
# Need to set /opt/ivle/lib to be on the import path
1099
"jail/usr/lib/python%s/site-packages/ivle.pth" % PYTHON_VERSION
1100
f = open(ivle_pth, 'w')
1101
f.write('/opt/ivle/lib\n')
1106
def copy_os_files_jail(dry):
1107
"""Copies necessary Operating System files from their usual locations
1108
into the jail/ directory of the cwd."""
1109
# Currently source paths are configured for Ubuntu.
1110
for filename in JAIL_FILES:
1111
copy_file_to_jail(filename, dry)
1112
for src, dst in JAIL_LINKS.items():
1113
action_symlink(src, dst, dry)
1114
for src, dst in JAIL_COPYTREES.items():
1115
action_copytree(src, dst, dry)
1117
def copy_file_to_jail(src, dry):
1118
"""Copies a single file from an absolute location into the same location
1119
within the jail. src must begin with a '/'. The jail will be located
1120
in a 'jail' subdirectory of the current path."""
1121
action_copyfile(src, 'jail' + src, dry)
1124
# Get "dry" and "nojail" variables from command line
1125
(opts, args) = getopt.gnu_getopt(args, "n",
1126
['dry', 'nojail', 'nosubjects'])
1128
dry = '-n' in opts or '--dry' in opts
1129
nojail = '--nojail' in opts
1130
nosubjects = '--nosubjects' in opts
1133
print "Dry run (no actions will be executed\n"
1135
if not dry and os.geteuid() != 0:
1136
print >>sys.stderr, "Must be root to run install"
1137
print >>sys.stderr, "(I need to chown some files)."
1140
# Create the target (install) directory
1141
action_mkdir(ivle_install_dir, dry)
1143
# Create bin and copy the compiled files there
1144
action_mkdir(os.path.join(ivle_install_dir, 'bin'), dry)
1145
tramppath = os.path.join(ivle_install_dir, 'bin/trampoline')
1146
action_copyfile('trampoline/trampoline', tramppath, dry)
1147
# chown trampoline to root and set setuid bit
1148
action_chown_setuid(tramppath, dry)
1150
# Create a scripts directory to put the usrmgt-server in.
1151
action_mkdir(os.path.join(ivle_install_dir, 'scripts'), dry)
1152
usrmgtpath = os.path.join(ivle_install_dir, 'scripts/usrmgt-server')
1153
action_copyfile('scripts/usrmgt-server', usrmgtpath, dry)
1154
action_chmod_x(usrmgtpath, dry)
1156
# Copy the www and lib directories using the list
1157
action_copylist(install_list.list_www, ivle_install_dir, dry)
1158
action_copylist(install_list.list_lib, ivle_install_dir, dry)
1160
# Copy the php directory
1161
forum_dir = "www/php/phpBB3"
1162
forum_path = os.path.join(ivle_install_dir, forum_dir)
1163
action_copytree(forum_dir, forum_path, dry)
1164
print "chown -R www-data:www-data %s" % forum_path
1166
os.system("chown -R www-data:www-data %s" % forum_path)
1169
# Copy the local jail directory built by the build action
1170
# to the jails template directory (it will be used as a template
1171
# for all the students' jails).
1172
action_copytree('jail', os.path.join(jail_base, 'template'), dry)
1174
# Copy the subjects and exercises directories across
1175
action_copylist(install_list.list_subjects, subjects_base, dry,
1176
srcdir="./subjects")
1177
action_copylist(install_list.list_exercises, exercises_base, dry,
1178
srcdir="./exercises")
1180
# Append IVLE path to ivle.pth in python site packages
1181
# (Unless it's already there)
1182
ivle_pth = os.path.join(sys.prefix,
1183
"lib/python%s/site-packages/ivle.pth" % PYTHON_VERSION)
1184
ivle_www = os.path.join(ivle_install_dir, "www")
1185
ivle_lib = os.path.join(ivle_install_dir, "lib")
1186
write_ivle_pth = True
1187
write_ivle_lib_pth = True
1189
file = open(ivle_pth, 'r')
1191
if line.strip() == ivle_www:
1192
write_ivle_pth = False
1193
elif line.strip() == ivle_lib:
1194
write_ivle_lib_pth = False
1196
except (IOError, OSError):
1199
action_append(ivle_pth, ivle_www)
1200
if write_ivle_lib_pth:
1201
action_append(ivle_pth, ivle_lib)
1205
def updatejails(args):
1206
# Get "dry" variable from command line
1207
(opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
1209
dry = '-n' in opts or '--dry' in opts
1212
print "Dry run (no actions will be executed\n"
1214
if not dry and os.geteuid() != 0:
1215
print >>sys.stderr, "Must be root to run install"
1216
print >>sys.stderr, "(I need to chown some files)."
1219
# Update the template jail directory in case it hasn't been installed
1221
action_copytree('jail', os.path.join(jail_base, 'template'), dry)
1223
# Re-link all the files in all students jails.
1224
for dir in os.listdir(jail_base):
1225
if dir == 'template': continue
1226
# First back up the student's home directory
1227
temp_home = os.tmpnam()
1228
action_rename(os.path.join(jail_base, dir, 'home'), temp_home, dry)
1229
# Delete the student's jail and relink the jail files
1230
action_linktree(os.path.join(jail_base, 'template'),
1231
os.path.join(jail_base, dir), dry)
1232
# Restore the student's home directory
1233
action_rename(temp_home, os.path.join(jail_base, dir, 'home'), dry)
1234
# Set up the user's home directory just in case they don't have a
1235
# directory for this yet
1236
action_mkdir(os.path.join(jail_base, dir, 'home', dir), dry)
1240
# The actions call Python os functions but print actions and handle dryness.
1241
# May still throw os exceptions if errors occur.
1244
"""Represents an error when running a program (nonzero return)."""
1245
def __init__(self, prog, retcode):
1247
self.retcode = retcode
1249
return str(self.prog) + " returned " + repr(self.retcode)
1251
def action_runprog(prog, args, dry):
1252
"""Runs a unix program. Searches in $PATH. Synchronous (waits for the
1253
program to return). Runs in the current environment. First prints the
1254
action as a "bash" line.
1256
Throws a RunError with a retcode of the return value of the program,
1257
if the program did not return 0.
1259
prog: String. Name of the program. (No path required, if in $PATH).
1260
args: [String]. Arguments to the program.
1261
dry: Bool. If True, prints but does not execute.
1263
print prog, string.join(args, ' ')
1265
ret = os.spawnvp(os.P_WAIT, prog, args)
1267
raise RunError(prog, ret)
1269
def action_remove(path, dry):
1270
"""Calls rmtree, deleting the target file if it exists."""
1274
shutil.rmtree(path, True)
1275
except OSError, (err, msg):
1276
if err != errno.EEXIST:
1278
# Otherwise, didn't exist, so we don't care
1280
def action_rename(src, dst, dry):
1281
"""Calls rename. Deletes the target if it already exists."""
1282
action_remove(dst, dry)
1283
print "mv ", src, dst
1287
except OSError, (err, msg):
1288
if err != errno.EEXIST:
1291
def action_mkdir(path, dry):
1292
"""Calls mkdir. Silently ignored if the directory already exists.
1293
Creates all parent directories as necessary."""
1294
print "mkdir -p", path
1298
except OSError, (err, msg):
1299
if err != errno.EEXIST:
1302
def action_copytree(src, dst, dry):
1303
"""Copies an entire directory tree. Symlinks are seen as normal files and
1304
copies of the entire file (not the link) are made. Creates all parent
1305
directories as necessary.
1307
See shutil.copytree."""
1308
# Allow copying over itself
1309
if (os.path.normpath(os.path.join(os.getcwd(),src)) ==
1310
os.path.normpath(os.path.join(os.getcwd(),dst))):
1312
action_remove(dst, dry)
1313
print "cp -r", src, dst
1315
shutil.copytree(src, dst, True)
1317
def action_linktree(src, dst, dry):
1318
"""Hard-links an entire directory tree. Same as copytree but the created
1319
files are hard-links not actual copies. Removes the existing destination.
1321
action_remove(dst, dry)
1322
print "<cp with hardlinks> -r", src, dst
1324
common.makeuser.linktree(src, dst)
1326
def action_copylist(srclist, dst, dry, srcdir="."):
1327
"""Copies all files in a list to a new location. The files in the list
1328
are read relative to the current directory, and their destinations are the
1329
same paths relative to dst. Creates all parent directories as necessary.
1330
srcdir is "." by default, can be overridden.
1332
for srcfile in srclist:
1333
dstfile = os.path.join(dst, srcfile)
1334
srcfile = os.path.join(srcdir, srcfile)
1335
dstdir = os.path.split(dstfile)[0]
1336
if not os.path.isdir(dstdir):
1337
action_mkdir(dstdir, dry)
1338
print "cp -f", srcfile, dstfile
1341
shutil.copyfile(srcfile, dstfile)
1342
shutil.copymode(srcfile, dstfile)
1343
except shutil.Error:
1346
def action_copyfile(src, dst, dry):
1347
"""Copies one file to a new location. Creates all parent directories
1349
Warn if file not found.
1351
dstdir = os.path.split(dst)[0]
1352
if not os.path.isdir(dstdir):
1353
action_mkdir(dstdir, dry)
1354
print "cp -f", src, dst
1357
shutil.copyfile(src, dst)
1358
shutil.copymode(src, dst)
1359
except (shutil.Error, IOError), e:
1360
print "Warning: " + str(e)
1362
def action_symlink(src, dst, dry):
1363
"""Creates a symlink in a given location. Creates all parent directories
1366
dstdir = os.path.split(dst)[0]
1367
if not os.path.isdir(dstdir):
1368
action_mkdir(dstdir, dry)
1369
# Delete existing file
1370
if os.path.exists(dst):
1372
print "ln -fs", src, dst
1374
os.symlink(src, dst)
1376
def action_append(ivle_pth, ivle_www):
1377
file = open(ivle_pth, 'a+')
1378
file.write(ivle_www + '\n')
1381
def action_chown_setuid(file, dry):
1382
"""Chowns a file to root, and sets the setuid bit on the file.
1383
Calling this function requires the euid to be root.
1384
The actual mode of path is set to: rws--s--s
1386
print "chown root:root", file
1388
os.chown(file, 0, 0)
1389
print "chmod a+xs", file
1390
print "chmod u+rw", file
1392
os.chmod(file, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
1393
| stat.S_ISUID | stat.S_IRUSR | stat.S_IWUSR)
1395
def action_chmod_x(file, dry):
1396
"""Chmod 755 a file (sets permissions to rwxr-xr-x)."""
1397
print "chmod 755", file
1399
os.chmod(file, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR
1400
| stat.S_IXGRP | stat.S_IRGRP | stat.S_IXOTH | stat.S_IROTH)
1403
def action_chmod_w(file, dry):
1404
"""Chmod 777 a file (sets permissions to rwxrwxrwx)."""
1405
print "chmod 777", file
1407
os.chmod(file, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR
1408
| stat.S_IXGRP | stat.S_IWGRP | stat.S_IRGRP | stat.S_IXOTH
1409
| stat.S_IWOTH | stat.S_IROTH)
1411
def query_user(default, prompt):
1412
"""Prompts the user for a string, which is read from a line of stdin.
1413
Exits silently if EOF is encountered. Returns the string, with spaces
1414
removed from the beginning and end.
1416
Returns default if a 0-length line (after spaces removed) was read.
1418
sys.stdout.write('%s\n (default: "%s")\n>' % (prompt, default))
1420
val = sys.stdin.readline()
1421
except KeyboardInterrupt:
1423
sys.stdout.write("\n")
1425
sys.stdout.write("\n")
1427
if val == '': sys.exit(1)
1428
# If empty line, return default
1430
if val == '': return default
1433
def filter_mutate(function, list):
1434
"""Like built-in filter, but mutates the given list instead of returning a
1435
new one. Returns None."""
1438
# Delete elements which do not match
1439
if not function(list[i]):
1443
def get_svn_revision():
1444
"""Returns either the current SVN revision of this build, or None"""
1446
svn = pysvn.Client()
1447
entry = svn.info('.')
1448
revnum = entry.revision.number
1449
except pysvn.ClientError, e:
96
1453
if __name__ == "__main__":