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]
86
"""A configuration option; one of the things written to conf.py."""
87
def __init__(self, option_name, default, prompt, comment):
88
"""Creates a configuration option.
89
option_name: Name of the variable in conf.py. Also name of the
90
command-line argument to setup.py conf.
91
default: Default value for this variable.
92
prompt: (Short) string presented during the interactive prompt in
94
comment: (Long) comment string stored in conf.py. Each line of this
95
string should begin with a '#'.
97
self.option_name = option_name
98
self.default = default
100
self.comment = comment
102
# Configuration options, defaults and descriptions
104
config_options.append(ConfigOption("root_dir", "/",
105
"""Root directory where IVLE is located (in URL space):""",
107
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
109
# eg. "/" or "/ivle"."""))
110
config_options.append(ConfigOption("ivle_install_dir", "/opt/ivle",
111
'Root directory where IVLE will be installed (on the local file '
114
# In the local file system, where IVLE is actually installed.
115
# This directory should contain the "www" and "bin" directories."""))
116
config_options.append(ConfigOption("jail_base", "/home/informatics/jails",
117
"""Location of Directories
118
=======================
119
Root directory where the jails (containing user files) are stored
120
(on the local file system):""",
122
# In the local file system, where are the student/user file spaces located.
123
# The user jails are expected to be located immediately in subdirectories of
124
# this location."""))
125
config_options.append(ConfigOption("subjects_base",
126
"/home/informatics/subjects",
127
"""Root directory where the subject directories (containing worksheets
128
and other per-subject files) are stored (on the local file system):""",
130
# In the local file system, where are the per-subject file spaces located.
131
# The individual subject directories are expected to be located immediately
132
# in subdirectories of this location."""))
133
config_options.append(ConfigOption("exercises_base",
134
"/home/informatics/exercises",
135
"""Root directory where the exercise directories (containing
136
subject-independent exercise sheets) are stored (on the local file
139
# In the local file system, where are the subject-independent exercise sheet
140
# file spaces located."""))
141
config_options.append(ConfigOption("tos_path",
142
"/home/informatics/tos.html",
143
"""Location where the Terms of Service document is stored (on the local
146
# In the local file system, where is the Terms of Service document located."""))
147
config_options.append(ConfigOption("motd_path",
148
"/home/informatics/motd.html",
149
"""Location where the Message of the Day document is stored (on the local
152
# In the local file system, where is the Message of the Day document
153
# located. This is an HTML file (just the body fragment), which will
154
# be displayed on the login page. It is optional."""))
155
config_options.append(ConfigOption("public_host", "public.localhost",
156
"""Hostname which will cause the server to go into "public mode",
157
providing login-free access to student's published work:""",
159
# The server goes into "public mode" if the browser sends a request with this
160
# host. This is for security reasons - we only serve public student files on a
161
# separate domain to the main IVLE site.
162
# Public mode does not use cookies, and serves only public content.
163
# Private mode (normal mode) requires login, and only serves files relevant to
164
# the logged-in user."""))
165
config_options.append(ConfigOption("allowed_uids", "33",
166
"""UID of the web server process which will run IVLE.
167
Only this user may execute the trampoline. May specify multiple users as
168
a comma-separated list.
171
# The User-ID of the web server process which will run IVLE, and any other
172
# users who are allowed to run the trampoline. This is stores as a string of
173
# comma-separated integers, simply because it is not used within Python, only
174
# used by the setup program to write to conf.h (see setup.py config)."""))
175
config_options.append(ConfigOption("db_host", "localhost",
176
"""PostgreSQL Database config
177
==========================
178
Hostname of the DB server:""",
180
### PostgreSQL Database config ###
181
# Database server hostname"""))
182
config_options.append(ConfigOption("db_port", "5432",
183
"""Port of the DB server:""",
185
# Database server port"""))
186
config_options.append(ConfigOption("db_dbname", "ivle",
187
"""Database name:""",
190
config_options.append(ConfigOption("db_forumdbname", "ivle_forum",
191
"""Forum Database name:""",
193
# Forum Database name"""))
194
config_options.append(ConfigOption("db_user", "postgres",
195
"""Username for DB server login:""",
197
# Database username"""))
198
config_options.append(ConfigOption("db_password", "",
199
"""Password for DB server login:
200
(Caution: This password is stored in plaintext in lib/conf/conf.py)""",
202
# Database password"""))
203
config_options.append(ConfigOption("auth_modules", "ldap_auth",
204
"""Authentication config
205
=====================
206
Comma-separated list of authentication modules. Only "ldap" is available
209
# Comma-separated list of authentication modules.
210
# These refer to importable Python modules in the www/auth directory.
211
# Modules "ldap" and "guest" are available in the source tree, but
212
# other modules may be plugged in to auth against organisation-specific
213
# auth backends."""))
214
config_options.append(ConfigOption("ldap_url", "ldaps://www.example.com",
215
"""(LDAP options are only relevant if "ldap" is included in the list of
217
URL for LDAP authentication server:""",
219
# URL for LDAP authentication server"""))
220
config_options.append(ConfigOption("ldap_format_string",
221
"uid=%s,ou=users,o=example",
222
"""Format string for LDAP auth request:
223
(Must contain a single "%s" for the user's login name)""",
225
# Format string for LDAP auth request
226
# (Must contain a single "%s" for the user's login name)"""))
227
config_options.append(ConfigOption("svn_addr", "http://svn.localhost/",
230
The base url for accessing subversion repositories:""",
232
# The base url for accessing subversion repositories."""))
233
config_options.append(ConfigOption("svn_conf", "/opt/ivle/svn/svn.conf",
234
"""The location of the subversion configuration file used by apache
235
to host the user repositories:""",
237
# The location of the subversion configuration file used by
238
# apache to host the user repositories."""))
239
config_options.append(ConfigOption("svn_repo_path", "/home/informatics/repositories",
240
"""The root directory for the subversion repositories:""",
242
# The root directory for the subversion repositories."""))
243
config_options.append(ConfigOption("svn_auth_ivle", "/opt/ivle/svn/ivle.auth",
244
"""The location of the password file used to authenticate users
245
of the subversion repository from the ivle server:""",
247
# The location of the password file used to authenticate users
248
# of the subversion repository from the ivle server."""))
249
config_options.append(ConfigOption("svn_auth_local", "/opt/ivle/svn/local.auth",
250
"""The location of the password file used to authenticate local users
251
of the subversion repository:""",
253
# The location of the password file used to authenticate local users
254
# of the subversion repository."""))
255
config_options.append(ConfigOption("usrmgt_host", "localhost",
256
"""User Management Server config
257
============================
258
The hostname where the usrmgt-server runs:""",
260
# The hostname where the usrmgt-server runs."""))
261
config_options.append(ConfigOption("usrmgt_port", "2178",
262
"""The port where the usrmgt-server runs:""",
264
# The port where the usrmgt-server runs."""))
265
config_options.append(ConfigOption("usrmgt_magic", "",
266
"""The password for the usrmgt-server:""",
268
# The password for the usrmgt-server."""))
270
# Try importing existing conf, but if we can't just set up defaults
271
# The reason for this is that these settings are used by other phases
272
# of setup besides conf, so we need to know them.
273
# Also this allows you to hit Return to accept the existing value.
275
confmodule = __import__("lib/conf/conf")
276
for opt in config_options:
278
globals()[opt.option_name] = confmodule.__dict__[opt.option_name]
280
globals()[opt.option_name] = opt.default
282
# Just set reasonable defaults
283
for opt in config_options:
284
globals()[opt.option_name] = opt.default
286
# Try importing install_list, but don't fail if we can't, because listmake can
287
# function without it.
293
# Mime types which will automatically be placed in the list by listmake.
294
# Note that listmake is not intended to be run by the final user (the system
295
# administrator who installs this), so the developers can customize the list
296
# as necessary, and include it in the distribution.
297
listmake_mimetypes = ['text/x-python', 'text/html',
298
'application/x-javascript', 'application/javascript',
299
'text/css', 'image/png', 'image/gif', 'application/xml']
301
# Main function skeleton from Guido van Rossum
302
# http://www.artima.com/weblogs/viewpost.jsp?thread=4829
33
304
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):
327
# Disallow run as root unless installing
328
if (operation != 'install' and operation != 'updatejails' and operation !=
330
and os.geteuid() == 0):
331
print >>sys.stderr, "I do not want to run this stage as root."
332
print >>sys.stderr, "Please run as a normal user."
74
334
# Call the requested operation's function
78
'build' : setup.build.build,
79
'install' : setup.install.install,
340
'listmake' : listmake,
342
'updatejails' : updatejails,
82
345
print >>sys.stderr, (
83
346
"""Invalid operation '%s'. Try python setup.py help."""
349
return oper_func(argv[2:])
351
# Operation functions
355
print """Usage: python setup.py operation [args]
356
Operation (and args) can be:
358
listmake (developer use only)
361
install [--nojail] [--nosubjects] [-n|--dry]
365
print """Usage: python setup.py help [operation]"""
370
if operation == 'help':
371
print """python setup.py help [operation]
372
Prints the usage message or detailed help on an operation, then exits."""
373
elif operation == 'listmake':
374
print """python setup.py listmake
375
(For developer use only)
376
Recurses through the source tree and builds a list of all files which should
377
be copied upon installation. This should be run by the developer before
378
cutting a distribution, and the listfile it generates should be included in
379
the distribution, avoiding the administrator having to run it."""
380
elif operation == 'config':
381
print """python setup.py config [args]
382
Configures IVLE with machine-specific details, most notably, various paths.
383
Either prompts the administrator for these details or accepts them as
384
command-line args. Will be interactive only if there are no arguments given.
385
Takes defaults from existing conf file if it exists.
387
To run IVLE out of the source directory (allowing development without having
388
to rebuild/install), just provide ivle_install_dir as the IVLE trunk
389
directory, and run build/install one time.
391
Creates lib/conf/conf.py and trampoline/conf.h.
394
for opt in config_options:
395
print " --" + opt.option_name
396
print """As explained in the interactive prompt or conf.py.
398
elif operation == 'build':
399
print """python -O setup.py build [--dry|-n]
400
Compiles all files and sets up a jail template in the source directory.
401
-O is recommended to cause compilation to be optimised.
403
Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
405
Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
406
Copies console/ to a location within the jail.
407
Copies OS programs and files to corresponding locations within the jail
408
(eg. python and Python libs, ld.so, etc).
409
Generates .pyc or .pyo files for all the IVLE .py files.
411
--dry | -n Print out the actions but don't do anything."""
412
elif operation == 'install':
413
print """sudo python setup.py install [--nojail] [--nosubjects][--dry|-n]
415
Create target install directory ($target).
417
Copy trampoline/trampoline to $target/bin.
418
chown and chmod the installed trampoline.
419
Copy www/ to $target.
420
Copy jail/ to jails __staging__ directory (unless --nojail specified).
421
Copy subjects/ to subjects directory (unless --nosubjects specified).
423
--nojail Do not copy the jail.
424
--nosubjects Do not copy the subjects and exercises directories.
425
--dry | -n Print out the actions but don't do anything."""
426
elif operation == 'updatejails':
427
print """sudo python setup.py updatejails [--dry|-n]
429
Copy jail/ to each subdirectory in jails directory.
431
--dry | -n Print out the actions but don't do anything."""
433
print >>sys.stderr, (
434
"""Invalid operation '%s'. Try python setup.py help."""
439
# We build two separate lists, by walking www and console
440
list_www = build_list_py_files('www')
441
list_lib = build_list_py_files('lib')
442
list_subjects = build_list_py_files('subjects', no_top_level=True)
443
list_exercises = build_list_py_files('exercises', no_top_level=True)
445
"scripts/python-console",
446
"scripts/fileservice",
447
"scripts/serveservice",
448
"scripts/usrmgt-server",
449
"scripts/diffservice",
451
# Make sure that the files generated by conf are in the list
452
# (since listmake is typically run before conf)
453
if "lib/conf/conf.py" not in list_lib:
454
list_lib.append("lib/conf/conf.py")
455
# Write these out to a file
457
# the files that will be created/overwritten
458
listfile = os.path.join(cwd, "install_list.py")
461
file = open(listfile, "w")
463
file.write("""# IVLE Configuration File
465
# Provides lists of all files to be installed by `setup.py install' from
466
# certain directories.
467
# Note that any files with the given filename plus 'c' or 'o' (that is,
468
# compiled .pyc or .pyo files) will be copied as well.
470
# List of all installable files in www directory.
472
writelist_pretty(file, list_www)
474
# List of all installable files in lib directory.
476
writelist_pretty(file, list_lib)
478
# List of all installable files in scripts directory.
480
writelist_pretty(file, list_scripts)
482
# List of all installable files in subjects directory.
483
# This is to install sample subjects and material.
485
writelist_pretty(file, list_subjects)
487
# List of all installable files in exercises directory.
488
# This is to install sample exercise material.
489
list_exercises = """)
490
writelist_pretty(file, list_exercises)
493
except IOError, (errno, strerror):
494
print "IO error(%s): %s" % (errno, strerror)
497
print "Successfully wrote install_list.py"
500
print ("You may modify the set of installable files before cutting the "
507
def build_list_py_files(dir, no_top_level=False):
508
"""Builds a list of all py files found in a directory and its
509
subdirectories. Returns this as a list of strings.
510
no_top_level=True means the file paths will not include the top-level
514
for (dirpath, dirnames, filenames) in os.walk(dir):
515
# Exclude directories beginning with a '.' (such as '.svn')
516
filter_mutate(lambda x: x[0] != '.', dirnames)
517
# All *.py files are added to the list
518
pylist += [os.path.join(dirpath, item) for item in filenames
519
if mimetypes.guess_type(item)[0] in listmake_mimetypes]
521
for i in range(0, len(pylist)):
522
_, pylist[i] = pylist[i].split(os.sep, 1)
525
def writelist_pretty(file, list):
526
"""Writes a list one element per line, to a file."""
532
file.write(' %s,\n' % repr(elem))
536
global db_port, usrmgt_port
537
# Set up some variables
540
# the files that will be created/overwritten
541
conffile = os.path.join(cwd, "lib/conf/conf.py")
542
jailconffile = os.path.join(cwd, "lib/conf/jailconf.py")
543
conf_hfile = os.path.join(cwd, "trampoline/conf.h")
544
phpBBconffile = os.path.join(cwd, "www/php/phpBB3/config.php")
545
usrmgtserver_initdfile = os.path.join(cwd, "doc/setup/usrmgt-server.init")
547
# Get command-line arguments to avoid asking questions.
550
for opt in config_options:
551
optnames.append(opt.option_name + "=")
552
(opts, args) = getopt.gnu_getopt(args, "", optnames)
555
print >>sys.stderr, "Invalid arguments:", string.join(args, ' ')
559
# Interactive mode. Prompt the user for all the values.
561
print """This tool will create the following files:
567
prompting you for details about your configuration. The file will be
568
overwritten if it already exists. It will *not* install or deploy IVLE.
570
Please hit Ctrl+C now if you do not wish to do this.
571
""" % (conffile, jailconffile, conf_hfile, phpBBconffile, usrmgtserver_initdfile)
573
# Get information from the administrator
574
# If EOF is encountered at any time during the questioning, just exit
577
for opt in config_options:
578
globals()[opt.option_name] = \
579
query_user(globals()[opt.option_name], opt.prompt)
582
# Non-interactive mode. Parse the options.
583
for opt in config_options:
584
if '--' + opt.option_name in opts:
585
globals()[opt.option_name] = opts['--' + opt.option_name]
587
# Error handling on input values
589
allowed_uids_list = map(int, allowed_uids.split(','))
591
print >>sys.stderr, (
592
"Invalid UID list (%s).\n"
593
"Must be a comma-separated list of integers." % allowed_uids)
596
db_port = int(db_port)
597
if db_port < 0 or db_port >= 65536: raise ValueError()
599
print >>sys.stderr, (
600
"Invalid DB port (%s).\n"
601
"Must be an integer between 0 and 65535." % repr(db_port))
604
usrmgt_port = int(usrmgt_port)
605
if usrmgt_port < 0 or usrmgt_port >= 65536: raise ValueError()
607
print >>sys.stderr, (
608
"Invalid user management port (%s).\n"
609
"Must be an integer between 0 and 65535." % repr(usrmgt_port))
612
# Generate the forum secret
613
forum_secret = hashlib.md5(uuid.uuid4().bytes).hexdigest()
615
# Write lib/conf/conf.py
618
conf = open(conffile, "w")
620
conf.write("""# IVLE Configuration File
622
# Miscellaneous application settings
625
for opt in config_options:
626
conf.write('%s\n%s = %s\n' % (opt.comment, opt.option_name,
627
repr(globals()[opt.option_name])))
629
# Add the forum secret to the config file (regenerated each config)
630
conf.write('forum_secret = "%s"\n' % (forum_secret))
633
except IOError, (errno, strerror):
634
print "IO error(%s): %s" % (errno, strerror)
637
print "Successfully wrote lib/conf/conf.py"
639
# Write conf/jailconf.py
642
conf = open(jailconffile, "w")
644
# In the "in-jail" version of conf, we don't need MOST of the details
645
# (it would be a security risk to have them here).
646
# So we just write root_dir, and jail_base is "/".
647
# (jail_base being "/" means "jail-relative" paths are relative to "/"
648
# when inside the jail.)
649
conf.write("""# IVLE Configuration File
651
# Miscellaneous application settings
652
# (User jail version)
655
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
657
# eg. "/" or "/ivle".
660
# In the local file system, where are the student/user file spaces located.
661
# The user jails are expected to be located immediately in subdirectories of
665
# The hostname for serving publicly accessible pages
667
""" % (repr(root_dir),repr(public_host)))
670
except IOError, (errno, strerror):
671
print "IO error(%s): %s" % (errno, strerror)
674
print "Successfully wrote lib/conf/jailconf.py"
676
# Write trampoline/conf.h
679
conf = open(conf_hfile, "w")
681
conf.write("""/* IVLE Configuration File
683
* Administrator settings required by trampoline.
684
* Note: trampoline will have to be rebuilt in order for changes to this file
688
/* In the local file system, where are the jails located.
689
* The trampoline does not allow the creation of a jail anywhere besides
690
* jail_base or a subdirectory of jail_base.
692
static const char* jail_base = "%s";
694
/* Which user IDs are allowed to run the trampoline.
695
* This list should be limited to the web server user.
696
* (Note that root is an implicit member of this list).
698
static const int allowed_uids[] = { %s };
699
""" % (repr(jail_base)[1:-1], repr(allowed_uids_list)[1:-1]))
700
# Note: The above uses PYTHON reprs, not C reprs
701
# However they should be the same with the exception of the outer
702
# characters, which are stripped off and replaced
705
except IOError, (errno, strerror):
706
print "IO error(%s): %s" % (errno, strerror)
709
print "Successfully wrote trampoline/conf.h"
711
# Write www/php/phpBB3/config.php
714
conf = open(phpBBconffile, "w")
717
if db_host == 'localhost':
718
forumdb_host = '127.0.0.1'
720
forumdb_host = db_host
723
// phpBB 3.0.x auto-generated configuration file
724
// Do not change anything in this file!
726
$dbhost = '""" + forumdb_host + """';
727
$dbport = '""" + str(db_port) + """';
728
$dbname = '""" + db_forumdbname + """';
729
$dbuser = '""" + db_user + """';
730
$dbpasswd = '""" + db_password + """';
732
$table_prefix = 'phpbb_';
734
$load_extensions = '';
735
@define('PHPBB_INSTALLED', true);
736
// @define('DEBUG', true);
737
//@define('DEBUG_EXTRA', true);
739
$forum_secret = '""" + forum_secret +"""';
743
except IOError, (errno, strerror):
744
print "IO error(%s): %s" % (errno, strerror)
747
print "Successfully wrote www/php/phpBB3/config.php"
749
# Write lib/conf/usrmgt-server.init
752
conf = open(usrmgtserver_initdfile, "w")
754
conf.write( '''#! /bin/sh
756
# Works for Ubuntu. Check before using on other distributions
759
# Provides: usrmgt-server
760
# Required-Start: $syslog $networking $urandom
761
# Required-Stop: $syslog
762
# Default-Start: 2 3 4 5
764
# Short-Description: IVLE user management server
765
# Description: Daemon connecting to the IVLE user management database.
768
PATH=/sbin:/bin:/usr/sbin:/usr/bin
769
DESC="IVLE user management server"
771
DAEMON=/opt/ivle/scripts/$NAME
772
DAEMON_ARGS="''' + str(usrmgt_port) + ''' ''' + usrmgt_magic + '''"
773
PIDFILE=/var/run/$NAME.pid
774
SCRIPTNAME=/etc/init.d/usrmgt-server
776
# Exit if the daemon does not exist
777
test -f $DAEMON || exit 0
779
# Load the VERBOSE setting and other rcS variables
780
[ -f /etc/default/rcS ] && . /etc/default/rcS
782
# Define LSB log_* functions.
783
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
784
. /lib/lsb/init-functions
787
# Function that starts the daemon/service
792
# 0 if daemon has been started
793
# 1 if daemon was already running
794
# 2 if daemon could not be started
795
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
797
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
800
# Add code here, if necessary, that waits for the process to be ready
801
# to handle requests from services started subsequently which depend
802
# on this one. As a last resort, sleep for some time.
806
# Function that stops the daemon/service
811
# 0 if daemon has been stopped
812
# 1 if daemon was already stopped
813
# 2 if daemon could not be stopped
814
# other if a failure occurred
815
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
817
[ "$RETVAL" = 2 ] && return 2
818
# Wait for children to finish too if this is a daemon that forks
819
# and if the daemon is only ever run from this initscript.
820
# If the above conditions are not satisfied then add some other code
821
# that waits for the process to drop all resources that could be
822
# needed by services started subsequently. A last resort is to
823
# sleep for some time.
824
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
825
[ "$?" = 2 ] && return 2
826
# Many daemons don't delete their pidfiles when they exit.
832
# Function that sends a SIGHUP to the daemon/service
836
# If the daemon can reload its configuration without
837
# restarting (for example, when it is sent a SIGHUP),
838
# then implement that here.
840
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
846
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
849
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
850
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
854
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
857
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
858
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
861
#reload|force-reload)
863
# If do_reload() is not implemented then leave this commented out
864
# and leave 'force-reload' as an alias for 'restart'.
866
#log_daemon_msg "Reloading $DESC" "$NAME"
870
restart|force-reload)
872
# If the "reload" option is implemented then remove the
873
# 'force-reload' alias
875
log_daemon_msg "Restarting $DESC" "$NAME"
882
1) log_end_msg 1 ;; # Old process is still running
883
*) log_end_msg 1 ;; # Failed to start
893
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
894
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
903
except IOError, (errno, strerror):
904
print "IO error(%s): %s" % (errno, strerror)
907
# fix permissions as the file contains the database password
909
os.chmod('doc/setup/usrmgt-server.init', 0600)
910
except OSError, (errno, strerror):
911
print "WARNING: Couldn't chmod doc/setup/usrmgt-server.init:"
912
print "OS error(%s): %s" % (errno, strerror)
914
print "Successfully wrote lib/conf/usrmgt-server.init"
917
print "You may modify the configuration at any time by editing"
922
print usrmgtserver_initdfile
927
# Get "dry" variable from command line
928
(opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
930
dry = '-n' in opts or '--dry' in opts
933
print "Dry run (no actions will be executed\n"
935
if not dry and os.geteuid() != 0:
936
print >>sys.stderr, "Must be root to run build"
937
print >>sys.stderr, "(I need to chroot)."
940
# Find out the revison number
941
revnum = get_svn_revision()
942
print "Building Revision %s"%str(revnum)
944
vfile = open('BUILD-VERSION','w')
945
vfile.write(str(revnum) + '\n')
948
# Compile the trampoline
950
os.chdir('trampoline')
951
action_runprog('make', [], dry)
954
# Create the jail and its subdirectories
955
# Note: Other subdirs will be made by copying files
956
action_runprog('./buildjail.sh', [], dry)
958
# Copy all console and operating system files into the jail
959
action_copylist(install_list.list_scripts, 'jail/opt/ivle', dry)
960
# Chmod the python console
961
action_chmod_x('jail/opt/ivle/scripts/python-console', dry)
962
action_chmod_x('jail/opt/ivle/scripts/fileservice', dry)
963
action_chmod_x('jail/opt/ivle/scripts/serveservice', dry)
965
# Also copy the IVLE lib directory into the jail
966
# This is necessary for running certain scripts
967
action_copylist(install_list.list_lib, 'jail/opt/ivle', dry)
968
# IMPORTANT: The file jail/opt/ivle/lib/conf/conf.py contains details
969
# which could compromise security if left in the jail (such as the DB
971
# The "safe" version is in jailconf.py. Delete conf.py and replace it with
973
action_copyfile('lib/conf/jailconf.py',
974
'jail/opt/ivle/lib/conf/conf.py', dry)
976
# Compile .py files into .pyc or .pyo files
977
compileall.compile_dir('www', quiet=True)
978
compileall.compile_dir('lib', quiet=True)
979
compileall.compile_dir('scripts', quiet=True)
980
compileall.compile_dir('jail/opt/ivle/lib', quiet=True)
982
# Set up ivle.pth inside the jail
983
# Need to set /opt/ivle/lib to be on the import path
985
"jail/usr/lib/python%s/site-packages/ivle.pth" % PYTHON_VERSION
986
f = open(ivle_pth, 'w')
987
f.write('/opt/ivle/lib\n')
992
def copy_file_to_jail(src, dry):
993
"""Copies a single file from an absolute location into the same location
994
within the jail. src must begin with a '/'. The jail will be located
995
in a 'jail' subdirectory of the current path."""
996
action_copyfile(src, 'jail' + src, dry)
999
# Get "dry" and "nojail" variables from command line
1000
(opts, args) = getopt.gnu_getopt(args, "n",
1001
['dry', 'nojail', 'nosubjects'])
1003
dry = '-n' in opts or '--dry' in opts
1004
nojail = '--nojail' in opts
1005
nosubjects = '--nosubjects' in opts
1008
print "Dry run (no actions will be executed\n"
1010
if not dry and os.geteuid() != 0:
1011
print >>sys.stderr, "Must be root to run install"
1012
print >>sys.stderr, "(I need to chown some files)."
1015
# Create the target (install) directory
1016
action_mkdir(ivle_install_dir, dry)
1018
# Create bin and copy the compiled files there
1019
action_mkdir(os.path.join(ivle_install_dir, 'bin'), dry)
1020
tramppath = os.path.join(ivle_install_dir, 'bin/trampoline')
1021
action_copyfile('trampoline/trampoline', tramppath, dry)
1022
# chown trampoline to root and set setuid bit
1023
action_chown_setuid(tramppath, dry)
1025
# Create a scripts directory to put the usrmgt-server in.
1026
action_mkdir(os.path.join(ivle_install_dir, 'scripts'), dry)
1027
usrmgtpath = os.path.join(ivle_install_dir, 'scripts/usrmgt-server')
1028
action_copyfile('scripts/usrmgt-server', usrmgtpath, dry)
1029
action_chmod_x(usrmgtpath, dry)
1031
# Copy the www and lib directories using the list
1032
action_copylist(install_list.list_www, ivle_install_dir, dry)
1033
action_copylist(install_list.list_lib, ivle_install_dir, dry)
1035
# Copy the php directory
1036
forum_dir = "www/php/phpBB3"
1037
forum_path = os.path.join(ivle_install_dir, forum_dir)
1038
action_copytree(forum_dir, forum_path, dry)
1039
print "chown -R www-data:www-data %s" % forum_path
1041
os.system("chown -R www-data:www-data %s" % forum_path)
1044
# Copy the local jail directory built by the build action
1045
# to the jails __staging__ directory (it will be used to help build
1046
# all the students' jails).
1047
action_copytree('jail', os.path.join(jail_base, '__staging__'), dry)
1049
# Copy the subjects and exercises directories across
1050
action_copylist(install_list.list_subjects, subjects_base, dry,
1051
srcdir="./subjects")
1052
action_copylist(install_list.list_exercises, exercises_base, dry,
1053
srcdir="./exercises")
1055
# Append IVLE path to ivle.pth in python site packages
1056
# (Unless it's already there)
1057
ivle_pth = os.path.join(sys.prefix,
1058
"lib/python%s/site-packages/ivle.pth" % PYTHON_VERSION)
1059
ivle_www = os.path.join(ivle_install_dir, "www")
1060
ivle_lib = os.path.join(ivle_install_dir, "lib")
1061
write_ivle_pth = True
1062
write_ivle_lib_pth = True
1064
file = open(ivle_pth, 'r')
1066
if line.strip() == ivle_www:
1067
write_ivle_pth = False
1068
elif line.strip() == ivle_lib:
1069
write_ivle_lib_pth = False
1071
except (IOError, OSError):
1074
action_append(ivle_pth, ivle_www)
1075
if write_ivle_lib_pth:
1076
action_append(ivle_pth, ivle_lib)
1079
# Create the ivle working revision record file
1080
action_mkdir(os.path.join(ivle_install_dir, 'version'), dry)
1081
ivle_revision_record_file = os.path.join(ivle_install_dir, 'version/ivle-revision.txt')
1084
conf = open(ivle_revision_record_file, "w")
1086
conf.write( "# IVLE code revision listing generated by running 'svn status -v ..' from " + os.getcwd() + "\n#\n\n")
1089
except IOError, (errno, strerror):
1090
print "IO error(%s): %s" % (errno, strerror)
1093
os.system("svn status -v .. >> %s" % ivle_revision_record_file)
1095
print "Wrote IVLE code revision status to %s" % ivle_revision_record_file
1099
def updatejails(args):
1100
# Get "dry" variable from command line
1101
(opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
1103
dry = '-n' in opts or '--dry' in opts
1106
print "Dry run (no actions will be executed\n"
1108
if not dry and os.geteuid() != 0:
1109
print >>sys.stderr, "Must be root to run install"
1110
print >>sys.stderr, "(I need to chown some files)."
1113
# Update the staging jail directory in case it hasn't been installed
1115
action_copytree('jail', os.path.join(jail_base, '__staging__'), dry)
1117
# Re-link all the files in all students jails.
1118
for dir in os.listdir(jail_base):
1119
if dir == '__staging__': continue
1120
# First back up the student's home directory
1121
temp_home = os.tmpnam()
1122
action_rename(os.path.join(jail_base, dir, 'home'), temp_home, dry)
1123
# Delete the student's jail and relink the jail files
1124
action_linktree(os.path.join(jail_base, '__staging__'),
1125
os.path.join(jail_base, dir), dry)
1126
# Restore the student's home directory
1127
action_rename(temp_home, os.path.join(jail_base, dir, 'home'), dry)
1128
# Set up the user's home directory just in case they don't have a
1129
# directory for this yet
1130
action_mkdir(os.path.join(jail_base, dir, 'home', dir), dry)
1134
# The actions call Python os functions but print actions and handle dryness.
1135
# May still throw os exceptions if errors occur.
1138
"""Represents an error when running a program (nonzero return)."""
1139
def __init__(self, prog, retcode):
1141
self.retcode = retcode
1143
return str(self.prog) + " returned " + repr(self.retcode)
1145
def action_runprog(prog, args, dry):
1146
"""Runs a unix program. Searches in $PATH. Synchronous (waits for the
1147
program to return). Runs in the current environment. First prints the
1148
action as a "bash" line.
1150
Throws a RunError with a retcode of the return value of the program,
1151
if the program did not return 0.
1153
prog: String. Name of the program. (No path required, if in $PATH).
1154
args: [String]. Arguments to the program.
1155
dry: Bool. If True, prints but does not execute.
1157
print prog, string.join(args, ' ')
1159
ret = os.spawnvp(os.P_WAIT, prog, args)
1161
raise RunError(prog, ret)
1163
def action_remove(path, dry):
1164
"""Calls rmtree, deleting the target file if it exists."""
1168
shutil.rmtree(path, True)
1169
except OSError, (err, msg):
1170
if err != errno.EEXIST:
1172
# Otherwise, didn't exist, so we don't care
1174
def action_rename(src, dst, dry):
1175
"""Calls rename. Deletes the target if it already exists."""
1176
action_remove(dst, dry)
1177
print "mv ", src, dst
1181
except OSError, (err, msg):
1182
if err != errno.EEXIST:
1185
def action_mkdir(path, dry):
1186
"""Calls mkdir. Silently ignored if the directory already exists.
1187
Creates all parent directories as necessary."""
1188
print "mkdir -p", path
1192
except OSError, (err, msg):
1193
if err != errno.EEXIST:
1196
def action_copytree(src, dst, dry):
1197
"""Copies an entire directory tree. Symlinks are seen as normal files and
1198
copies of the entire file (not the link) are made. Creates all parent
1199
directories as necessary.
1201
See shutil.copytree."""
1202
# Allow copying over itself
1203
if (os.path.normpath(os.path.join(os.getcwd(),src)) ==
1204
os.path.normpath(os.path.join(os.getcwd(),dst))):
1206
action_remove(dst, dry)
1207
print "cp -r", src, dst
1209
shutil.copytree(src, dst, True)
1211
def action_linktree(src, dst, dry):
1212
"""Hard-links an entire directory tree. Same as copytree but the created
1213
files are hard-links not actual copies. Removes the existing destination.
1215
action_remove(dst, dry)
1216
print "<cp with hardlinks> -r", src, dst
1218
common.makeuser.linktree(src, dst)
1220
def action_copylist(srclist, dst, dry, srcdir="."):
1221
"""Copies all files in a list to a new location. The files in the list
1222
are read relative to the current directory, and their destinations are the
1223
same paths relative to dst. Creates all parent directories as necessary.
1224
srcdir is "." by default, can be overridden.
1226
for srcfile in srclist:
1227
dstfile = os.path.join(dst, srcfile)
1228
srcfile = os.path.join(srcdir, srcfile)
1229
dstdir = os.path.split(dstfile)[0]
1230
if not os.path.isdir(dstdir):
1231
action_mkdir(dstdir, dry)
1232
print "cp -f", srcfile, dstfile
1235
shutil.copyfile(srcfile, dstfile)
1236
shutil.copymode(srcfile, dstfile)
1237
except shutil.Error:
1240
def action_copyfile(src, dst, dry):
1241
"""Copies one file to a new location. Creates all parent directories
1243
Warn if file not found.
1245
dstdir = os.path.split(dst)[0]
1246
if not os.path.isdir(dstdir):
1247
action_mkdir(dstdir, dry)
1248
print "cp -f", src, dst
1251
shutil.copyfile(src, dst)
1252
shutil.copymode(src, dst)
1253
except (shutil.Error, IOError), e:
1254
print "Warning: " + str(e)
1256
def action_symlink(src, dst, dry):
1257
"""Creates a symlink in a given location. Creates all parent directories
1260
dstdir = os.path.split(dst)[0]
1261
if not os.path.isdir(dstdir):
1262
action_mkdir(dstdir, dry)
1263
# Delete existing file
1264
if os.path.exists(dst):
1266
print "ln -fs", src, dst
1268
os.symlink(src, dst)
1270
def action_append(ivle_pth, ivle_www):
1271
file = open(ivle_pth, 'a+')
1272
file.write(ivle_www + '\n')
1275
def action_chown_setuid(file, dry):
1276
"""Chowns a file to root, and sets the setuid bit on the file.
1277
Calling this function requires the euid to be root.
1278
The actual mode of path is set to: rws--s--s
1280
print "chown root:root", file
1282
os.chown(file, 0, 0)
1283
print "chmod a+xs", file
1284
print "chmod u+rw", file
1286
os.chmod(file, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
1287
| stat.S_ISUID | stat.S_IRUSR | stat.S_IWUSR)
1289
def action_chmod_x(file, dry):
1290
"""Chmod 755 a file (sets permissions to rwxr-xr-x)."""
1291
print "chmod 755", file
1293
os.chmod(file, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR
1294
| stat.S_IXGRP | stat.S_IRGRP | stat.S_IXOTH | stat.S_IROTH)
1296
def query_user(default, prompt):
1297
"""Prompts the user for a string, which is read from a line of stdin.
1298
Exits silently if EOF is encountered. Returns the string, with spaces
1299
removed from the beginning and end.
1301
Returns default if a 0-length line (after spaces removed) was read.
1303
sys.stdout.write('%s\n (default: "%s")\n>' % (prompt, default))
1305
val = sys.stdin.readline()
1306
except KeyboardInterrupt:
1308
sys.stdout.write("\n")
1310
sys.stdout.write("\n")
1312
if val == '': sys.exit(1)
1313
# If empty line, return default
1315
if val == '': return default
1318
def filter_mutate(function, list):
1319
"""Like built-in filter, but mutates the given list instead of returning a
1320
new one. Returns None."""
1323
# Delete elements which do not match
1324
if not function(list[i]):
1328
def get_svn_revision():
1329
"""Returns either the current SVN revision of this build, or None"""
1331
svn = pysvn.Client()
1332
entry = svn.info('.')
1333
revnum = entry.revision.number
1334
except pysvn.ClientError, e:
88
1338
if __name__ == "__main__":