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

« back to all changes in this revision

Viewing changes to services/usrmgt-server

  • Committer: William Grant
  • Date: 2010-02-15 03:34:19 UTC
  • Revision ID: grantw@unimelb.edu.au-20100215033419-oy2ivgyxpg6kszey
Update copyright year in the documentation.

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