From 0ed1248ba83b6b472853c685679f3e8554f7329f Mon Sep 17 00:00:00 2001 From: Odei Alba Date: Wed, 19 Jan 2022 17:37:05 +0100 Subject: [PATCH] Add custom commands --- README.md | 76 ++++++++++++++++++++++++++++++ assets/web/commands/mcommon | 21 +++++++++ assets/web/commands/mfixversion | 4 ++ assets/web/commands/minstall | 20 ++++++++ assets/web/commands/mtest | 17 +++++++ assets/web/commands/mutil | 17 +++++++ assets/web/scripts/fixversions.php | 53 +++++++++++++++++++++ base.yml | 5 ++ bin/mbash | 4 ++ bin/moodle-docker-bash | 4 ++ dockerfiles/Dockerfilewebserver | 15 ++++++ 11 files changed, 236 insertions(+) create mode 100644 assets/web/commands/mcommon create mode 100644 assets/web/commands/mfixversion create mode 100644 assets/web/commands/minstall create mode 100644 assets/web/commands/mtest create mode 100644 assets/web/commands/mutil create mode 100644 assets/web/scripts/fixversions.php create mode 100755 bin/mbash create mode 100755 bin/moodle-docker-bash create mode 100644 dockerfiles/Dockerfilewebserver diff --git a/README.md b/README.md index e6fce983b01..6555e902a0d 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ Notes: * The behat faildump directory is exposed at http://localhost:8000/_/faildumps/. * Use `MOODLE_DOCKER_BROWSER` to switch the browser you want to run the test against. You need to recreate your containers using `bin/moodle-docker-compose` as described below, if you change it. +* Check the [Custom commands](#custom-commands) section for more options. ## Use containers for running phpunit tests @@ -110,6 +111,7 @@ OK (2 tests, 7 assertions) Notes: * If you want to run test with coverage report, use command: `bin/moodle-docker-compose exec webserver phpdbg -qrr vendor/bin/phpunit --coverage-text auth_manual_testcase auth/manual/tests/manual_test.php` +* Check the [Custom commands](#custom-commands) section for more options. ## Use containers for manual testing @@ -122,6 +124,7 @@ Notes: * Moodle is configured to listen on `http://localhost:8000/`. * Mailhog is listening on `http://localhost:8000/_/mail` to view emails which Moodle has sent out. * The admin `username` you need to use for logging in is `admin` by default. You can customize it by passing `--adminuser='myusername'` +* Check the [Custom commands](#custom-commands) section for more options. ## Use containers for running behat tests for the Moodle App @@ -158,6 +161,9 @@ Started at 13-07-2020, 18:34 3m3.17s (55.02Mb) ``` +Notes: +* Check the [Custom commands](#custom-commands) section for more options. + If you are going with the second option, this *can* be used for local development of the Moodle App, given that the `moodleapp` container serves the app on the local 8100 port. However, this is intended to run Behat tests that require interacting with a local Moodle environment. Normal development should be easier calling `npm start` in the host system. By all means, if you don't want to have npm installed locally you can go full Docker executing the following commands before starting the containers: @@ -190,6 +196,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`. diff --git a/assets/web/commands/mcommon b/assets/web/commands/mcommon new file mode 100644 index 00000000000..934a1489ef8 --- /dev/null +++ b/assets/web/commands/mcommon @@ -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 \ No newline at end of file diff --git a/assets/web/commands/mfixversion b/assets/web/commands/mfixversion new file mode 100644 index 00000000000..97f03855282 --- /dev/null +++ b/assets/web/commands/mfixversion @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +eval "cp ../scripts/fixversions.php fixversions.php" +eval "php fixversions.php" +eval "rm fixversions.php" \ No newline at end of file diff --git a/assets/web/commands/minstall b/assets/web/commands/minstall new file mode 100644 index 00000000000..545e20b1587 --- /dev/null +++ b/assets/web/commands/minstall @@ -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=\"admin@example.com\" ${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 \ No newline at end of file diff --git a/assets/web/commands/mtest b/assets/web/commands/mtest new file mode 100644 index 00000000000..e3f8acca27e --- /dev/null +++ b/assets/web/commands/mtest @@ -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 diff --git a/assets/web/commands/mutil b/assets/web/commands/mutil new file mode 100644 index 00000000000..07fa0d2b00f --- /dev/null +++ b/assets/web/commands/mutil @@ -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 \ No newline at end of file diff --git a/assets/web/scripts/fixversions.php b/assets/web/scripts/fixversions.php new file mode 100644 index 00000000000..fc5e77e09ed --- /dev/null +++ b/assets/web/scripts/fixversions.php @@ -0,0 +1,53 @@ +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(); +$needsupgrade = false; +$wasdowngraded = false; + +$plugininfo = $manager->get_plugins(); +foreach ($plugininfo as $type => $plugins) { + foreach ($plugins as $name => $plugin) { + if ($plugin->get_status() !== core_plugin_manager::PLUGIN_STATUS_DOWNGRADE) { + $needsupgrade = $needsupgrade || $plugin->get_status() !== core_plugin_manager::PLUGIN_STATUS_UPTODATE; + 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)); + $wasdowngraded = true; + } +} + +// 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}"); + $wasdowngraded = true; +} else if ('' . $CFG->version !== '' . $version) { + $needsupgrade = true; +} + +if ($wasdowngraded && !$needsupgrade) { + // Update version hash so Moodle doesn't think that we need to run upgrade. + $manager::reset_caches(); + set_config('allversionshash', core_component::get_all_versions_hash()); +} + +// Purge relevant caches again. +$manager::reset_caches(); +$configcache->purge(); \ No newline at end of file diff --git a/base.yml b/base.yml index c407afaafa6..d42e4d443bf 100644 --- a/base.yml +++ b/base.yml @@ -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: diff --git a/bin/mbash b/bin/mbash new file mode 100755 index 00000000000..42563f4191b --- /dev/null +++ b/bin/mbash @@ -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 '${@}'" \ No newline at end of file diff --git a/bin/moodle-docker-bash b/bin/moodle-docker-bash new file mode 100755 index 00000000000..63bbd5814fb --- /dev/null +++ b/bin/moodle-docker-bash @@ -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}'" \ No newline at end of file diff --git a/dockerfiles/Dockerfilewebserver b/dockerfiles/Dockerfilewebserver new file mode 100644 index 00000000000..bd55e5a4b8c --- /dev/null +++ b/dockerfiles/Dockerfilewebserver @@ -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 \ No newline at end of file