Skip to content

Add support for ./moodle-docker.env #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@ to the instance you expect to. See
[envvars](https://docs.docker.com/compose/reference/envvars/)
to see more about `docker-compose` environment variables.

## Environment setting defaults via ./moodle-docker.env

In Linux and macOS it is also possible to created `moodle-docker.env` file
in directory where `bin/moodle-docker-compose` is executed, the file contents are
then used as default environment values.

If `moodle-docker.env` is in Moodle code root, then MOODLE_DOCKER_WWWROOT default
is set to Moodle directory and COMPOSE_PROJECT_NAME defaults to Moodle directory name.

Example:

1. Create `/path/to/moodle/moodle-docker.env` file with the following content:
```
# file location /path/to/moodle/moodle-docker.env
MOODLE_DOCKER_DB=pgsql
```
2. Copy config.docker-template.php file to `/path/to/moodle/config.php`
3. Start containers:
```bash
cd /path/to/moodle
/path/to/moodle-docker/bin/moodle-docker-compose up -d
```
4. Switch to MariaDB temporarily:
```bash
cd /path/to/moodle
/path/to/moodle-docker/bin/moodle-docker-compose down
export MOODLE_DOCKER_DB=mariadb
/path/to/moodle-docker/bin/moodle-docker-compose up -d
```

## Use containers for running behat tests

```bash
Expand Down
28 changes: 28 additions & 0 deletions bin/include/env.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@ECHO OFF

IF EXIST "moodle-docker.env" (
FOR /F "usebackq tokens=1* delims== eol=#" %%i IN (moodle-docker.env) DO (
IF NOT DEFINED %%i (
SET %%i=%%j
)
)

IF EXIST "lib/moodlelib.php" (
IF "%MOODLE_DOCKER_WWWROOT%"=="" (
SET MOODLE_DOCKER_WWWROOT=%cd%
)
IF "%COMPOSE_PROJECT_NAME%"=="" (
FOR %%* IN (.) DO SET COMPOSE_PROJECT_NAME=%%~nx*
)
)
)

IF NOT EXIST "%MOODLE_DOCKER_WWWROOT%" (
ECHO Error: MOODLE_DOCKER_WWWROOT is not set or not an existing directory
EXIT /B 1
)

IF "%MOODLE_DOCKER_DB%"=="" (
ECHO Error: MOODLE_DOCKER_DB is not set
EXIT /B 1
)
32 changes: 32 additions & 0 deletions bin/include/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

# If moodle-docker.env file found then use it as default environment values.
filename="moodle-docker.env"
if [ -f $filename ]; then
envbackup=$( export -p)
export $(grep -v '^#' $filename | xargs)
eval "$envbackup"
if [ -z "$MOODLE_DOCKER_WWWROOT" ] && [ -f 'lib/moodlelib.php' ];
then
# We know that moodle is in current directory, so use it as default value.
currentdir="$( pwd -P )";
export MOODLE_DOCKER_WWWROOT="$currentdir";
fi
if [ -z "$COMPOSE_PROJECT_NAME" ] && [ -f 'lib/moodlelib.php' ];
then
# Use moodle directory name as default Compose project name.
name="$( basename "$( pwd -P )" )"
export COMPOSE_PROJECT_NAME="$name";
fi
fi

if [ ! -d "$MOODLE_DOCKER_WWWROOT" ]; then
echo 'Error: MOODLE_DOCKER_WWWROOT is not set or not an existing directory'
exit 1
fi

if [ -z "$MOODLE_DOCKER_DB" ];
then
echo 'Error: MOODLE_DOCKER_DB is not set'
exit 1
fi
13 changes: 2 additions & 11 deletions bin/moodle-docker-compose
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,8 @@ set -e
thisfile=$( readlink "${BASH_SOURCE[0]}" ) || thisfile="${BASH_SOURCE[0]}"
basedir="$( cd "$( dirname "$thisfile" )/../" && pwd -P )"

if [ ! -d "$MOODLE_DOCKER_WWWROOT" ];
then
echo 'Error: $MOODLE_DOCKER_WWWROOT is not set or not an existing directory'
exit 1
fi

if [ -z "$MOODLE_DOCKER_DB" ];
then
echo 'Error: $MOODLE_DOCKER_DB is not set'
exit 1
fi
# Load all environment settings.
source "${basedir}/bin/include/env.sh"

export ASSETDIR="${basedir}/assets"

Expand Down
13 changes: 4 additions & 9 deletions bin/moodle-docker-compose.cmd
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
@ECHO OFF

IF NOT EXIST "%MOODLE_DOCKER_WWWROOT%" (
ECHO Error: MOODLE_DOCKER_WWWROOT is not set or not an existing directory
EXIT /B 1
)

IF "%MOODLE_DOCKER_DB%"=="" (
ECHO Error: MOODLE_DOCKER_DB is not set
EXIT /B 1
)
setlocal

PUSHD %cd%
CD %~dp0..
SET BASEDIR=%cd%
POPD

call %BASEDIR%\bin\include\env.cmd || EXIT /B 1

SET ASSETDIR=%BASEDIR%\assets

SET COMPOSE_CONVERT_WINDOWS_PATHS=true
Expand Down
3 changes: 3 additions & 0 deletions bin/moodle-docker-wait-for-app
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ set -e
thisfile=$( readlink "${BASH_SOURCE[0]}" ) || thisfile="${BASH_SOURCE[0]}"
basedir="$( cd "$( dirname "$thisfile" )/../" && pwd -P )"

# Load all environment settings.
source "${basedir}/bin/include/env.sh"

if [[ ! -z "$MOODLE_DOCKER_BROWSER" ]] && [[ "$MOODLE_DOCKER_BROWSER" == "chrome" ]] && ([[ ! -z "$MOODLE_DOCKER_APP_PATH" ]] || [[ ! -z "$MOODLE_DOCKER_APP_VERSION" ]] || [[ ! -z "$MOODLE_APP_VERSION" ]]);
then
until $basedir/bin/moodle-docker-compose logs moodleapp | grep -q -E 'dev server running: |Angular Live Development Server is listening|Configuration complete; ready for start up';
Expand Down
7 changes: 2 additions & 5 deletions bin/moodle-docker-wait-for-db
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ set -e
thisfile=$( readlink "${BASH_SOURCE[0]}" ) || thisfile="${BASH_SOURCE[0]}"
basedir="$( cd "$( dirname "$thisfile" )/../" && pwd -P )"

if [ -z "$MOODLE_DOCKER_DB" ];
then
echo 'Error: $MOODLE_DOCKER_DB is not set'
exit 1
fi
# Load all environment settings.
source "${basedir}/bin/include/env.sh"

if [ "$MOODLE_DOCKER_DB" = "mssql" ];
then
Expand Down