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

« back to all changes in this revision

Viewing changes to services/usrmgt-server

  • Committer: William Grant
  • Date: 2009-02-23 23:47:02 UTC
  • mfrom: (1099.1.211 new-dispatch)
  • Revision ID: grantw@unimelb.edu.au-20090223234702-db4b1llly46ignwo
Merge from lp:~ivle-dev/ivle/new-dispatch.

Pretty much everything changes. Reread the setup docs. Backup your databases.
Every file is now in a different installed location, the configuration system
is rewritten, the dispatch system is rewritten, URLs are different, the
database is different, worksheets and exercises are no longer on the
filesystem, we use a templating engine, jail service protocols are rewritten,
we don't repeat ourselves, we have authorization rewritten, phpBB is gone,
and probably lots of other things that I cannot remember.

This is certainly the biggest commit I have ever made, and hopefully
the largest I ever will.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import os
 
4
import sys
 
5
import logging
 
6
 
 
7
import ivle.conf
 
8
import ivle.database
 
9
import ivle.chat
 
10
import ivle.makeuser
 
11
 
 
12
# usage:
 
13
#   usrmgt-server <port> <magic>
 
14
 
 
15
# User management operations:
 
16
#   - Create local user
 
17
#   - [Re]Create jail for a user
 
18
#       - Create a svn repository for a user
 
19
#           - create repository
 
20
#           - svn config
 
21
#           - svn auth
 
22
#       - /etc/passwd entry
 
23
#   - Disable a user's account
 
24
#   - Enable a user's account
 
25
#   - Remove a user
 
26
#   - Rebuild svn config
 
27
#   - Rebuild svn auth file
 
28
#   - Rebuild passwd + push to nodes.
 
29
 
 
30
def activate_user(store, props):
 
31
    """Create the on-disk stuff for the given user.
 
32
       Sets the state of the user in the db from pending to enabled.
 
33
       Expected properties:
 
34
        login       - the user name for the jail
 
35
                      STRING REQUIRED
 
36
       Return Value: None
 
37
    """
 
38
 
 
39
    os.umask(0022) # Bad, but start_server sets it worse.
 
40
 
 
41
    login = props['login']
 
42
 
 
43
    # FIXME: check we're pending
 
44
 
 
45
    # Get the full User object from the db associated with this
 
46
    user = ivle.database.User.get_by_login(store, login)
 
47
 
 
48
    # make svn config/auth
 
49
    repopath = os.path.join(ivle.conf.svn_repo_path, 'users', login)
 
50
    logging.debug("Creating user's Subversion repository")
 
51
    ivle.makeuser.make_svn_repo(repopath, throw_on_error=True)
 
52
 
 
53
    rebuild_svn_config(store, props)
 
54
 
 
55
    logging.debug("Adding Subversion authentication")
 
56
    passwd = ivle.makeuser.make_svn_auth(store, login,
 
57
                                         throw_on_error=True)
 
58
 
 
59
    logging.debug("Creating jail")
 
60
    ivle.makeuser.make_jail(user)
 
61
 
 
62
    logging.info("Enabling user")
 
63
    user.state = u'enabled'
 
64
 
 
65
    return {"response": "okay"}
 
66
 
 
67
def rebuild_svn_config(store, props):
 
68
    """Rebuilds the svn config file
 
69
    Return value:
 
70
        response (okay, failure)
 
71
    """
 
72
    try:
 
73
        ivle.makeuser.rebuild_svn_config(store)
 
74
    except Exception, e:
 
75
        logging.warning('Rebuild of Subversion authorization config failed!')
 
76
        return{'response': 'failure', 'msg': repr(e)}
 
77
 
 
78
    return {'response': 'okay'}
 
79
 
 
80
def rebuild_svn_group_config(store, props):
 
81
    """Rebuilds the svn group config file
 
82
    Return value:
 
83
        response (okay, failure)
 
84
    """
 
85
    try:
 
86
        ivle.makeuser.rebuild_svn_group_config(store)
 
87
    except Exception, e:
 
88
        logging.warning(
 
89
            'Rebuild of Subversion group authorization config failed!')
 
90
        return{'response': 'failure', 'msg': repr(e)}
 
91
 
 
92
    return {'response': 'okay'}
 
93
 
 
94
def create_group_repository(store, props):
 
95
    """Creates on disk repository for the given group
 
96
    Expected properties:
 
97
        subj_short_name, year, semester, groupnm
 
98
    Return value:
 
99
        response (okay, failure)
 
100
    """
 
101
 
 
102
    subj_short_name = props['subj_short_name']
 
103
    year = props['year']
 
104
    semester = props['semester']
 
105
    groupnm = props['groupnm']
 
106
 
 
107
    namespace = "_".join([subj_short_name, year, semester, groupnm])
 
108
    repopath = os.path.join(ivle.conf.svn_repo_path, 'groups', namespace)
 
109
    logging.debug("Creating Subversion repository %s"%repopath)
 
110
    try:
 
111
        ivle.makeuser.make_svn_repo(repopath)
 
112
    except Exception, e:
 
113
        logging.error("Failed to create Subversion repository %s: %s"%
 
114
            (repopath,repr(e)))
 
115
        return {'response': 'failure', 'msg': repr(e)}
 
116
 
 
117
    return {'response': 'okay'}
 
118
 
 
119
actions = {
 
120
        'activate_user':activate_user,
 
121
        'create_group_repository':create_group_repository,
 
122
        'rebuild_svn_config':rebuild_svn_config,
 
123
        'rebuild_svn_group_config':rebuild_svn_group_config,
 
124
    }
 
125
 
 
126
def initializer():
 
127
    logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
 
128
    logging.info("Starting usrmgt server on port %d (pid = %d)" %
 
129
                 (ivle.conf.usrmgt_port, pid))
 
130
 
 
131
    try:
 
132
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
 
133
        pidfile.write('%d\n' % os.getpid())
 
134
        pidfile.close()
 
135
    except IOError, (errno, strerror):
 
136
        print "Couldn't write PID file. IO error(%s): %s" % (errno, strerror)
 
137
        sys.exit(1)
 
138
 
 
139
def dispatch(props):
 
140
    logging.debug(repr(props))
 
141
 
 
142
    store = ivle.database.get_store()
 
143
    action = props.keys()[0]
 
144
    res = actions[action](store, props[action])
 
145
 
 
146
    if res['response'] == 'okay':
 
147
        store.commit()
 
148
    else:
 
149
        store.rollback()
 
150
    store.close()
 
151
    return res
 
152
 
 
153
if __name__ == "__main__":
 
154
    pid = os.getpid()
 
155
 
 
156
    ivle.chat.start_server(ivle.conf.usrmgt_port, ivle.conf.usrmgt_magic,
 
157
                           True, dispatch, initializer)