diff --git a/README.md b/README.md index b8e36635ec1..1449dccb769 100644 --- a/README.md +++ b/README.md @@ -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 git@github.com:catalyst/docker_moodle.git docker_moodle +git clone git@github.com: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 git@github.com: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 git@github.com: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 @@ -42,25 +106,25 @@ 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 +control dbrestore moodle ``` ## Running Tests @@ -68,14 +132,13 @@ Restore db locally: 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 ``` diff --git a/control b/control index 768ae8c3be0..f520b70080d 100755 --- a/control +++ b/control @@ -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 ;; *) diff --git a/docker-compose.yml b/docker-compose.yml index 9b6027e68e6..c1f7c6034a2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/docker/moodle/Dockerfile b/docker/moodle/Dockerfile index 7096385f6d4..a505ab64d7a 100644 --- a/docker/moodle/Dockerfile +++ b/docker/moodle/Dockerfile @@ -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 @@ -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"] \ No newline at end of file diff --git a/docker/moodle/custom-php-fpm.conf b/docker/moodle/custom-php-fpm.conf new file mode 100644 index 00000000000..d873b954928 --- /dev/null +++ b/docker/moodle/custom-php-fpm.conf @@ -0,0 +1,2 @@ +[www] +clear_env = no diff --git a/docker/moodle/entrypoint.sh b/docker/moodle/entrypoint.sh index ef28cfbf8ae..fa2e01a4e98 100644 --- a/docker/moodle/entrypoint.sh +++ b/docker/moodle/entrypoint.sh @@ -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" \ No newline at end of file diff --git a/docker/moodle/nginx-site b/docker/moodle/nginx-site index 7624ebae560..5a4d152e3f4 100644 --- a/docker/moodle/nginx-site +++ b/docker/moodle/nginx-site @@ -1,7 +1,7 @@ server { listen 80 default_server; - root /siteroot; + root /var/www/site; index index.php index.html index.htm; diff --git a/docker/moodle/php.ini b/docker/moodle/php.ini index 6552ce8565a..3c8e24abffb 100644 --- a/docker/moodle/php.ini +++ b/docker/moodle/php.ini @@ -640,7 +640,7 @@ report_memleaks = On ; Development Value: "GPCS" ; Production Value: "GPCS"; ; http://php.net/variables-order -variables_order = "GPCS" +variables_order = "EGPCS" ; This directive determines which super global data (G,P & C) should be ; registered into the super global array REQUEST. If so, it also determines diff --git a/docker/moodle/xdebug.ini b/docker/moodle/xdebug.ini index 4a5c3180556..9b1832a0cb0 100644 --- a/docker/moodle/xdebug.ini +++ b/docker/moodle/xdebug.ini @@ -1,6 +1,6 @@ zend_extension=xdebug.so xdebug.remote_handler=dbgp -xdebug.remote_port=9000 +xdebug.remote_port=9003 xdebug.remote_autostart=1 xdebug.remote_enable=1 xdebug.remote_connect_back=1 diff --git a/moodle-config b/moodle-config index 689ac248819..a920f2f6736 100644 --- a/moodle-config +++ b/moodle-config @@ -26,7 +26,10 @@ $CFG->phpunit_dbpass = 'password'; $CFG->phpunit_prefix = 'phpu_'; $CFG->phpunit_dataroot = '/var/lib/testsitedata'; -$CFG->wwwroot = 'http://localhost'; +$name = getenv('MOODLE_NAME'); + +$CFG->wwwroot = "http://{$name}.docker"; +$CFG->reverseproxy = true; $CFG->dataroot = '/var/lib/sitedata'; @@ -36,7 +39,12 @@ $CFG->admin = 'admin'; @error_reporting(E_ALL | E_STRICT); // NOT FOR PRODUCTION SERVERS! $CFG->debug = (E_ALL | E_STRICT); // === DEBUG_DEVELOPER - NOT FOR PRODUCTION SERVERS! -$CFG->noemailever = true; +$mailhost = getenv('MOODLE_MAIL_HOST'); +if (!empty($mailhost)) { + $CFG->smtphosts = $mailhost . ':1025'; +} else { + $CFG->noemailever = true; +} $CFG->directorypermissions = 0777;