~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
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
7
import ivle.conf
8
import ivle.db
9
import ivle.chat
10
import ivle.makeuser
1072 by matt.giuca
Renamed scripts to services.
11
12
# usage:
13
#   usrmgt-server <port> <magic>
14
15
# User management operations:
16
#   - Create local user
17
#   - [Re]Create jail for a user
18
#       - Create a svn repository for a user
19
#           - create repository
20
#           - svn config
21
#           - svn auth
22
#       - /etc/passwd entry
23
#   - Disable a user's account
24
#   - Enable a user's account
25
#   - Remove a user
26
#   - Rebuild svn config
27
#   - Rebuild svn auth file
28
#   - Rebuild passwd + push to nodes.
29
30
def activate_user(props):
31
    """Create the on-disk stuff for the given user.
32
       Sets the state of the user in the db from pending to enabled.
33
       Expected properties:
34
        login       - the user name for the jail
35
                      STRING REQUIRED
36
       Return Value: None
37
    """
38
39
    os.umask(0022) # Bad, but start_server sets it worse.
40
41
    login = props['login']
42
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
43
    db = ivle.db.DB()
1072 by matt.giuca
Renamed scripts to services.
44
45
    try:
46
47
        # FIXME: check we're pending
48
49
        details = db.get_user(login)
50
51
        # make svn config/auth
52
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
53
        repopath = os.path.join(ivle.conf.svn_repo_path, 'users', login)
1072 by matt.giuca
Renamed scripts to services.
54
        logging.debug("Creating user's Subversion repository")
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
55
        ivle.makeuser.make_svn_repo(repopath, throw_on_error=True)
1072 by matt.giuca
Renamed scripts to services.
56
57
        rebuild_svn_config(props)
58
59
        logging.debug("Adding Subversion authentication")
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
60
        passwd = ivle.makeuser.make_svn_auth(login, throw_on_error=True)
1072 by matt.giuca
Renamed scripts to services.
61
        logging.debug("passwd: %s" % passwd)
62
63
        logging.debug("Creating jail")
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
64
        ivle.makeuser.make_jail(login, details.unixid, svn_pass=passwd)
1072 by matt.giuca
Renamed scripts to services.
65
66
        logging.info("Enabling user")
67
        db.update_user(login, state='enabled')
68
69
        return {"response": "okay"}
70
71
    finally:
72
        db.close()
73
74
def rebuild_svn_config(props):
75
    """Rebuilds the svn config file
76
    Return value:
77
        response (okay, failure)
78
    """
79
    try:
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
80
        ivle.makeuser.rebuild_svn_config()
1072 by matt.giuca
Renamed scripts to services.
81
    except Exception, e:
82
        logging.warning('Rebuild of Subversion authorization config failed!')
83
        return{'response': 'failure', 'msg': repr(e)}
84
85
    return {'response': 'okay'}
86
87
def rebuild_svn_group_config(props):
88
    """Rebuilds the svn group config file
89
    Return value:
90
        response (okay, failure)
91
    """
92
    try:
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
93
        ivle.makeuser.rebuild_svn_group_config()
1072 by matt.giuca
Renamed scripts to services.
94
    except Exception, e:
95
        logging.warning(
96
            'Rebuild of Subversion group authorization config failed!')
97
        return{'response': 'failure', 'msg': repr(e)}
98
99
    return {'response': 'okay'}
100
101
def create_group_repository(props):
102
    """Creates on disk repository for the given group
103
    Expected properties:
104
        subj_short_name, year, semester, groupnm
105
    Return value:
106
        response (okay, failure)
107
    """
108
109
    subj_short_name = props['subj_short_name']
110
    year = props['year']
111
    semester = props['semester']
112
    groupnm = props['groupnm']
113
114
    namespace = "_".join([subj_short_name, year, semester, groupnm])
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
115
    repopath = os.path.join(ivle.conf.svn_repo_path, 'groups', namespace)
1072 by matt.giuca
Renamed scripts to services.
116
    logging.debug("Creating Subversion repository %s"%repopath)
117
    try:
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
118
        ivle.makeuser.make_svn_repo(repopath)
1072 by matt.giuca
Renamed scripts to services.
119
    except Exception, e:
120
        logging.error("Failed to create Subversion repository %s: %s"%
121
            (repopath,repr(e)))
122
        return {'response': 'failure', 'msg': repr(e)}
123
124
    return {'response': 'okay'}
125
126
actions = {
127
        'activate_user':activate_user,
128
        'create_group_repository':create_group_repository,
129
        'rebuild_svn_config':rebuild_svn_config,
130
        'rebuild_svn_group_config':rebuild_svn_group_config,
131
    }
132
133
def initializer():
134
    try:
135
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
136
        pidfile.write('%d\n' % os.getpid())
137
        pidfile.close()
138
    except IOError, (errno, strerror):
139
        print "Couldn't write PID file. IO error(%s): %s" % (errno, strerror)
140
        sys.exit(1)
141
142
def dispatch(props):
143
    logging.debug(repr(props))
144
    action = props.keys()[0]
145
    return actions[action](props[action])
146
147
if __name__ == "__main__":
148
    pid = os.getpid()
149
150
    logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
151
    logging.info("Starting usrmgt server on port %d (pid = %d)" %
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
152
                 (ivle.conf.usrmgt_port, pid))
1072 by matt.giuca
Renamed scripts to services.
153
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
154
    ivle.chat.start_server(ivle.conf.usrmgt_port, ivle.conf.usrmgt_magic,
155
                           True, dispatch, initializer)