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

1072 by matt.giuca
Renamed scripts to services.
1
#!/usr/bin/python
2
3
import os
4
import sys
5
import logging
6
1203 by William Grant
Give get_store() a config in usrmgt-server as well.
7
import ivle.config
1080.1.7 by matt.giuca
The new ivle.database.User class is now used in Request and usrmgt, which
8
import ivle.database
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
9
import ivle.chat
10
import ivle.makeuser
1072 by matt.giuca
Renamed scripts to services.
11
1235 by Matt Giuca
usrmgt-server: Removed all references to ivle.conf. Now uses the config
12
config = ivle.config.Config()
13
1072 by matt.giuca
Renamed scripts to services.
14
# usage:
15
#   usrmgt-server <port> <magic>
16
17
# User management operations:
18
#   - Create local user
19
#   - [Re]Create jail for a user
20
#       - Create a svn repository for a user
21
#           - create repository
22
#           - svn config
23
#           - svn auth
24
#       - /etc/passwd entry
25
#   - Disable a user's account
26
#   - Enable a user's account
27
#   - Remove a user
28
#   - Rebuild svn config
29
#   - Rebuild svn auth file
30
#   - Rebuild passwd + push to nodes.
31
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
32
def activate_user(store, props, config):
1072 by matt.giuca
Renamed scripts to services.
33
    """Create the on-disk stuff for the given user.
34
       Sets the state of the user in the db from pending to enabled.
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
35
    @param config: An ivle.config.Config object.
1072 by matt.giuca
Renamed scripts to services.
36
       Expected properties:
37
        login       - the user name for the jail
38
                      STRING REQUIRED
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
39
    @return: None
1072 by matt.giuca
Renamed scripts to services.
40
    """
41
42
    os.umask(0022) # Bad, but start_server sets it worse.
43
44
    login = props['login']
45
1080.1.17 by me at id
ivle.makeuser: svn auth manipulation functions now use storm as much as
46
    # FIXME: check we're pending
47
48
    # Get the full User object from the db associated with this
49
    user = ivle.database.User.get_by_login(store, login)
50
51
    # make svn config/auth
1235 by Matt Giuca
usrmgt-server: Removed all references to ivle.conf. Now uses the config
52
    repopath = os.path.join(config['paths']['svn']['repo_path'],
53
                            'users', login)
1080.1.17 by me at id
ivle.makeuser: svn auth manipulation functions now use storm as much as
54
    logging.debug("Creating user's Subversion repository")
55
    ivle.makeuser.make_svn_repo(repopath, throw_on_error=True)
56
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
57
    rebuild_svn_config(store, props, config)
1080.1.17 by me at id
ivle.makeuser: svn auth manipulation functions now use storm as much as
58
59
    logging.debug("Adding Subversion authentication")
1230 by Matt Giuca
ivle.makeuser: make_svn_auth now requires a config argument.
60
    passwd = ivle.makeuser.make_svn_auth(store, login, config,
1080.1.17 by me at id
ivle.makeuser: svn auth manipulation functions now use storm as much as
61
                                         throw_on_error=True)
62
63
    logging.debug("Creating jail")
1231 by Matt Giuca
ivle.makeuser: make_jail now requires a config argument.
64
    ivle.makeuser.make_jail(user, config)
1080.1.17 by me at id
ivle.makeuser: svn auth manipulation functions now use storm as much as
65
66
    logging.info("Enabling user")
67
    user.state = u'enabled'
68
69
    return {"response": "okay"}
70
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
71
def rebuild_svn_config(store, props, config):
1072 by matt.giuca
Renamed scripts to services.
72
    """Rebuilds the svn config file
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
73
    @param config: An ivle.config.Config object.
74
    @return: response (okay, failure)
1072 by matt.giuca
Renamed scripts to services.
75
    """
76
    try:
1229 by Matt Giuca
ivle.makeuser: rebuild_svn_config and rebuild_svn_group_config now require a
77
        ivle.makeuser.rebuild_svn_config(store, config)
1072 by matt.giuca
Renamed scripts to services.
78
    except Exception, e:
79
        logging.warning('Rebuild of Subversion authorization config failed!')
80
        return{'response': 'failure', 'msg': repr(e)}
81
82
    return {'response': 'okay'}
