-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmoodle-update
executable file
·617 lines (451 loc) · 17.1 KB
/
moodle-update
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#!/bin/bash
#======================================================#
# THIS VERSION #
#======================================================#
# Leuphana Universität Lüneburg #
# #
# Author: #
# Sebastian Sennewald #
# #
# Sources: #
# https://github.com/mizcm/moodle-update-script #
# #
# License: #
# GPLv3, see LICENSE file #
# #
#======================================================#
#======================================================#
# FORKED FROM #
#======================================================#
# Bergische Universität Wuppertal #
# #
# Author: #
# Sebastian Sennewald #
# #
# Sources: #
# https://github.com/buwzim/moodle-update-script #
# #
# License: #
# GPLv3, see LICENSE file #
# #
#======================================================#
# No Colour
NC='\033[0m'
# Colour Codes
WHITE='\033[1;37m'
LIGHTGREEN='\033[1;32m'
ORANGE='\033[0;33m'
LIGHTRED='\033[1;31m'
LIGHTPURPLE='\033[1;35m'
LIGHTCYAN='\033[1;36m'
# functions
function checkstatus() {
# set to default
CONTINUE="null"
if test $? -ne 0
then
# ask user to continue
until test "$CONTINUE" = "y" -o "$CONTINUE" = "n"
do
printf "${LIGHTRED}%s${NC} ${ORANGE}%s${NC}" "FAILED > Continue?" "(y/n)" 1>&2
read -r CONTINUE
done
if test "$CONTINUE" = "n"
then
printf "\n"
printf "%s \n" "process cancelled"
printf "\n"
exit 66
fi
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
fi
# set to default
CONTINUE="null"
}
# start
tput clear
printf "\n"
printf "\n"
printf "${WHITE}%s${NC}\n" "+------------------------------------+"
printf "${WHITE}%s${NC}\n" "+ moodle update script +"
printf "${WHITE}%s${NC}\n" "+------------------------------------+"
printf "\n"
# get script path
SCRIPT_PATH=$(dirname "$(realpath $0)")
# check if version parameter is given
if test -z $1
then
printf "%s" "enter target version (\"x.y\"): "
read -r MOODLE_VERSION_NEXT
else
MOODLE_VERSION_NEXT=$1
fi
# check and create settings file
if test ! -e $SCRIPT_PATH/settings.conf
then
# create default settings if file doesn't exists
printf "%-30s %s" "create settings file" ": "
echo '# Do not delete this line' > $SCRIPT_PATH/settings.conf
echo 'SETTINGS_LOADED="TRUE"' >> $SCRIPT_PATH/settings.conf
echo ' ' >> $SCRIPT_PATH/settings.conf
echo '# Adapt this list to your settings' >> $SCRIPT_PATH/settings.conf
echo 'WEBUSER="www-data"' >> $SCRIPT_PATH/settings.conf
echo 'WEB_PATH="/var/www"' >> $SCRIPT_PATH/settings.conf
echo 'PHP_PATH="/usr/bin/php"' >> $SCRIPT_PATH/settings.conf
echo 'CURL_PATH="/usr/bin/curl"' >> $SCRIPT_PATH/settings.conf
echo 'UNZIP_PATH="/usr/bin/unzip"' >> $SCRIPT_PATH/settings.conf
checkstatus
fi
# load settings
source $SCRIPT_PATH/settings.conf
# check if file got loaded
printf "%-30s %s" "load settings" ": "
if test "$SETTINGS_LOADED" = "TRUE"
then
printf "${LIGHTGREEN}%s${NC}\n" "OK"
else
printf "${LIGHTRED}%s${NC}\n" "FAILED" 1>&2
exit 2
fi
# set settings
CONTINUE="null"
MOODLE_LINK_NAME="moodle"
MOODLE_PATH="$WEB_PATH/$MOODLE_LINK_NAME"
MOODLE_CONFIG="$MOODLE_PATH/config.php"
# check moodle config file
printf "%-30s %s" "moodle config" ": "
if test ! -e $MOODLE_CONFIG
then
printf "${LIGHTRED}%s${NC} \n" "FAILED" 1>&2
exit 3
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
echo "$1" | grep "^moodle_[0-9]*\.[0-9]*_" > /dev/null
fi
# get moodle data path
TEMP1=$(grep "^\$CFG->dataroot" $MOODLE_PATH/config.php | cut -d" " -f4)
TEMP2=${TEMP1//\'}
MOODLE_DATA_PATH=${TEMP2//\;}
# check moodle data path
printf "%-30s %s" "moodle data folder" ": "
if test ! -d $MOODLE_DATA_PATH
then
printf "${LIGHTRED}%s${NC} \n" "FAILED" 1>&2
exit 4
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
fi
# language files
LANG_LICENSE_EN="$MOODLE_PATH/lang/en/license.php"
LANG_LICENSE_DE="$MOODLE_DATA_PATH/lang/de/license.php"
# build url version parameter
MOODLE_VERSION_NEXT_URL=${MOODLE_VERSION_NEXT//.}
# get current moodle version
if test -h $MOODLE_PATH
then
LINK_CURRENT_TARGET=$(readlink "$MOODLE_PATH")
MOODLE_PATH_REAL="$LINK_CURRENT_TARGET"
MOODLE_VERSION_NOW=${LINK_CURRENT_TARGET//$WEB_PATH\/moodle_}
fi
# check if url parameter is given
if test $2
then
MOODLE_DOWNLOAD_URL=$2
MOODLE_DOWNLOAD_FILE=${MOODLE_DOWNLOAD_URL##*/}
else
MOODLE_DOWNLOAD_FILE="moodle-latest-${MOODLE_VERSION_NEXT_URL}.zip"
MOODLE_DOWNLOAD_URL="https://download.moodle.org/download.php/direct/stable${MOODLE_VERSION_NEXT_URL}/$MOODLE_DOWNLOAD_FILE"
# example v3.9: https://download.moodle.org/download.php/direct/stable39/moodle-latest-39.zip
# example v4.1: https://download.moodle.org/download.php/direct/stable401/moodle-latest-401.zip
fi
MOODLE_TEMP_PATH="$WEB_PATH/moodle_temp"
MOODLE_NEXT="moodle_$MOODLE_VERSION_NEXT"
MOODLE_NEXT_PATH="$WEB_PATH/$MOODLE_NEXT"
BACKUP_DATE=$(date '+%F_%H-%M-%S')
BACKUP_NAME="moodle_${MOODLE_VERSION_NOW}_$BACKUP_DATE"
BACKUP_PATH="$WEB_PATH/$BACKUP_NAME"
# ask if versions are corret
printf "\n"
printf "%-30s ${LIGHTCYAN}%s${NC} \n" "target version" ": $MOODLE_VERSION_NEXT"
printf "%-30s ${LIGHTCYAN}%s${NC} \n" "current version" ": $MOODLE_VERSION_NOW"
printf "\n"
# show download url
printf "%-30s %s ${LIGHTPURPLE}%s${NC} \n" "download url" ":" "$MOODLE_DOWNLOAD_URL"
# check web path: folder
printf "%-30s %s" "web path (folder)" ": "
if test ! -d $WEB_PATH
then
printf "${LIGHTRED}%s${NC} \n" "FAILED" 1>&2
exit 6
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
fi
# download latest moodle
printf "%-30s %s" "download moodle" ": "
wget -q "$MOODLE_DOWNLOAD_URL" --directory-prefix="$WEB_PATH/"
checkstatus
# file size
printf "%-30s %s" "file size (MB)" ": "
FILESIZE=$(cat $WEB_PATH/$MOODLE_DOWNLOAD_FILE | wc -c)
FILESIZE=$(( FILESIZE / 1024 / 1024 ))
printf "${LIGHTCYAN}%d${NC}" "$FILESIZE"
if test $FILESIZE -eq 0
then
printf "${LIGHTRED}%s${NC} \n" " FAILED" 1>&2
exit 5
else
printf "\n"
fi
# check web path: access
printf "%-30s %s" "web path (access)" ": "
if test ! -e $WEB_PATH
then
printf "${LIGHTRED}%s${NC} \n" "FAILED" 1>&2
exit 7
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
fi
# check php
printf "%-30s %s" "php" ": "
if test ! -e $PHP_PATH
then
printf "%s \n" "FAILED" 1>&2
exit 8
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
fi
# check cURL
printf "%-30s %s" "cURL" ": "
if test ! -e $CURL_PATH
then
printf "${LIGHTRED}%s${NC} \n" "FAILED" 1>&2
exit 9
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
fi
# check unzip
printf "%-30s %s" "unzip" ": "
if test ! -e $UNZIP_PATH
then
printf "${LIGHTRED}%s${NC} \n" "FAILED" 1>&2
exit 14
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
fi
# check current moodle path
printf "%-30s %s" "moodle path" ": "
if test ! -d $MOODLE_PATH_REAL
then
printf "${LIGHTRED}%s${NC} \n" "FAILED" 1>&2
exit 10
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
fi
# go to web folder
printf "%-30s %s" "go to web folder" ": "
cd $WEB_PATH
checkstatus
# all pre-checks done
printf "\n"
printf "%s \n" "all pre-checks successfull"
# ask user to continue
until test "$CONTINUE" = "y" -o "$CONTINUE" = "n"
do
printf "${ORANGE}%-30s${NC} %s" "continue? (y/n)" ": "
read -n 1 CONTINUE
done
if test "$CONTINUE" = "n"
then
# delete downloaded zip file
printf "\n%-30s %s" "delete downloaded file" ": "
rm $WEB_PATH/$MOODLE_DOWNLOAD_FILE
checkstatus
printf "\n${LIGHTGREEN}%s${NC} \n\n" "goodbye."
exit 11
fi
# user wants to continue
printf "\n"
# moodle -> maintenance mode -> enable
printf "%-30s %s" "enable maintenance" ": "
$PHP_PATH $MOODLE_PATH_REAL/admin/cli/maintenance.php --enableold > /dev/null
checkstatus
# time to wait in maintenance mode until work continues
printf "${ORANGE}%-30s${NC} %s" "minutes to wait" ": "
read -r WAIT_FOR_MAINTENANCE
until test $WAIT_FOR_MAINTENANCE -ge 0
do
printf "\n"
printf "${ORANGE}%-30s${NC} %s" "minutes to wait" ": "
read -r WAIT_FOR_MAINTENANCE
done
printf "%-30s %s" "waiting ..." ": "
sleep ${WAIT_FOR_MAINTENANCE}m
printf "${LIGHTGREEN}%s${NC}\n" "OK"
# stay in maintenance mode?
until test "$STAY_IN_MAINTENANCE" = "y" -o "$STAY_IN_MAINTENANCE" = "n"
do
printf "${ORANGE}%-30s${NC} %s" "stay in maintenance (y/n)" ": "
read -n 1 STAY_IN_MAINTENANCE
done
printf "\n"
# create backup
printf "%-30s %s" "moodle backup" ": "
cp -r $MOODLE_PATH_REAL/ $BACKUP_PATH/
checkstatus
# delete old moodle folder
printf "%-30s %s" "delete old moodle" ": "
rm -r "$MOODLE_PATH_REAL"
checkstatus
# unzip
printf "%-30s %s" "unzip to temp folder" ": "
#check if file exists
if test -e $WEB_PATH/$MOODLE_DOWNLOAD_FILE
then
unzip -q $WEB_PATH/$MOODLE_DOWNLOAD_FILE -d $MOODLE_TEMP_PATH
checkstatus
else
printf "%s \n" "FAILED: no file to unzip!" 1>&2
exit 13
fi
printf "%-30s %s" "move to version folder" ": "
mv $MOODLE_TEMP_PATH/moodle/ $MOODLE_NEXT_PATH/
checkstatus
# clean up
printf "%-30s %s" "delete temp folder" ": "
rm -r $MOODLE_TEMP_PATH/
checkstatus
printf "%-30s %s" "delete downloaded file" ": "
rm $MOODLE_DOWNLOAD_FILE
checkstatus
# copy config
printf "%-30s %s" "copy: config.php" ": "
if test -e $BACKUP_PATH/config.php
then
cp $BACKUP_PATH/config.php $MOODLE_NEXT_PATH/config.php
checkstatus
else
printf "${LIGHTRED}%s${NC} \n" "file missing" 1>&2
exit 12
fi
# copy plugins
printf "\n%-30s %s" "previous plugins" ": "
# create lists of all plugins
$(cd $BACKUP_PATH; find . -name 'version.php' > /tmp/moodle-update-diff-list_old.txt)
$(cd $MOODLE_NEXT_PATH; find . -name 'version.php' > /tmp/moodle-update-diff-list_new.txt)
# diff which plugins are only in the old moodle directory
PLUGIN_DIFF=$(diff /tmp/moodle-update-diff-list_old.txt /tmp/moodle-update-diff-list_new.txt | grep "<")
# clean up
PLUGIN_DIFF=${PLUGIN_DIFF//< .\//}
PLUGIN_DIFF=${PLUGIN_DIFF///version.php/}
echo $PLUGIN_DIFF > /tmp/moodle-update-diff-list_final.txt
# create array of missing plugins
PLUGIN_DIFF_ARRAY=($PLUGIN_DIFF)
# print array
printf "${LIGHTCYAN}%d${NC}\n" "${#PLUGIN_DIFF_ARRAY[*]}"
# choose copy mode
until test "$COPYALL" = "y" -o "$COPYALL" = "n"
do
printf "${ORANGE}%-30s${NC} %s" "copy ALL plugins? (y/n)" ": "
read -n 1 COPYALL
echo " "
done
# iterate through missing plugins
for PLUGIN_PATH in ${PLUGIN_DIFF_ARRAY[@]}
do
# pre-define user input if copy mode is set to ALL
if test "$COPYALL" = "y"
then
CONTINUE="y"
fi
# copy folder if wanted
until test "$CONTINUE" = "y" -o "$CONTINUE" = "n"
do
printf "%-66s %s" "Copy $PLUGIN_PATH? (y/n)" ": "
read -n 1 -s CONTINUE
done
# copy yes
if test "$CONTINUE" = "y"
then
# create parent folder if missing
if test ! -d $MOODLE_NEXT_PATH/$PLUGIN_PATH
then
mkdir -p $MOODLE_NEXT_PATH/$PLUGIN_PATH
fi
cp -r $BACKUP_PATH/$PLUGIN_PATH/ $MOODLE_NEXT_PATH/${PLUGIN_PATH%/*}
if test "$COPYALL" = "y"
then
printf "%-46s %s" "Copy $PLUGIN_PATH" ": "
fi
checkstatus
else
printf "%s \n" "SKIPPED"
fi
# set to default
CONTINUE="null"
done
# update sym link
printf "\n"
printf "%-46s %s" "update symlink" ": "
rm $MOODLE_PATH && ln -s $MOODLE_NEXT_PATH $MOODLE_PATH
checkstatus
# update moodle
printf "%-30s \n" "update moodle ..."
$PHP_PATH $MOODLE_PATH/admin/cli/upgrade.php --non-interactive
if test $? -ne 0
then
printf "\n"
printf "%-30s %s ${LIGHTRED}%s${NC}\n" "upgrade moodle" ":" " FAILED" 1>&2
exit 13
else
printf "\n"
printf "%-30s %s ${LIGHTGREEN}%s${NC}\n" "upgrade moodle" ":" "OK"
fi
# modify license lang strings
printf "%-30s %s" "language strings (en)" ": "
grep 'ungeprueft' $LANG_LICENSE_EN > /dev/null
if test $? -eq 1
then
echo "\$string['freielizenz'] = 'Freie Lizenz';" >> $LANG_LICENSE_EN
echo "\$string['52a'] = 'VG Wort';" >> $LANG_LICENSE_EN
echo "\$string['gemeinfrei'] = 'Gemeinfrei';" >> $LANG_LICENSE_EN
echo "\$string['ungeprueft'] = 'Ungeprüft';" >> $LANG_LICENSE_EN
printf "${LIGHTGREEN}%s${NC}\n" "OK"
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
fi
printf "%-30s %s" "custom language strings (de)" ": "
grep 'ungeprueft' $LANG_LICENSE_DE > /dev/null
if test $? -eq 1
then
echo "\$string['freielizenz'] = 'Freie Lizenz';" >> $LANG_LICENSE_DE
echo "\$string['52a'] = 'VG Wort';" >> $LANG_LICENSE_DE
echo "\$string['gemeinfrei'] = 'Gemeinfrei';" >> $LANG_LICENSE_DE
echo "\$string['ungeprueft'] = 'Ungeprüft';" >> $LANG_LICENSE_DE
printf "${LIGHTGREEN}%s${NC}\n" "OK"
else
printf "${LIGHTGREEN}%s${NC}\n" "OK"
fi
# php exec
printf "%-30s %s" "make cli scripts executable" ": "
chmod +x $MOODLE_PATH/admin/cli/*.php
checkstatus
# set rights
printf "%-30s %s" "update permissions" ": "
chown -R "$WEBUSER:$WEBUSER" $MOODLE_NEXT_PATH && chown -R "$WEBUSER:$WEBUSER" $MOODLE_PATH
checkstatus
# moodle -> maintenance mode -> disable
if test "$STAY_IN_MAINTENANCE" = "n"
then
printf "%-30s %s" "disable maintenance" ": "
$PHP_PATH $MOODLE_PATH/admin/cli/maintenance.php --disable > /dev/null
checkstatus
else
printf "%-30s %s ${LIGHTGREEN}%s${NC}\n" "stay in maintenance" ":" "OK"
fi
# delete cache
printf "%-30s %s" "purge cache" ": "
$PHP_PATH $MOODLE_PATH/admin/cli/purge_caches.php
checkstatus
# the end
printf "\n\n${LIGHTGREEN}%s${NC}\n\n" "All jobs done."