From 3b172beddd6a6343cd6d12d837bded5079d50934 Mon Sep 17 00:00:00 2001 From: Andy Footner Date: Wed, 29 Nov 2023 16:48:01 +0100 Subject: [PATCH] chore: update local stack for consistency Refs: local-stack --- local/.env.example | 6 +- local/README.md | 36 +++++++++- local/docker-compose.yml | 70 ++++++++++--------- local/install.sh | 50 ++++++++----- local/shared/settings/services.yml | 4 +- local/shared/settings/settings.local.php | 39 +++++++++-- local/shared/settings/settings.memcache.php | 2 +- .../shared/settings/settings.site.php.example | 44 ++++++++++++ 8 files changed, 184 insertions(+), 67 deletions(-) create mode 100644 local/shared/settings/settings.site.php.example diff --git a/local/.env.example b/local/.env.example index 00cd476e..17aa1ba8 100644 --- a/local/.env.example +++ b/local/.env.example @@ -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 diff --git a/local/README.md b/local/README.md index 51c3c836..d0438ebf 100644 --- a/local/README.md +++ b/local/README.md @@ -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 @@ -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`. @@ -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. diff --git a/local/docker-compose.yml b/local/docker-compose.yml index 65278dc2..3bce8914 100644 --- a/local/docker-compose.yml +++ b/local/docker-compose.yml @@ -5,10 +5,11 @@ networks: default: proxy: name: ${PROXY:-proxy} - external: true + external: TRUE volumes: site-database: + site-database-log: site-public: site-private: @@ -19,7 +20,7 @@ services: container_name: $PROJECT_NAME-memcache environment: MEMCACHE_MAX_MEMORY: 64 - ports: + expose: - "11211" networks: - default @@ -29,15 +30,16 @@ 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 @@ -45,18 +47,12 @@ services: - 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" @@ -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}}" diff --git a/local/install.sh b/local/install.sh index 795b2f94..d463ab96 100755 --- a/local/install.sh +++ b/local/install.sh @@ -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 @@ -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 @@ -40,6 +44,12 @@ while getopts "hmicdxv" opt; do d) install_dev_dependencies="yes" ;; + u) + update="yes" + ;; + s) + stop="yes" + ;; x) shutdown="yes" ;; @@ -60,6 +70,13 @@ 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." @@ -67,6 +84,14 @@ if [ "$shutdown" = "yes" ]; then 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 @@ -74,13 +99,13 @@ if [ "$create_image" = "yes" ]; then 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." @@ -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 diff --git a/local/shared/settings/services.yml b/local/shared/settings/services.yml index d9601b4c..6ceab606 100644 --- a/local/shared/settings/services.yml +++ b/local/shared/settings/services.yml @@ -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: # @@ -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. diff --git a/local/shared/settings/settings.local.php b/local/shared/settings/settings.local.php index 3533bcb1..5a34e717 100644 --- a/local/shared/settings/settings.local.php +++ b/local/shared/settings/settings.local.php @@ -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. @@ -145,7 +145,7 @@ $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'])) { @@ -153,8 +153,33 @@ } 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; +// } diff --git a/local/shared/settings/settings.memcache.php b/local/shared/settings/settings.memcache.php index ee45137f..ce44c54c 100644 --- a/local/shared/settings/settings.memcache.php +++ b/local/shared/settings/settings.memcache.php @@ -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. diff --git a/local/shared/settings/settings.site.php.example b/local/shared/settings/settings.site.php.example new file mode 100644 index 00000000..2f13f739 --- /dev/null +++ b/local/shared/settings/settings.site.php.example @@ -0,0 +1,44 @@ +