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
# build and install IVLE in 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 __staging__ directory (unless --nojail specified).
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/libapt-pkg-libc6.6-6.so.4.5',
116
'/usr/lib/libdb-4.6.so',
117
'/usr/lib/libexpat.so.1',
118
'/usr/lib/libgcrypt.so.11',
119
'/usr/lib/libgnutls.so.13',
120
'/usr/lib/libgpg-error.so.0',
121
'/usr/lib/libgssapi_krb5.so.2',
122
'/usr/lib/libk5crypto.so.3',
123
'/usr/lib/libkrb5.so.3',
124
'/usr/lib/libkrb5support.so.0',
125
'/usr/lib/liblber.so.2',
126
'/usr/lib/liblber-2.4.so.2',
127
'/usr/lib/libldap_r.so.2',
128
'/usr/lib/libldap_r-2.4.so.2',
129
'/usr/lib/libneon.so.27',
130
'/usr/lib/libpq.so.5',
131
'/usr/lib/libsasl2.so.2',
132
'/usr/lib/libsqlite3.so.0',
133
'/usr/lib/libsvn_client-1.so.1',
134
'/usr/lib/libsvn_delta-1.so.1',
135
'/usr/lib/libsvn_diff-1.so.1',
136
'/usr/lib/libsvn_fs-1.so.1',
137
'/usr/lib/libsvn_fs_base-1.so.1',
138
'/usr/lib/libsvn_fs_fs-1.so.1',
139
'/usr/lib/libsvn_ra-1.so.1',
140
'/usr/lib/libsvn_ra_dav-1.so.1',
141
'/usr/lib/libsvn_ra_local-1.so.1',
142
'/usr/lib/libsvn_ra_svn-1.so.1',
143
'/usr/lib/libsvn_repos-1.so.1',
144
'/usr/lib/libsvn_subr-1.so.1',
145
'/usr/lib/libsvn_wc-1.so.1',
146
'/usr/lib/libtasn1.so.3',
147
'/usr/lib/libxml2.so.2',
148
# Needed by matplotlib
149
'/usr/lib/i686/cmov/libssl.so.0.9.8',
150
'/usr/lib/i686/cmov/libcrypto.so.0.9.8',
151
'/lib/tls/i686/cmov/libnsl.so.1',
152
'/usr/lib/libz.so.1',
153
'/usr/lib/atlas/liblapack.so.3',
154
'/usr/lib/atlas/libblas.so.3',
155
'/usr/lib/libg2c.so.0',
156
'/usr/lib/libstdc++.so.6',
157
'/usr/lib/libfreetype.so.6',
158
'/usr/lib/libpng12.so.0',
159
'/usr/lib/libBLT.2.4.so.8.4',
160
'/usr/lib/libtk8.4.so.0',
161
'/usr/lib/libtcl8.4.so.0',
162
'/usr/lib/tcl8.4/init.tcl',
163
'/usr/lib/libX11.so.6',
164
'/usr/lib/libXau.so.6',
165
'/usr/lib/libXdmcp.so.6',
166
'/lib/libgcc_s.so.1',
169
'/lib/libnss_dns.so.2',
170
'/lib/libnss_mdns4_minimal.so.2',
175
'/etc/nsswitch.conf',
176
'/lib/libnss_files.so.2',
178
'/usr/lib/libjpeg.so.62',
180
'/usr/lib/libxslt.so.1',
181
'/usr/lib/libexslt.so.0',
182
# Needed by elementtree
183
'/usr/lib/libtidy-0.99.so.0',
185
# Symlinks to make within the jail. Src mapped to dst.
187
'python%s' % PYTHON_VERSION: 'jail/usr/bin/python',
189
# Trees to copy. Src mapped to dst (these will be passed to action_copytree).
191
'/usr/lib/python%s' % PYTHON_VERSION:
192
'jail/usr/lib/python%s' % PYTHON_VERSION,
193
'/var/lib/python-support/python%s' % PYTHON_VERSION:
194
'jail/var/lib/python-support/python%s' %PYTHON_VERSION,
195
'/usr/share/matplotlib': 'jail/usr/share/matplotlib',
196
'/etc/ld.so.conf.d': 'jail/etc/ld.so.conf.d',
197
'/usr/share/pycentral': 'jail/usr/share/pycentral',
198
'/usr/share/pycentral-data': 'jail/usr/share/pycentral-data',
199
'/usr/share/nltk': 'jail/usr/share/nltk',
203
"""A configuration option; one of the things written to conf.py."""
204
def __init__(self, option_name, default, prompt, comment):
205
"""Creates a configuration option.
206
option_name: Name of the variable in conf.py. Also name of the
207
command-line argument to setup.py conf.
208
default: Default value for this variable.
209
prompt: (Short) string presented during the interactive prompt in
211
comment: (Long) comment string stored in conf.py. Each line of this
212
string should begin with a '#'.
214
self.option_name = option_name
215
self.default = default
217
self.comment = comment
219
# Configuration options, defaults and descriptions
221
config_options.append(ConfigOption("root_dir", "/",
222
"""Root directory where IVLE is located (in URL space):""",
224
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
226
# eg. "/" or "/ivle"."""))
227
config_options.append(ConfigOption("ivle_install_dir", "/opt/ivle",
228
'Root directory where IVLE will be installed (on the local file '
231
# In the local file system, where IVLE is actually installed.
232
# This directory should contain the "www" and "bin" directories."""))
233
config_options.append(ConfigOption("jail_base", "/home/informatics/jails",
234
"""Location of Directories
235
=======================
236
Root directory where the jails (containing user files) are stored
237
(on the local file system):""",
239
# In the local file system, where are the student/user file spaces located.
240
# The user jails are expected to be located immediately in subdirectories of
241
# this location."""))
242
config_options.append(ConfigOption("subjects_base",
243
"/home/informatics/subjects",
244
"""Root directory where the subject directories (containing worksheets
245
and other per-subject files) are stored (on the local file system):""",
247
# In the local file system, where are the per-subject file spaces located.
248
# The individual subject directories are expected to be located immediately
249
# in subdirectories of this location."""))
250
config_options.append(ConfigOption("exercises_base",
251
"/home/informatics/exercises",
252
"""Root directory where the exercise directories (containing
253
subject-independent exercise sheets) are stored (on the local file
256
# In the local file system, where are the subject-independent exercise sheet
257
# file spaces located."""))
258
config_options.append(ConfigOption("tos_path",
259
"/home/informatics/tos.html",
260
"""Location where the Terms of Service document is stored (on the local
263
# In the local file system, where is the Terms of Service document located."""))
264
config_options.append(ConfigOption("motd_path",
265
"/home/informatics/motd.html",
266
"""Location where the Message of the Day document is stored (on the local
269
# In the local file system, where is the Message of the Day document
270
# located. This is an HTML file (just the body fragment), which will
271
# be displayed on the login page. It is optional."""))
272
config_options.append(ConfigOption("public_host", "public.localhost",
273
"""Hostname which will cause the server to go into "public mode",
274
providing login-free access to student's published work:""",
276
# The server goes into "public mode" if the browser sends a request with this
277
# host. This is for security reasons - we only serve public student files on a
278
# separate domain to the main IVLE site.
279
# Public mode does not use cookies, and serves only public content.
280
# Private mode (normal mode) requires login, and only serves files relevant to
281
# the logged-in user."""))
282
config_options.append(ConfigOption("allowed_uids", "33",
283
"""UID of the web server process which will run IVLE.
284
Only this user may execute the trampoline. May specify multiple users as
285
a comma-separated list.
288
# The User-ID of the web server process which will run IVLE, and any other
289
# users who are allowed to run the trampoline. This is stores as a string of
290
# comma-separated integers, simply because it is not used within Python, only
291
# used by the setup program to write to conf.h (see setup.py config)."""))
292
config_options.append(ConfigOption("db_host", "localhost",
293
"""PostgreSQL Database config
294
==========================
295
Hostname of the DB server:""",
297
### PostgreSQL Database config ###
298
# Database server hostname"""))
299
config_options.append(ConfigOption("db_port", "5432",
300
"""Port of the DB server:""",
302
# Database server port"""))
303
config_options.append(ConfigOption("db_dbname", "ivle",
304
"""Database name:""",
307
config_options.append(ConfigOption("db_forumdbname", "ivle_forum",
308
"""Forum Database name:""",
310
# Forum Database name"""))
311
config_options.append(ConfigOption("db_user", "postgres",
312
"""Username for DB server login:""",
314
# Database username"""))
315
config_options.append(ConfigOption("db_password", "",
316
"""Password for DB server login:
317
(Caution: This password is stored in plaintext in lib/conf/conf.py)""",
319
# Database password"""))
320
config_options.append(ConfigOption("auth_modules", "ldap_auth",
321
"""Authentication config
322
=====================
323
Comma-separated list of authentication modules. Only "ldap" is available
326
# Comma-separated list of authentication modules.
327
# These refer to importable Python modules in the www/auth directory.
328
# Modules "ldap" and "guest" are available in the source tree, but
329
# other modules may be plugged in to auth against organisation-specific
330
# auth backends."""))
331
config_options.append(ConfigOption("ldap_url", "ldaps://www.example.com",
332
"""(LDAP options are only relevant if "ldap" is included in the list of
334
URL for LDAP authentication server:""",
336
# URL for LDAP authentication server"""))
337
config_options.append(ConfigOption("ldap_format_string",
338
"uid=%s,ou=users,o=example",
339
"""Format string for LDAP auth request:
340
(Must contain a single "%s" for the user's login name)""",
342
# Format string for LDAP auth request
343
# (Must contain a single "%s" for the user's login name)"""))
344
config_options.append(ConfigOption("svn_addr", "http://svn.localhost/",
347
The base url for accessing subversion repositories:""",
349
# The base url for accessing subversion repositories."""))
350
config_options.append(ConfigOption("svn_conf", "/opt/ivle/svn/svn.conf",
351
"""The location of the subversion configuration file used by apache
352
to host the user repositories:""",
354
# The location of the subversion configuration file used by
355
# apache to host the user repositories."""))
356
config_options.append(ConfigOption("svn_repo_path", "/home/informatics/repositories",
357
"""The root directory for the subversion repositories:""",
359
# The root directory for the subversion repositories."""))
360
config_options.append(ConfigOption("svn_auth_ivle", "/opt/ivle/svn/ivle.auth",
361
"""The location of the password file used to authenticate users
362
of the subversion repository from the ivle server:""",
364
# The location of the password file used to authenticate users
365
# of the subversion repository from the ivle server."""))
366
config_options.append(ConfigOption("svn_auth_local", "/opt/ivle/svn/local.auth",
367
"""The location of the password file used to authenticate local users
368
of the subversion repository:""",
370
# The location of the password file used to authenticate local users
371
# of the subversion repository."""))
372
config_options.append(ConfigOption("usrmgt_host", "localhost",
373
"""User Management Server config
374
============================
375
The hostname where the usrmgt-server runs:""",
377
# The hostname where the usrmgt-server runs."""))
378
config_options.append(ConfigOption("usrmgt_port", "2178",
379
"""The port where the usrmgt-server runs:""",
381
# The port where the usrmgt-server runs."""))
382
config_options.append(ConfigOption("usrmgt_magic", "",
383
"""The password for the usrmgt-server:""",
385
# The password for the usrmgt-server."""))
387
# Try importing existing conf, but if we can't just set up defaults
388
# The reason for this is that these settings are used by other phases
389
# of setup besides conf, so we need to know them.
390
# Also this allows you to hit Return to accept the existing value.
392
confmodule = __import__("lib/conf/conf")
393
for opt in config_options:
395
globals()[opt.option_name] = confmodule.__dict__[opt.option_name]
397
globals()[opt.option_name] = opt.default
399
# Just set reasonable defaults
400
for opt in config_options:
401
globals()[opt.option_name] = opt.default
403
# Try importing install_list, but don't fail if we can't, because listmake can
404
# function without it.
410
# Mime types which will automatically be placed in the list by listmake.
411
# Note that listmake is not intended to be run by the final user (the system
412
# administrator who installs this), so the developers can customize the list
413
# as necessary, and include it in the distribution.
414
listmake_mimetypes = ['text/x-python', 'text/html',
415
'application/x-javascript', 'application/javascript',
416
'text/css', 'image/png', 'image/gif', 'application/xml']
418
# Main function skeleton from Guido van Rossum
419
# http://www.artima.com/weblogs/viewpost.jsp?thread=4829
33
421
def main(argv=None):
56
oper_func = call_operator(operation)
57
return oper_func(argv[2:])
61
print """Usage: python setup.py operation [options]
67
For help and options for a specific operation use 'help [operation]'."""
70
oper_func = call_operator(operator)
71
oper_func(['operator','--help'])
73
def call_operator(operation):
444
# Disallow run as root unless installing
445
if (operation != 'install' and operation != 'updatejails'
446
and os.geteuid() == 0):
447
print >>sys.stderr, "I do not want to run this stage as root."
448
print >>sys.stderr, "Please run as a normal user."
74
450
# Call the requested operation's function
78
'build' : setup.build.build,
79
'install' : setup.install.install,
456
'listmake' : listmake,
458
'updatejails' : updatejails,
82
461
print >>sys.stderr, (
83
462
"""Invalid operation '%s'. Try python setup.py help."""
465
return oper_func(argv[2:])
467
# Operation functions
471
print """Usage: python setup.py operation [args]
472
Operation (and args) can be:
474
listmake (developer use only)
477
install [--nojail] [--nosubjects] [-n|--dry]
481
print """Usage: python setup.py help [operation]"""
486
if operation == 'help':
487
print """python setup.py help [operation]
488
Prints the usage message or detailed help on an operation, then exits."""
489
elif operation == 'listmake':
490
print """python setup.py listmake
491
(For developer use only)
492
Recurses through the source tree and builds a list of all files which should
493
be copied upon installation. This should be run by the developer before
494
cutting a distribution, and the listfile it generates should be included in
495
the distribution, avoiding the administrator having to run it."""
496
elif operation == 'config':
497
print """python setup.py config [args]
498
Configures IVLE with machine-specific details, most notably, various paths.
499
Either prompts the administrator for these details or accepts them as
500
command-line args. Will be interactive only if there are no arguments given.
501
Takes defaults from existing conf file if it exists.
503
To run IVLE out of the source directory (allowing development without having
504
to rebuild/install), just provide ivle_install_dir as the IVLE trunk
505
directory, and run build/install one time.
507
Creates lib/conf/conf.py and trampoline/conf.h.
510
for opt in config_options:
511
print " --" + opt.option_name
512
print """As explained in the interactive prompt or conf.py.
514
elif operation == 'build':
515
print """python -O setup.py build [--dry|-n]
516
Compiles all files and sets up a jail template in the source directory.
517
-O is recommended to cause compilation to be optimised.
519
Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
521
Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
522
Copies console/ to a location within the jail.
523
Copies OS programs and files to corresponding locations within the jail
524
(eg. python and Python libs, ld.so, etc).
525
Generates .pyc or .pyo files for all the IVLE .py files.
527
--dry | -n Print out the actions but don't do anything."""
528
elif operation == 'install':
529
print """sudo python setup.py install [--nojail] [--nosubjects][--dry|-n]
531
Create target install directory ($target).
533
Copy trampoline/trampoline to $target/bin.
534
chown and chmod the installed trampoline.
535
Copy www/ to $target.
536
Copy jail/ to jails __staging__ directory (unless --nojail specified).
537
Copy subjects/ to subjects directory (unless --nosubjects specified).
539
--nojail Do not copy the jail.
540
--nosubjects Do not copy the subjects and exercises directories.
541
--dry | -n Print out the actions but don't do anything."""
542
elif operation == 'updatejails':
543
print """sudo python setup.py updatejails [--dry|-n]
545
Copy jail/ to each subdirectory in jails directory.
547
--dry | -n Print out the actions but don't do anything."""
549
print >>sys.stderr, (
550
"""Invalid operation '%s'. Try python setup.py help."""
555
# We build two separate lists, by walking www and console
556
list_www = build_list_py_files('www')
557
list_lib = build_list_py_files('lib')
558
list_subjects = build_list_py_files('subjects', no_top_level=True)
559
list_exercises = build_list_py_files('exercises', no_top_level=True)
561
"scripts/python-console",
562
"scripts/fileservice",
563
"scripts/serveservice",
564
"scripts/usrmgt-server",
565
"scripts/diffservice",
567
# Make sure that the files generated by conf are in the list
568
# (since listmake is typically run before conf)
569
if "lib/conf/conf.py" not in list_lib:
570
list_lib.append("lib/conf/conf.py")
571
# Write these out to a file
573
# the files that will be created/overwritten
574
listfile = os.path.join(cwd, "install_list.py")
577
file = open(listfile, "w")
579
file.write("""# IVLE Configuration File
581
# Provides lists of all files to be installed by `setup.py install' from
582
# certain directories.
583
# Note that any files with the given filename plus 'c' or 'o' (that is,
584
# compiled .pyc or .pyo files) will be copied as well.
586
# List of all installable files in www directory.
588
writelist_pretty(file, list_www)
590
# List of all installable files in lib directory.
592
writelist_pretty(file, list_lib)
594
# List of all installable files in scripts directory.
596
writelist_pretty(file, list_scripts)
598
# List of all installable files in subjects directory.
599
# This is to install sample subjects and material.
601
writelist_pretty(file, list_subjects)
603
# List of all installable files in exercises directory.
604
# This is to install sample exercise material.
605
list_exercises = """)
606
writelist_pretty(file, list_exercises)
609
except IOError, (errno, strerror):
610
print "IO error(%s): %s" % (errno, strerror)
613
print "Successfully wrote install_list.py"
616
print ("You may modify the set of installable files before cutting the "
623
def build_list_py_files(dir, no_top_level=False):
624
"""Builds a list of all py files found in a directory and its
625
subdirectories. Returns this as a list of strings.
626
no_top_level=True means the file paths will not include the top-level
630
for (dirpath, dirnames, filenames) in os.walk(dir):
631
# Exclude directories beginning with a '.' (such as '.svn')
632
filter_mutate(lambda x: x[0] != '.', dirnames)
633
# All *.py files are added to the list
634
pylist += [os.path.join(dirpath, item) for item in filenames
635
if mimetypes.guess_type(item)[0] in listmake_mimetypes]
637
for i in range(0, len(pylist)):
638
_, pylist[i] = pylist[i].split(os.sep, 1)
641
def writelist_pretty(file, list):
642
"""Writes a list one element per line, to a file."""
648
file.write(' %s,\n' % repr(elem))
652
global db_port, usrmgt_port
653
# Set up some variables
656
# the files that will be created/overwritten
657
conffile = os.path.join(cwd, "lib/conf/conf.py")
658
jailconffile = os.path.join(cwd, "lib/conf/jailconf.py")
659
conf_hfile = os.path.join(cwd, "trampoline/conf.h")
660
phpBBconffile = os.path.join(cwd, "www/php/phpBB3/config.php")
661
usrmgtserver_initdfile = os.path.join(cwd, "doc/setup/usrmgt-server.init")
663
# Get command-line arguments to avoid asking questions.
666
for opt in config_options:
667
optnames.append(opt.option_name + "=")
668
(opts, args) = getopt.gnu_getopt(args, "", optnames)
671
print >>sys.stderr, "Invalid arguments:", string.join(args, ' ')
675
# Interactive mode. Prompt the user for all the values.
677
print """This tool will create the following files:
683
prompting you for details about your configuration. The file will be
684
overwritten if it already exists. It will *not* install or deploy IVLE.
686
Please hit Ctrl+C now if you do not wish to do this.
687
""" % (conffile, jailconffile, conf_hfile, phpBBconffile, usrmgtserver_initdfile)
689
# Get information from the administrator
690
# If EOF is encountered at any time during the questioning, just exit
693
for opt in config_options:
694
globals()[opt.option_name] = \
695
query_user(globals()[opt.option_name], opt.prompt)
698
# Non-interactive mode. Parse the options.
699
for opt in config_options:
700
if '--' + opt.option_name in opts:
701
globals()[opt.option_name] = opts['--' + opt.option_name]
703
# Error handling on input values
705
allowed_uids_list = map(int, allowed_uids.split(','))
707
print >>sys.stderr, (
708
"Invalid UID list (%s).\n"
709
"Must be a comma-separated list of integers." % allowed_uids)
712
db_port = int(db_port)
713
if db_port < 0 or db_port >= 65536: raise ValueError()
715
print >>sys.stderr, (
716
"Invalid DB port (%s).\n"
717
"Must be an integer between 0 and 65535." % repr(db_port))
720
usrmgt_port = int(usrmgt_port)
721
if usrmgt_port < 0 or usrmgt_port >= 65536: raise ValueError()
723
print >>sys.stderr, (
724
"Invalid user management port (%s).\n"
725
"Must be an integer between 0 and 65535." % repr(usrmgt_port))
728
# Generate the forum secret
729
forum_secret = hashlib.md5(uuid.uuid4().bytes).hexdigest()
731
# Write lib/conf/conf.py
734
conf = open(conffile, "w")
736
conf.write("""# IVLE Configuration File
738
# Miscellaneous application settings
741
for opt in config_options:
742
conf.write('%s\n%s = %s\n' % (opt.comment, opt.option_name,
743
repr(globals()[opt.option_name])))
745
# Add the forum secret to the config file (regenerated each config)
746
conf.write('forum_secret = "%s"\n' % (forum_secret))
749
except IOError, (errno, strerror):
750
print "IO error(%s): %s" % (errno, strerror)
753
print "Successfully wrote lib/conf/conf.py"
755
# Write conf/jailconf.py
758
conf = open(jailconffile, "w")
760
# In the "in-jail" version of conf, we don't need MOST of the details
761
# (it would be a security risk to have them here).
762
# So we just write root_dir, and jail_base is "/".
763
# (jail_base being "/" means "jail-relative" paths are relative to "/"
764
# when inside the jail.)
765
conf.write("""# IVLE Configuration File
767
# Miscellaneous application settings
768
# (User jail version)
771
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
773
# eg. "/" or "/ivle".
776
# In the local file system, where are the student/user file spaces located.
777
# The user jails are expected to be located immediately in subdirectories of
781
# The hostname for serving publicly accessible pages
783
""" % (repr(root_dir),repr(public_host)))
786
except IOError, (errno, strerror):
787
print "IO error(%s): %s" % (errno, strerror)
790
print "Successfully wrote lib/conf/jailconf.py"
792
# Write trampoline/conf.h
795
conf = open(conf_hfile, "w")
797
conf.write("""/* IVLE Configuration File
799
* Administrator settings required by trampoline.
800
* Note: trampoline will have to be rebuilt in order for changes to this file
804
/* In the local file system, where are the jails located.
805
* The trampoline does not allow the creation of a jail anywhere besides
806
* jail_base or a subdirectory of jail_base.
808
static const char* jail_base = "%s";
810
/* Which user IDs are allowed to run the trampoline.
811
* This list should be limited to the web server user.
812
* (Note that root is an implicit member of this list).
814
static const int allowed_uids[] = { %s };
815
""" % (repr(jail_base)[1:-1], repr(allowed_uids_list)[1:-1]))
816
# Note: The above uses PYTHON reprs, not C reprs
817
# However they should be the same with the exception of the outer
818
# characters, which are stripped off and replaced
821
except IOError, (errno, strerror):
822
print "IO error(%s): %s" % (errno, strerror)
825
print "Successfully wrote trampoline/conf.h"
827
# Write www/php/phpBB3/config.php
830
conf = open(phpBBconffile, "w")
833
if db_host == 'localhost':
834
forumdb_host = '127.0.0.1'
836
forumdb_host = db_host
839
// phpBB 3.0.x auto-generated configuration file
840
// Do not change anything in this file!
842
$dbhost = '""" + forumdb_host + """';
843
$dbport = '""" + str(db_port) + """';
844
$dbname = '""" + db_forumdbname + """';
845
$dbuser = '""" + db_user + """';
846
$dbpasswd = '""" + db_password + """';
848
$table_prefix = 'phpbb_';
850
$load_extensions = '';
851
@define('PHPBB_INSTALLED', true);
852
// @define('DEBUG', true);
853
//@define('DEBUG_EXTRA', true);
855
$forum_secret = '""" + forum_secret +"""';
859
except IOError, (errno, strerror):
860
print "IO error(%s): %s" % (errno, strerror)
863
print "Successfully wrote www/php/phpBB3/config.php"
865
# Write lib/conf/usrmgt-server.init
868
conf = open(usrmgtserver_initdfile, "w")
870
conf.write( '''#! /bin/sh
872
# Works for Ubuntu. Check before using on other distributions
875
# Provides: usrmgt-server
876
# Required-Start: $syslog $networking $urandom
877
# Required-Stop: $syslog
878
# Default-Start: 2 3 4 5
880
# Short-Description: IVLE user management server
881
# Description: Daemon connecting to the IVLE user management database.
884
PATH=/sbin:/bin:/usr/sbin:/usr/bin
885
DESC="IVLE user management server"
887
DAEMON=/opt/ivle/scripts/$NAME
888
DAEMON_ARGS="''' + str(usrmgt_port) + ''' ''' + usrmgt_magic + '''"
889
PIDFILE=/var/run/$NAME.pid
890
SCRIPTNAME=/etc/init.d/usrmgt-server
892
# Exit if the daemon does not exist
893
test -f $DAEMON || exit 0
895
# Load the VERBOSE setting and other rcS variables
896
[ -f /etc/default/rcS ] && . /etc/default/rcS
898
# Define LSB log_* functions.
899
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
900
. /lib/lsb/init-functions
903
# Function that starts the daemon/service
908
# 0 if daemon has been started
909
# 1 if daemon was already running
910
# 2 if daemon could not be started
911
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
913
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
916
# Add code here, if necessary, that waits for the process to be ready
917
# to handle requests from services started subsequently which depend
918
# on this one. As a last resort, sleep for some time.
922
# Function that stops the daemon/service
927
# 0 if daemon has been stopped
928
# 1 if daemon was already stopped
929
# 2 if daemon could not be stopped
930
# other if a failure occurred
931
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
933
[ "$RETVAL" = 2 ] && return 2
934
# Wait for children to finish too if this is a daemon that forks
935
# and if the daemon is only ever run from this initscript.
936
# If the above conditions are not satisfied then add some other code
937
# that waits for the process to drop all resources that could be
938
# needed by services started subsequently. A last resort is to
939
# sleep for some time.
940
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
941
[ "$?" = 2 ] && return 2
942
# Many daemons don't delete their pidfiles when they exit.
948
# Function that sends a SIGHUP to the daemon/service
952
# If the daemon can reload its configuration without
953
# restarting (for example, when it is sent a SIGHUP),
954
# then implement that here.
956
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
962
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
965
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
966
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
970
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
973
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
974
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
977
#reload|force-reload)
979
# If do_reload() is not implemented then leave this commented out
980
# and leave 'force-reload' as an alias for 'restart'.
982
#log_daemon_msg "Reloading $DESC" "$NAME"
986
restart|force-reload)
988
# If the "reload" option is implemented then remove the
989
# 'force-reload' alias
991
log_daemon_msg "Restarting $DESC" "$NAME"
998
1) log_end_msg 1 ;; # Old process is still running
999
*) log_end_msg 1 ;; # Failed to start
1009
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
1010
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
1019
except IOError, (errno, strerror):
1020
print "IO error(%s): %s" % (errno, strerror)
1023
# fix permissions as the file contains the database password
1025
os.chmod('doc/setup/usrmgt-server.init', 0600)
1026
except OSError, (errno, strerror):
1027
print "WARNING: Couldn't chmod doc/setup/usrmgt-server.init:"
1028
print "OS error(%s): %s" % (errno, strerror)
1030
print "Successfully wrote lib/conf/usrmgt-server.init"
1033
print "You may modify the configuration at any time by editing"
1038
print usrmgtserver_initdfile
1043
# Get "dry" variable from command line
1044
(opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
1046
dry = '-n' in opts or '--dry' in opts
1049
print "Dry run (no actions will be executed\n"
1051
# Find out the revison number
1052
revnum = get_svn_revision()
1053
print "Building Revision %s"%str(revnum)
1055
vfile = open('BUILD-VERSION','w')
1056
vfile.write(str(revnum) + '\n')
1059
# Compile the trampoline
1060
curdir = os.getcwd()
1061
os.chdir('trampoline')
1062
action_runprog('make', [], dry)
1065
# Create the jail and its subdirectories
1066
# Note: Other subdirs will be made by copying files
1067
action_mkdir('jail', dry)
1068
action_mkdir('jail/home', dry)
1069
action_mkdir('jail/tmp', dry)
1071
# Chmod the tmp directory to world writable
1072
action_chmod_w('jail/tmp', dry)
1074
# Copy all console and operating system files into the jail
1075
action_copylist(install_list.list_scripts, 'jail/opt/ivle', dry)
1076
copy_os_files_jail(dry)
1077
# Chmod the python console
1078
action_chmod_x('jail/opt/ivle/scripts/python-console', dry)
1079
action_chmod_x('jail/opt/ivle/scripts/fileservice', dry)
1080
action_chmod_x('jail/opt/ivle/scripts/serveservice', dry)
1082
# Also copy the IVLE lib directory into the jail
1083
# This is necessary for running certain scripts
1084
action_copylist(install_list.list_lib, 'jail/opt/ivle', dry)
1085
# IMPORTANT: The file jail/opt/ivle/lib/conf/conf.py contains details
1086
# which could compromise security if left in the jail (such as the DB
1088
# The "safe" version is in jailconf.py. Delete conf.py and replace it with
1090
action_copyfile('lib/conf/jailconf.py',
1091
'jail/opt/ivle/lib/conf/conf.py', dry)
1093
# Compile .py files into .pyc or .pyo files
1094
compileall.compile_dir('www', quiet=True)
1095
compileall.compile_dir('lib', quiet=True)
1096
compileall.compile_dir('scripts', quiet=True)
1097
compileall.compile_dir('jail/opt/ivle/lib', quiet=True)
1099
# Set up ivle.pth inside the jail
1100
# Need to set /opt/ivle/lib to be on the import path
1102
"jail/usr/lib/python%s/site-packages/ivle.pth" % PYTHON_VERSION
1103
f = open(ivle_pth, 'w')
1104
f.write('/opt/ivle/lib\n')
1109
def copy_os_files_jail(dry):
1110
"""Copies necessary Operating System files from their usual locations
1111
into the jail/ directory of the cwd."""
1112
# Currently source paths are configured for Ubuntu.
1113
for filename in JAIL_FILES:
1114
copy_file_to_jail(filename, dry)
1115
for src, dst in JAIL_LINKS.items():
1116
action_symlink(src, dst, dry)
1117
for src, dst in JAIL_COPYTREES.items():
1118
action_copytree(src, dst, dry)
1120
def copy_file_to_jail(src, dry):
1121
"""Copies a single file from an absolute location into the same location
1122
within the jail. src must begin with a '/'. The jail will be located
1123
in a 'jail' subdirectory of the current path."""
1124
action_copyfile(src, 'jail' + src, dry)
1127
# Get "dry" and "nojail" variables from command line
1128
(opts, args) = getopt.gnu_getopt(args, "n",
1129
['dry', 'nojail', 'nosubjects'])
1131
dry = '-n' in opts or '--dry' in opts
1132
nojail = '--nojail' in opts
1133
nosubjects = '--nosubjects' in opts
1136
print "Dry run (no actions will be executed\n"
1138
if not dry and os.geteuid() != 0:
1139
print >>sys.stderr, "Must be root to run install"
1140
print >>sys.stderr, "(I need to chown some files)."
1143
# Create the target (install) directory
1144
action_mkdir(ivle_install_dir, dry)
1146
# Create bin and copy the compiled files there
1147
action_mkdir(os.path.join(ivle_install_dir, 'bin'), dry)
1148
tramppath = os.path.join(ivle_install_dir, 'bin/trampoline')
1149
action_copyfile('trampoline/trampoline', tramppath, dry)
1150
# chown trampoline to root and set setuid bit
1151
action_chown_setuid(tramppath, dry)
1153
# Create a scripts directory to put the usrmgt-server in.
1154
action_mkdir(os.path.join(ivle_install_dir, 'scripts'), dry)
1155
usrmgtpath = os.path.join(ivle_install_dir, 'scripts/usrmgt-server')
1156
action_copyfile('scripts/usrmgt-server', usrmgtpath, dry)
1157
action_chmod_x(usrmgtpath, dry)
1159
# Copy the www and lib directories using the list
1160
action_copylist(install_list.list_www, ivle_install_dir, dry)
1161
action_copylist(install_list.list_lib, ivle_install_dir, dry)
1163
# Copy the php directory
1164
forum_dir = "www/php/phpBB3"
1165
forum_path = os.path.join(ivle_install_dir, forum_dir)
1166
action_copytree(forum_dir, forum_path, dry)
1167
print "chown -R www-data:www-data %s" % forum_path
1169
os.system("chown -R www-data:www-data %s" % forum_path)
1172
# Copy the local jail directory built by the build action
1173
# to the jails __staging__ directory (it will be used to help build
1174
# all the students' jails).
1175
action_copytree('jail', os.path.join(jail_base, '__staging__'), dry)
1177
# Copy the subjects and exercises directories across
1178
action_copylist(install_list.list_subjects, subjects_base, dry,
1179
srcdir="./subjects")
1180
action_copylist(install_list.list_exercises, exercises_base, dry,
1181
srcdir="./exercises")
1183
# Append IVLE path to ivle.pth in python site packages
1184
# (Unless it's already there)
1185
ivle_pth = os.path.join(sys.prefix,
1186
"lib/python%s/site-packages/ivle.pth" % PYTHON_VERSION)
1187
ivle_www = os.path.join(ivle_install_dir, "www")
1188
ivle_lib = os.path.join(ivle_install_dir, "lib")
1189
write_ivle_pth = True
1190
write_ivle_lib_pth = True
1192
file = open(ivle_pth, 'r')
1194
if line.strip() == ivle_www:
1195
write_ivle_pth = False
1196
elif line.strip() == ivle_lib:
1197
write_ivle_lib_pth = False
1199
except (IOError, OSError):
1202
action_append(ivle_pth, ivle_www)
1203
if write_ivle_lib_pth:
1204
action_append(ivle_pth, ivle_lib)
1207
# Create the ivle working revision record file
1208
action_mkdir(os.path.join(ivle_install_dir, 'version'), dry)
1209
ivle_revision_record_file = os.path.join(ivle_install_dir, 'version/ivle-revision.txt')
1212
conf = open(ivle_revision_record_file, "w")
1214
conf.write( "# IVLE code revision listing generated by running 'svn status -v ..' from " + os.getcwd() + "\n#\n\n")
1217
except IOError, (errno, strerror):
1218
print "IO error(%s): %s" % (errno, strerror)
1221
os.system("svn status -v .. >> %s" % ivle_revision_record_file)
1223
print "Wrote IVLE code revision status to %s" % ivle_revision_record_file
1227
def updatejails(args):
1228
# Get "dry" variable from command line
1229
(opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
1231
dry = '-n' in opts or '--dry' in opts
1234
print "Dry run (no actions will be executed\n"
1236
if not dry and os.geteuid() != 0:
1237
print >>sys.stderr, "Must be root to run install"
1238
print >>sys.stderr, "(I need to chown some files)."
1241
# Update the staging jail directory in case it hasn't been installed
1243
action_copytree('jail', os.path.join(jail_base, '__staging__'), dry)
1245
# Re-link all the files in all students jails.
1246
for dir in os.listdir(jail_base):
1247
if dir == '__staging__': continue
1248
# First back up the student's home directory
1249
temp_home = os.tmpnam()
1250
action_rename(os.path.join(jail_base, dir, 'home'), temp_home, dry)
1251
# Delete the student's jail and relink the jail files
1252
action_linktree(os.path.join(jail_base, '__staging__'),
1253
os.path.join(jail_base, dir), dry)
1254
# Restore the student's home directory
1255
action_rename(temp_home, os.path.join(jail_base, dir, 'home'), dry)
1256
# Set up the user's home directory just in case they don't have a
1257
# directory for this yet
1258
action_mkdir(os.path.join(jail_base, dir, 'home', dir), dry)
1262
# The actions call Python os functions but print actions and handle dryness.
1263
# May still throw os exceptions if errors occur.
1266
"""Represents an error when running a program (nonzero return)."""
1267
def __init__(self, prog, retcode):
1269
self.retcode = retcode
1271
return str(self.prog) + " returned " + repr(self.retcode)
1273
def action_runprog(prog, args, dry):
1274
"""Runs a unix program. Searches in $PATH. Synchronous (waits for the
1275
program to return). Runs in the current environment. First prints the
1276
action as a "bash" line.
1278
Throws a RunError with a retcode of the return value of the program,
1279
if the program did not return 0.
1281
prog: String. Name of the program. (No path required, if in $PATH).
1282
args: [String]. Arguments to the program.
1283
dry: Bool. If True, prints but does not execute.
1285
print prog, string.join(args, ' ')
1287
ret = os.spawnvp(os.P_WAIT, prog, args)
1289
raise RunError(prog, ret)
1291
def action_remove(path, dry):
1292
"""Calls rmtree, deleting the target file if it exists."""
1296
shutil.rmtree(path, True)
1297
except OSError, (err, msg):
1298
if err != errno.EEXIST:
1300
# Otherwise, didn't exist, so we don't care
1302
def action_rename(src, dst, dry):
1303
"""Calls rename. Deletes the target if it already exists."""
1304
action_remove(dst, dry)
1305
print "mv ", src, dst
1309
except OSError, (err, msg):
1310
if err != errno.EEXIST:
1313
def action_mkdir(path, dry):
1314
"""Calls mkdir. Silently ignored if the directory already exists.
1315
Creates all parent directories as necessary."""
1316
print "mkdir -p", path
1320
except OSError, (err, msg):
1321
if err != errno.EEXIST:
1324
def action_copytree(src, dst, dry):
1325
"""Copies an entire directory tree. Symlinks are seen as normal files and
1326
copies of the entire file (not the link) are made. Creates all parent
1327
directories as necessary.
1329
See shutil.copytree."""
1330
# Allow copying over itself
1331
if (os.path.normpath(os.path.join(os.getcwd(),src)) ==
1332
os.path.normpath(os.path.join(os.getcwd(),dst))):
1334
action_remove(dst, dry)
1335
print "cp -r", src, dst
1337
shutil.copytree(src, dst, True)
1339
def action_linktree(src, dst, dry):
1340
"""Hard-links an entire directory tree. Same as copytree but the created
1341
files are hard-links not actual copies. Removes the existing destination.
1343
action_remove(dst, dry)
1344
print "<cp with hardlinks> -r", src, dst
1346
common.makeuser.linktree(src, dst)
1348
def action_copylist(srclist, dst, dry, srcdir="."):
1349
"""Copies all files in a list to a new location. The files in the list
1350
are read relative to the current directory, and their destinations are the
1351
same paths relative to dst. Creates all parent directories as necessary.
1352
srcdir is "." by default, can be overridden.
1354
for srcfile in srclist:
1355
dstfile = os.path.join(dst, srcfile)
1356
srcfile = os.path.join(srcdir, srcfile)
1357
dstdir = os.path.split(dstfile)[0]
1358
if not os.path.isdir(dstdir):
1359
action_mkdir(dstdir, dry)
1360
print "cp -f", srcfile, dstfile
1363
shutil.copyfile(srcfile, dstfile)
1364
shutil.copymode(srcfile, dstfile)
1365
except shutil.Error:
1368
def action_copyfile(src, dst, dry):
1369
"""Copies one file to a new location. Creates all parent directories
1371
Warn if file not found.
1373
dstdir = os.path.split(dst)[0]
1374
if not os.path.isdir(dstdir):
1375
action_mkdir(dstdir, dry)
1376
print "cp -f", src, dst
1379
shutil.copyfile(src, dst)
1380
shutil.copymode(src, dst)
1381
except (shutil.Error, IOError), e:
1382
print "Warning: " + str(e)
1384
def action_symlink(src, dst, dry):
1385
"""Creates a symlink in a given location. Creates all parent directories
1388
dstdir = os.path.split(dst)[0]
1389
if not os.path.isdir(dstdir):
1390
action_mkdir(dstdir, dry)
1391
# Delete existing file
1392
if os.path.exists(dst):
1394
print "ln -fs", src, dst
1396
os.symlink(src, dst)
1398
def action_append(ivle_pth, ivle_www):
1399
file = open(ivle_pth, 'a+')
1400
file.write(ivle_www + '\n')
1403
def action_chown_setuid(file, dry):
1404
"""Chowns a file to root, and sets the setuid bit on the file.
1405
Calling this function requires the euid to be root.
1406
The actual mode of path is set to: rws--s--s
1408
print "chown root:root", file
1410
os.chown(file, 0, 0)
1411
print "chmod a+xs", file
1412
print "chmod u+rw", file
1414
os.chmod(file, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
1415
| stat.S_ISUID | stat.S_IRUSR | stat.S_IWUSR)
1417
def action_chmod_x(file, dry):
1418
"""Chmod 755 a file (sets permissions to rwxr-xr-x)."""
1419
print "chmod 755", file
1421
os.chmod(file, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR
1422
| stat.S_IXGRP | stat.S_IRGRP | stat.S_IXOTH | stat.S_IROTH)
1425
def action_chmod_w(file, dry):
1426
"""Chmod 777 a file (sets permissions to rwxrwxrwx)."""
1427
print "chmod 777", file
1429
os.chmod(file, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR
1430
| stat.S_IXGRP | stat.S_IWGRP | stat.S_IRGRP | stat.S_IXOTH
1431
| stat.S_IWOTH | stat.S_IROTH)
1433
def query_user(default, prompt):
1434
"""Prompts the user for a string, which is read from a line of stdin.
1435
Exits silently if EOF is encountered. Returns the string, with spaces
1436
removed from the beginning and end.
1438
Returns default if a 0-length line (after spaces removed) was read.
1440
sys.stdout.write('%s\n (default: "%s")\n>' % (prompt, default))
1442
val = sys.stdin.readline()
1443
except KeyboardInterrupt:
1445
sys.stdout.write("\n")
1447
sys.stdout.write("\n")
1449
if val == '': sys.exit(1)
1450
# If empty line, return default
1452
if val == '': return default
1455
def filter_mutate(function, list):
1456
"""Like built-in filter, but mutates the given list instead of returning a
1457
new one. Returns None."""
1460
# Delete elements which do not match
1461
if not function(list[i]):
1465
def get_svn_revision():
1466
"""Returns either the current SVN revision of this build, or None"""
1468
svn = pysvn.Client()
1469
entry = svn.info('.')
1470
revnum = entry.revision.number
1471
except pysvn.ClientError, e:
88
1475
if __name__ == "__main__":