-
Notifications
You must be signed in to change notification settings - Fork 5
/
new_test_site
executable file
·287 lines (226 loc) · 6.93 KB
/
new_test_site
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
#!/bin/bash
# Create a Moodle site on a CentOS server running Apache and MySQL/MariaDB.
# PHP and MySQL/MariaDB must be alread installed and configured.
# Leon Stringer <[email protected]>
local_repo=/usr/share/repos/moodle
branch=MOODLE_405_STABLE
upstream_repo=git://git.moodle.org/moodle.git
tag=""
verbose=0
OPTIND=1
function show_help () {
cat << EOF
Usage: new_test_site [[-b branch_ver] | [-t tag]] [-g git_repo] domain-name
-b branch_ver Optional major Moodle version, e.g. 35 for Moodle 3.5.
If not specified $branch is used. Minimum: 25 (for
Moodle 2.5). Cannot be used with -t.
-t tag Optional Moodle minor version, e.g. v3.5.1 Cannot be
used with -b.
-g git_repo Optional upstream Git repository to use. By default
$upstream_repo is used. If the source code
directory is present this is used, avoiding Git clone.
-v Verbose output.
Examples:
new_test_site moodle1.example.com
new_test_site -b 35 -g https://github.com/moodle/moodle.git \\
moodle2.example.com
new_test_site -t v3.6.2 moodle3.example.com
EOF
}
function log () {
if [ "$verbose" -eq "1" ]; then
echo "$@"
fi
}
function dbtype () {
local dbname=$1
local dbpass=$2
dbversion=`mysql -u $dbname -p$dbpass -BN --execute="SELECT VERSION()" 2> /dev/null`
if [[ $dbversion == *"MariaDB" ]]; then
dbtype="mariadb"
else
dbtype="mysqli"
fi
}
while getopts "g:b:t:h?v" opt; do
case "$opt" in
b) branch=MOODLE_${OPTARG}_STABLE
;;
g) upstream_repo=$OPTARG
;;
t) tag=$OPTARG
regex="v([0-9]+)\.([0-9]+)\."
if [[ $tag =~ $regex ]]; then
branch=MOODLE_${BASH_REMATCH[1]}${BASH_REMATCH[2]}_STABLE
else
>&2 echo "Error: Tag $tag does not match 'vX.Y.Z'."
exit 1
fi
;;
h|\?) show_help
exit 0
;;
v) verbose=1
;;
esac
done
shift $((OPTIND-1))
if [ "$#" -ne 1 ]; then
>&2 echo "Error: Missing domain name."
exit 1
fi
which openssl > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
>&2 echo "Error: openssl not found on path"
exit 1
fi
apache_conf_dir=/etc/httpd/conf.d
cert_dir=/etc/pki/tls/certs
key_dir=/etc/pki/tls/private
www_user=apache
www_service=httpd
key_file_mode=600
key_owner_g=root
php_fpm_conf=/etc/php-fpm.d/www.conf
os_id=`grep -h "^ID=" /etc/*-release | cut -d'=' -f 2`
if [ "$os_id" == "debian" ]; then
log "Using file system locations for Debian."
apache_conf_dir=/etc/apache2/sites-enabled
cert_dir=/etc/ssl/certs
key_dir=/etc/ssl/private
www_user=www-data
www_service=apache2
key_file_mode=640
key_owner_g=ssl-cert
else
log "Using file system locations for CentOS/RHEL/Fedora."
fi
if [ -f "$php_fpm_conf" ]; then # Is there a PHP-FPM config file?
# Then attempt to read the user setting.
fpm_user=`grep -E "^user = [^;\s]+" /etc/php-fpm.d/www.conf | sed 's/^user\s\+=\s\+//'`
if [ "$?" -eq "0" ] && [ ! -z "$fpm_user" ]; then
www_user=$fpm_user
fi
fi
log "PHP appears to be accessing files as user '$www_user'."
domain_name=$1
conf_file=$apache_conf_dir/$domain_name.conf
cert_file=$cert_dir/$domain_name.crt
key_file=$key_dir/$domain_name.key
sitedir=/var/www/$domain_name
moodle=$sitedir/moodle
dataroot=$sitedir/moodledata
dbname=${domain_name//./_}
dbpass=`openssl rand -base64 12` # https://unix.stackexchange.com/a/306107
lang=${LANG:0:2}
log "Language detected: $lang"
if [ -e "$conf_file" ]; then
>&2 echo "Error: $conf_file already exists."
exit 1
fi
if [ -e "$cert_file" ]; then
>&2 echo "Error: $cert_file already exists."
exit 1
fi
if [ -e "$key_file" ]; then
>&2 echo "Error: $key_file already exists."
exit 1
fi
log "Creating https://$domain_name"
mysql -u root -p --execute="CREATE DATABASE \`$dbname\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;CREATE USER '$dbname'@'localhost' IDENTIFIED BY '$dbpass';GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON \`$dbname\`.* TO '$dbname'@'localhost'"
if [ "$?" -ne "0" ]; then
>&2 echo "Error: Unable to create database or database user."
exit 1
fi
if [ -d "$sitedir" ]; then
mkdir $dataroot
else
mkdir $sitedir $dataroot
fi
if [ "$?" -ne "0" ]; then
>&2 echo "Error: Unable to create directories."
exit 1
fi
chown $www_user: $dataroot
selinux_status=`getenforce 2> /dev/null`
if [ "$?" -eq "0" ] && [ "$selinux_status" == "Enforcing" ]; then
semanage fcontext -a -t httpd_sys_rw_content_t "$dataroot"
restorecon -v "$dataroot" > /dev/null
fi
# If there's a moodle dir, assume this is a source code dir. If this is an
# existing site the config.php will stop install.php (and if there's an
# existing database with the same name then the CREATE DATABASE would have
# failed previously).
if [ ! -d "$moodle" ]; then
git_clone_args=("-q")
# Is there a local Git repo for Moodle?
if [ -d "$local_repo" ]; then
git_clone_args+=("--reference" "$local_repo")
fi
git_clone_args+=("$upstream_repo")
if [ ! -z "$tag" ]; then
git_clone_args+=("--branch" $tag)
else
git_clone_args+=("--branch" $branch)
fi
git_clone_args+=("$moodle")
git clone ${git_clone_args[@]}
fi
if [ "$?" -ne "0" ]; then
>&2 echo "Error: Git clone failed."
exit 1
fi
install_args=("--wwwroot=https://$domain_name" "--dbname=$dbname" "--dbuser=$dbname" "--dbpass=$dbpass" "--fullname=Test Site" "--shortname=Home" "--adminpass=Password-1" "--agree-license" "--non-interactive" "--lang=$lang")
# Initialise $dbtype to mariadb or mysqli as required
dbtype $dbname $dbpass
# dbtype mariadb introduced with Moodle 2.6
if [ "$branch" == "MOODLE_25_STABLE" ]; then
dbtype="mysqli"
fi
install_args+=("--dbtype=$dbtype")
# adminemail introduced in Moodle 2.9, check version is not older.
old_versions="MOODLE_2[5-8]_STABLE"
if [[ ! "$branch" =~ $old_versions ]]; then
install_args+=("[email protected]")
fi
installoutput=$(mktemp -t "$(basename $0).XXXXXXXXXX")
php -f $sitedir/moodle/admin/cli/install.php -- "${install_args[@]}" > $installoutput
if [ "$?" -ne "0" ]; then
>&2 echo "Error: Install command failed."
tail $installoutput
rm -f $installoutput
exit 1
fi
rm -f $installoutput
chown $www_user $sitedir/moodle/config.php
openssl req -x509 -newkey rsa:4096 -keyout $key_file -out $cert_file -days 720 -nodes -subj "/CN=$domain_name" > /dev/null
if [ "$?" -ne "0" ]; then
>&2 echo "Error: Unable to create self-signed certificates."
exit 1
fi
chmod $key_file_mode $key_file
chown root:$key_owner_g $key_file
cat << EOF > $conf_file
<VirtualHost *:443>
ServerName $domain_name
DocumentRoot $sitedir/moodle
SSLEngine on
SSLCertificateFile $cert_file
SSLCertificateKeyFile $key_file
</VirtualHost>
EOF
if [ "$?" -ne "0" ]; then
>&2 echo "Error: Unable to create Apache config file."
exit 1
fi
apachectl configtest > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
>&2 echo "Error: Apache configuration error."
exit 1
fi
systemctl reload $www_service
if [ "$?" -ne "0" ]; then
>&2 echo "Error: Error reloading Apache."
exit 1
fi
log "Completed successfully."