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

chore: update local stack for consistency #690

Merged
merged 1 commit into from
Nov 30, 2023
Merged
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
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=response-local.test

MYSQL_DB=response-local
MYSQL_USER=response-local
MYSQL_PASS=response-local
MYSQL_DB=response
MYSQL_USER=response
MYSQL_PASS=response

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.
70 changes: 36 additions & 34 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 @@ -19,7 +20,7 @@ services:
container_name: $PROJECT_NAME-memcache
environment:
MEMCACHE_MAX_MEMORY: 64
ports:
expose:
- "11211"
networks:
- default
Expand All @@ -29,34 +30,29 @@ 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:
- memcache
- mysql
volumes:
# Mount local settings from this folder.
- "./shared/settings:/srv/www/shared/settings:ro"
# 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 a database 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 @@ -66,31 +62,37 @@ services:
# Mount configuration and allow overriding it.
- "../config:/srv/www/config:rw"
- "../config_dev:/srv/www/config_dev:rw"
# Mount nginx custom configuration.
- "../docker/etc/nginx/custom:/etc/nginx/custom"
# Mount contrib modules and themes.
# 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"
# Mount nginx custom configuration.
# - "../docker/etc/nginx/custom:/etc/nginx/custom"
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=local
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
50 changes: 33 additions & 17 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,27 +70,42 @@ 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
echo "Build local image."
make IMAGE_NAME=$IMAGE_NAME IMAGE_TAG=$IMAGE_TAG
fi;

# Create the site and mysql containers.
echo "Create the site and mysql containers."
# Create the site, memcache and mysql containers.
echo "Create the site, memcache and mysql containers."
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
# Wait a bit for memcache and mysql to be ready.
echo "Wait a bit for memcache and mysql to be ready."
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
4 changes: 2 additions & 2 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 Down
39 changes: 32 additions & 7 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,16 +145,41 @@
$settings['config_sync_directory'] = '/srv/www/config';

// Hash salt.
$settings['hash_salt'] = 'response-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'];
}
// Change kint depth_limit setting.
if (class_exists('Kint')) {
// Set the depth_limit to prevent out-of-memory.
\Kint::$depth_limit = 4;

// 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';
}

// // Change kint depth_limit setting.
// if (class_exists('Kint')) {
// // Set the depth_limit to prevent out-of-memory.
// \Kint::$depth_limit = 4;
// }
2 changes: 1 addition & 1 deletion local/shared/settings/settings.memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// Configure memcache.
$settings['memcache']['servers'] = ['memcache:11211' => 'default'];
$settings['memcache']['bins'] = ['default' => 'default'];
$settings['memcache']['key_prefix'] = 'unocha-local';
$settings['memcache']['key_prefix'] = 'local';
$settings['cache']['default'] = 'cache.backend.memcache';

// Performance tweaks.
Expand Down
Loading