~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to setup/configure.py

setup.build: Removed import of ivle.conf.conf.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
1
# IVLE - Informatics Virtual Learning Environment
3
2
# Copyright (C) 2007-2009 The University of Melbourne
4
3
#
16
15
# along with this program; if not, write to the Free Software
17
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
17
 
 
18
# Module: setup/config
19
19
# Author: Matt Giuca, Refactored by David Coles
 
20
# Date:   03/07/2008
20
21
 
21
22
'''Configures IVLE with machine-specific details, most notably, various paths.
22
23
Either prompts the administrator for these details or accepts them as
23
24
command-line args.
24
25
 
25
 
Creates etc/ivle.conf
 
26
Creates ivle/conf/conf.py and bin/trampoline/trampoline/conf.h.
26
27
'''
27
28
 
28
29
import optparse
29
30
import getopt
30
31
import os
31
32
import sys
32
 
import stat
33
33
import hashlib
34
34
import uuid
35
35
 
 
36
from setup.util import query_user
36
37
import ivle.config
37
38
 
38
39
import configobj
75
76
# ('/usr/local' for the usual install, '/usr' for distribution packages)""",
76
77
    ask=False))
77
78
 
 
79
config_options.append(ConfigOption("paths/site_packages",
 
80
    None,
 
81
    """site-packages directory in Python, where Python libraries are to be
 
82
installed. May be left as the default, in which case the value will be
 
83
computed from prefix and the current Python version:""",
 
84
    """
 
85
# 'site-packages' directory in Python, where Python libraries are to be
 
86
# installed. May be None (recommended), in which case the value will be
 
87
# computed from prefix and the current Python version.""", ask=False))
 
88
 
78
89
config_options.append(ConfigOption("paths/data",
79
90
    "/var/lib/ivle",
80
91
    "In the local file system, where user-modifiable data files should be "
92
103
# In the local file system, where IVLE error logs should be located.""",
93
104
    ask=False))
94
105
 
95
 
