Skip to content

Commit

Permalink
Add custom commands
Browse files Browse the repository at this point in the history
  • Loading branch information
odeialba committed Jan 20, 2022
1 parent 8c59c0b commit 1f5598e
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 0 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,76 @@ bin/moodle-docker-compose stop
bin/moodle-docker-compose start
```

## Custom commands

### moodle-docker-bash
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:
```bash
~$ bin/moodle-docker-bash webserver php -v
PHP 7.4.23 (cli) (built: Sep 3 2021 18:14:02) ( NTS )
```
```bash
~$ bin/moodle-docker-bash db psql --version
psql (PostgreSQL) 11.13 (Debian 11.13-1.pgdg90+1)
```

### mbash
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:
```bash
~$ bin/mbash php -v
PHP 7.4.23 (cli) (built: Sep 3 2021 18:14:02) ( NTS )
```

### minstall
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:
```bash
~$ bin/mbash minstall moodle --fullname="Moodle first instance" --adminpass="admin"
-------------------------------------------------------------------------------
== Setting up database ==
-->System
```
```bash
~$ bin/mbash minstall phpunit
Initialising Moodle PHPUnit test environment...
```
```bash
~$ bin/mbash minstall behat
You are already using the latest available Composer version 2.1.8 (stable channel).
Installing dependencies from lock file (including require-dev)
```

### mtest
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:
```bash
~$ bin/mbash mtest phpunit --filter auth_manual_testcase
Moodle 3.11.3 (Build: 20210913), 8c02bd32af238dfc83727fb4260b9caf1b622fdb
Php: 7.4.23, pgsql: 11.13 (Debian 11.13-1.pgdg90+1), OS: Linux 5.10.47-linuxkit x86_64
```
```bash
~$ bin/mbash mtest behat --tags=@auth_manual
Running single behat site:
```

### mutil
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:
```bash
~$ bin/mbash mutil phpunit --drop
Purging dataroot:
Dropping tables:
```
```bash
~$ bin/mbash mutil behat --drop
Dropping tables:
```

### mfixversion
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:
```bash
~$ bin/mbash mfixversion
-------------------------------------------------------------------------------
== Resetting all version numbers ==
```

## Environment variables

You can change the configuration of the docker images by setting various environment variables **before** calling `bin/moodle-docker-compose up`.
Expand Down
21 changes: 21 additions & 0 deletions assets/web/commands/mcommon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
command=''
quoteopen='no'
for (( i=2; i<=$#; i++)); do
part=${!i}
if [[ "$quoteopen" == 'yes' && "$part" == "-"* ]]; then
command="$command\""
quoteopen='no'
fi
eqsign="${part//[^=]}"
if [ ${#eqsign} -eq 1 ]; then
part="${part//=/=\"}"
quoteopen='yes'
fi
command="$command $part"
done

if [ "$quoteopen" == 'yes' ]; then
command="$command\""
quoteopen='no'
fi
4 changes: 4 additions & 0 deletions assets/web/commands/mfixversion
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
eval "cp ../scripts/fixversions.php fixversions.php"
eval "php fixversions.php"
eval "rm fixversions.php"
20 changes: 20 additions & 0 deletions assets/web/commands/minstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/mcommon"

case "$1" in
moodle)
eval "php admin/cli/install_database.php --agree-license --fullname=\"Moodle\" --shortname=\"moodle\" --summary=\"Moodle site\" --adminpass=\"admin\" --adminemail=\"[email protected]\" ${command}"
;;
phpunit)
eval "php admin/tool/phpunit/cli/init.php ${command}"
;;
behat)
eval "php admin/tool/behat/cli/init.php -a -o ${command}"
;;
*)
SCRIPT_NAME=`basename "$0"`
echo "Usage: $SCRIPT_NAME {moodle|phpunit|behat} [arguments]"
exit 1
esac
17 changes: 17 additions & 0 deletions assets/web/commands/mtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/mcommon"

case "$1" in
phpunit)
eval "vendor/bin/phpunit ${command}"
;;
behat)
eval "php admin/tool/behat/cli/run.php ${command}"
;;
*)
SCRIPT_NAME=`basename "$0"`
echo "Usage: $SCRIPT_NAME {phpunit|behat} [arguments]"
exit 1
esac
17 changes: 17 additions & 0 deletions assets/web/commands/mutil
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/mcommon"

case "$1" in
phpunit)
eval "php admin/tool/phpunit/cli/util.php ${command}"
;;
behat)
eval "php admin/tool/behat/cli/util.php ${command}"
;;
*)
SCRIPT_NAME=`basename "$0"`
echo "Usage: $SCRIPT_NAME {phpunit|behat} [arguments]"
exit 1
esac
40 changes: 40 additions & 0 deletions assets/web/scripts/fixversions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

define('CLI_SCRIPT', true);
require(getcwd().'/config.php');
require_once($CFG->libdir.'/clilib.php');
require("$CFG->dirroot/version.php");

cli_separator();
cli_heading('Resetting all version numbers');

$manager = core_plugin_manager::instance();

// Purge caches to make sure we have the fresh information about versions.
$manager::reset_caches();
$configcache = cache::make('core', 'config');
$configcache->purge();

$plugininfo = $manager->get_plugins();
foreach ($plugininfo as $type => $plugins) {
foreach ($plugins as $name => $plugin) {
if ($plugin->get_status() !== core_plugin_manager::PLUGIN_STATUS_DOWNGRADE) {
continue;
}

$frankenstyle = sprintf("%s_%s", $type, $name);

mtrace("Updating {$frankenstyle} from {$plugin->versiondb} to {$plugin->versiondisk}");
$DB->set_field('config_plugins', 'value', $plugin->versiondisk, array('name' => 'version', 'plugin' => $frankenstyle));
}
}

// Check that the main version hasn't changed.
if ((float) $CFG->version !== $version) {
set_config('version', $version);
mtrace("Updated main version from {$CFG->version} to {$version}");
}

// Purge relevant caches again.
$manager::reset_caches();
$configcache->purge();
5 changes: 5 additions & 0 deletions base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ version: "2"
services:
webserver:
image: "moodlehq/moodle-php-apache:${MOODLE_DOCKER_PHP_VERSION}"
build:
context: "."
dockerfile: "./dockerfiles/Dockerfilewebserver"
args:
MOODLE_DOCKER_PHP_VERSION: "${MOODLE_DOCKER_PHP_VERSION}"
depends_on:
- db
volumes:
Expand Down
4 changes: 4 additions & 0 deletions bin/mbash
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
eval "$DIR/moodle-docker-compose exec webserver bash -c '${@}'"
4 changes: 4 additions & 0 deletions bin/moodle-docker-bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
eval "$DIR/moodle-docker-compose exec $1 bash -c '${@:2}'"
15 changes: 15 additions & 0 deletions dockerfiles/Dockerfilewebserver
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ARG MOODLE_DOCKER_PHP_VERSION=7.4
FROM moodlehq/moodle-php-apache:${MOODLE_DOCKER_PHP_VERSION}

# Custom commands
COPY assets/web/commands/mcommon /usr/local/bin/mcommon
RUN chmod +x /usr/local/bin/mcommon
COPY assets/web/commands/minstall /usr/local/bin/minstall
RUN chmod +x /usr/local/bin/minstall
COPY assets/web/commands/mutil /usr/local/bin/mutil
RUN chmod +x /usr/local/bin/mutil
COPY assets/web/commands/mtest /usr/local/bin/mtest
RUN chmod +x /usr/local/bin/mtest
COPY assets/web/commands/mfixversion /usr/local/bin/mfixversion
RUN chmod +x /usr/local/bin/mfixversion
COPY assets/web/scripts /var/www/scripts

0 comments on commit 1f5598e

Please sign in to comment.