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

« back to all changes in this revision

Viewing changes to scripts/usrmgt-server

  • Committer: wagrant
  • Date: 2008-08-07 05:30:07 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:988
buildjail: Install python-nltk-data (it's now in my repository).
           Also don't bother to remove stuff from the jail - we no longer
           need to hardlink it hundreds of times, and it means we can use
           the jail normally afterwards.

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 conf
 
8
import common.db
 
9
import common.chat
 
10
import common.makeuser
 
11
import common.studpath
 
12
 
 
13
# usage:
 
14
#   usrmgt-server <port> <magic>
 
15
 
 
16
# User management operations:
 
17
#   - Create local user
 
18
#   - [Re]Create jail for a user
 
19
#       - Create a svn repository for a user
 
20
#           - create repository
 
21
#           - svn config
 
22
#           - svn auth
 
23
#       - /etc/passwd entry
 
24
#   - Disable a user's account
 
25
#   - Enable a user's account
 
26
#   - Remove a user
 
27
#   - Rebuild svn config
 
28
#   - Rebuild svn auth file
 
29
#   - Rebuild passwd + push to nodes.
 
30
 
 
31
def activate_user(props):
 
32
    """Create the on-disk stuff for the given user.
 
33
       Sets the state of the user in the db from pending to enabled.
 
34
       Expected properties:
 
35
        login       - the user name for the jail
 
36
                      STRING REQUIRED
 
37
       Return Value: None
 
38
    """
 
39
 
 
40
    os.umask(0022) # Bad, but start_server sets it worse.
 
41
 
 
42
    login = props['login']
 
43
 
 
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
        logging.debug("Rebuilding Subversion authorization config")
 
59
        try:
 
60
            common.makeuser.rebuild_svn_config()
 
61
        except:
 
62
            logging.warning('Rebuild of Subversion authorization config failed!')
 
63
 
 
64
        logging.debug("Adding Subversion authentication")
 
65
        passwd = common.makeuser.make_svn_auth(login, throw_on_error=False)
 
66
        logging.debug("passwd: %s" % passwd)
 
67
 
 
68
        logging.debug("Creating jail")
 
69
        common.makeuser.make_jail(login, details.unixid, svn_pass=passwd)
 
70
 
 
71
        logging.info("Enabling user")
 
72
        db.update_user(login, state='enabled')
 
73
 
 
74
        return {"response": "okay"}
 
75
 
 
76
    finally:
 
77
        db.close()
 
78
 
 
79
def create_group_repository(props):
 
80
    """Creates on disk repository for the given group
 
81
    Expected properties:
 
82
        subj_short_name, year, semester, groupnm
 
83
    Return value:
 
84
        response (okay, failure)
 
85
    """
 
86
 
 
87
    subj_short_name = props['subj_short_name']
 
88
    year = props['year']
 
89
    semester = props['semester']
 
90
    groupnm = props['groupnm']
 
91
 
 
92
    namespace = "_".join([subj_short_name, year, semester, groupnm])
 
93
    repopath = os.path.join(conf.svn_repo_path, 'groups', namespace)
 
94
    logging.debug("Creating Subversion repository %s"%repopath)
 
95
    try:
 
96
        common.makeuser.make_svn_repo(repopath)
 
97
    except Exception, e:
 
98
        logging.error("Failed to create Subversion repository %s: %s"%
 
99
            (repopath,repr(e)))
 
100
        return {'response': 'failure', 'msg': repr(e)}
 
101
 
 
102
    return {'response': 'okay'}
 
103
 
 
104
actions = {
 
105
        'activate_user':activate_user,
 
106
        'create_group_repository':create_group_repository,
 
107
    }
 
108
 
 
109
def initializer():
 
110
    try:
 
111
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
 
112
        pidfile.write('%d\n' % os.getpid())
 
113
        pidfile.close()
 
114
    except IOError, (errno, strerror):
 
115
        print "Couldn't write PID file. IO error(%s): %s" % (errno, strerror)
 
116
        sys.exit(1)
 
117
 
 
118
def dispatch(props):
 
119
    logging.debug(repr(props))
 
120
    action = props.keys()[0]
 
121
    return actions[action](props[action])
 
122
 
 
123
if __name__ == "__main__":
 
124
    pid = os.getpid()
 
125
 
 
126
    logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
 
127
    logging.info("Starting usrmgt server on port %d (pid = %d)" %
 
128
                 (conf.usrmgt_port, pid))
 
129
 
 
130
    common.chat.start_server(conf.usrmgt_port, conf.usrmgt_magic, True, dispatch, initializer)