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

« back to all changes in this revision

Viewing changes to services/usrmgt-server

  • Committer: mattgiuca
  • Date: 2008-01-12 15:35:53 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:201
Added "test" directory.
Added make_date_test.py, a short script I wrote to test the date format
algorithm committed in the previous revision.

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
 
    os.umask(0022) # Bad, but start_server sets it worse.
43
 
 
44
 
    login = props['login']
45
 
 
46
 
    # FIXME: check we're pending
47
 
 
48
 
    # Get the full User object from the db associated with this
49
 
    user = ivle.database.User.get_by_login(store, login)
50
 
 
51
 
    # make svn config/auth
52
 
    repopath = os.path.join(config['paths']['svn']['repo_path'],
53
 
                            'users', login)
54
 
    logging.debug("Creating user's Subversion repository")
55
 
    ivle.makeuser.make_svn_repo(repopath, throw_on_error=True)
56
 
 
57
 
    rebuild_svn_config(store, props, config)
58
 
 
59
 
    logging.debug("Adding Subversion authentication")
60
 
    passwd = ivle.makeuser.make_svn_auth(store, login, config,
61
 
                                         throw_on_error=True)
62
 
 
63
 
    logging.debug("Creating jail")
64
 
    ivle.makeuser.make_jail(user, config)
65
 
 
66
 
    logging.info("Enabling user")
67
 
    user.state = u'enabled'
68
 
 
69
 
    return {"response": "okay"}
70
 
 
71
 
def rebuild_svn_config(store, props, config):
72
 
    """Rebuilds the svn config file
73
 
    @param config: An ivle.config.Config object.
74
 
    @return: response (okay, failure)
75
 
    """
76
 
    try:
77
 
        ivle.makeuser.rebuild_svn_config(store, config)
78
 
    except Exception, e:
79
 
        logging.warning('Rebuild of Subversion authorization config failed!')
80
 
        return{'response': 'failure', 'msg': repr(e)}
81
 
 
82
 
    return {'response': 'okay'}
83
 
 
84
 
def rebuild_svn_group_config(store, props, config):
85
 
    """Rebuilds the svn group config file
86
 
    @param config: An ivle.config.Config object.
87
 
    @return: response (okay, failure)
88
 
    """
89
 
    try:
90
 
        ivle.makeuser.rebuild_svn_group_config(store, config)
91
 
    except Exception, e:
92
 
        logging.warning(
93
 
            'Rebuild of Subversion group authorization config failed!')
94
 
        return{'response': 'failure', 'msg': repr(e)}
95
 
 
96
 
    return {'response': 'okay'}
97
 
 
98
 
def create_group_repository(store, props, config):
99
 
    """Creates on disk repository for the given group
100
 
    @param config: An ivle.config.Config object.
101
 
    Expected properties:
102
 
        subj_short_name, year, semester, groupnm
103
 
    @return: response (okay, failure)
104
 
    """
105
 
 
106
 
    subj_short_name = props['subj_short_name']
107
 
    year = props['year']
108
 
    semester = props['semester']
109
 
    groupnm = props['groupnm']
110
 
 
111
 
    namespace = "_".join([subj_short_name, year, semester, groupnm])
112
 
    repopath = os.path.join(config['paths']['svn']['repo_path'],
113
 
                            'groups', namespace)
114
 
    logging.debug("Creating Subversion repository %s"%repopath)
115
 
    try:
116
 
        ivle.makeuser.make_svn_repo(repopath)
117
 
    except Exception, e:
118
 
        logging.error("Failed to create Subversion repository %s: %s"%
119
 
            (repopath,repr(e)))
120
 
        return {'response': 'failure', 'msg': repr(e)}
121
 
 
122
 
    return {'response': 'okay'}
123
 
 
124
 
actions = {
125
 
        'activate_user':activate_user,
126
 
        'create_group_repository':create_group_repository,
127
 
        'rebuild_svn_config':rebuild_svn_config,
128
 
        'rebuild_svn_group_config':rebuild_svn_group_config,
129
 
    }
130
 
 
131
 
def initializer():
132
 
    logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
133
 
    logging.info("Starting usrmgt server on port %d (pid = %d)" %
134
 
                 (config['usrmgt']['port'], pid))
135
 
 
136
 
    try:
137
 
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
138
 
        pidfile.write('%d\n' % os.getpid())
139
 
        pidfile.close()
140
 
    except IOError, (errno, strerror):
141
 
        print "Couldn't write PID file. IO error(%s): %s" % (errno, strerror)
142
 
        sys.exit(1)
143
 
 
144
 
def dispatch(props):
145
 
    logging.debug(repr(props))
146
 
 
147
 
    store = ivle.database.get_store(config)
148
 
    action = props.keys()[0]
149
 
    res = actions[action](store, props[action], config)
150
 
 
151
 
    if res['response'] == 'okay':
152
 
        store.commit()
153
 
    else:
154
 
        store.rollback()
155
 
    store.close()
156
 
    return res
157
 
 
158
 
if __name__ == "__main__":
159
 
    pid = os.getpid()
160
 
 
161
 
    ivle.chat.start_server(config['usrmgt']['port'],config['usrmgt']['magic'],
162
 
                           True, dispatch, initializer)