Skip to content

Commit 717a111

Browse files
committed
MDL-87718: Add support for Composer-installed Moodle
This commit adds support for Moodle sites installed using Composer. To support this we add: - a new module for `moodle-composer` which uses the official composer install script to download and install composer, placing it into `/usr/local/bin`. - a new `COMPOSERINSTALL` environment variable to check whether a composer install was requested - a new `PHPWORKINGDIR` environment variable to hold the install location - an `composer.json` file When no composer installation is requested, no changes are made. When a composer install is requested: - The Moodle codebase is copied to `/var/www/html` (no change) - The `PHPWORKINGDIR` is set to `/var/www/composed` - The `APACHE_DOCUMENT_ROOT` is set to `/var/www/composed/moodle/public` - The `composer.json` file is copied to the `PHPWORKINGDIR` - this file uses `/var/www/html` as a path repository to supply the Moodle code under test - The `PUBLICROOT` is set to `moodle/public` - The official composer install tooling is used to install Composer - The standard config-template.php is copied into `PHPWORKINGDIR` - The `require_once` for `setup.php` is removed from the configuration - Composer is used to install Moodle dependencies
1 parent 59e41cf commit 717a111

File tree

11 files changed

+233
-5
lines changed

11 files changed

+233
-5
lines changed

runner/main/jobtypes/behat/behat.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
# Behat needed variables to go to the env file.
2121
function behat_to_env_file() {
2222
local env=(
23+
COMPOSERINSTALL
24+
PHPWORKINGDIR
25+
PUBLICROOT
26+
2327
DBTYPE
2428
DBTAG
2529
DBHOST
@@ -91,6 +95,8 @@ function behat_to_summary() {
9195
echo "== BEHAT_INCREASE_TIMEOUT: ${BEHAT_INCREASE_TIMEOUT}"
9296
echo "== BEHAT_INIT_ARGS: ${BEHAT_INIT_ARGS}"
9397
echo "== MOODLE_CONFIG: ${MOODLE_CONFIG}"
98+
echo "== PHPWORKINGDIR: ${PHPWORKINGDIR}"
99+
echo "== COMPOSERINSTALL: ${COMPOSERINSTALL}"
94100
if [[ -n "${GOOD_COMMIT}" ]] || [[ -n "${BAD_COMMIT}" ]]; then
95101
echo "== GOOD_COMMIT: ${GOOD_COMMIT}"
96102
echo "== BAD_COMMIT: ${BAD_COMMIT}"
@@ -139,6 +145,7 @@ function behat_modules() {
139145
moodle-core-copy
140146
docker-healthy
141147
docker-summary
148+
moodle-composer
142149
)
143150
echo "${modules[@]}"
144151
}

runner/main/jobtypes/phpunit/phpunit.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# PHPUnit needed variables to go to the env file.
2121
function phpunit_to_env_file() {
2222
local env=(
23+
COMPOSERINSTALL
24+
PHPWORKINGDIR
2325
PUBLICROOT
2426

2527
DBTYPE
@@ -64,6 +66,8 @@ function phpunit_to_summary() {
6466
echo "== PHPUNIT_FILTER: ${PHPUNIT_FILTER}"
6567
echo "== PHPUNIT_TESTSUITE: ${PHPUNIT_TESTSUITE}"
6668
echo "== MOODLE_CONFIG: ${MOODLE_CONFIG}"
69+
echo "== PHPWORKINGDIR: ${PHPWORKINGDIR}"
70+
echo "== COMPOSERINSTALL: ${COMPOSERINSTALL}"
6771
if [[ -n "${GOOD_COMMIT}" ]] || [[ -n "${BAD_COMMIT}" ]]; then
6872
echo "== GOOD_COMMIT: ${GOOD_COMMIT}"
6973
echo "== BAD_COMMIT: ${BAD_COMMIT}"
@@ -103,6 +107,7 @@ function phpunit_modules() {
103107
moodle-core-copy
104108
docker-healthy
105109
docker-summary
110+
moodle-composer
106111
)
107112
echo "${modules[@]}"
108113
}

runner/main/modules/docker-php/docker-php.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function docker-php_setup() {
5454
--name "${WEBSERVER}" \
5555
--detach \
5656
--env-file "${ENVIROPATH}" \
57+
--workdir="${PHPWORKINGDIR}" \
5758
-e "APACHE_DOCUMENT_ROOT=${APACHE_DOCUMENT_ROOT}" \
5859
-v "${COMPOSERCACHE}:/var/www/.composer:rw" \
5960
-v "${SHAREDDIR}":/shared \
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
# Create a new directory for the composer installation.
4+
5+
PHPWORKINGDIR="${PHPWORKINGDIR:-/var/www/composed}"
6+
mkdir -p ${PHPWORKINGDIR}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"type": "project",
3+
"require": {
4+
"moodle/composer-installer": "^1",
5+
"moodle/moodle-composer-scaffold": "^1",
6+
"moodle/moodle": "@dev",
7+
"moodle/moodle-testing": "^1.0"
8+
},
9+
"repositories": [
10+
{
11+
"name": "moodleundertest",
12+
"type": "path",
13+
"url": "/var/www/html"
14+
}
15+
],
16+
"config": {
17+
"allow-plugins": {
18+
"moodle/composer-installer": true,
19+
"moodle/moodle-composer-scaffold": true
20+
}
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
4+
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
5+
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
6+
7+
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
8+
then
9+
>&2 echo 'ERROR: Invalid installer checksum'
10+
rm composer-setup.php
11+
exit 1
12+
fi
13+
14+
php composer-setup.php --quiet
15+
RESULT=$?
16+
rm composer-setup.php
17+
18+
mv composer.phar /usr/local/bin/composer
19+
exit $RESULT
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This file is part of the Moodle Continuous Integration Project.
4+
#
5+
# Moodle is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# Moodle is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with Moodle. If not, see <https://www.gnu.org/licenses/>.
17+
18+
# Moodle module functions for Composer-based Moodle installations.
19+
20+
# This module defines the following env variables.
21+
function moodle-composer_env() {
22+
env=(
23+
)
24+
echo "${env[@]}"
25+
}
26+
27+
# Moodle composer module checks.
28+
function moodle-composer_check() {
29+
# These env variables must be set for the module to work.
30+
verify_env CODEDIR
31+
32+
# We can't verify the WEBSERVER env variable here, as the _check method
33+
# is executed before the docker-php_setup method that defines it.
34+
}
35+
36+
# Moodle composer module config.
37+
function moodle-composer_setup() {
38+
if [[ "${COMPOSERINSTALL}" != "1" ]]; then
39+
return
40+
fi
41+
42+
# Install composer.
43+
docker cp "${BASEDIR}/modules/moodle-composer/install-composer.sh" "${WEBSERVER}":/tmp/install-composer.sh
44+
docker exec -u root "${WEBSERVER}" bash -c "chmod +x /tmp/install-composer.sh ; /tmp/install-composer.sh"
45+
46+
# Create a composer.json.
47+
echo "Copying composer.json to the web server."
48+
docker cp "${BASEDIR}/modules/moodle-composer/composer.json" "${WEBSERVER}":${PHPWORKINGDIR}/composer.json
49+
docker exec -u root "${WEBSERVER}" bash -c "sed -i '/require.*setup.php/d' ${PHPWORKINGDIR}/config.php"
50+
51+
# Configure composer.
52+
docker exec \
53+
"${WEBSERVER}" \
54+
git config --global --add safe.directory /var/www/html
55+
56+
# Run composer install.
57+
echo "Running composer install. This may take a while..."
58+
docker exec \
59+
-u www-data \
60+
"${WEBSERVER}" \
61+
bash -c "composer install --no-interaction --prefer-dist --optimize-autoloader"
62+
echo "Composer install finished"
63+
}

runner/main/modules/moodle-core-copy/moodle-core-copy.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function moodle-core-copy_setup() {
5353
echo "== Copying code in place."
5454
docker cp "${CODEDIR}"/. "${WEBSERVER}":/var/www/html
5555
docker exec "${WEBSERVER}" chown -R www-data:www-data /var/www/html
56+
docker exec "${WEBSERVER}" chown -R www-data:www-data "${PHPWORKINGDIR}"
5657

5758
# TODO: Maybe make this a separate module, so it can be reused in other places.
5859
# Once copied, if we are going to need access to the full history, we'll need to
@@ -82,8 +83,8 @@ function moodle-core-copy_setup() {
8283

8384
# Copy the config.php in place
8485
echo "== Copying configuration in place."
85-
docker cp "${BASEDIR}/modules/docker-php/config.template.php" "${WEBSERVER}":/var/www/html/config.php
86-
docker exec "${WEBSERVER}" chown -R www-data:www-data /var/www/html/config.php
86+
docker cp "${BASEDIR}/modules/docker-php/config.template.php" "${WEBSERVER}":"${PHPWORKINGDIR}/config.php"
87+
docker exec "${WEBSERVER}" chown -R www-data:www-data "${PHPWORKINGDIR}/config.php"
8788

8889
# Copy the plugins in place.
8990
if [[ -n "$PLUGINSTOINSTALL" ]]; then

runner/main/run.sh

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,37 @@ if [[ -z ${WORKSPACE:-} ]]; then
4646
WORKSPACE="${MKTEMP}/workspace"
4747
fi
4848

49+
# Composer install (1 to enable, 0 to disable).
50+
COMPOSERINSTALL="${COMPOSERINSTALL:-}"
51+
52+
# Normalise the COMPOSERINSTALL to 0, or 1.
53+
if [[ -n ${COMPOSERINSTALL} ]] && {
54+
[[ ${COMPOSERINSTALL,,} == "true" ]] || {
55+
[[ ${COMPOSERINSTALL} =~ ^[0-9]+$ ]] && [[ ${COMPOSERINSTALL} -gt 0 ]]; }; }; then
56+
COMPOSERINSTALL=1
57+
else
58+
COMPOSERINSTALL=0
59+
fi
60+
4961
# Base directory where the code is located.
5062
CODEDIR="${CODEDIR:-${WORKSPACE}/moodle}"
5163
APACHE_DOCUMENT_ROOT="${APACHE_DOCUMENT_ROOT:-/var/www/html}"
5264
PUBLICROOT="${PUBLICROOT:-}"
53-
if [[ -d "${CODEDIR}/public" ]]; then
54-
PUBLICROOT="public/"
55-
APACHE_DOCUMENT_ROOT="/var/www/html/public"
65+
66+
# PHP working directory within the container.
67+
# This is used when creating the container, and when running most commands.
68+
PHPWORKINGDIR="${PHPWORKINGDIR:-}"
69+
70+
if [[ "${COMPOSERINSTALL}" -eq 0 ]]; then
71+
PHPWORKINGDIR="${PHPWORKINGDIR:-/var/www/html}"
72+
if [[ -d "${CODEDIR}/public" ]]; then
73+
PUBLICROOT="public/"
74+
APACHE_DOCUMENT_ROOT="/var/www/html/public"
75+
fi
76+
else
77+
PHPWORKINGDIR="${PHPWORKINGDIR:-/var/www/composed}"
78+
PUBLICROOT="moodle/public/"
79+
APACHE_DOCUMENT_ROOT="${PHPWORKINGDIR}/moodle/public"
5680
fi
5781

5882
# Fail if CODEDIR does not exist.

test/behat_test.bats

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,54 @@ teardown() {
9595
assert_output --partial "Running: php public/admin/tool/behat/cli/run.php"
9696
assert_output --partial "== Exit code: 0"
9797
}
98+
99+
100+
@test "Behat tests: run parallel job using Composer" {
101+
# Set all the required variables.
102+
JOBTYPE="behat"
103+
PHP_VERSION="8.4"
104+
DBTYPE="pgsql"
105+
CODEDIR="${MOODLE_CI_RUNNER_GITDIR}"
106+
BEHAT_TAGS="@mod_label&&@core,~@mod_assign"
107+
BEHAT_PARALLEL="2"
108+
COMPOSERINSTALL="1"
109+
110+
# Checkout main
111+
run git_moodle_checkout MDL-87716-main-alt https://github.com/andrewnicols/moodle.git
112+
assert_success
113+
114+
# Run the job
115+
run launch_runner
116+
assert_success
117+
assert_output --partial "== JOBTYPE: behat"
118+
assert_output --partial "== PHP version: 8.4"
119+
assert_output --partial "== DBTYPE: pgsql"
120+
assert_output --partial "== BEHAT_TAGS: @mod_label&&@core,~@mod_assign"
121+
assert_output --partial "== DBREPLICAS: 0"
122+
assert_output --partial "== Exit code: 0"
123+
}
124+
125+
@test "Behat tests: run single job using Composer" {
126+
# Set all the required variables.
127+
JOBTYPE="behat"
128+
PHP_VERSION="8.4"
129+
DBTYPE="pgsql"
130+
CODEDIR="${MOODLE_CI_RUNNER_GITDIR}"
131+
BEHAT_TAGS="@mod_label&&@core,~@mod_assign"
132+
BEHAT_PARALLEL="1"
133+
COMPOSERINSTALL="1"
134+
135+
# Checkout main
136+
run git_moodle_checkout MDL-87716-main-alt https://github.com/andrewnicols/moodle.git
137+
assert_success
138+
139+
# Run the job
140+
run launch_runner
141+
assert_success
142+
assert_output --partial "== JOBTYPE: behat"
143+
assert_output --partial "== PHP version: 8.4"
144+
assert_output --partial "== DBTYPE: pgsql"
145+
assert_output --partial "== BEHAT_TAGS: @mod_label&&@core,~@mod_assign"
146+
assert_output --partial "== DBREPLICAS: 0"
147+
assert_output --partial "== Exit code: 0"
148+
}

0 commit comments

Comments
 (0)