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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python

import os
import sys
import logging

import ivle.conf
import ivle.database
import ivle.chat
import ivle.makeuser

# usage:
#   usrmgt-server <port> <magic>

# User management operations:
#   - Create local user
#   - [Re]Create jail for a user
#       - Create a svn repository for a user
#           - create repository
#           - svn config
#           - svn auth
#       - /etc/passwd entry
#   - Disable a user's account
#   - Enable a user's account
#   - Remove a user
#   - Rebuild svn config
#   - Rebuild svn auth file
#   - Rebuild passwd + push to nodes.

def activate_user(store, props):
    """Create the on-disk stuff for the given user.
       Sets the state of the user in the db from pending to enabled.
       Expected properties:
        login       - the user name for the jail
                      STRING REQUIRED
       Return Value: None
    """

    os.umask(0022) # Bad, but start_server sets it worse.

    login = props['login']

    # FIXME: check we're pending

    # Get the full User object from the db associated with this
    user = ivle.database.User.get_by_login(store, login)

    # make svn config/auth
    repopath = os.path.join(ivle.conf.svn_repo_path, 'users', login)
    logging.debug("Creating user's Subversion repository")
    ivle.makeuser.make_svn_repo(repopath, throw_on_error=True)

    rebuild_svn_config(store, props)

    logging.debug("Adding Subversion authentication")
    passwd = ivle.makeuser.make_svn_auth(store, login,
                                         throw_on_error=True)

    logging.debug("Creating jail")
    ivle.makeuser.make_jail(user)

    logging.info("Enabling user")
    user.state = u'enabled'

    return {"response": "okay"}

def rebuild_svn_config(store, props):
    """Rebuilds the svn config file
    Return value:
        response (okay, failure)
    """
    try:
        ivle.makeuser.rebuild_svn_config(store)
    except Exception, e:
        logging.warning('Rebuild of Subversion authorization config failed!')
        return{'response': 'failure', 'msg': repr(e)}

    return {'response': 'okay'}

def rebuild_svn_group_config(store, props):
    """Rebuilds the svn group config file
    Return value:
        response (okay, failure)
    """
    try:
        ivle.makeuser.rebuild_svn_group_config(store)
    except Exception, e:
        logging.warning(
            'Rebuild of Subversion group authorization config failed!')
        return{'response': 'failure', 'msg': repr(e)}

    return {'response': 'okay'}

def create_group_repository(store, props):
    """Creates on disk repository for the given group
    Expected properties:
        subj_short_name, year, semester, groupnm
    Return value:
        response (okay, failure)
    """

    subj_short_name = props['subj_short_name']
    year = props['year']
    semester = props['semester']
    groupnm = props['groupnm']

    namespace = "_".join([subj_short_name, year, semester, groupnm])
    repopath = os.path.join(ivle.conf.svn_repo_path, 'groups', namespace)
    logging.debug("Creating Subversion repository %s"%repopath)
    try:
        ivle.makeuser.make_svn_repo(repopath)
    except Exception, e:
        logging.error("Failed to create Subversion repository %s: %s"%
            (repopath,repr(e)))
        return {'response': 'failure', 'msg': repr(e)}

    return {'response': 'okay'}

actions = {
        'activate_user':activate_user,
        'create_group_repository':create_group_repository,
        'rebuild_svn_config':rebuild_svn_config,
        'rebuild_svn_group_config':rebuild_svn_group_config,
    }

def initializer():
    logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
    logging.info("Starting usrmgt server on port %d (pid = %d)" %
                 (ivle.conf.usrmgt_port, pid))

    try:
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
        pidfile.write('%d\n' % os.getpid())
        pidfile.close()
    except IOError, (errno, strerror):
        print "Couldn't write PID file. IO error(%s): %s" % (errno, strerror)
        sys.exit(1)

def dispatch(props):
    logging.debug(repr(props))

    store = ivle.database.get_store()
    action = props.keys()[0]
    res = actions[action](store, props[action])

    if res['response'] == 'okay':
        store.commit()
    else:
        store.rollback()
    store.close()
    return res

if __name__ == "__main__":
    pid = os.getpid()

    ivle.chat.start_server(ivle.conf.usrmgt_port, ivle.conf.usrmgt_magic,
                           True, dispatch, initializer)