Skip to content

Commit

Permalink
Merge pull request #213 from UN-OCHA/local-stack-update-for-consistency
Browse files Browse the repository at this point in the history
chore: update local stack for consistency
  • Loading branch information
lazysoundsystem authored Nov 30, 2023
2 parents 4300eeb + ebcf107 commit c73a677
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 55 deletions.
6 changes: 3 additions & 3 deletions local/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ IMAGE_TAG=local

SITE_DOMAIN=slt-local.test

MYSQL_DB=slt-local
MYSQL_USER=slt-local
MYSQL_PASS=slt-local
MYSQL_DB=slt
MYSQL_USER=slt
MYSQL_PASS=slt

PHP_ENVIRONMENT=local
PHP_XDEBUG=true
36 changes: 33 additions & 3 deletions local/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Local stack

The `local` folder contains a script and docker/drupal configuration to create an instance of the site locally.
The `local` folder contains scripts and docker/drupal configuration to create an instance of the site locally.

## Setup

Expand All @@ -9,18 +9,46 @@ The `local` folder contains a script and docker/drupal configuration to create a

## Scripts

**Important:** Run the script from the root of the repository.
**Important:** Run the scripts from the root of the repository.

The script `./local/install.sh -h` is used to create/stop/remove containers etc. Run `./local/install.sh -h` to see the script options.
The script `./local/install.sh` is used to create/stop/remove containers etc. Run `./local/install.sh -h` to see the script options.

The script `./local/exec.sh` is a shortcut for `docker compose -f local/docker-compose.yml exec`

To run additional docker compose commands, use `docker compose -f local/docker-compose.yml` + `command`.

## Create instance

1. Run `./local/install.sh -m -c -i` to create an instance of the site (empty database) from the existing config.
2. Run `./local/exec.sh -T -u appuser site drush sqlc < path-to-db-dump.sql` to restore a database dump.
3. Run `./local/install.sh -d` to install the dev dependencies, import the config and update the database.

## Stop/start containers

- Run `./local/install.sh -s` to stop the containers
- Run `./local/install.sh` to start the containers

## Shutdown/recreate containers

- Run `./local/install.sh -x` to stop and remove the containers.
- Run `./local/install.sh -d` to recreate the containers and install the dev dependencies.

**Note:** Run `./local/install.sh -x -v` to completely clean up a local instance (remove containers and volumes). Follow the "create instance" above to recreate an instance.

## Update site image

After modifications to the composer files (for example, after the automatic composer update), it is recommended to recreate the local site image:

- Run `./local/install.sh -m -d` to recreate the site image and install the dev dependencies.

## Update service/base images

When a new image used by a service has been created by the OPS team (ex: new mysql or php image):

- Run `./local/install.sh -u -d` to pull the service and base site images, recreate the local site image and the containers and install the dev dependencies.

When an image **with a new tag** has been created, then update the `local/docker-compose.yml` or the `docker/Dockerfile` accordingly before running the update command above.

## Composer

- Install all the modules (including dev) with `./local/exec.sh -w /srv/www site composer install`.
Expand All @@ -34,6 +62,8 @@ Enter the container with `./local/exec.sh -w /srv/www site sh` and edit files wi

Alternatively clone the core or contrib module/theme somewhere and mount the relevant folder/files by editing the `local/docker-compose.yml` file.

Ex: `"../contrib-module-repo/:/srv/www/html/modules/contrib/contrib-module-name:rw"`.

## Local proxy