83
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
84
def rebuild_svn_group_config(store, props, config):
1072 by matt.giuca
Renamed scripts to services.
85
    """Rebuilds the svn group config file
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
86
    @param config: An ivle.config.Config object.
87
    @return: response (okay, failure)
1072 by matt.giuca
Renamed scripts to services.
88
    """
89
    try:
1229 by Matt Giuca
ivle.makeuser: rebuild_svn_config and rebuild_svn_group_config now require a
90
        ivle.makeuser.rebuild_svn_group_config(store, config)
1072 by matt.giuca
Renamed scripts to services.
91
    except Exception, e:
92
        logging.warning(
93
            'Rebuild of Subversion group authorization config failed!')
94
        return{'response': 'failure', 'msg': repr(e)}
95
96
    return {'response': 'okay'}
97
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
98
def create_group_repository(store, props, config):
1072 by matt.giuca
Renamed scripts to services.
99
    """Creates on disk repository for the given group
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
100
    @param config: An ivle.config.Config object.
1072 by matt.giuca
Renamed scripts to services.
101
    Expected properties:
102
        subj_short_name, year, semester, groupnm
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
103
    @return: response (okay, failure)
1072 by matt.giuca
Renamed scripts to services.
104
    """
105
106
    subj_short_name = props['subj_short_name']
107
    year = props['year']
108
    semester = props['semester']
109
    groupnm = props['groupnm']
110
111
    namespace = "_".join([subj_short_name, year, semester, groupnm])
1235 by Matt Giuca
usrmgt-server: Removed all references to ivle.conf. Now uses the config
112
    repopath = os.path.join(config['paths']['svn']['repo_path'],
113
                            'groups', namespace)
1072 by matt.giuca
Renamed scripts to services.
114
    logging.debug("Creating Subversion repository %s"%repopath)
115
    try:
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
116
        ivle.makeuser.make_svn_repo(repopath)
1072 by matt.giuca
Renamed scripts to services.
117
    except Exception, e:
118
        logging.error("Failed to create Subversion repository %s: %s"%
119
            (repopath,repr(e)))
120
        return {'response': 'failure', 'msg': repr(e)}
121
122
    return {'response': 'okay'}
123
124
actions = {
125
        'activate_user':activate_user,
126
        'create_group_repository':create_group_repository,
127
        'rebuild_svn_config':rebuild_svn_config,
128
        'rebuild_svn_group_config':rebuild_svn_group_config,
129
    }
130
131
def initializer():
1099.1.177 by William Grant
usrmgt-server now starts logging once it's daemonised.
132
    logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
133
    logging.info("Starting usrmgt server on port %d (pid = %d)" %
1235 by Matt Giuca
usrmgt-server: Removed all references to ivle.conf. Now uses the config
134
                 (config['usrmgt']['port'], pid))
1099.1.177 by William Grant
usrmgt-server now starts logging once it's daemonised.
135
1072 by matt.giuca
Renamed scripts to services.
136
    try:
137
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
138
        pidfile.write('%d\n' % os.getpid())
139
        pidfile.close()
140
    except IOError, (errno, strerror):
141
        print "Couldn't write PID file. IO error(%s): %s" % (errno, strerror)
142
        sys.exit(1)
143
144
def dispatch(props):
145
    logging.debug(repr(props))
1080.1.17 by me at id
ivle.makeuser: svn auth manipulation functions now use storm as much as
146
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
147
    store = ivle.database.get_store(config)
1072 by matt.giuca
Renamed scripts to services.
148
    action = props.keys()[0]
1228 by Matt Giuca
usrmgt-server: Pass a Config object through all of the "actions".
149
    res = actions[action](store, props[action], config)
1080.1.17 by me at id
ivle.makeuser: svn auth manipulation functions now use storm as much as
150
151
    if res['response'] == 'okay':
152
        store.commit()
153
    else:
154
        store.rollback()
155
    store.close()
156
    return res
1072 by matt.giuca
Renamed scripts to services.
157
158
if __name__ == "__main__":
159
    pid = os.getpid()
160
1235 by Matt Giuca
usrmgt-server: Removed all references to ivle.conf. Now uses the config
161
    ivle.chat.start_server(config['usrmgt']['port'],config['usrmgt']['magic'],
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
162
                           True, dispatch, initializer)