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

« back to all changes in this revision

Viewing changes to services/usrmgt-server

  • Committer: matt.giuca
  • Date: 2009-01-12 00:33:53 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1072
Renamed scripts to services.
Updated all references (we hope). :)

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