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

« back to all changes in this revision

Viewing changes to scripts/usrmgt-server

  • Committer: wagrant
  • Date: 2008-08-09 05:16:21 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:997
specialhome: Support group workspaces! Yay! Involved a bit of poking to
             get it all working, but it does.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
 
3
import os
3
4
import sys
 
5
import logging
4
6
 
 
7
import conf
5
8
import common.db
6
9
import common.chat
7
10
import common.makeuser
 
11
import common.studpath
8
12
 
9
13
# usage:
10
14
#   usrmgt-server <port> <magic>
16
20
#           - create repository
17
21
#           - svn config
18
22
#           - svn auth
19
 
#       - Checkout repository as home directory
20
23
#       - /etc/passwd entry
21
24
#   - Disable a user's account
22
25
#   - Enable a user's account
25
28
#   - Rebuild svn auth file
26
29
#   - Rebuild passwd + push to nodes.
27
30
 
28
 
 
29
 
def create_user(props):
30
 
    """Create the database record for the given user.
31
 
       Expected properties:
32
 
        username    - used as a unix login name and svn repository name.
33
 
                      STRING REQUIRED 
34
 
        uid         - the unix uid under which execution will take place
35
 
                      on the behalf of the user. Don't use 0! If not specified
36
 
                      or None, one will be allocated from the configured
37
 
                      numeric range.
38
 
                      INT OPTIONAL
39
 
        password    - the clear-text password for the user. If this property is
40
 
                      absent or None, this is an indication that external
41
 
                      authentication should be used (i.e. LDAP).
42
 
                      STRING OPTIONAL
43
 
        email       - the user's email address.
44
 
                      STRING OPTIONAL
45
 
        nick        - the display name to use.
46
 
                      STRING REQUIRED
47
 
        fullname    - The name of the user for results and/or other official
48
 
                      purposes.
49
 
                      STRING REQUIRED
50
 
        rolenm      - The user's role. Must be one of "anyone", "student",
51
 
                      "tutor", "lecturer", "admin".
52
 
                      STRING/ENUM REQUIRED
53
 
        studentid   - If supplied and not None, the student id of the user for
54
 
                      results and/or other official purposes.
55
 
                      STRING OPTIONAL
56
 
       Return Value: the uid associated with the user. INT
57
 
    """
58
 
 
59
 
    # FIXME: the IVLE server must check that an admin is doing this!
60
 
 
61
 
    if 'uid' not in props or props['uid'] is None:
62
 
        raise NotImplementedError, "No algorithm for creating uids yet!"
63
 
        # uid = invent-uid
64
 
        # props['uid'] = uid
65
 
 
66
 
    username = props['username']
67
 
    uid = props['uid']
68
 
    try:
69
 
        password = props['password']
70
 
    except KeyError:
71
 
        password = None
72
 
    try:
73
 
        email = props['email']
74
 
    except KeyError:
75
 
        email = None
76
 
    nick = props['nick']
77
 
    fullname = props['fullname']
78
 
    rolenm = props['rolenm']
79
 
    try:
80
 
        studentid = props['studentid']
81
 
    except KeyError:
82
 
        studentid = None
83
 
    common.makeuser.make_user_db(username, uid, password, email, nick,
84
 
                                 fullname, rolenm, studentid)
85
 
 
86
 
    return uid
87
 
 
88
31
def activate_user(props):
89
32
    """Create the on-disk stuff for the given user.
90
33
       Sets the state of the user in the db from pending to enabled.
94
37
       Return Value: None
95
38
    """
96
39
 
 
40
    os.umask(0022) # Bad, but start_server sets it worse.
 
41
 
97
42
    login = props['login']
98
43
 
99
44
    db = common.db.DB()
104
49
 
105
50
        details = db.get_user(login)
106
51
 
107
 
        # FIXME: make svn config/auth
108
 
 
109
 
        # FIXME: etc, etc.
110
 
 
111
 
        common.makeuser.make_jail(login, details['unixid'])
112
 
 
 
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")
113
68
        db.update_user(login, state='enabled')
114
69
 
115
70
        return {"response": "okay"}
117
72
    finally:
118
73
        db.close()
119
74
 
 
75
def rebuild_svn_config(props):
 
76
    """Rebuilds the svn config file
 
77
    Return value:
 
78
        response (okay, failure)
 
79
    """
 
80
    try:
 
81
        common.makeuser.rebuild_svn_config()
 
82
    except Exception, e:
 
83
        logging.warning('Rebuild of Subversion authorization config failed!')
 
84
        return{'response': 'failure', 'msg': repr(e)}
 
85
 
 
86
    return {'response': 'okay'}
 
87
 
 
88
def rebuild_svn_group_config(props):
 
89
    """Rebuilds the svn group config file
 
90
    Return value:
 
91
        response (okay, failure)
 
92
    """
 
93
    try:
 
94
        common.makeuser.rebuild_svn_group_config()
 
95
    except Exception, e:
 
96
        logging.warning(
 
97
            'Rebuild of Subversion group authorization config failed!')
 
98
        return{'response': 'failure', 'msg': repr(e)}
 
99
 
 
100
    return {'response': 'okay'}
 
101
 
 
102
def create_group_repository(props):
 
103
    """Creates on disk repository for the given group
 
104
    Expected properties:
 
105
        subj_short_name, year, semester, groupnm
 
106
    Return value:
 
107
        response (okay, failure)
 
108
    """
 
109
 
 
110
    subj_short_name = props['subj_short_name']
 
111
    year = props['year']
 
112
    semester = props['semester']
 
113
    groupnm = props['groupnm']
 
114
 
 
115
    namespace = "_".join([subj_short_name, year, semester, groupnm])
 
116
    repopath = os.path.join(conf.svn_repo_path, 'groups', namespace)
 
117
    logging.debug("Creating Subversion repository %s"%repopath)
 
118
    try:
 
119
        common.makeuser.make_svn_repo(repopath)
 
120
    except Exception, e:
 
121
        logging.error("Failed to create Subversion repository %s: %s"%
 
122
            (repopath,repr(e)))
 
123
        return {'response': 'failure', 'msg': repr(e)}
 
124
 
 
125
    return {'response': 'okay'}
 
126
 
120
127
actions = {
121
 
        'create_user':create_user,
122
 
        'activate_user':activate_user
 
128
        'activate_user':activate_user,
 
129
        'create_group_repository':create_group_repository,
 
130
        'rebuild_svn_config':rebuild_svn_config,
 
131
        'rebuild_svn_group_config':rebuild_svn_group_config,
123
132
    }
124
133
 
 
134
def initializer():
 
135
    try:
 
136
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
 
137
        pidfile.write('%d\n' % os.getpid())
 
138
        pidfile.close()
 
139
    except IOError, (errno, strerror):
 
140
        print "Couldn't write PID file. IO error(%s): %s" % (errno, strerror)
 
141
        sys.exit(1)
 
142
 
125
143
def dispatch(props):
 
144
    logging.debug(repr(props))
126
145
    action = props.keys()[0]
127
146
    return actions[action](props[action])
128
147
 
129
148
if __name__ == "__main__":
130
 
    port = int(sys.argv[1])
131
 
    magic = sys.argv[2]
132
 
 
133
 
    common.chat.start_server(port, magic, False, dispatch)
 
149
    pid = os.getpid()
 
150
 
 
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)