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

1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
1
#!/usr/bin/env python
2
# IVLE - Informatics Virtual Learning Environment
3
# Copyright (C) 2007-2008 The University of Melbourne
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19
# Program: MountAllUsers
20
# Author:  William Grant
21
# Date:    4/7/2008
22
1281.2.1 by David Coles
Updated to work on bind mounts
23
# Script to bind-mount jails for all users on the system.
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
24
# Requires root to run.
25
26
import sys
27
import os
28
import os.path
29
import optparse
30
import logging
31
1201 by William Grant
ivle.database.get_store() now takes a configuration object.
32
import ivle.config
1080.1.23 by me at id
bin/ivle-{list,mountall}users: Don't use ivle.db.
33
import ivle.database
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
34
35
p = optparse.OptionParser()
36
p.add_option('--verbose', '-v', action='store_true')
37
p.add_option('--unmount', '-u', action='store_true')
38
options, arguments = p.parse_args()
39
40
if options.verbose:
41
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
42
                        level=logging.DEBUG)
43
else:
44
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
45
                        level=logging.WARNING)
46
47
if os.getuid() != 0:
48
    print >> sys.stderr, "%s must be run as root" % \
49
                          os.path.basename(sys.argv[0])
50
    sys.exit(1)
51
1281.2.1 by David Coles
Updated to work on bind mounts
52
config = ivle.config.Config()
53
store = ivle.database.get_store(config)
1080.1.23 by me at id
bin/ivle-{list,mountall}users: Don't use ivle.db.
54
users = store.find(ivle.database.User).order_by(ivle.database.User.login)
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
55
1281.2.1 by David Coles
Updated to work on bind mounts
56
logging.info("mass bind mount started")
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
57
1080.1.23 by me at id
bin/ivle-{list,mountall}users: Don't use ivle.db.
58
for user in users:
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
59
    login = user.login
60
    # This is where we'll mount to...
1281.2.1 by David Coles
Updated to work on bind mounts
61
    destdir = os.path.join(config['paths']['jails']['mounts'], login)
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
62
    # ... and this is where we'll get the user bits.
1281.2.1 by David Coles
Updated to work on bind mounts
63
    srcdir = os.path.join(config['paths']['jails']['src'], login)
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
64
65
    try:
66
        if not options.unmount:
67
            if not os.path.exists(srcdir):
68
                logging.info("user %s doesn't have a jail - skipping" % login)
69
                continue
70
            if not os.path.exists(destdir):
71
                logging.debug("user %s had no mountpoint - creating" % login)
72
                os.mkdir(destdir)
1281.2.1 by David Coles
Updated to work on bind mounts
73
            if os.path.exists(os.path.join(destdir, 'lib')):
74
                logging.info("%s's jail appears mounted. skipping." % login)
75
                continue
76
            # Mount read only root template
77
            if os.system('/bin/mount --bind -o ro %s %s'
78
                    % (config['paths']['jails']['template'], destdir)) == 0:
79
                logging.debug("mounted user %s's root template" % login)
80
            else:
81
                logging.debug("failed to mount %s's root template!" % login)
82
                continue
83
            # Mount homedir
84
            if os.system('/bin/mount --bind %s %s'
85
                    % (os.path.join(srcdir,'home'), os.path.join(destdir,'home'))) == 0:
86
                logging.debug("mounted user %s's home." % login)
87
            else:
88
                logging.error("failed to mount user %s's home!" % login)
89
                os.system("/bin/umount %s" % destdir)
90
                continue
91
            # Mount tmp
92
            if os.system('/bin/mount --bind %s %s'
93
                    % (os.path.join(srcdir,'tmp'), os.path.join(destdir,'tmp'))) == 0:
94
                logging.debug("mounted user %s's tmp." % login)
95
            else:
96
                logging.error("failed to mount user %s's tmp!" % login)
97
                os.system("/bin/umount %s" % os.path.join(destdir,'home'))
98
                os.system("/bin/umount %s" % destdir)
99
                continue
100
            logging.info("mounted user %s's jail" % login)
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
101
        else:
1281.2.1 by David Coles
Updated to work on bind mounts
102
            os.system("/bin/umount %s" % os.path.join(destdir,'tmp'))
103
            os.system("/bin/umount %s" % os.path.join(destdir,'home'))
104
            os.system("/bin/umount %s" % destdir)
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
105
            logging.info("unmounted user %s's jail." % login)
106
    except Exception, message:
107
        logging.warning(str(message))
108
        continue
109
110
logging.info("mount completed successfully")