Skip to content
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

Multiple Moodle sites at the same time - 2004 psql #10

Open
wants to merge 11 commits into
base: 2004-psql
Choose a base branch
from
95 changes: 79 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,96 @@

- Docker
- Docker-compose
- Nginx installed locally

## Usage
## Set up

1. Clone this repository
1. Clone this repository to the folder where all your moodle projects are stored (e.g ~/projects)

```
git clone [email protected]:catalyst/docker_moodle.git docker_moodle
git clone [email protected]:catalyst/docker_moodle.git ~/projects/docker_moodle
```

2. Clone Moodle code into siteroot
2. Set environment variable PROJECTS_DIR to where all your moodle projects are stored (including just cloned docker_moodle)

```
sudo bash -c 'echo "PROJECTS_DIR=/home/dmitriim/projects" >> /etc/environment'
```

```
cd docker_moodle
git clone [email protected]:moodle/moodle.git siteroot
echo "export PROJECTS_DIR=/home/dmitriim/projects" >> ~/.bashrc
source ~/.bashrc
```

3. Copy site config across
2. Symlink control file to your /usr/local/bin/control

```
cp moodle-config siteroot/config.php
sudo ln -s ~/project/docker_moodle/control /usr/local/bin/control
```

3. Make ure that you local nginx server got client_max_body_size set to 8000M

```
sudo vi /etc/nginx/nginx.conf
```

```
http {
...
client_max_body_size 8000M;
...

}
```

4. Set up MailHog locally (it will be used for all your instances). Note: 192.168.56.1 is your local IP address,
and it should be available from your docker containers.

```
docker run --detach --name=mailhog --publish=192.168.56.1:8025:8025 --publish=192.168.56.1:1025:1025 --restart=unless-stopped mailhog/mailhog
```

```
sudo bash -c 'echo "MOODLE_MAIL_HOST=192.168.56.1" >> /etc/environment'
```

```
echo "export MOODLE_MAIL_HOST=192.168.56.1" >> ~/.bashrc
source ~/.bashrc
```

MailHog UI will be available on http://192.168.56.1:8025

## Usage

1. Clone Moodle code to your to where all your moodle projects are stored (e.g ~/projects)

```
git clone [email protected]:moodle/moodle.git ~/projects/moodle
```

2. Create project (a name of the project should match the folder where your moodle code is located)

```
sudo control create moodle
```

4. Start containers

```
docker-compose up
control start moodle
```

5. Stop containers

```
control stop moodle
```

6. Delete project

```
sudo control delete moodle
```

## Utility Commands
Expand All @@ -42,40 +106,39 @@ To change container names, change name in yaml file and control file.
Enter web container:

```
./control web
control web moodle
```

Enter db container:

```
./control db
control db moodle
```

Enter test database container:

```
./control testdb
control testdb moodle
```

Restore db locally:

```
./control dbrestore <filename.gz>
control dbrestore moodle <filename.gz>
```

## Running Tests

To setup the testing environment run:

```
./control web
composer install
control web moodle
php admin/tool/phpunit/cli/init.php
```

To run tests:

```
./control web
control web
./vendor/bin/phpunit
```
156 changes: 148 additions & 8 deletions control
Original file line number Diff line number Diff line change
@@ -1,28 +1,168 @@
#! /bin/bash

## Change to directory of where this script is located
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "$DIR" || exit;
projecsdir=$PROJECTS_DIR

ok="$(tput setaf 2)OK:$(tput sgr0)"
error="$(tput setaf 1)ERROR:$(tput sgr0)"

case "$1" in
web)
webcont="$(docker-compose ps -q moodle)"
webcont="$(docker ps -q -f name=$2_moodle)"
docker exec -ti $webcont bash
;;

db)
dbcont="$(docker-compose ps -q db)"
dbcont="$(docker ps -q -f name=$2_db)"
docker exec -ti $dbcont psql -U moodle_user moodle
;;

testdb)
dbtestcont="$(docker-compose ps -q test-db)"
dbtestcont="$(docker ps -q -f name=$2_test-db)"
docker exec -ti $dbtestcont psql -U moodle_user moodle
;;

dbrestore)
dbcont="$(docker-compose ps -q db)"
gunzip < $2 | docker exec -i $dbcont psql -U moodle_user postgres
dbcont="$(docker ps -q -f name=$2_db)"
gunzip < $3 | docker exec -i $dbcont psql -U moodle_user moodle
;;

dbbackup)
backupfile=$projecsdir/$2-moodle.sql.gz
dbcont="$(docker ps -q -f name=$2_db)"
docker exec -i $dbcont pg_dump -U moodle_user moodle | gzip > $backupfile
echo Backup file is avaliable here $backupfile
;;

start)
export COMPOSE_PROJECT_NAME=$2
export MOODLE_WWWROOT=$projecsdir/$2
export MOODLE_NAME=$2
siteport="$(cat $MOODLE_WWWROOT/siteport~)"
export MOODLE_PORT=$siteport

cd $projecsdir/docker_moodle

docker-compose up
;;

stop)
export COMPOSE_PROJECT_NAME=$2
export MOODLE_WWWROOT=$projecsdir/$2
export MOODLE_NAME=$2
siteport="$(cat $MOODLE_WWWROOT/siteport~)"
export MOODLE_PORT=$siteport

cd $projecsdir/docker_moodle

docker-compose stop
;;

create)
if [ "$(whoami)" != 'root' ]; then
echo $error You have no permission to run $0 as non-root user. Use sudo!
exit 1;
fi

