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

« back to all changes in this revision

Viewing changes to services/usrmgt-server

  • Committer: David Coles
  • Date: 2010-08-30 03:26:13 UTC
  • Revision ID: coles.david@gmail.com-20100830032613-d14vng0jkelniu3l
python-console: Fix globals broken with new JSON library.

simplejson always returns unicode strings. cJSON would return ordinary strings 
if possible. cPickle.loads() only accepts strings. At present we use pickle 
version 0 so they should all works as ASCII strings. Higher versions of pickle 
are not plain ASCII and are likely to break this and so this should be fixed 
at some point.

Also replaced unconditional exception with one that catches Pickle errors. Not 
sure the best way to report failures of these functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
import sys
5
5
import logging
6
6
 
7
 
import ivle.conf
 
7
import ivle.config
8
8
import ivle.database
9
9
import ivle.chat
10
10
import ivle.makeuser
11
11
 
 
12
config = ivle.config.Config()
 
13
 
12
14
# usage:
13
15
#   usrmgt-server <port> <magic>
14
16
 
27
29
#   - Rebuild svn auth file
28
30
#   - Rebuild passwd + push to nodes.
29
31
 
30
 
def activate_user(store, props):
 
32
def activate_user(store, props, config):
31
33
    """Create the on-disk stuff for the given user.
32
34
       Sets the state of the user in the db from pending to enabled.
 
35
    @param config: An ivle.config.Config object.
33
36
       Expected properties:
34
37
        login       - the user name for the jail
35
38
                      STRING REQUIRED
36
 
       Return Value: None
 
39
    @return: None
37
40
    """
38
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
 
39
48
    os.umask(0022) # Bad, but start_server sets it worse.
40
49
 
41
50
    login = props['login']
46
55
    user = ivle.database.User.get_by_login(store, login)
47
56
 
48
57
    # make svn config/auth
49
 
    repopath = os.path.join(ivle.conf.svn_repo_path, 'users', login)
 
58
    repopath = os.path.join(config['paths']['svn']['repo_path'],
 
59
                            'users', login)
50
60
    logging.debug("Creating user's Subversion repository")
51
61
    ivle.makeuser.make_svn_repo(repopath, throw_on_error=True)
52
62
 
53
 
    rebuild_svn_config(store, props)
 
63
    rebuild_svn_config(store, props, config)
54
64
 
55
65
    logging.debug("Adding Subversion authentication")
56
 
    passwd = ivle.makeuser.make_svn_auth(store, login,
 
66
    passwd = ivle.makeuser.make_svn_auth(store, login, config,
57
67
                                         throw_on_error=True)
58
68
 
59
69
    logging.debug("Creating jail")
60
 
    ivle.makeuser.make_jail(user)
 
70
    ivle.makeuser.make_jail(user, config)
61
71
 
62
72
    logging.info("Enabling user")
63
73
    user.state = u'enabled'
64
74
 
65
75
    return {"response": "okay"}
66
76
 
67
 
def rebuild_svn_config(store, props):
 
77
def rebuild_svn_config(store, props, config):
68
78
    """Rebuilds the svn config file
69
 
    Return value:
70
 
        response (okay, failure)
 
79
    @param config: An ivle.config.Config object.
 
80
    @return: response (okay, failure)
71
81
    """
72
82
    try:
73
 
        ivle.makeuser.rebuild_svn_config(store)
 
83
        ivle.makeuser.rebuild_svn_config(store, config)
74
84
    except Exception, e:
75
85
        logging.warning('Rebuild of Subversion authorization config failed!')
76
86
        return{'response': 'failure', 'msg': repr(e)}
77
87
 
78
88
    return {'response': 'okay'}
79
89
 
80
 
def rebuild_svn_group_config(store, props):
 
90
def rebuild_svn_group_config(store, props, config):
81
91
    """Rebuilds the svn group config file
82
 
    Return value:
83
 
        response (okay, failure)
 
92
    @param config: An ivle.config.Config object.
 
93
    @return: response (okay, failure)
84
94
    """
85
95
    try:
86
 
        ivle.makeuser.rebuild_svn_group_config(store)
 
96
        ivle.makeuser.rebuild_svn_group_config(store, config)
87
97
    except Exception, e:
88
98
        logging.warning(
89
99
            'Rebuild of Subversion group authorization config failed!')
91
101
 
92
102
    return {'response': 'okay'}
93
103
 
94
 
def create_group_repository(store, props):
 
104
def create_group_repository(store, props, config):
95
105
    """Creates on disk repository for the given group
 
106
    @param config: An ivle.config.Config object.
96
107
    Expected properties:
97
108
        subj_short_name, year, semester, groupnm
98
 
    Return value:
99
 
        response (okay, failure)
 
109
    @return: response (okay, failure)
100
110
    """
101
111
 
102
112
    subj_short_name = props['subj_short_name']
105
115
    groupnm = props['groupnm']
106
116
 
107
117
    namespace = "_".join([subj_short_name, year, semester, groupnm])
108
 
    repopath = os.path.join(ivle.conf.svn_repo_path, 'groups', namespace)
 
118
    repopath = os.path.join(config['paths']['svn']['repo_path'],
 
119
                            'groups', namespace)
109
120
    logging.debug("Creating Subversion repository %s"%repopath)
110
121
    try:
111
122
        ivle.makeuser.make_svn_repo(repopath)
124
135
    }
125
136
 
126
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
 
127
142
    try:
128
143
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
129
144
        pidfile.write('%d\n' % os.getpid())
135
150
def dispatch(props):
136
151
    logging.debug(repr(props))
137
152
 
138
 
    store = ivle.database.get_store()
 
153
    store = ivle.database.get_store(config)
139
154
    action = props.keys()[0]
140
 
    res = actions[action](store, props[action])
 
155
    res = actions[action](store, props[action], config)
141
156
 
142
157
    if res['response'] == 'okay':
143
158
        store.commit()
149
164
if __name__ == "__main__":
150
165
    pid = os.getpid()
151
166
 
152
 
    logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
153
 
    logging.info("Starting usrmgt server on port %d (pid = %d)" %
154
 
                 (ivle.conf.usrmgt_port, pid))
155
 
 
156
 
    ivle.chat.start_server(ivle.conf.usrmgt_port, ivle.conf.usrmgt_magic,
 
167
    ivle.chat.start_server(config['usrmgt']['port'],config['usrmgt']['magic'],
157
168
                           True, dispatch, initializer)