Check the [setup-notes](https://github.com/UN-OCHA/local-reverse-proxy/blob/main/setup-notes.md) for first-time set-up of a local reverse proxy.
65 changes: 35 additions & 30 deletions local/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ networks:
default:
proxy:
name: ${PROXY:-proxy}
external: true
external: TRUE

volumes:
site-database:
site-database-log:
site-public:
site-private:

Expand All @@ -18,32 +19,28 @@ services:
hostname: $PROJECT_NAME-mysql
container_name: $PROJECT_NAME-mysql
environment:
- MYSQL_DB=$MYSQL_DB
- MYSQL_USER=$MYSQL_USER
- MYSQL_PASS=$MYSQL_PASS
MYSQL_DB:
MYSQL_USER:
MYSQL_PASS:
volumes:
- "site-database:/var/lib/mysql:rw"
- "site-database-log:/var/log/mysql:rw"
networks:
- default

drupal:
site:
image: public.ecr.aws/unocha/$IMAGE_NAME:$IMAGE_TAG
hostname: $PROJECT_NAME-site
container_name: $PROJECT_NAME-site
depends_on:
- mysql
volumes:
- "./shared/settings:/srv/www/shared/settings:ro"
# Mount composer files.
- "../composer.json:/srv/www/composer.json:rw"
- "../composer.lock:/srv/www/composer.lock:rw"
- "../composer.patches.json:/srv/www/composer.patches.json:rw"
- "../scripts:/srv/www/scripts:ro"
# Mount a database directory for import.
- "../database:/srv/www/database:rw"
# Mount volumes for the private and public files.
- "site-public:/srv/www/html/sites/default/files:rw"
- "site-private:/srv/www/html/sites/default/private:rw"
# A place for database backups to be installed.
- "../database:/srv/www/database:ro"
# Mount the folders needed for the tests.
- "../phpcs.xml:/srv/www/phpcs.xml:ro"
- "../phpunit.xml:/srv/www/phpunit.xml:ro"
Expand All @@ -53,27 +50,35 @@ services:
# Mount configuration and allow overriding it.
- "../config:/srv/www/config:rw"
- "../config_dev:/srv/www/config_dev:rw"
# Mount the composer files and patches.
- "../composer.json:/srv/www/composer.json:rw"
- "../composer.patches.json:/srv/www/composer.patches.json:rw"
- "../composer.lock:/srv/www/composer.lock:rw"
- "../PATCHES:/srv/www/PATCHES:rw"
# # Mount contrib modules and themes.
# - "../html/modules/contrib:/srv/www/html/modules/contrib:rw"
# - "../html/themes/contrib:/srv/www/html/themes/contrib:rw"
environment:
- TERM=xterm
- ENVIRONMENT=dev
- NGINX_SERVERNAME=$SITE_DOMAIN
- NGINX_OVERRIDE_PROTOCOL=HTTP,$SITE_DOMAIN
- DRUSH_OPTIONS_URI=https://$SITE_DOMAIN
- DRUPAL_DB_DATABASE=$MYSQL_DB
- DRUPAL_DB_USERNAME=$MYSQL_USER
- DRUPAL_DB_PASSWORD=$MYSQL_PASS
- DRUPAL_DB_HOST=mysql
- DRUPAL_DB_DRIVER=mysql
- PHP_ENVIRONMENT=dev
- PHP_XDEBUG=true
TERM: xterm
ENVIRONMENT: local
NGINX_SERVERNAME: $SITE_DOMAIN
NGINX_OVERRIDE_PROTOCOL: HTTP,$SITE_DOMAIN
DRUSH_OPTIONS_URI: https://$SITE_DOMAIN
DRUPAL_DB_DATABASE: $MYSQL_DB
DRUPAL_DB_USERNAME: $MYSQL_USER
DRUPAL_DB_PASSWORD: $MYSQL_PASS
DRUPAL_DB_HOST: mysql
DRUPAL_DB_DRIVER: mysql
PHP_ENVIRONMENT:
PHP_XDEBUG:
# Rate limiting.
- NGINX_LIMIT_HUMANS=1024r/s
- NGINX_BURST_HUMANS=1024
NGINX_LIMIT_HUMANS: 1024r/s
NGINX_BURST_HUMANS: 1024
# Local proxy.
- VIRTUAL_HOST=$SITE_DOMAIN
- VIRTUAL_PORT=80
- VIRTUAL_NETWORK=${PROXY:-proxy}
- HTTPS_METHOD=noredirect
VIRTUAL_HOST: $SITE_DOMAIN
VIRTUAL_PORT: 80
VIRTUAL_NETWORK: ${PROXY:-proxy}
HTTPS_METHOD: noredirect
labels:
caddy: $SITE_DOMAIN
caddy.reverse_proxy: "{{upstreams}}"
Expand Down
42 changes: 29 additions & 13 deletions local/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ usage() {
echo "-i : Install site" >&2
echo "-c : Use existing config to install site" >&2
echo "-d : Install dev dependencies" >&2
echo "-u : Pull latest service and base images and recreate containers" >&2
echo "-s : Stop the site containers" >&2
echo "-x : Shutdown and remove the site containers" >&2
echo "-v : Also remove the volumes when shutting down the containers" >&2
exit 1
Expand All @@ -19,11 +21,13 @@ create_image="no"
install_site="no"
use_existing_config="no"
install_dev_dependencies="no"
update="no"
stop="no"
shutdown="no"
shutdown_options=""

# Parse options.
while getopts "hmicdxv" opt; do
while getopts "hmicdusxv" opt; do
case $opt in
h)
usage
Expand All @@ -40,6 +44,12 @@ while getopts "hmicdxv" opt; do
d)
install_dev_dependencies="yes"
;;
u)
update="yes"
;;
s)
stop="yes"
;;
x)
shutdown="yes"
;;
Expand All @@ -60,13 +70,28 @@ function docker_compose {
# They are only available in this script as we don't export them.
source local/.env

# Stop the containers.
if [ "$stop" = "yes" ]; then
echo "Stop the containers."
docker_compose stop || true
exit 0
fi

# Stop and remove the containers.
if [ "$shutdown" = "yes" ]; then
echo "Stop and remove the containers."
docker_compose down $shutdown_options || true
exit 0
fi

# Update the image.
if [ "$update" = "yes" ]; then
echo "Pull service images."
docker_compose pull --ignore-pull-failures
echo "Pull base site image."
docker pull "$(grep -E -o "FROM ([^ ]+)$" docker/Dockerfile | awk '{print $2}')"
create_image="yes"
fi;

# Build local image.
if [ "$create_image" = "yes" ]; then
Expand All @@ -80,7 +105,7 @@ docker_compose up -d --remove-orphans

# Wait a bit for mysql to be ready.
echo "Wait a bit for mysql to be ready."
sleep 10
sleep 5

# Dump some information about the created containers.
echo "Dump some information about the created containers."
Expand Down Expand Up @@ -124,17 +149,8 @@ if [ "$install_dev_dependencies" = "yes" ]; then
echo "Install the dev dependencies."
docker_compose exec -w /srv/www site composer install

# Import the configuration.
docker_compose exec -u appuser site drush -y cr

# Import the configuration.
docker_compose exec -u appuser site drush -y updatedb --no-post-updates

# Import the configuration.
docker_compose exec -u appuser site drush -y cim

# Import the configuration.
docker_compose exec -u appuser site drush -y updatedb
# CLear the cache, import the configuration etc.
docker_compose exec -u appuser site drush -y deploy

# Enable the devel module.
docker_compose exec -u appuser site drush -y en devel
Expand Down
6 changes: 3 additions & 3 deletions local/shared/settings/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
parameters:
http.response.debug_cacheability_headers: false
http.response.debug_cacheability_headers: true
twig.config:
# Twig debugging:
#
Expand All @@ -23,7 +23,7 @@ parameters:
#
# Not recommended in production environments
# @default false
debug: false
debug: true
# Twig auto-reload:
#
# Automatically recompile Twig templates whenever the source code changes.
Expand All @@ -32,7 +32,7 @@ parameters:
#
# Not recommended in production environments
# @default null
auto_reload: false
auto_reload: null
# Twig cache:
#
# By default, Twig templates will be compiled and stored in the filesystem
Expand Down
30 changes: 27 additions & 3 deletions local/shared/settings/settings.local.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
/**
* Disable CSS and JS aggregation.
*/
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
$config['system.performance']['css']['preprocess'] = TRUE;
$config['system.performance']['js']['preprocess'] = TRUE;

/**
* Disable the render cache.
Expand Down Expand Up @@ -145,11 +145,35 @@
$settings['config_sync_directory'] = '/srv/www/config';

// Hash salt.
$settings['hash_salt'] = 'slt-local-site-salt';
$settings['hash_salt'] = 'local-site-salt';

// Ensure the dev_mod module configuration is not saved.
if (isset($settings['config_exclude_modules']) && is_array($settings['config_exclude_modules'])) {
$settings['config_exclude_modules'][] = 'dev_mode';
} else {
$settings['config_exclude_modules'] = ['dev_mode'];
}

// Ensure the devel module configuration is not saved.
if (isset($settings['config_exclude_modules']) && is_array($settings['config_exclude_modules'])) {
$settings['config_exclude_modules'][] = 'devel';
} else {
$settings['config_exclude_modules'] = ['devel'];
}

// Ensure the stage_file_proxy module configuration is not saved.
if (isset($settings['config_exclude_modules']) && is_array($settings['config_exclude_modules'])) {
$settings['config_exclude_modules'][] = 'stage_file_proxy';
} else {
$settings['config_exclude_modules'] = ['stage_file_proxy'];
}

// Enable/disable page/render caching and css/js aggregation.
$no_cache = TRUE;
if (!empty($no_cache)) {
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['page'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
}
Loading

0 comments on commit c73a677

Please sign in to comment.