sitehost=$2.docker
siteport="$(shuf -i 500-1000 -n 1)"

export COMPOSE_PROJECT_NAME=$2
export MOODLE_WWWROOT=$projecsdir/$2
export MOODLE_NAME=$2
export MOODLE_PORT=$siteport

cd $projecsdir/docker_moodle

# Backing up exiting config.php
cp $MOODLE_WWWROOT/config.php $MOODLE_WWWROOT/config.php-backup
echo $ok Backed up existing config.php "($MOODLE_WWWROOT/config.php-backup)"

# Creating a new config.ph from the template.
cp moodle-config $MOODLE_WWWROOT/config.php
echo $ok Created config.php "($MOODLE_WWWROOT/config.php)"

# Saving the project port.
echo $siteport > $MOODLE_WWWROOT/siteport~
echo $ok Saved the project port "($MOODLE_WWWROOT/siteport~)"

# Creating a local nginx rule.
if ! echo "
server {
listen 80;
server_name $sitehost;

location / {
proxy_pass http://localhost:$siteport;
}
}
" > /etc/nginx/sites-available/$sitehost
then
echo $error There is an ERROR creating $sitehost file
exit;
else
echo $ok New nginx site created "(/etc/nginx/sites-available/$sitehost)"
fi

# Enabling a local nginx site.
if ! ln -s /etc/nginx/sites-available/$sitehost /etc/nginx/sites-enabled/$sitehost
then
echo $error There is an ERROR creating $sitehost symlink
exit;
else
echo $ok New nginx site enabled "(/etc/nginx/sites-enabled/$sitehost)"
fi

# Add a new host to /etc/hosts.
if ! echo "127.0.0.1 $sitehost" >> /etc/hosts
then
echo $error ERROR: Not able to write in /etc/hosts
exit;
else
echo $ok New host name added to /etc/hosts file "($sitehost)"
fi

service nginx restart
echo $ok Local Nginx restarted
echo $ok Start your project using $0 start $2.
echo $ok To access the project, navigate to http://$sitehost in your browser.
;;

delete)
if [ "$(whoami)" != 'root' ]; then
echo You have no permission to run $0 as non-root user. Use sudo!
exit 1;
fi

export COMPOSE_PROJECT_NAME=$2
export MOODLE_WWWROOT=$projecsdir/$2
export MOODLE_NAME=$2

sitehost=$2.docker
siteport="$(cat $MOODLE_WWWROOT/siteport~)"

export MOODLE_PORT=$siteport

rm -rf /etc/nginx/sites-available/$sitehost
echo "$ok Deleted nginx site enabled (/etc/nginx/sites-enabled/$sitehost)"

rm -rf /etc/nginx/sites-enabled/$sitehost
echo $ok New nginx site enabled "(/etc/nginx/sites-enabled/$sitehost)"

rm $MOODLE_WWWROOT/siteport~
echo $ok New nginx site enabled "(/etc/nginx/sites-enabled/$sitehost)"

service nginx restart

sed -i "/$sitehost/d" /etc/hosts
echo $ok Deleted host name from /etc/hosts file "($sitehost)"

cd "$projecsdir/docker_moodle"

echo $ok Removing docker components...

docker-compose down
docker image rm $2_moodle
echo $ok Project deleted
;;

*)
Expand Down
9 changes: 7 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ services:
moodle:
build: ./docker/moodle
ports:
- "127.0.0.1:80:80"
- "127.0.0.1:${MOODLE_PORT}:80"
volumes:
- ./siteroot:/siteroot
- ${MOODLE_WWWROOT}:/var/www/site
environment:
MOODLE_NAME: "${MOODLE_NAME}"
MOODLE_PORT: "${MOODLE_PORT}"
MOODLE_WWWROOT: "${MOODLE_WWWROOT}"
MOODLE_MAIL_HOST: "${MOODLE_MAIL_HOST}"
db:
image: postgres:10.15
environment:
Expand Down
6 changes: 4 additions & 2 deletions docker/moodle/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ RUN apt-get update -y \
php-xml \
php-xmlrpc \
php-zip \
vim
vim\
chromium-browser

RUN locale-gen en_AU.UTF-8

Expand All @@ -40,10 +41,11 @@ COPY nginx-site /etc/nginx/sites-available/default
COPY nginx.conf /etc/nginx/nginx.conf
COPY xdebug.ini /etc/php/7.4/mods-available/xdebug.ini
COPY php.ini /etc/php/7.4/fpm/php.ini
COPY custom-php-fpm.conf /etc/php/7.4/fpm/pool.d/custom-php-fpm.conf

COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh

WORKDIR /siteroot
WORKDIR /var/www/site

ENTRYPOINT ["/entrypoint.sh"]
2 changes: 2 additions & 0 deletions docker/moodle/custom-php-fpm.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[www]
clear_env = no
3 changes: 3 additions & 0 deletions docker/moodle/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

echo "env[MOODLE_NAME] = ${MOODLE_NAME}" >> /etc/php/7.4/fpm/pool.d/custom-php-fpm.conf
echo "env[MOODLE_MAIL_HOST] = ${MOODLE_MAIL_HOST}" >> /etc/php/7.4/fpm/pool.d/custom-php-fpm.conf

service php7.4-fpm start

exec "nginx"
2 changes: 1 addition & 1 deletion docker/moodle/nginx-site
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
server {
listen 80 default_server;

root /siteroot;
root /var/www/site;

index index.php index.html index.htm;

Expand Down
Loading