~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
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python

import os
import sys
import logging

import ivle.config
import ivle.database
import ivle.chat
import ivle.makeuser

config = ivle.config.Config()

# 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, config):
    """Create the on-disk stuff for the given user.
       Sets the state of the user in the db from pending to enabled.
    @param config: An ivle.config.Config object.
       Expected properties:
        login       - the user name for the jail
                      STRING REQUIRED
    @return: None
    """

    if not os.path.exists(config['paths']['jails']['template']):
        return {
            'response': 'error',
            'message': 'Template jail has not been built -- '
                       'do you need to run ivle-buildjail?'}

    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(config['paths']['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, config)

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

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

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

    return {"response": "okay"}

def rebuild_svn_config(store, props, config):
    """Rebuilds the svn config file
    @param config: An ivle.config.Config object.
    @return: response (okay, failure)
    """
    try:
        ivle.makeuser.rebuild_svn_config(store, config)
    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, config):
    """Rebuilds the svn group config file
    @param config: An ivle.config.Config object.
    @return: response (okay, failure)
    """
    try:
        ivle.makeuser.rebuild_svn_group_config(store, config)
    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, config):
    """Creates on disk repository for the given group
    @param config: An ivle.config.Config object.
    Expected properties:
        subj_short_name, year, semester, groupnm
    @return: 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(config['paths']['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)" %
                 (config['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(config)
    action = props.keys()[0]
    res = actions[action](store, props[action], config)

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

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

    ivle.chat.start_server(config['usrmgt']['port'],config['usrmgt']['magic'],
                           True, dispatch, initializer)