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

« back to all changes in this revision

Viewing changes to ivle/makeuser.py

Drop www/ stuff from setup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
import filecmp
46
46
import logging
47
47
import ivle.conf
48
 
import ivle.db
49
48
import ivle.pulldown_subj
50
49
 
 
50
from ivle.database import ProjectGroup
 
51
 
51
52
def chown_to_webserver(filename):
52
53
    """
53
54
    Chowns a file so the web server user owns it.
105
106
def rebuild_svn_group_config(store):
106
107
    """Build the complete SVN configuration file for groups
107
108
    """
108
 
    conn = ivle.db.DB()
109
 
    groups = conn.get_all('project_group',
110
 
        ['groupid', 'groupnm', 'projectsetid'])
111
109
    f = open(ivle.conf.svn_group_conf + ".new", "w")
112
110
    f.write("# IVLE SVN Group Repositories Configuration\n")
113
111
    f.write("# Auto-generated on %s\n" % time.asctime())
114
112
    f.write("\n")
115
 
    for g in groups:
116
 
        projectsetid = g['projectsetid']
117
 
        offeringinfo = conn.get_offering_info(projectsetid)
118
 
        subj_short_name = offeringinfo['subj_short_name']
119
 
        year = offeringinfo['year']
120
 
        semester = offeringinfo['semester']
121
 
        reponame = "_".join([subj_short_name, year, semester, g['groupnm']])
 
113
    for group in store.find(ProjectGroup):
 
114
        offering = group.project_set.offering
 
115
        reponame = "_".join([offering.subject.short_name,
 
116
                             offering.semester.year,
 
117
                             offering.semester.semester,
 
118
                             group.name])
122
119
        f.write("[%s:/]\n"%reponame)
123
 
        users = conn.get_projectgroup_members(g['groupid'])
124
 
        for u in users:
125
 
            f.write("%s = rw\n"%u['login'])
 
120
        for user in group.members:
 
121
            f.write("%s = rw\n" % user.login)
126
122
        f.write("\n")
127
123
    f.close()
128
124
    os.rename(ivle.conf.svn_group_conf + ".new", ivle.conf.svn_group_conf)
244
240
        # Chmod to rwxr-xr-x (755)
245
241
        os.chmod(userhomedir, 0755)
246
242
 
247
 
    make_conf_py(user.login, userdir, ivle.conf.jail_system, user.svn_pass)
 
243
    make_conf_py(user.login, userdir, user.svn_pass)
248
244
    make_etc_passwd(user.login, userdir, ivle.conf.jail_system, user.unixid)
249
245
 
250
246
    return userhomedir
251
247
 
252
 
def make_conf_py(username, user_jail_dir, staging_dir, svn_pass):
 
248
def make_conf_py(username, user_jail_dir, svn_pass):
253
249
    """
254
250
    Creates (overwriting any existing file, and creating directories) a
255
251
    file ${python_site_packages}/ivle/conf/conf.py in a given user's jail.
256
252
    username: Username.
257
253
    user_jail_dir: User's jail dir, ie. ivle.conf.jail_base + username
258
 
    staging_dir: The dir with the staging copy of the jail. (With the
259
 
        template conf.py file).
260
 
    svn_pass: As with make_jail. User's SVN password, but if not supplied,
261
 
        will look up in the DB.
 
254
    svn_pass: User's SVN password.
262
255
    """
263
 
    template_conf_path = os.path.join(staging_dir,
264
 
            ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
265
256
    conf_path = os.path.join(user_jail_dir,
266
257
            ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
267
258
    os.makedirs(os.path.dirname(conf_path))
268
259
 
269
 
    # Read the contents of the template conf file
270
 
    try:
271
 
        template_conf_file = open(template_conf_path, "r")
272
 
        template_conf_data = template_conf_file.read()
273
 
        template_conf_file.close()
274
 
    except:
275
 
        # Couldn't open template conf.py for some reason
276
 
        # Just treat it as empty file
277
 
        template_conf_data = ("# Warning: Problem building config script.\n"
278
 
                              "# Could not find template conf.py file.\n")
 
260
    # In the "in-jail" version of conf, we don't need MOST of the details
 
261
    # (it would be a security risk to have them here).
 
262
    # So we just write root_dir, and jail_base is "/".
 
263
    # (jail_base being "/" means "jail-relative" paths are relative to "/"
 
264
    # when inside the jail.)
279
265
 
 
266
    # XXX: jail_base is wrong and shouldn't be here. Unfortunately, jail code
 
267
    #      uses ivle.studpath.url_to_{local,jailpaths}, both of which use
 
268
    #      jail_base. Note that they don't use the bits of the return value
 
269
    #      that depend on jail_base, so it can be any string.
280
270
    conf_file = open(conf_path, "w")
281
 
    conf_file.write(template_conf_data)
282
 
    conf_file.write("\n# The login name for the owner of the jail\n")
283
 
    conf_file.write("login = %s\n" % repr(username))
284
 
    conf_file.write("\n")
285
 
    conf_file.write("# The subversion-only password for the owner of "
286
 
        "the jail\n")
287
 
    conf_file.write("svn_pass = %s\n" % repr(svn_pass))
 
271
    conf_file.write("""# IVLE jail configuration
 
272
 
 
273
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
 
274
# with this).
 
275
# eg. "/" or "/ivle".
 
276
root_dir = %(root_dir)r
 
277
 
 
278
# This value is not relevant inside the jail, but must remain for now. See
 
279
# the XXX in ivle.makeuser.make_conf_py.
 
280
jail_base = '/'
 
281
 
 
282
# The hostname for serving publicly accessible pages
 
283
public_host = %(public_host)r
 
284
 
 
285
# The URL under which the Subversion repositories are located.
 
286
svn_addr = %(svn_addr)r
 
287
 
 
288
# The login name for the owner of the jail
 
289
login = %(username)r
 
290
 
 
291
# The subversion-only password for the owner of the jail
 
292
svn_pass = %(svn_pass)r
 
293
""" % {'root_dir': ivle.conf.root_dir,
 
294
       'public_host': ivle.conf.public_host,
 
295
       'svn_addr': ivle.conf.svn_addr,
 
296
       'username': username,
 
297
       'svn_pass': svn_pass,
 
298
      })
288
299
    conf_file.close()
289
300
 
290
301
    # Make this file world-readable
308
319
                      % (username, unixid, unixid, username))
309
320
    passwd_file.close()
310
321
 
311
 
def make_user_db(throw_on_error = True, **kwargs):
312
 
    """Creates a user's entry in the database, filling in all the fields.
313
 
    All arguments must be keyword args. They are the fields in the table.
314
 
    However, instead of supplying a "passhash", you must supply a
315
 
    "password" argument, which will be hashed internally.
316
 
    Also do not supply a state. All users are created in the "no_agreement"
317
 
    state.
318
 
    Also pulls the user's subjects using the configured subject pulldown
319
 
    module, and adds enrolments to the DB.
320
 
    Throws an exception if the user already exists.
321
 
    """
322
 
    dbconn = ivle.db.DB()
323
 
    dbconn.create_user(**kwargs)
324
 
    dbconn.close()
325
 
 
326
 
    # Pulldown subjects and add enrolments
327
 
    ivle.pulldown_subj.enrol_user(kwargs['login'])
328
 
 
329
322
def mount_jail(login):
330
323
    # This is where we'll mount to...
331
324
    destdir = os.path.join(ivle.conf.jail_base, login)