-
Notifications
You must be signed in to change notification settings - Fork 5
/
latest
executable file
·168 lines (131 loc) · 4.84 KB
/
latest
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
#!/bin/bash
# Upgrade a Moodle site to the latest stable version of the specified branch.
# This script will DELETE EXISTING FILES in the Moodle source code directory.
# Leon Stringer <[email protected]>
# Target version to upgrade to.
branch=400
version=4.0.1
requiresversion=3.6
# Files that are not included in the Moodle source code download but which
# should be kept when upgrading.
filestokeep=(config.php php.ini)
phpbin=/usr/bin/php
moodledir=$PWD
# Write param to stderr.
function error_message () {
>&2 echo $1
}
# Output error message and quit.
function fatal_error () {
error_message "Error: $1"
exit 1
}
if [ ! -f "$moodledir/config.php" ]; then
fatal_error "Cannot find config.php. Is this a Moodle directory?"
fi
# Check existing Moodle version meets the minimum version that can be
# upgraded to the target version.
versioncheck=`php -r "define('CLI_SCRIPT', true);require('config.php');echo version_compare(\\$CFG->release, '$requiresversion');"`
if [ $versioncheck == -1 ]; then
fatal_error "Existing Moodle version is older than $requiresversion."
fi
# Get the Moodledata location (in case this in the source code directory).
moodledata=`$phpbin -r 'define("CLI_SCRIPT", true);require("config.php");echo $CFG->dataroot;'`
# Temporary directory the contents of the Moodle source code directory will be
# moved to.
workingdir=$(mktemp -d "$HOME/moodle.XXXXXXXXXX")
if [ "$?" -ne "0" ]; then
fatal_error "Failed to create temporary directory."
fi
# Get a list of additional (non-core) plugins.
extpluginscript=$(mktemp "$workingdir/XXXXXXXXXX.php")
if [ "$?" -ne "0" ]; then
fatal_error "Failed to create temporary file for PHP script."
fi
# Create a script to report the additional plugins.
cat << 'EOF' > $extpluginscript
<?php
define('CLI_SCRIPT', true);
require('config.php');
$pluginman = core_plugin_manager::instance();
foreach ($pluginman->get_plugins() as $type => $plugins) {
foreach ($plugins as $shortname => $plugin) {
if ($plugin->source == core_plugin_manager::PLUGIN_SOURCE_EXTENSION) {
echo "{$plugin->rootdir}" . PHP_EOL;
}
}
}
EOF
extplugindirs=$(mktemp "$workingdir/plugins.XXXXXXXXXX")
if [ "$?" -ne "0" ]; then
fatal_error "Failed to create temporary file for plugin list."
fi
# Run the script writing a list of directories containing additional plugins
# to file $extplugindirs.
$phpbin $extpluginscript > $extplugindirs
if [ "$?" -ne "0" ]; then
fatal_error "Failed to create temporary file for plugin list."
fi
# Download the target Moodle version's source code.
# There doesn't appear to be a static URL which always points to the latest
# stable version.
downloadurl=https://download.moodle.org/download.php/direct/stable${branch}/moodle-latest-${branch}.tgz
#downloadurl=https://download.moodle.org/download.php/direct/stable${branch}/moodle-${version}.tgz
tarball=$(mktemp "$workingdir/download.XXXXXXXXXX")
if [ "$?" -ne "0" ]; then
fatal_error "Failed to create temporary file for download."
fi
wget -O "$tarball" $downloadurl
if [ "$?" -ne "0" ]; then
fatal_error "Failed to download Moodle."
fi
# Move existing Moodle version's source code files to temporary directory.
mv "$moodledir"/* "$workingdir"
if [ "$?" -ne "0" ]; then
fatal_error "Failed to move source code to temporary directory $workingdir."
fi
# Extract target version's source code into Moodle directory.
tar -C "$moodledir" --strip-components=1 -xzf "$tarball"
if [ "$?" -ne "0" ]; then
fatal_error "Failed to extract $tarball into $moodledir."
fi
successful=true
# For files that must be kept, such as config.php, move these back from the
# temporary directory into Moodle directory.
for file in ${filestokeep[@]}; do
if [ -f "$workingdir/$file" ]; then
mv "$workingdir/$file" "$moodledir"
if [ "$?" -ne "0" ]; then
error "Failed to move $workingdir/$file to $moodledir"
successful=false
fi
fi
done
# Move any external/additional plugins from the temporary directory back to
# the Moodle directory.
while read -r line; do
# If the plugin dir is in the Moodle dir (it should always be).
if [[ "$line" == "$moodledir"* ]]; then
plugindir=${line:${#moodledir}}
mv ${workingdir}${plugindir} ${moodledir}${plugindir}
if [ "$?" -ne "0" ]; then
error "Failed to move ${workingdir}${plugindir} to ${moodledir}${plugindir}"
successful=false
fi
fi
done < $extplugindirs
# Is moodledata a subdirectory in the source code directory? If necessary
# move it back to its original location.
if [[ "$moodledata" == "$moodledir"/* ]]; then
moodledata=${moodledata:${#moodledir}}
# Check if moodledata directory actually got moved. If it's hidden
# (and it should be) then it won't have been moved.
if [ -d "$workingdir/$moodledata" ]; then
mv "$workingdir/$moodledata" "$moodledir"
fi
fi
if ! $success; then
fatal_error "Completed with errors. Leaving behind temporary directory $workingdir."
else
rm -rf "$workingdir"
fi