-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcreate_course.sh
More file actions
376 lines (306 loc) · 18.7 KB
/
create_course.sh
File metadata and controls
376 lines (306 loc) · 18.7 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/bin/bash
########################################################################################################################
########################################################################################################################
# this script must be run by root or sudo
if [[ "$UID" -ne "0" ]] ; then
echo "ERROR: This script must be run by root or sudo"
exit
fi
########################################################################################################################
########################################################################################################################
CONF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/../config
SUBMITTY_REPOSITORY_DIR=$(jq -r '.submitty_repository' "${CONF_DIR}/submitty.json")
SUBMITTY_INSTALL_DIR=$(jq -r '.submitty_install_dir' "${CONF_DIR}/submitty.json")
SUBMITTY_DATA_DIR=$(jq -r '.submitty_data_dir' "${CONF_DIR}/submitty.json")
SUBMISSION_URL=$(jq -r '.submission_url' "${CONF_DIR}/submitty.json")
PHP_USER=$(jq -r '.php_user' "${CONF_DIR}/submitty_users.json")
DAEMON_USER=$(jq -r '.daemon_user' "${CONF_DIR}/submitty_users.json")
CGI_USER=$(jq -r '.cgi_user' "${CONF_DIR}/submitty_users.json")
COURSE_BUILDERS_GROUP=$(jq -r '.course_builders_group' "${CONF_DIR}/submitty_users.json")
DATABASE_HOST=$(jq -r '.database_host' "${CONF_DIR}/database.json")
DATABASE_PORT=$(jq -r '.database_port' "${CONF_DIR}/database.json")
DATABASE_USER=$(jq -r '.database_user' "${CONF_DIR}/database.json")
DATABASE_PASS=$(jq -r '.database_password' "${CONF_DIR}/database.json")
DATABASE_COURSE_USER=$(jq -r '.database_course_user' "${CONF_DIR}/database.json")
echo $DATABASE_COURSE_USER
########################################################################################################################
########################################################################################################################
CONN_STRING="-h ${DATABASE_HOST} -U ${DATABASE_USER} -p ${DATABASE_PORT}"
# Check that Submitty Master DB exists.
PGPASSWORD="${DATABASE_PASS}" psql ${CONN_STRING} -lqt | cut -d \| -f 1 | grep -qw submitty
if [[ "$?" -ne "0" ]] ; then
echo "ERROR: Submitty master database doesn't exist."
exit
fi
#Ensure that tables exist within Submitty Master DB.
sql="SELECT count(*) FROM pg_tables WHERE schemaname='public' AND tablename IN ('terms','courses','courses_users','sessions','users');"
table_count=`PGPASSWORD="${DATABASE_PASS}" psql ${CONN_STRING} -d submitty -tAc "${sql}"`
if [[ "$table_count" -ne "5" ]] ; then
echo "ERROR: Submitty Master DB is invalid."
exit
fi
archived=0
self_registration_type=0
while :; do
case $1 in
--archive)
archived=1
echo "archive"
;;
--all-self-registration)
self_registration_type=2
echo "all self registration"
;;
--request-self-registration)
self_registration_type=1
echo "request self registration"
;;
*) # No more options, so break out of the loop.
break
esac
shift
done
# Check that there are exactly 4 command line arguments.
if [[ "$#" -ne "4" ]] ; then
echo "ERROR: Usage, wrong number of arguments"
echo " create_course.sh [--all-self-registration|--request-self-registration|--archive] <semester> <course> <instructor username> <ta group>"
exit
fi
semester="$1"
course="$2"
instructor="$3"
ta_www_group="$4"
echo -e "\nCREATE COURSE:"
echo -e " semester: $semester"
echo -e " course: $course"
echo -e " instructor: $instructor"
echo -e " ta_www_group: $ta_www_group"
echo -e " status: $archived"
echo -e " self_registration_type: $self_registration_type\n"
########################################################################################################################
########################################################################################################################
# ERROR CHECKING ON THE ARGUMENTS
# confirm that the instructor user exists
if ! id -u "$instructor" >/dev/null 2>&1 ; then
echo -e "ERROR: $instructor user does not exist\n"
exit
fi
# confirm that the ta_www_group exists
if ! getent group "$ta_www_group" >/dev/null 2>&1 ; then
echo -e "ERROR: $ta_www_group group does not exist\n"
exit
fi
# confirm that the instructor is a member of the $COURSE_BUILDERS_GROUP
if ! groups "$instructor" | grep -q "\b${COURSE_BUILDERS_GROUP}\b" ; then
echo -e "ERROR: $instructor is not in group $COURSE_BUILDERS_GROUP\n"
exit
fi
# confirm that the instructor, submitty_daemon, submitty_php, and submitty_cgi are members of the
# ta_www_group
if ! groups "$instructor" | grep -q "\b${ta_www_group}\b" ; then
echo -e "ERROR: $instructor is not in group $ta_www_group\n"
exit
fi
if ! groups "$PHP_USER" | grep -q "\b${ta_www_group}\b" ; then
echo -e "ERROR: $PHP_USER is not in group $ta_www_group\n"
exit
fi
if ! groups "$DAEMON_USER" | grep -q "\b${ta_www_group}\b" ; then
echo -e "ERROR: $DAEMON_USER is not in group $ta_www_group\n"
exit
fi
if ! groups "$CGI_USER" | grep -q "\b${ta_www_group}\b" ; then
echo -e "ERROR: $CGI_USER is not in group $ta_www_group\n"
exit
fi
# NOTE: the ta_www_group should also contain the usernames of any
# additional instructors and/or head TAs who need read/write
# access to these files
TERM_EXISTS=$(PGPASSWORD=${DATABASE_PASS} psql ${CONN_STRING} -d submitty -AXtc "SELECT COUNT(*) FROM terms WHERE term_id='${semester}'")
if [[ "$TERM_EXISTS" -eq "0" ]] ; then
echo "ERROR: Provided term ${semester} doesn't exist."
exit
fi
# FIXME: add some error checking on the $semester and $course
# variables
#
# (not clear how to do this since these variables could have quite
# different structure at different schools)
########################################################################################################################
########################################################################################################################
course_dir="$SUBMITTY_DATA_DIR/courses/$semester/$course"
if [ -d "$course_dir" ]; then
echo -e "ERROR: specific course directory $course_dir already exists"
exit
fi
########################################################################################################################
########################################################################################################################
DATABASE_NAME="submitty_${semester}_${course}"
########################################################################################################################
########################################################################################################################
#this function takes a single argument, the name of the file to be edited
function replace_fillin_variables {
sed -i -e "s|__CREATE_COURSE__FILLIN__SUBMITTY_INSTALL_DIR__|$SUBMITTY_INSTALL_DIR|g" "$1"
sed -i -e "s|__CREATE_COURSE__FILLIN__SUBMITTY_DATA_DIR__|$SUBMITTY_DATA_DIR|g" "$1"
sed -i -e "s|__CREATE_COURSE__FILLIN__SUBMISSION_URL__|$SUBMISSION_URL|g" "$1"
sed -i -e "s|__CREATE_COURSE__FILLIN__SEMESTER__|$semester|g" "$1"
sed -i -e "s|__CREATE_COURSE__FILLIN__COURSE__|$course|g" "$1"
sed -i -e "s|__CREATE_COURSE__FILLIN__DATABASE_NAME__|$DATABASE_NAME|g" "$1"
# FIXME: Add some error checking to make sure these values were filled in correctly
}
########################################################################################################################
########################################################################################################################
if [ ! -d "$SUBMITTY_DATA_DIR" ]; then
echo -e "ERROR: base directory $SUBMITTY_DATA_DIR does not exist\n"
exit
fi
if [ ! -d "$SUBMITTY_DATA_DIR/courses" ]; then
echo -e "ERROR: courses directory $SUBMITTY_DATA_DIR/courses does not exist\n"
exit
fi
if [ ! -d "$SUBMITTY_DATA_DIR/courses/$semester" ]; then
mkdir "$SUBMITTY_DATA_DIR/courses/$semester"
chown "root:$COURSE_BUILDERS_GROUP" "$SUBMITTY_DATA_DIR/courses/$semester"
chmod 751 "$SUBMITTY_DATA_DIR/courses/$semester"
fi
########################################################################################################################
########################################################################################################################
function create_and_set {
permissions="$1"
owner="$2"
group="$3"
directory="$4"
mkdir "$directory"
chown "$owner:$group" "$directory"
chmod "$permissions" "$directory"
}
# top level course directory
# drwxrws--- instructor ta_www_group ./
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir"
# drwxrws--- instructor ta_www_group build/
# drwxrws--- instructor ta_www_group config/
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/build"
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/config"
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/config/build"
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/config/form"
# NOTE: when homework is installed, grading executables, code, & datafiles are placed here
# drwxr-s--- instructor ta_www_group bin/
# drwxr-s--- instructor ta_www_group provided_code/
# drwxr-s--- instructor ta_www_group test_input/
# drwxr-s--- instructor ta_www_group test_output/
# drwxr-s--- instructor ta_www_group custom_validation_code/
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/bin"
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/provided_code"
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/instructor_solution"
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/test_input"
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/test_output"
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/custom_validation_code"
# NOTE: on each student submission, files are written to these directories
# drwxr-s--- $PHP_USER ta_www_group submissions/
# drwxrws--- $DAEMON_USER ta_www_group submissions_processed/
# drwxr-s--- $PHP_USER ta_www_group config_upload/
# drwxr-s--- $PHP_USER ta_www_group site/
# drwxr-s--- $DAEMON_USER ta_www_group results/
# drwxr-s--- $DAEMON_USER ta_www_group results_public/
# drwxr-s--- $DAEMON_USER ta_www_group checkout/
# drwxr-s--- $DAEMON_USER ta_www_group uploads/
# drwxr-s--- $PHP_USER ta_www_group uploads/bulk_pdf/
# drwxr-s--- $PHP_USER ta_www_group uploads/polls/
# drwxrws--- $DAEMON_USER ta_www_group uploads/split_pdf/
# drwxr-s--- $PHP_USER ta_www_group uploads/student_images/
# drwxr-s--- $PHP_USER ta_www_group uploads/student_images/tmp
# drwxrws--- $PHP_USER ta_www_group uploads/seating
# drwxrws--- $PHP_USER ta_www_group rainbow_grades
# drwxrws--- $DAEMON_USER ta_www_group lichen/
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/submissions"
create_and_set u=rwx,g=rwxs,o= "$DAEMON_USER" "$ta_www_group" "$course_dir/submissions_processed"
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/forum_attachments"
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/annotations"
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/config_upload"
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/site"
create_and_set u=rwx,g=rxs,o= "$DAEMON_USER" "$ta_www_group" "$course_dir/results"
create_and_set u=rwx,g=rxs,o= "$DAEMON_USER" "$ta_www_group" "$course_dir/generated_output"
create_and_set u=rwx,g=rxs,o= "$DAEMON_USER" "$ta_www_group" "$course_dir/results_public"
create_and_set u=rwx,g=rxs,o= "$DAEMON_USER" "$ta_www_group" "$course_dir/checkout"
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/uploads"
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/uploads/bulk_pdf"
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/uploads/polls"
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/uploads/student_images"
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/uploads/student_images/tmp"
create_and_set u=rwx,g=rxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/uploads/course_materials"
create_and_set u=rwx,g=rwxs,o= "$DAEMON_USER" "$ta_www_group" "$course_dir/uploads/split_pdf"
create_and_set u=rwx,g=rwxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/uploads/seating"
create_and_set u=rwx,g=rwxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/rainbow_grades"
create_and_set u=rwx,g=rwxs,o= "$DAEMON_USER" "$ta_www_group" "$course_dir/lichen"
# NOTE: instructor uploads TA HW grade reports & overall grade scores here
# drwxrws--- instructor ta_www_group reports
# drwxrws--- instructor ta_www_group reports/summary_html
# drwxrws--- $PHP_USER ta_www_group reports/all_grades
# drwxrws--- $PHP_USER ta_www_group reports/seating
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/reports"
create_and_set u=rwx,g=rwxs,o= "$instructor" "$ta_www_group" "$course_dir/reports/summary_html"
create_and_set u=rwx,g=rwxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/reports/all_grades"
create_and_set u=rwx,g=rwxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/reports/seating"
create_and_set u=rwx,g=rwxs,o= "$PHP_USER" "$ta_www_group" "$course_dir/reports/polls"
########################################################################################################################
########################################################################################################################
# copy the build_course.sh script
cp "$SUBMITTY_INSTALL_DIR/sbin/build_course.sh" "$course_dir/BUILD_${course}.sh"
chown "$instructor:$ta_www_group" "$course_dir/BUILD_${course}.sh"
chmod 770 "$course_dir/BUILD_${course}.sh"
replace_fillin_variables "$course_dir/BUILD_${course}.sh"
# copy the config file for TA grading & replace the variables
cp "${SUBMITTY_INSTALL_DIR}/site/config/course_template.json" "${course_dir}/config/config.json"
chown "${PHP_USER}:${ta_www_group}" "${course_dir}/config/config.json"
chmod 660 "${course_dir}/config/config.json"
replace_fillin_variables "${course_dir}/config/config.json"
echo -e "Creating database ${DATABASE_NAME}\n"
PGPASSWORD=${DATABASE_PASS} psql ${CONN_STRING} -d postgres -c "CREATE DATABASE ${DATABASE_NAME}"
if [[ "$?" -ne "0" ]] ; then
echo "ERROR: Failed to create database ${DATABASE_NAME}"
exit
fi
PGPASSWORD=${DATABASE_PASS} psql ${CONN_STRING} -d ${DATABASE_NAME} -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO ${DATABASE_COURSE_USER};"
if [[ "$?" -ne "0" ]] ; then
echo "ERROR: Failed to grant table privileges to course database user"
exit
fi
PGPASSWORD=${DATABASE_PASS} psql ${CONN_STRING} -d ${DATABASE_NAME} -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, UPDATE ON SEQUENCES TO ${DATABASE_COURSE_USER};"
if [[ "$?" -ne "0" ]] ; then
echo "ERROR: Failed to grant sequence privileges to course database user"
exit
fi
PGPASSWORD=${DATABASE_PASS} psql ${CONN_STRING} -d submitty -c "INSERT INTO courses (term, course, group_name, owner_name, self_registration_type)
VALUES ('${semester}', '${course}', '${ta_www_group}', '${instructor}', ${self_registration_type});"
if [[ "$?" -ne "0" ]] ; then
echo "ERROR: Failed to add this course to the master Submitty database."
echo "HINT: 'insert or update on table \"courses\" violates foreign key constraint...'"
echo " may indicate that term ${semester} does not exist in master DB."
echo " To fix, try running 'create_term.sh'."
exit
fi
python3 "${SUBMITTY_REPOSITORY_DIR}/migration/run_migrator.py" -e course --course "${semester}" "${course}" migrate --initial
if [[ $? -ne "0" ]] ; then
PGPASSWORD=${DATABASE_PASS} psql ${CONN_STRING} -d submitty -c "DELETE FROM courses WHERE term='${semester}' AND course='${course}';"
echo "ERROR: Failed to create tables within database ${DATABASE_NAME}"
exit
fi
PGPASSWORD=${DATABASE_PASS} psql ${CONN_STRING} -d ${DATABASE_NAME} -c "INSERT INTO categories_list (category_desc, rank, visible_date) VALUES ('General Questions', 0, NULL);
INSERT INTO categories_list (category_desc, rank, visible_date) VALUES ('Homework Help', 1, NULL);
INSERT INTO categories_list (category_desc, rank, visible_date) VALUES ('Quizzes', 2, NULL);
INSERT INTO categories_list (category_desc, rank, visible_date) VALUES ('Tests', 3, NULL);"
if [[ "$?" -ne "0" ]] ; then
echo "ERROR: Failed create default discussion forum categories."
exit
fi
if [[ "$archived" -eq "1" ]] ; then
PGPASSWORD=${DATABASE_PASS} psql ${CONN_STRING} -d submitty -c "UPDATE courses SET status=2 WHERE term='${semester}' AND course='${course}';"
echo "Archived Course ${course}"
fi
echo -e "\nSUCCESS!\n\n"
########################################################################################################################
########################################################################################################################
echo -e "SUCCESS! new course $course $semester CREATED HERE: $course_dir"
echo -e "SUCCESS! course page url ${SUBMISSION_URL}/${semester}/${course}"
########################################################################################################################
########################################################################################################################