-
Notifications
You must be signed in to change notification settings - Fork 13
/
default.nix
370 lines (346 loc) · 11.8 KB
/
default.nix
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
{ pkgs ? import <nixpkgs> {} }:
let
mariadb_data_dir = "./mariadb_data";
mariadb_socket = "/tmp/mysqld.sock";
adminer_port = 8080;
moodle_db_name = "moodle";
moodle_sql_file = "./MoodleSQL.sql";
root_password = "root";
myphp = pkgs.php.buildEnv {extensions = (phpExtensions: with phpExtensions; [
pkgs.php.extensions.dom
pkgs.php.extensions.mbstring
pkgs.php.extensions.tokenizer
pkgs.php.extensions.xmlwriter
pkgs.php.extensions.simplexml
pkgs.php.extensions.opcache
pkgs.php.extensions.iconv
pkgs.php.extensions.mysqli
pkgs.php.extensions.zip
pkgs.php.extensions.gd
pkgs.php.extensions.intl
pkgs.php.extensions.fileinfo
pkgs.php.extensions.sodium
pkgs.php.extensions.ctype
pkgs.php.extensions.bcmath
pkgs.php.extensions.calendar
pkgs.php.extensions.curl
pkgs.php.extensions.exif
pkgs.php.extensions.filter
pkgs.php.extensions.ftp
pkgs.php.extensions.gettext
pkgs.php.extensions.gmp
pkgs.php.extensions.imap
pkgs.php.extensions.ldap
pkgs.php.extensions.openssl
pkgs.php.extensions.posix
pkgs.php.extensions.session
pkgs.php.extensions.soap
pkgs.php.extensions.sockets
pkgs.php.extensions.sysvsem
pkgs.php.extensions.xmlreader
pkgs.php.extensions.zlib
]);
extraConfig = "max_input_vars = 5000
memory_limit = 256M
post_max_size = 50M
upload_max_filesize = 50M";
};
in
pkgs.mkShell {
buildInputs = with pkgs; [
myphp
mariadb
wget
curl
procps
lsof
php.packages.composer
# php.packages.php-codesniffer
glibcLocales
unzip
selenium-server-standalone
geckodriver
git
unixtools.netstat
(if stdenv.isDarwin then null else firefox)
];
shellHook = ''
MOODLE_ROOT="$(realpath server/moodle)"
export LANG="en_AU.UTF-8" #Why does it need to be Australian? Nobody knows...
export LC_ALL="en_AU.UTF-8"
# export PHPRC=`realpath server/php/php.ini`
# Function to check and kill existing processes
kill_existing() {
local process=$1
if pgrep -x "$process" > /dev/null; then
echo "Killing existing $process process..."
pkill -9 -x "$process"
sleep 2
fi
}
# Function to kill process using a specific port
kill_port_user() {
local port=$1
local pid=$(lsof -ti:$port)
if [ ! -z "$pid" ]; then
echo "Killing process using port $port..."
kill -9 $pid
sleep 2
fi
}
if [[ "$OSTYPE" != "darwin"* ]]; then
# Ensure MariaDB data directory exists
mkdir -p ${mariadb_data_dir}
# Initialize MariaDB if not already done
if [ ! -d "${mariadb_data_dir}/mysql" ]; then
mysql_install_db --datadir=${mariadb_data_dir}
# Start MariaDB temporarily to set up the database
mysqld --datadir=${mariadb_data_dir} --socket=${mariadb_socket} --skip-grant-tables &
TEMP_MYSQL_PID=$!
while [ ! -S ${mariadb_socket} ];
do
echo "no socket file. Waiting 1 secs and trying again";
sleep 1
done
echo "MariaDB is up continuing.";
# Set root password and authentication method
echo "Setting root password..."
mysql -uroot -S${mariadb_socket} <<EOF
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY '${root_password}';
FLUSH PRIVILEGES;
SET GLOBAL max_connections = 200;
EOF
# Verify root password
echo "Verifying root password..."
mysql -uroot -p${root_password} -S${mariadb_socket} -e "SELECT 1;" || {
echo "Error: Root password verification failed."
kill -9 $TEMP_MYSQL_PID
wait -9 $TEMP_MYSQL_PID
exit 1
}
mysql -uroot -p${root_password} -S${mariadb_socket} -e "CREATE DATABASE IF NOT EXISTS ${moodle_db_name}" || {
echo "Error: Failed to create database ${moodle_db_name}."
kill -9 $TEMP_MYSQL_PID
wait -9 $TEMP_MYSQL_PID
exit 1
}
if [ -f "${moodle_sql_file}" ]; then
mysql -uroot -p${root_password} -S${mariadb_socket} ${moodle_db_name} -e "source ${moodle_sql_file}" && {
echo "SQL file imported successfully."
} || {
echo "Error: Failed to import SQL file."
}
else
echo "Warning: ${moodle_sql_file} not found. Database created but not populated."
fi
kill -9 $TEMP_MYSQL_PID
wait $TEMP_MYSQL_PID
fi
else
if [ ! -d "mariadb_data" ]; then
unzip mariadb_data.zip
fi
fi
# Kill existing MariaDB and PHP processes
kill_existing "mysqld"
kill_existing "php"
# kill_port_user ${toString adminer_port}
# Start MariaDB
start_mariadb() {
echo "Starting MariaDB..."
mysqld --datadir=${mariadb_data_dir} --socket=${mariadb_socket} &
MARIADB_PID=$!
while [ ! -S ${mariadb_socket} ];
do
echo "no socket file. Waiting 1 secs and trying again";
sleep 1
done
echo "MariaDB is up continuing.";
}
import_db(){
if [ -f "${moodle_sql_file}" ]; then
mysql -uroot -proot -S${mariadb_socket} <<EOF
source ${moodle_sql_file};
EOF
fi
}
# Start Adminer
start_adminer() {
echo "Starting Adminer on port ${toString adminer_port}..."
if [ ! -f "adminer.php" ]; then
echo "Downloading Adminer..."
wget https://github.com/vrana/adminer/releases/download/v4.8.1/adminer-4.8.1.php -O adminer.php
fi
if [ -f "adminer.php" ]; then
echo "Starting Adminer..."
echo "<?php
// Always serve adminer.php regardless of the request URI
require 'adminer.php';
?>" > adminer_router.php
php -S 0.0.0.0:${toString adminer_port} adminer_router.php &
ADMINER_PID=$!
else
echo "Error: Failed to download Adminer. Using a minimal PHP script instead."
echo "<?php
echo '<h1>Adminer Not Found</h1>';
echo '<p>Failed to download Adminer. Here are some debug details:</p>';
echo '<h2>PHP Info:</h2>';
phpinfo();
?>" > adminer_fallback.php
php -S 0.0.0.0:${toString adminer_port} adminer_fallback.php &
ADMINER_PID=$!
fi
}
get_certificate(){
curl --etag-compare etag.txt --etag-save etag.txt --remote-name https://curl.se/ca/cacert.pem -o "cacert.pem"
}
# Start PHP built-in server for Moodle
start_php_server() {
echo "Starting PHP built-in server for Moodle..."
php -S 127.0.0.1:8000 -t ./server/moodle &
PHP_SERVER_PID=$!
}
# Function to stop services
stop_services() {
echo "Stopping services..."
kill -9 $MARIADB_PID $ADMINER_PID $PHP_SERVER_PID $SELENIUM_PID 2>/dev/null
rm -f ${mariadb_socket}
rm -f ./adminer_router.php
}
check_phpunit() {
if [ ! -f "$MOODLE_ROOT/vendor/bin/phpunit" ]; then
echo "no phpunit installed installing it now"
install_phpunit
fi
if [ ! -f "$MOODLE_ROOT/vendor/bin/phpunit" ]; then
echo "installing failed exiting"
exit 1
fi
echo "phpunit found"
}
install_phpunit() {
CURRENT_PATH="$(pwd)"
cd "$MOODLE_ROOT"
composer require --dev phpunit/phpunit
echo "success installing phpunit"
cd "$CURRENT_PATH"
}
create_config(){
if [ ! -f "$MOODLE_ROOT/config.php" ]; then
echo "no config found creating a new one";
php server/moodle/admin/cli/install.php \
--lang=en \
--wwwroot="http://localhost:8000/" \
--dataroot="$(pwd)/server/moodledata" \
--dbtype="mariadb" \
--dbhost="127.0.0.1" \
--dbuser="root" \
--dbpass="root" \
--dbport=3306 \
--dbsocket="${mariadb_socket}" \
--dbname="moodle" \
--prefix="mdl_" \
--non-interactive \
--agree-license \
--skip-database \
--allow-unstable \
--fullname="Moodle testing thing" \
--shortname="mtt" \
--adminpass="hunter2"
else
echo "config found skipping creating a new";
fi
if [ -f "$MOODLE_ROOT/config.php" ]; then
if ! grep -Fxq "\$CFG->phpunit_dataroot = '$(realpath "server/moodledata/phpunit")';" "server/moodle/config.php"; then
echo "adding phpunit to config";
echo "\$CFG->phpunit_prefix = 'phpu_';" >>"server/moodle/config.php"
echo "\$CFG->phpunit_dataroot = '$(realpath "server/moodledata/phpunit")';">>"server/moodle/config.php"
else
echo "phpunit found in config skipping modifying it";
fi
fi
}
runit(){
check_phpunit
create_config
php server/moodle/admin/tool/phpunit/cli/init.php
CURRENT_PATH="$(pwd)"
cd "$MOODLE_ROOT"
for file in `find mod/livequiz/tests/phpunit -type f`
do
echo Running tests in $file
vendor/bin/phpunit --verbose $file
done
cd "$CURRENT_PATH"
}
sniffit(){
if [ ! -d "$MOODLE_ROOT/vendor/moodlehq" ]; then
echo "no codesniffer installed installing it now"
CURRENT_PATH="$(pwd)"
cd $MOODLE_ROOT
composer config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer require moodlehq/moodle-cs
cd "$CURRENT_PATH"
fi
if [ ! -d "$MOODLE_ROOT/vendor/moodlehq" ]; then
echo "codesniffer installed failed exiting"
exit 1
fi
if [[ ":$PATH:" != *"$MOODLE_ROOT/vendor/bin"* ]]; then
export PATH="$MOODLE_ROOT/vendor/bin:$PATH"
fi
phpcs $MOODLE_ROOT/mod/livequiz/
}
fixit(){
phpcbf $MOODLE_ROOT/mod/livequiz/
}
beit(){
CURRENT_PATH="$(pwd)"
cd $MOODLE_ROOT
if [ ! -f "$MOODLE_ROOT/moodle-browser-config/init.php" ]; then
git clone https://github.com/andrewnicols/moodle-browser-config
if [ -d "$MOODLE_ROOT/moodle-browser-config" ] && [ ! -f "$MOODLE_ROOT/moodle-browser-config/init.php" ]; then
cd "$MOODLE_ROOT/moodle-browser-config"
git stash
cd "$MOODLE_ROOT"
fi
fi
cd ../moodledata
BEHAT_PATH="$(pwd)"
mkdir behat
cd $MOODLE_ROOT
if ! grep -Fxq "\$CFG->behat_dataroot = \$CFG->dataroot . '/behat';" "config.php"; then
sed -i "/^require_once(__DIR__ . '\/lib\/setup.php');/i \$CFG->behat_dataroot = \$CFG->dataroot . '/behat';" config.php
sed -i "/^require_once(__DIR__ . '\/lib\/setup.php');/i \$CFG->behat_wwwroot = 'http:\/\/127.0.0.1:8000';" config.php
sed -i "/^require_once(__DIR__ . '\/lib\/setup.php');/i \$CFG->behat_dataroot_parent = \$CFG->dataroot . '\/behat';" config.php
sed -i "/^require_once(__DIR__ . '\/lib\/setup.php');/i \$CFG->behat_prefix = 'beh_';" config.php
sed -i "/^require_once(__DIR__ . '\/lib\/setup.php');/i require_once('$MOODLE_ROOT/moodle-browser-config/init.php');" config.php
else
echo "phpunit found in config skipping modifying it";
fi
php admin/tool/behat/cli/init.php
if ! netstat -tuln | grep -q ':4444'; then
selenium-server &
SELENIUM_PID=$!
fi
vendor/bin/behat --config $BEHAT_PATH/behat/behatrun/behat/behat.yml --profile=firefox --tags @mod_livequiz
cd $CURRENT_PATH
}
# Trap to ensure services are stopped when exiting the shell
trap stop_services EXIT
# Start services
start_mariadb
start_adminer
start_php_server
echo "MariaDB, Adminer, and PHP server are now running."
echo "Adminer is available at http://localhost:${toString adminer_port}"
echo "Moodle is available at http://localhost:8000"
echo "To connect to MariaDB, use:"
echo " Host: 127.0.0.1 or localhost"
echo " Username: root"
echo " Password: ${root_password}"
echo " Database: ${moodle_db_name}"
echo "Press Ctrl+C to stop the services and exit."
'';
}