config_options.append(ConfigOption("urls/public_host",
96
 
    "public.ivle.localhost",
 
106
config_options.append(ConfigOption("urls/public_host", "public.localhost",
97
107
    """Hostname which will cause the server to go into "public mode",
98
108
providing login-free access to student's published work:""",
99
109
    """
104
114
# Private mode (normal mode) requires login, and only serves files relevant to
105
115
# the logged-in user."""))
106
116
 
107
 
config_options.append(ConfigOption("media/version", None,
108
 
    """Version of IVLE media resources (must change on each upgrade):""",
 
117
config_options.append(ConfigOption("os/allowed_uids", "33",
 
118
    """UID of the web server process which will run IVLE.
 
119
Only this user may execute the trampoline. May specify multiple users as
 
120
a comma-separated list.
 
121
    (eg. "1002,78")""",
109
122
    """
110
 
# Version string for IVLE media resource URLs. When set, they are aggressively
111
 
# cached by the browser, so it must be either left unset or changed each time
112
 
# a media file is changed.""", ask=False))
 
123
# The User-ID of the web server process which will run IVLE, and any other
 
124
# users who are allowed to run the trampoline. This is stores as a string of
 
125
# comma-separated integers, simply because it is not used within Python, only
 
126
# used by the setup program to write to conf.h (see setup.py config).""",
 
127
    ask=False))
113
128
 
114
129
config_options.append(ConfigOption("database/host", "localhost",
115
130
    """PostgreSQL Database config
128
143
    """
129
144
# Database name"""))
130
145
 
 
146
config_options.append(ConfigOption("plugins/forum/dbname", "ivle_forum",
 
147
    """Forum Database name:""",
 
148
    """
 
149
# Forum Database name"""))
 
150
 
131
151
config_options.append(ConfigOption("database/username", "postgres",
132
152
    """Username for DB server login:""",
133
153
    """
135
155
 
136
156
config_options.append(ConfigOption("database/password", "",
137
157
    """Password for DB server login:
138
 
    (Caution: This password is stored in plaintext!)""",
 
158
    (Caution: This password is stored in plaintext in ivle/conf/conf.py)""",
139
159
    """
140
160
# Database password"""))
141
161
 
178
198
# other modules may be plugged in to pulldown against organisation-specific
179
199
# pulldown backends.""", ask=False))
180
200
 
181
 
config_options.append(ConfigOption("urls/svn_addr",
182
 
    "http://svn.ivle.localhost/",
 
201
config_options.append(ConfigOption("urls/svn_addr", "http://svn.localhost/",
183
202
    """Subversion config
184
203
=================
185
204
The base url for accessing subversion repositories:""",
203
222
    """
204
223
# The password for the usrmgt-server.""", ask=False))
205
224
 
206
 
config_options.append(ConfigOption("jail/suite", "hardy",
207
 
    """The distribution release to use to build the jail:""",
208
 
    """
209
 
# The distribution release to use to build the jail.""", ask=True))
210
 
 
211
 
config_options.append(ConfigOption("jail/mirror", "archive.ubuntu.com",
212
 
    """The archive mirror to use to build the jail:""",
213
 
    """
214
 
# The archive mirror to use to build the jail.""", ask=True))
215
 
 
216
 
config_options.append(ConfigOption("jail/devmode", False,
217
 
    """Whether jail development mode be activated:""",
218
 
    """
219
 
# Should jail development mode be activated?""", ask=False))
220
 
 
221
 
# The password for the usrmgt-server.""", ask=False))
222
 
def query_user(default, prompt):
223
 
    """Prompts the user for a string, which is read from a line of stdin.
224
 
    Exits silently if EOF is encountered. Returns the string, with spaces
225
 
    removed from the beginning and end.
226
 
 
227
 
    Returns default if a 0-length line (after spaces removed) was read.
228
 
    """
229
 
    if default is None:
230
 
        # A default of None means the value will be computed specially, so we
231
 
        # can't really tell you what it is
232
 
        defaultstr = "computed"
233
 
    elif isinstance(default, basestring):
234
 
        defaultstr = '"%s"' % default
235
 
    else:
236
 
        defaultstr = repr(default)
237
 
    sys.stdout.write('%s\n    (default: %s)\n>' % (prompt, defaultstr))
238
 
    try:
239
 
        val = sys.stdin.readline()
240
 
    except KeyboardInterrupt:
241
 
        # Ctrl+C
242
 
        sys.stdout.write("\n")
243
 
        sys.exit(1)
244
 
    sys.stdout.write("\n")
245
 
    # If EOF, exit
246
 
    if val == '': sys.exit(1)
247
 
    # If empty line, return default
248
 
    val = val.strip()
249
 
    if val == '': return default
250
 
    return val
251
 
 
252
225
def configure(args):
 
226
    # Call the real function
 
227
    return __configure(args)
 
228
 
 
229
def __configure(args):
253
230
    # Try importing existing conf, but if we can't just set up defaults
254
231
    # The reason for this is that these settings are used by other phases
255
232
    # of setup besides conf, so we need to know them.
268
245
        try:
269
246
            conf.get_by_path(opt.option_name)
270
247
        except KeyError:
271
 
            # If the default is None, omit it
272
 
            # Else ConfigObj will write the string 'None' to the conf file
273
 
            if opt.default is not None:
274
 
                conf.set_by_path(opt.option_name, opt.default)
 
248
            conf.set_by_path(opt.option_name, opt.default)
275
249
 
276
250
    # Store comments in the conf object
277
251
    for opt in config_options:
278
 
        # Omitted if the key doesn't exist
279
252
        conf.set_by_path(opt.option_name, comment=opt.comment)
280
253
 
281
254
    # Set up some variables
282
255
    cwd = os.getcwd()
283
256
 
284
257
    # the files that will be created/overwritten
285
 
    try:
286
 
        confdir = os.environ['IVLECONF']
287
 
    except KeyError:
288
 
        confdir = '/etc/ivle'
289
 
 
290
 
    conffile = os.path.join(confdir, 'ivle.conf')
291
 
    plugindefaultfile = os.path.join(confdir, 'plugins.d/000default.conf')
 
258
    conffile = os.path.join(cwd, "etc/ivle.conf")
 
259
    conf_hfile = os.path.join(cwd, "bin/trampoline/conf.h")
 
260
    phpBBconffile = os.path.join(cwd, "www/php/phpBB3/config.php")
292
261
 
293
262
    # Get command-line arguments to avoid asking questions.
294
263
 
298
267
    (opts, args) = getopt.gnu_getopt(args, "", optnames)
299
268
 
300
269
    if args != []:
301
 
        print >>sys.stderr, "Invalid arguments:", ' '.join(args)
 
270
        print >>sys.stderr, "Invalid arguments:", string.join(args, ' ')
302
271
        return 2
303
272
 
304
273
    if opts == []:
305
274
        # Interactive mode. Prompt the user for all the values.
306
275
 
307
 
        print """This tool will create %s, prompting you for details about
308
 
your configuration. The file will be updated with modified options if it already
309
 
exists. If it does not already exist, it will be created with sane defaults and
310
 
restrictive permissions.
311
 
 
312
 
%s will also be overwritten with the default list of plugins.
 
276
        print """This tool will create the following files:
 
277
    %s
 
278
    %s
 
279
    %s
 
280
prompting you for details about your configuration. The file will be
 
281
overwritten if it already exists. It will *not* install or deploy IVLE.
313
282
 
314
283
Please hit Ctrl+C now if you do not wish to do this.
315
 
""" % (conffile, plugindefaultfile)
 
284
""" % (conffile, conf_hfile, phpBBconffile)
316
285
 
317
286
        # Get information from the administrator
318
287
        # If EOF is encountered at any time during the questioning, just exit
332
301
 
333
302
    # Error handling on input values
334
303
    try:
 
304
        allowed_uids_list = map(int,
 
305
                                conf['os']['allowed_uids'].split(','))
 
306
    except ValueError:
 
307
        print >>sys.stderr, (
 
308
        "Invalid UID list (%s).\n"
 
309
        "Must be a comma-separated list of integers." %
 
310
            conf['os']['allowed_uids'])
 
311
        return 1
 
312
    try:
335
313
        conf['database']['port'] = int(conf['database']['port'])
336
314
        if (conf['database']['port'] < 0
337
315
            or conf['database']['port'] >= 65536):
338
316
            raise ValueError()
339
317
    except ValueError:
340
 
        if conf['database']['port'] == '' or conf['database']['port'] is None:
341
 
            pass
342
 
        else:
343
 
            print >>sys.stderr, (
344
 
            "Invalid DB port (%s).\n"
345
 
            "Must be an integer between 0 and 65535." %
346
 
                repr(conf['database']['port']))
347
 
            return 1
 
318
        print >>sys.stderr, (
 
319
        "Invalid DB port (%s).\n"
 
320
        "Must be an integer between 0 and 65535." %
 
321
            repr(conf['database']['port']))
 
322
        return 1
348
323
    try:
349
324
        conf['usrmgt']['port'] = int(conf['usrmgt']['port'])
350
325
        if (conf['usrmgt']['port'] < 0 or conf['usrmgt']['port'] >= 65536):
357
332
        return 1
358
333
 
359
334
    # By default we generate the magic randomly.
360
 
    try:
361
 
        conf['usrmgt']['magic']     # Throw away; just check for KeyError
362
 
    except KeyError:
 
335
    if conf['usrmgt']['magic'] is None:
363
336
        conf['usrmgt']['magic'] = hashlib.md5(uuid.uuid4().bytes).hexdigest()
364
337
 
365
 
    clobber_permissions = not os.path.exists(conffile)
 
338
    # Generate the forum secret
 
339
    forum_secret = hashlib.md5(uuid.uuid4().bytes).hexdigest()
366
340
 
367
341
    # Write ./etc/ivle.conf (even if we loaded from a different filename)
368
342
    conf.filename = conffile
 
343
 
369
344
    conf.initial_comment = ["# IVLE Configuration File"]
 
345
 
 
346
    # Add the forum secret to the config file (regenerated each config)
 
347
    config_options.append(ConfigOption('plugins/forum/secret', None, '', ''))
 
348
    conf['plugins']['forum']['secret'] = forum_secret
 
349
 
370
350
    conf.write()
371
351
 
372
 
    # We need to restrict permissions on a new file, as it contains
373
 
    # a nice database password.
374
 
    if clobber_permissions:
375
 
        os.chown(conffile, 33, 33) # chown to www-data
376
 
        os.chmod(conffile, stat.S_IRUSR | stat.S_IWUSR) # No g/o perms!
377
 
 
378
352
    print "Successfully wrote %s" % conffile
379
353
 
380
 
    plugindefault = open(plugindefaultfile, 'w')
381
 
    plugindefault.write("""# IVLE default plugin configuration file
382
 
[ivle.webapp.core#Plugin]
383
 
[ivle.webapp.admin.user#Plugin]
384
 
[ivle.webapp.tutorial#Plugin]
385
 
[ivle.webapp.admin.subject#Plugin]
386
 
[ivle.webapp.filesystem.browser#Plugin]
387
 
[ivle.webapp.filesystem.diff#Plugin]
388
 
[ivle.webapp.filesystem.svnlog#Plugin]
389
 
[ivle.webapp.filesystem.serve#Plugin]
390
 
[ivle.webapp.groups#Plugin]
391
 
[ivle.webapp.console#Plugin]
392
 
[ivle.webapp.security#Plugin]
393
 
[ivle.webapp.media#Plugin]
394
 
[ivle.webapp.help#Plugin]
395
 
[ivle.webapp.tos#Plugin]
396
 
[ivle.webapp.userservice#Plugin]
397
 
[ivle.webapp.fileservice#Plugin]
398
 
[ivle.webapp.submit#Plugin]
399
 
""")
400
 
    plugindefault.close()
401
 
    print "Successfully wrote %s" % plugindefaultfile
402
 
 
403
 
    print
404
 
    print "You may modify the configuration at any time by editing " + conffile
 
354
    # Write bin/trampoline/conf.h
 
355
 
 
356
    conf_h = open(conf_hfile, "w")
 
357
 
 
358
    # XXX Compute jail_base, jail_src_base and jail_system. These will
 
359
    # ALSO be done by the boilerplate code, but we need them here in order
 
360
    # to write to the C file.
 
361
    jail_base = os.path.join(conf['paths']['data'], 'jailmounts')
 
362
    jail_src_base = os.path.join(conf['paths']['data'], 'jails')
 
363
    jail_system = os.path.join(jail_src_base, '__base__')
 
364
 
 
365
    conf_h.write("""/* IVLE Configuration File
 
366
 * conf.h
 
367
 * Administrator settings required by trampoline.
 
368
 * Note: trampoline will have to be rebuilt in order for changes to this file
 
369
 * to take effect.
 
370
 */
 
371
 
 
372
#define IVLE_AUFS_JAILS
 
373
 
 
374
/* In the local file system, where are the jails located.
 
375
 * The trampoline does not allow the creation of a jail anywhere besides
 
376
 * jail_base or a subdirectory of jail_base.
 
377
 */
 
378
static const char* jail_base = "%s";
 
379
static const char* jail_src_base = "%s";
 
380
static const char* jail_system = "%s";
 
381
 
 
382
/* Which user IDs are allowed to run the trampoline.
 
383
 * This list should be limited to the web server user.
 
384
 * (Note that root is an implicit member of this list).
 
385
 */
 
386
static const int allowed_uids[] = { %s };
 
387
""" % (repr(jail_base)[1:-1], repr(jail_src_base)[1:-1],
 
388
       repr(jail_system)[1:-1], repr(allowed_uids_list)[1:-1]))
 
389
    # Note: The above uses PYTHON reprs, not C reprs
 
390
    # However they should be the same with the exception of the outer
 
391
    # characters, which are stripped off and replaced
 
392
 
 
393
    conf_h.close()
 
394
 
 
395
    print "Successfully wrote %s" % conf_hfile
 
396
 
 
397
    # Write www/php/phpBB3/config.php
 
398
 
 
399
    conf_php = open(phpBBconffile, "w")
 
400
    
 
401
    # php-pg work around
 
402
    if conf['database']['host'] == 'localhost':
 
403
        forumdb_host = '127.0.0.1'
 
404
    else:
 
405
        forumdb_host = conf['database']['host']
 
406
 
 
407
    conf_php.write( """<?php
 
408
// phpBB 3.0.x auto-generated configuration file
 
409
// Do not change anything in this file!
 
410
$dbms = 'postgres';
 
411
$dbhost = '""" + forumdb_host + """';
 
412
$dbport = '""" + str(conf['database']['port']) + """';
 
413
$dbname = '""" + conf['plugins']['forum']['dbname'] + """';
 
414
$dbuser = '""" + conf['database']['username'] + """';
 
415
$dbpasswd = '""" + conf['database']['password'] + """';
 
416
 
 
417
$table_prefix = 'phpbb_';
 
418
$acm_type = 'file';
 
419
$load_extensions = '';
 
420
@define('PHPBB_INSTALLED', true);
 
421
// @define('DEBUG', true);
 
422
//@define('DEBUG_EXTRA', true);
 
423
 
 
424
$forum_secret = '""" + forum_secret +"""';
 
425
?>"""   )
 
426
    
 
427
    conf_php.close()
 
428
 
 
429
    print "Successfully wrote %s" % phpBBconffile
 
430
 
 
431
    print
 
432
    print "You may modify the configuration at any time by editing"
 
433
    print conffile
 
434
    print conf_hfile
 
435
    print phpBBconffile
 
436
    print
405
437
    
406
438
    return 0
407
 
 
408
 
def main(argv=None):
409
 
    if argv is None:
410
 
        argv = sys.argv
411
 
 
412
 
    # Print the opening spiel including the GPL notice
413
 
 
414
 
    print """IVLE - Informatics Virtual Learning Environment Setup
415
 
Copyright (C) 2007-2009 The University of Melbourne
416
 
IVLE comes with ABSOLUTELY NO WARRANTY.
417
 
This is free software, and you are welcome to redistribute it
418
 
under certain conditions. See LICENSE.txt for details.
419
 
 
420
 
IVLE Configuration
421
 
"""
422
 
 
423
 
    return configure(argv[1:])
424
 
 
425
 
if __name__ == "__main__":
426
 
    sys.exit(main())