Skip to content

Commit 1f5598e

Browse files
committed
Add custom commands
1 parent 8c59c0b commit 1f5598e

File tree

11 files changed

+217
-0
lines changed

11 files changed

+217
-0
lines changed

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,76 @@ bin/moodle-docker-compose stop
190190
bin/moodle-docker-compose start
191191
```
192192

193+
## Custom commands
194+
195+
### moodle-docker-bash
196+
This script was created to easily run any command inside any container. First parameter will be the container name and second one will be the command. Example:
197+
```bash
198+
~$ bin/moodle-docker-bash webserver php -v
199+
PHP 7.4.23 (cli) (built: Sep 3 2021 18:14:02) ( NTS )
200+
```
201+
```bash
202+
~$ bin/moodle-docker-bash db psql --version
203+
psql (PostgreSQL) 11.13 (Debian 11.13-1.pgdg90+1)
204+
```
205+
206+
### mbash
207+
As most of the commands using the `moodle-docker-bash` script will be run on the `webserver` container, this is a shortcut of that script that runs the commands only in the `webserver` container. Example:
208+
```bash
209+
~$ bin/mbash php -v
210+
PHP 7.4.23 (cli) (built: Sep 3 2021 18:14:02) ( NTS )
211+
```
212+
213+
### minstall
214+
This script was created to be automatically installed in the webserver container and to easily run any install command. First parameter will be the database to install (moodle, phpunit or behat) and the rest will be all the parameters that want to be used to override the default one. Note that this script needs to be run either withing the container shell or using `moodle-docker-bash`. Examples:
215+
```bash
216+
~$ bin/mbash minstall moodle --fullname="Moodle first instance" --adminpass="admin"
217+
-------------------------------------------------------------------------------
218+
== Setting up database ==
219+
-->System
220+
```
221+
```bash
222+
~$ bin/mbash minstall phpunit
223+
Initialising Moodle PHPUnit test environment...
224+
```
225+
```bash
226+
~$ bin/mbash minstall behat
227+
You are already using the latest available Composer version 2.1.8 (stable channel).
228+
Installing dependencies from lock file (including require-dev)
229+
```
230+
231+
### mtest
232+
This script was created to be automatically installed in the webserver container and to easily run any test command. First parameter will be the tests to be run (phpunit or behat) and the rest will be all the parameters that want to be used to override the default ones. Note that this script needs to be run either withing the container shell or using `moodle-docker-bash`. Examples:
233+
```bash
234+
~$ bin/mbash mtest phpunit --filter auth_manual_testcase
235+
Moodle 3.11.3 (Build: 20210913), 8c02bd32af238dfc83727fb4260b9caf1b622fdb
236+
Php: 7.4.23, pgsql: 11.13 (Debian 11.13-1.pgdg90+1), OS: Linux 5.10.47-linuxkit x86_64
237+
```
238+
```bash
239+
~$ bin/mbash mtest behat --tags=@auth_manual
240+
Running single behat site:
241+
```
242+
243+
### mutil
244+
This script was created to be automatically installed in the webserver container and to easily access the `util.php` files of phpunit and behat. First parameter will be the test environment (phpunit or behat) and the rest will be all the parameters that want to be used to override the default ones. Note that this script needs to be run either withing the container shell or using `moodle-docker-bash`. Examples:
245+
```bash
246+
~$ bin/mbash mutil phpunit --drop
247+
Purging dataroot:
248+
Dropping tables:
249+
```
250+
```bash
251+
~$ bin/mbash mutil behat --drop
252+
Dropping tables:
253+
```
254+
255+
### mfixversion
256+
After increasing the version number in a branch, going back to the master branch might cause version problems. This script was created to easily solve that issue. Note that this script needs to be run either withing the container shell or using `moodle-docker-bash`. Example:
257+
```bash
258+
~$ bin/mbash mfixversion
259+
-------------------------------------------------------------------------------
260+
== Resetting all version numbers ==
261+
```
262+
193263
## Environment variables
194264

195265
You can change the configuration of the docker images by setting various environment variables **before** calling `bin/moodle-docker-compose up`.

assets/web/commands/mcommon

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
command=''
3+
quoteopen='no'
4+
for (( i=2; i<=$#; i++)); do
5+
part=${!i}
6+
if [[ "$quoteopen" == 'yes' && "$part" == "-"* ]]; then
7+
command="$command\""
8+
quoteopen='no'
9+
fi
10+
eqsign="${part//[^=]}"
11+
if [ ${#eqsign} -eq 1 ]; then
12+
part="${part//=/=\"}"
13+
quoteopen='yes'
14+
fi
15+
command="$command $part"
16+
done
17+
18+
if [ "$quoteopen" == 'yes' ]; then
19+
command="$command\""
20+
quoteopen='no'
21+
fi

assets/web/commands/mfixversion

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
eval "cp ../scripts/fixversions.php fixversions.php"
3+
eval "php fixversions.php"
4+
eval "rm fixversions.php"

assets/web/commands/minstall

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
DIR="${BASH_SOURCE%/*}"
3+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
4+
. "$DIR/mcommon"
5+
6+
case "$1" in
7+
moodle)
8+
eval "php admin/cli/install_database.php --agree-license --fullname=\"Moodle\" --shortname=\"moodle\" --summary=\"Moodle site\" --adminpass=\"admin\" --adminemail=\"[email protected]\" ${command}"
9+
;;
10+
phpunit)
11+
eval "php admin/tool/phpunit/cli/init.php ${command}"
12+
;;
13+
behat)
14+
eval "php admin/tool/behat/cli/init.php -a -o ${command}"
15+
;;
16+
*)
17+
SCRIPT_NAME=`basename "$0"`
18+
echo "Usage: $SCRIPT_NAME {moodle|phpunit|behat} [arguments]"
19+
exit 1
20+
esac

assets/web/commands/mtest

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
DIR="${BASH_SOURCE%/*}"
3+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
4+
. "$DIR/mcommon"
5+
6+
case "$1" in
7+
phpunit)
8+
eval "vendor/bin/phpunit ${command}"
9+
;;
10+
behat)
11+
eval "php admin/tool/behat/cli/run.php ${command}"
12+
;;
13+
*)
14+
SCRIPT_NAME=`basename "$0"`
15+
echo "Usage: $SCRIPT_NAME {phpunit|behat} [arguments]"
16+
exit 1
17+
esac

assets/web/commands/mutil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
DIR="${BASH_SOURCE%/*}"
3+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
4+
. "$DIR/mcommon"
5+
6+
case "$1" in
7+
phpunit)
8+
eval "php admin/tool/phpunit/cli/util.php ${command}"
9+
;;
10+
behat)
11+
eval "php admin/tool/behat/cli/util.php ${command}"
12+
;;
13+
*)
14+
SCRIPT_NAME=`basename "$0"`
15+
echo "Usage: $SCRIPT_NAME {phpunit|behat} [arguments]"
16+
exit 1
17+
esac

assets/web/scripts/fixversions.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
define('CLI_SCRIPT', true);
4+
require(getcwd().'/config.php');
5+
require_once($CFG->libdir.'/clilib.php');
6+
require("$CFG->dirroot/version.php");
7+
8+
cli_separator();
9+
cli_heading('Resetting all version numbers');
10+
11+
$manager = core_plugin_manager::instance();
12+
13+
// Purge caches to make sure we have the fresh information about versions.
14+
$manager::reset_caches();
15+
$configcache = cache::make('core', 'config');
16+
$configcache->purge();
17+
18+
$plugininfo = $manager->get_plugins();
19+
foreach ($plugininfo as $type => $plugins) {
20+
foreach ($plugins as $name => $plugin) {
21+
if ($plugin->get_status() !== core_plugin_manager::PLUGIN_STATUS_DOWNGRADE) {
22+
continue;
23+
}
24+
25+
$frankenstyle = sprintf("%s_%s", $type, $name);
26+
27+
mtrace("Updating {$frankenstyle} from {$plugin->versiondb} to {$plugin->versiondisk}");
28+
$DB->set_field('config_plugins', 'value', $plugin->versiondisk, array('name' => 'version', 'plugin' => $frankenstyle));
29+
}
30+
}
31+
32+
// Check that the main version hasn't changed.
33+
if ((float) $CFG->version !== $version) {
34+
set_config('version', $version);
35+
mtrace("Updated main version from {$CFG->version} to {$version}");
36+
}
37+
38+
// Purge relevant caches again.
39+
$manager::reset_caches();
40+
$configcache->purge();

base.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ version: "2"
22
services:
33
webserver:
44
image: "moodlehq/moodle-php-apache:${MOODLE_DOCKER_PHP_VERSION}"
5+
build:
6+
context: "."
7+
dockerfile: "./dockerfiles/Dockerfilewebserver"
8+
args:
9+
MOODLE_DOCKER_PHP_VERSION: "${MOODLE_DOCKER_PHP_VERSION}"
510
depends_on:
611
- db
712
volumes:

bin/mbash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
DIR="${BASH_SOURCE%/*}"
3+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
4+
eval "$DIR/moodle-docker-compose exec webserver bash -c '${@}'"

bin/moodle-docker-bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
DIR="${BASH_SOURCE%/*}"
3+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
4+
eval "$DIR/moodle-docker-compose exec $1 bash -c '${@:2}'"

dockerfiles/Dockerfilewebserver

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ARG MOODLE_DOCKER_PHP_VERSION=7.4
2+
FROM moodlehq/moodle-php-apache:${MOODLE_DOCKER_PHP_VERSION}
3+
4+
# Custom commands
5+
COPY assets/web/commands/mcommon /usr/local/bin/mcommon
6+
RUN chmod +x /usr/local/bin/mcommon
7+
COPY assets/web/commands/minstall /usr/local/bin/minstall
8+
RUN chmod +x /usr/local/bin/minstall
9+
COPY assets/web/commands/mutil /usr/local/bin/mutil
10+
RUN chmod +x /usr/local/bin/mutil
11+
COPY assets/web/commands/mtest /usr/local/bin/mtest
12+
RUN chmod +x /usr/local/bin/mtest
13+
COPY assets/web/commands/mfixversion /usr/local/bin/mfixversion
14+
RUN chmod +x /usr/local/bin/mfixversion
15+
COPY assets/web/scripts /var/www/scripts

0 commit comments

Comments
 (0)