2
# IVLE - Informatics Virtual Learning Environment
3
# Copyright (C) 2007-2009 The University of Melbourne
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.
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.
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
20
Script to load the sample data into a fresh IVLE instance, for testing or
35
from ivle.config import Config
39
format='%(asctime)s %(levelname)s %(message)s',
42
def runprog_stderr(*popenargs, **kwargs):
43
"""Run a program, using subprocess.Popen.
44
Return None if the program had a 0 return code.
45
Return the string stderr if the program failed.
47
kwargs['stderr'] = subprocess.PIPE
49
proc = subprocess.Popen(*popenargs, **kwargs)
50
_, stderr = proc.communicate()
51
if proc.returncode == 0:
57
print "Must run %s as root." % os.path.basename(sys.argv[0])
60
usage = """usage: %prog [OPTIONS] <SQL-FILE>
61
Loads the sample data into the installed IVLE instance."""
63
parser = optparse.OptionParser(usage)
64
parser.add_option("-f", "--force",
65
action="store_true", dest="force",
66
help="destroy all data without prompting",
69
parser.add_option("--pg-user",
70
action="store", dest="pg_user",
71
help="database super-user (for dropping and creating db) "
72
"(default: postgres)",
76
(options, args) = parser.parse_args()
79
parser.error("incorrect number of arguments")
83
dbconfig = Config()['database']
85
# Try creating the database (if it succeeds, no harm)
86
logging.info("Creating database \"%s\"." % dbconfig["name"])
87
errmsg = runprog_stderr(["sudo", "-u", options.pg_user, "createdb", "-O",
88
dbconfig["username"], dbconfig["name"]])
89
if errmsg is not None:
90
# Couldn't create the DB
91
if errmsg.strip().endswith("already exists"):
92
logging.info("Database already exists.")
93
# The database already exists (most common error)
94
# Drop and re-create the database, if the user is absolutely sure
97
drop = raw_input("Do you want to delete all existing data? "
98
"THIS WILL DROP YOUR DATABASE!\n[yes/no]> ")
99
except (KeyboardInterrupt, EOFError):
102
if drop.strip().lower() != "yes":
106
# Unmount all the jails
107
logging.info("Unmounting all users.")
108
subprocess.check_call(["ivle-mountallusers", "-u"])
111
logging.info("Dropping database \"%s\"." % dbconfig["name"])
112
errmsg = subprocess.check_call(["sudo", "-u", options.pg_user,
113
"dropdb", dbconfig["name"]])
115
logging.info("Creating database \"%s\"." % dbconfig["name"])
116
errmsg = subprocess.check_call(["sudo", "-u", options.pg_user,
117
"createdb", "-O",dbconfig["username"],
120
logging.error(errmsg.strip())
123
# Create "plpgsql" language
124
logging.info("Creating language plpgsql.")
125
errmsg = subprocess.check_call(["sudo", "-u", options.pg_user, "createlang",
126
"plpgsql", dbconfig["name"]])
128
# Populate with database schema
129
logging.info("Populating database with schema.")
130
ivleconn = psycopg2.connect(
131
host='localhost', database=dbconfig['name'], user=dbconfig['username'],
132
password=dbconfig['password'])
133
ivlecursor = ivleconn.cursor()
134
logging.info("Populating database with schema.")
135
schemafile = open("userdb/users.sql")
136
ivlecursor.execute(schemafile.read())
139
# Build or rebuild all of the users' filesystems and subversion repos
140
logging.info("Creating data directories.")
141
subprocess.check_call(["ivle-createdatadirs"])
143
# Move all of the users' filesystems and subversion repos out of the way
144
# (This will clean out the user dirs because there are no users in the DB.)
145
logging.info("Moving existing user filesystems and repos out of the way.")
146
subprocess.check_call(["ivle-refreshfilesystem"])
148
# Populate with sample data
149
logging.info("Populating database with sample data.")
150
file = open("examples/db/sample.sql")
151
proc = subprocess.Popen(["sudo", "-u", "postgres", "psql", dbconfig["name"]],
158
# Build all of the users' filesystems and subversion repos
159
# (This will create fresh user dirs and repos because the jails were empty.)
160
logging.info("Building sample users' filesystems and repos.")
161
subprocess.check_call(["ivle-refreshfilesystem"])
164
dbconfig = config['database']
166
# Populate some of the users' Subversion repos
167
def temp_checkout(svnclient, username):
168
"""Checkout user `username`'s repo to a temporary directory.
169
@return: The temporary workspace directory.
171
# Do the checkout over HTTP, since we then use the user's own credentials
172
repourl = config['urls']['svn_addr'] + '/users/' + username
173
# Ignore warnings about the use of tempnam
174
warnings.simplefilter('ignore')
175
tempdir = os.tempnam()
176
warnings.resetwarnings()
177
svnclient.checkout(repourl, tempdir)
180
logging.info("Populating student repositories")
181
def svnload(dumpfile, repo):
182
"""Run svnadmin load.
183
@param dumpfile: Dump file to load from, relative to examples/userrepos.
184
@param repo: Repo to write to, relative to /var/lib/ivle/svn/repositories.
186
f = open(os.path.join("examples/userrepos", dumpfile), "rb")
187
repo = os.path.join(config['paths']['svn']['repo_path'], repo)
188
errmsg = runprog_stderr(["sudo", "-u", "www-data", "svnadmin", "load",
190
if errmsg is not None:
191
logging.error(errmsg)
194
# Load the known SVN dump files
195
svnload("studenta.dump", "users/studenta")
196
svnload("group1.dump", "groups/ivle-102_2009_2_group1")