1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# Module: setup/config
19
# Author: Matt Giuca, Refactored by David Coles
22
'''Configures IVLE with machine-specific details, most notably, various paths.
23
Either prompts the administrator for these details or accepts them as
26
Creates ivle/conf/conf.py and bin/trampoline/trampoline/conf.h.
36
from setup.util import query_user
41
# conf_options maps option names to values
45
"""A configuration option; one of the things written to conf.py."""
46
def __init__(self, option_name, default, prompt, comment, ask=True):
47
"""Creates a configuration option.
48
option_name: Name of the variable in conf.py. Also name of the
49
command-line argument to setup.py conf.
50
default: Default value for this variable.
51
prompt: (Short) string presented during the interactive prompt in
53
comment: (Long) comment string stored in conf.py. Each line of this
54
string should begin with a '#'.
55
ask: Whether to ask the question in the default config run.
57
self.option_name = option_name
58
self.default = default
60
self.comment = comment
63
# Configuration options, defaults and descriptions
66
config_options.append(ConfigOption("urls/root", "/",
67
"""Root directory where IVLE is located (in URL space):""",
69
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
71
# eg. "/" or "/ivle".""", ask=False))
73
config_options.append(ConfigOption("paths/prefix", "/usr/local",
74
"""In the local file system, the prefix to the system directory where IVLE
75
is installed. (This should either be /usr or /usr/local):""",
77
# In the local file system, the prefix to the system directory where IVLE is
78
# installed. This should either be '/usr' or '/usr/local'.
79
# ('/usr/local' for the usual install, '/usr' for distribution packages)""",
82
config_options.append(ConfigOption("paths/site_packages",
84
"""site-packages directory in Python, where Python libraries are to be
85
installed. May be left as the default, in which case the value will be
86
computed from prefix and the current Python version:""",
88
# 'site-packages' directory in Python, where Python libraries are to be
89
# installed. May be None (recommended), in which case the value will be
90
# computed from prefix and the current Python version.""", ask=False))
92
config_options.append(ConfigOption("paths/data",
94
"In the local file system, where user-modifiable data files should be "
97
# In the local file system, where user-modifiable data files should be
98
# located.""", ask=False))
100
config_options.append(ConfigOption("paths/logs",
102
"""Directory where IVLE log files are stored (on the local
103
file system). Note - this must be writable by the user the IVLE server
104
process runs as (usually www-data):""",
106
# In the local file system, where IVLE error logs should be located.""",
109
config_options.append(ConfigOption("urls/public_host", "public.localhost",
110
"""Hostname which will cause the server to go into "public mode",
111
providing login-free access to student's published work:""",
113
# The server goes into "public mode" if the browser sends a request with this
114
# host. This is for security reasons - we only serve public student files on a
115
# separate domain to the main IVLE site.
116
# Public mode does not use cookies, and serves only public content.
117
# Private mode (normal mode) requires login, and only serves files relevant to
118
# the logged-in user."""))
120
config_options.append(ConfigOption("os/allowed_uids", "33",
121
"""UID of the web server process which will run IVLE.
122
Only this user may execute the trampoline. May specify multiple users as
123
a comma-separated list.
126
# The User-ID of the web server process which will run IVLE, and any other
127
# users who are allowed to run the trampoline. This is stores as a string of
128
# comma-separated integers, simply because it is not used within Python, only
129
# used by the setup program to write to conf.h (see setup.py config).""",
132
config_options.append(ConfigOption("database/host", "localhost",
133
"""PostgreSQL Database config
134
==========================
135
Hostname of the DB server:""",
137
# Database server hostname"""))
139
config_options.append(ConfigOption("database/port", "5432",
140
"""Port of the DB server:""",
142
# Database server port"""))
144
config_options.append(ConfigOption("database/name", "ivle",
145
"""Database name:""",
149
config_options.append(ConfigOption("plugins/forum/dbname", "ivle_forum",
150
"""Forum Database name:""",
152
# Forum Database name"""))
154
config_options.append(ConfigOption("database/username", "postgres",
155
"""Username for DB server login:""",
157
# Database username"""))
159
config_options.append(ConfigOption("database/password", "",
160
"""Password for DB server login:
161
(Caution: This password is stored in plaintext in ivle/conf/conf.py)""",
163
# Database password"""))
165
config_options.append(ConfigOption("auth/modules", "",
166
"""Authentication config
167
=====================
168
Comma-separated list of authentication modules.""",
170
# Comma-separated list of authentication modules.
171
# Note that auth is always enabled against the local database, and NO OTHER
172
# auth is enabled by default. This section is for specifying additional auth
174
# These refer to importable Python modules in the www/auth directory.
175
# Modules "ldap_auth" and "guest" are available in the source tree, but
176
# other modules may be plugged in to auth against organisation-specific
177
# auth backends.""", ask=False))
179
config_options.append(ConfigOption("auth/ldap_url", "ldaps://www.example.com",
180
"""(LDAP options are only relevant if "ldap" is included in the list of
182
URL for LDAP authentication server:""",
184
# URL for LDAP authentication server""", ask=False))
186
config_options.append(ConfigOption("auth/ldap_format_string",
187
"uid=%s,ou=users,o=example",
188
"""Format string for LDAP auth request:
189
(Must contain a single "%s" for the user's login name)""",
191
# Format string for LDAP auth request
192
# (Must contain a single "%s" for the user's login name)""", ask=False))
194
config_options.append(ConfigOption("auth/subject_pulldown_modules", "",
195
"""Comma-separated list of subject pulldown modules.
196
Add proprietary modules to automatically enrol students in subjects.""",
198
# Comma-separated list of subject pulldown modules.
199
# These refer to importable Python modules in the lib/pulldown_subj directory.
200
# Only "dummy_subj" is available in the source tree (an example), but
201
# other modules may be plugged in to pulldown against organisation-specific
202
# pulldown backends.""", ask=False))
204
config_options.append(ConfigOption("urls/svn_addr", "http://svn.localhost/",
207
The base url for accessing subversion repositories:""",
209
# The base url for accessing subversion repositories."""))
211
config_options.append(ConfigOption("usrmgt/host", "localhost",
212
"""User Management Server config
213
============================
214
The hostname where the usrmgt-server runs:""",
216
# The hostname where the usrmgt-server runs."""))
218
config_options.append(ConfigOption("usrmgt/port", "2178",
219
"""The port where the usrmgt-server runs:""",
221
# The port where the usrmgt-server runs.""", ask=False))
223
config_options.append(ConfigOption("usrmgt/magic", None,
224
"""The password for the usrmgt-server:""",
226
# The password for the usrmgt-server.""", ask=False))
229
# Call the real function
230
return __configure(args)
232
def __configure(args):
233
# Try importing existing conf, but if we can't just set up defaults
234
# The reason for this is that these settings are used by other phases
235
# of setup besides conf, so we need to know them.
236
# Also this allows you to hit Return to accept the existing value.
238
conf = ivle.config.Config()
239
except ivle.config.ConfigError:
240
# Couldn't find a config file anywhere.
241
# Create a new blank config object (not yet bound to a file)
242
# All lookups (below) will fail, so it will be initialised with all
243
# the default values.
244
conf = ivle.config.Config(blank=True)
246
for opt in config_options:
248
conf_options[opt.option_name] = conf.get_by_path(opt.option_name)
250
conf_options[opt.option_name] = opt.default
252
# Set up some variables
255
# the files that will be created/overwritten
256
conffile = os.path.join(cwd, "etc/ivle.conf")
257
conf_hfile = os.path.join(cwd, "bin/trampoline/conf.h")
258
phpBBconffile = os.path.join(cwd, "www/php/phpBB3/config.php")
260
# Get command-line arguments to avoid asking questions.
263
for opt in config_options:
264
optnames.append(opt.option_name + "=")
265
(opts, args) = getopt.gnu_getopt(args, "", optnames)
268
print >>sys.stderr, "Invalid arguments:", string.join(args, ' ')
272
# Interactive mode. Prompt the user for all the values.
274
print """This tool will create the following files:
278
prompting you for details about your configuration. The file will be
279
overwritten if it already exists. It will *not* install or deploy IVLE.
281
Please hit Ctrl+C now if you do not wish to do this.
282
""" % (conffile, conf_hfile, phpBBconffile)
284
# Get information from the administrator
285
# If EOF is encountered at any time during the questioning, just exit
288
for opt in config_options:
290
conf_options[opt.option_name] = \
291
query_user(conf_options[opt.option_name], opt.prompt)
294
# Non-interactive mode. Parse the options.
295
for opt in config_options:
296
if '--' + opt.option_name in opts:
297
conf_options[opt.option_name] = opts['--' + opt.option_name]
299
# Error handling on input values
301
allowed_uids_list = map(int,
302
conf_options['os/allowed_uids'].split(','))
304
print >>sys.stderr, (
305
"Invalid UID list (%s).\n"
306
"Must be a comma-separated list of integers." %
307
conf_options['os/allowed_uids'])
310
conf_options['database/port'] = int(conf_options['database/port'])
311
if (conf_options['database/port'] < 0
312
or conf_options['database/port'] >= 65536):
315
print >>sys.stderr, (
316
"Invalid DB port (%s).\n"
317
"Must be an integer between 0 and 65535." %
318
repr(conf_options['database/port']))
321
conf_options['usrmgt/port'] = int(conf_options['usrmgt/port'])
322
if (conf_options['usrmgt/port'] < 0
323
or conf_options['usrmgt/port'] >= 65536):
326
print >>sys.stderr, (
327
"Invalid user management port (%s).\n"
328
"Must be an integer between 0 and 65535." %
329
repr(conf_options['usrmgt/port']))
332
# By default we generate the magic randomly.
333
if conf_options['usrmgt/magic'] is None:
334
conf_options['usrmgt/magic'] = \
335
hashlib.md5(uuid.uuid4().bytes).hexdigest()
337
# Generate the forum secret
338
forum_secret = hashlib.md5(uuid.uuid4().bytes).hexdigest()
340
# Write ./etc/ivle.conf (even if we loaded from a different filename)
341
conf.filename = conffile
343
conf.initial_comment = ["# IVLE Configuration File"]
345
# Add the forum secret to the config file (regenerated each config)
346
config_options.append(ConfigOption('plugins/forum/secret', None, '', ''))
347
conf_options['plugins/forum/secret'] = forum_secret
349
for opt in config_options:
350
value = conf_options[opt.option_name]
351
if value is not None:
352
conf.set_by_path(opt.option_name, value, opt.comment)
356
print "Successfully wrote %s" % conffile
358
# Write bin/trampoline/conf.h
360
conf = open(conf_hfile, "w")
362
# XXX Compute jail_base, jail_src_base and jail_system. These will
363
# ALSO be done by the boilerplate code, but we need them here in order
364
# to write to the C file.
365
jail_base = os.path.join(conf_options['paths/data'], 'jailmounts')
366
jail_src_base = os.path.join(conf_options['paths/data'], 'jails')
367
jail_system = os.path.join(jail_src_base, '__base__')
369
conf.write("""/* IVLE Configuration File
371
* Administrator settings required by trampoline.
372
* Note: trampoline will have to be rebuilt in order for changes to this file
376
#define IVLE_AUFS_JAILS
378
/* In the local file system, where are the jails located.
379
* The trampoline does not allow the creation of a jail anywhere besides
380
* jail_base or a subdirectory of jail_base.
382
static const char* jail_base = "%s";
383
static const char* jail_src_base = "%s";
384
static const char* jail_system = "%s";
386
/* Which user IDs are allowed to run the trampoline.
387
* This list should be limited to the web server user.
388
* (Note that root is an implicit member of this list).
390
static const int allowed_uids[] = { %s };
391
""" % (repr(jail_base)[1:-1], repr(jail_src_base)[1:-1],
392
repr(jail_system)[1:-1], repr(allowed_uids_list)[1:-1]))
393
# Note: The above uses PYTHON reprs, not C reprs
394
# However they should be the same with the exception of the outer
395
# characters, which are stripped off and replaced
399
print "Successfully wrote %s" % conf_hfile
401
# Write www/php/phpBB3/config.php
403
conf = open(phpBBconffile, "w")
406
if conf_options['database/host'] == 'localhost':
407
forumdb_host = '127.0.0.1'
409
forumdb_host = conf_options['database/host']
412
// phpBB 3.0.x auto-generated configuration file
413
// Do not change anything in this file!
415
$dbhost = '""" + forumdb_host + """';
416
$dbport = '""" + str(conf_options['database/port']) + """';
417
$dbname = '""" + conf_options['plugins/forum/dbname'] + """';
418
$dbuser = '""" + conf_options['database/username'] + """';
419
$dbpasswd = '""" + conf_options['database/password'] + """';
421
$table_prefix = 'phpbb_';
423
$load_extensions = '';
424
@define('PHPBB_INSTALLED', true);
425
// @define('DEBUG', true);
426
//@define('DEBUG_EXTRA', true);
428
$forum_secret = '""" + forum_secret +"""';
433
print "Successfully wrote %s" % phpBBconffile
436
print "You may modify the configuration at any time by editing"