Skip to content

Commit

Permalink
Merge pull request #1970 from danskernesdigitalebibliotek/ddfher-186-…
Browse files Browse the repository at this point in the history
…bnf-dev-setup

Ddfher 186 bnf dev setup
  • Loading branch information
xendk authored Jan 20, 2025
2 parents 4e7927d + 2f5a1cc commit 984daf1
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ docker-compose.override.yml

# Danish translations folder
/config/sync/language/da

# We use this in the BNF setup, it should never be committed.
/web/sites/sites.php
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ You can find the full documentation, along with setup instructions in either the
or directly in [the docs folder](docs/)

**tl;dr:** Run `task dev:reset` to get the site up and running locally.

For a environment that consists of both a CMS site and a BNF site, run
`task dev:bnf:enable` before running reset.
67 changes: 43 additions & 24 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ vars:
# Where is the docker file(s) we use for our builds residing?
LAGOON_DIR: "lagoon"

DOCKER_COMPOSE_FILES_DEFAULT: "-f docker-compose.yml"
DOCKER_COMPOSE_FILES_BNF:
sh: if [[ -f web/sites/sites.php ]]; then echo "-f docker-compose.bnf.yml"; fi
DOCKER_COMPOSE_FILES_DEFAULT: "-f docker-compose.yml {{ .DOCKER_COMPOSE_FILES_BNF}}"
DOCKER_COMPOSE_FILES: "{{.DOCKER_COMPOSE_FILES | default .DOCKER_COMPOSE_FILES_DEFAULT }}"
DOCKER_COMPOSE_FILES_CI: "{{.DOCKER_COMPOSE_FILES}} -f docker-compose.ci.yml"

Expand Down Expand Up @@ -70,6 +72,20 @@ tasks:
- sh: "[ ! -z {{.CR_PAT}} ]"
msg: "Env variable CR_PAT is not set or empty."

dev:bnf:enable:
summary: Set env up for BNF development.
silent: true
cmds:
- cp assets/sites.php web/sites
- echo 'Enabled, remember to run "task dev:reset"'

dev:bnf:disable:
summary: Reset env to non-BNF development.
silent: true
cmds:
- rm web/sites/sites.php
- echo 'Disabled, remember to run "task dev:reset"'

dev:format:
summary: Lint custom code, and fix when possible
cmds:
Expand Down Expand Up @@ -101,6 +117,13 @@ tasks:
cmds:
- docker compose {{ .DOCKER_COMPOSE_FILES }} run --rm cli sh -c "{{.CLI_ARGS}}"

dev:bnfcli:
summary: dev:cli for BNF site, a noop if BNF not enabled
status:
- "[[ -z \"{{ .DOCKER_COMPOSE_FILES_BNF}}\" ]]"
cmds:
- docker compose {{ .DOCKER_COMPOSE_FILES }} run --rm bnfcli sh -c "{{.CLI_ARGS}}"

dev:start:
summary: Run docker compose
deps:
Expand Down Expand Up @@ -153,30 +176,24 @@ tasks:
# Start local environment.
- task dev:start
# Install site.
- task dev:cli -- drush site-install --existing-config -y
# Practice shows that the cache needs to be cleared to avoid configuration
# errors even after a site install.
- task dev:cache:clear:drupal
# Import translations.
- |
if [ -n "${SKIP_LANGUAGE_IMPORT}" ]; then
echo "Skipping language import due to SKIP_LANGUAGE_IMPORT environment variable"
else
task dev:cli -- drush locale-check
task dev:cli -- drush locale-update
task dev:cli -- drush dpl_po:import-remote-config-po da https://danskernesdigitalebibliotek.github.io/dpl-cms/translations/da.config.po
fi
# Clear all caches to ensure we have a pristine setup.
- task dev:cache:clear:all
# Run deploy hooks.
- task dev:cli -- drush deploy -y
# Ensure site is reachable and warm any caches
- task dev:cli -- curl --silent --show-error --fail --output /dev/null http://varnish:8080/
# Enable dev modules.
- task dev:enable-dev-tools
- task dev:create-users
# Show a one-time login to the local site.
- task dev:cli -- ./dev-scripts/install-site.sh {{ if .SKIP_LANGUAGE_IMPORT }}--skip-language-import{{ end }}
# Possibly install the BNF site.
- task dev:bnfcli -- ./dev-scripts/install-site.sh --no-content {{ if .SKIP_LANGUAGE_IMPORT }}--skip-language-import{{ end }}
- task dev:bnfcli -- drush --yes pm:enable bnf_server
- task dev:bnfcli -- drush --yes config:set system.site name BNF
- task: dev:bnf:set-client
# Show a one-time login to the local site(s).
- task dev:cli -- drush user-login
- task dev:bnfcli -- drush user-login

# Set CMS site to point to BNF site. This needs to be a task so we
# can quietly skip it if BNF isn't enabled.
dev:bnf:set-client:
internal: true
status:
- "[[ -z \"{{ .DOCKER_COMPOSE_FILES_BNF}}\" ]]"
cmds:
- task dev:cli -- drush config:set --yes bnf_client.settings base_url https://https/

dev:openid:configure:
desc: Set openid connect settings based on .env variables. And run cron.
Expand All @@ -192,6 +209,7 @@ tasks:
dev:enable-dev-tools:
desc: Enable dev modules and settings, which are not to be used in Prod. They are config-ignored
cmds:
# If changing this, keep the corresponding line in ./dev-scripts/install-site.sh in sync.
- task dev:cli -- drush install -y devel dpl_example_content field_ui purge_ui restui uuid_url views_ui dblog

dev:enable-xdebug:
Expand All @@ -208,6 +226,7 @@ tasks:

dev:create-users:
desc: Create test users, with test roles
# If changing this, keep the corresponding lines in ./dev-scripts/install-site.sh in sync.
cmds:
- task dev:cli -- drush user:create editor --password="test"
- task dev:cli -- drush user:role:add 'editor' editor
Expand Down
17 changes: 17 additions & 0 deletions assets/sites.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

// phpcs:ignoreFile

$sites = [];

// HTTP_HOST is not set when running PHPUnit tests, so check for existence
// first.
if (isset($_SERVER['HTTP_HOST'])) {
// Add in the host name to sites if it starts with `bnf-`. This way we don't
// have to worry about what TLD people use (docker or local), nor the composer
// project name (dpl-cms per default, but could be anything).
$host = $_SERVER['HTTP_HOST'];
if (preg_match('/^bnf-/', $host)) {
$sites[$host] = 'bnf';
}
}
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,13 @@
],
"file-mapping": {
"[web-root]/sites/default/all.settings.php": "assets/all.settings.php",
"[web-root]/sites/bnf/all.settings.php": "assets/all.settings.php",
"[web-root]/sites/default/development.settings.php": "assets/development.settings.php",
"[web-root]/sites/bnf/development.settings.php": "assets/development.settings.php",
"[web-root]/sites/default/local.services.yml": "assets/local.services.yml",
"[web-root]/sites/bnf/local.services.yml": "assets/local.services.yml",
"[web-root]/sites/default/local.settings.php": "assets/local.settings.php",
"[web-root]/sites/bnf/local.settings.php": "assets/local.settings.php",
"[project-root]/drush/sites/lagoon.site.yml": "assets/lagoon.site.yml",
"[web-root]/.eslintrc.json": false,
"[project-root]/.gitattributes": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions dev-scripts/install-site.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash

set -aeo pipefail

SKIP_LANGUAGE_IMPORT=""

while [[ "$#" -gt 0 ]]; do
case $1 in
--skip-language-import) SKIP_LANGUAGE_IMPORT="true" ;;
--no-content) SKIP_CONTENT="true" ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done

# Install site.
drush site-install --existing-config -y

# Practice shows that the cache needs to be cleared to avoid configuration
# errors even after a site install.
drush cache:rebuild -y

# Import translations.
if [[ $SKIP_LANGUAGE_IMPORT == "true" ]]; then
echo "Skipping language import due to SKIP_LANGUAGE_IMPORT environment variable"
else
drush locale-check
drush locale-update
drush dpl_po:import-remote-config-po da https://danskernesdigitalebibliotek.github.io/dpl-cms/translations/da.config.po
fi

# Clear all caches to ensure we have a pristine setup.
drush cache:rebuild -y
drush cache:rebuild-external -y

# Run deploy hooks.
drush deploy -y

# Ensure site is reachable and warm any caches
curl --silent --show-error --fail --output /dev/null http://varnish:8080/

# Enable dev modules (see task dev:enable-dev-tools).
MODULES=(devel field_ui purge_ui restui uuid_url views_ui dblog)

if [[ $SKIP_CONTENT != "true" ]]; then
MODULES=("${MODULES[@]}" dpl_example_content)
fi
drush install -y "${MODULES[@]}"

# Create test users (see task dev:create-users).
drush user:create editor --password="test"
drush user:role:add 'editor' editor

drush user:create administrator --password="test"
drush user:role:add 'administrator' administrator

drush user:create mediator --password="test"
drush user:role:add 'mediator' mediator

drush user:create local_administrator --password="test"
drush user:role:add 'local_administrator' local_administrator

drush user:create external_system --password="external_system"
drush user:role:add 'external_system' external_system

drush user:create patron --password="test"
drush user:role:add 'patron' patron
136 changes: 136 additions & 0 deletions docker-compose.bnf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

# These should match the same sections in docker-compose.yml.
x-volumes:
&default-volumes
volumes:
- 'projectroot:/app'

x-user:
&default-user
user: ${UID:-1000}

x-environment:
&default-environment
# This is the only that differs from docker-compose.yml
LAGOON_ROUTE: &default-url https://bnf-${COMPOSE_PROJECT_NAME}.${DEV_TLD:-docker}
LAGOON_PROJECT: 'dplcms'
LAGOON_ENVIRONMENT: 'local'
LAGOON_ENVIRONMENT_TYPE: ${LAGOON_ENVIRONMENT_TYPE:-local}
WEBROOT: web
#
MARIADB_HOST: bnfmariadb
REDIS_HOST: bnfredis
VARNISH_BACKEND_HOST: bnfnginx
NGINX_FASTCGI_PASS: bnfphp

services:
bnfhttps:
image: ghcr.io/reload/https-proxy:proxy
ports:
- '80'
- '443'
depends_on:
- bnfvarnish
labels:
lagoon.type: none
volumes:
- 'projectroot:/app'
- '${HOME}/.local/share/mkcert:/rootCA:ro'
- '${HOME}/.local/share/dev_certificates:/cert:rw'
environment:
NGINX_DOCUMENT_ROOT: /app/web
NGINX_PROXY_PASS: http://bnfvarnish:8080
VIRTUAL_PROTO: https
VIRTUAL_HOST: >-
bnf-${COMPOSE_PROJECT_NAME}.${DEV_TLD:-docker}
working_dir: /app

bnfvarnish: # Caching HTTP reverse proxy that serves (mostly) anonymous requests.
# https://docs.lagoon.sh/lagoon/docker-images/varnish
image: uselagoon/varnish-6-drupal:latest
depends_on:
- bnfnginx
labels:
dev.orbstack.domains: bnf-${COMPOSE_PROJECT_NAME}.local
links:
- bnfnginx # links varnish to the nginx in this docker-compose project, or it would try to connect to any nginx running in docker
<< : *default-user # uses the defined user from top
ports:
# Exposing the port to make it accessible locally without proxies.
- "8080"
environment:
<< : *default-environment
# VARNISH_BYPASS: "true" # Add this to disable caching in varnish.
# Is used by [nginx-proxy](https://github.com/nginx-proxy/nginx-proxy) or [dory](https://github.com/FreedomBen/dory)
VIRTUAL_HOST: ${COMPOSE_PROJECT_NAME}-bnfvarnish.${DEV_TLD:-docker}
VIRTUAL_PORT: 8080

bnfcli: # cli container, will be used for executing composer and any local commands (drush, drupal, etc.)
# https://docs.lagoon.sh/lagoon/docker-images/nginx/nginx-drupal
build:
context: .
dockerfile: lagoon/cli.dockerfile
image: &cli-image uselagoon/php-8.1-cli-drupal:latest # this image will be reused as `CLI_IMAGE` in subsequent Docker builds
<< : *default-volumes # loads the defined volumes from the top
user: root
environment:
<< : *default-environment # loads the defined environment variables from the top
# Uncomment to enable xdebug for cli tools
#XDEBUG_ENABLE: "true"

bnfnginx: # Webserver in front of php-fpm. Serves static assets.
# https://docs.lagoon.sh/lagoon/docker-images/nginx/nginx-drupal
build:
context: .
dockerfile: lagoon/nginx.dockerfile
args:
CLI_IMAGE: *cli-image # Inject the name of the cli image
depends_on:
- bnfphp
# loads the defined volumes and user from the top
<<: [*default-volumes, *default-user]
environment:
<< : *default-environment # loads the defined environment variables from the top
# Route that should be used locally.
VIRTUAL_HOST: bnfnginx.${COMPOSE_PROJECT_NAME}.${DEV_TLD:-docker}

bnfphp: # php-fpm server that executes php requests.
# https://docs.lagoon.sh/lagoon/docker-images/php-fpm
build:
context: .
dockerfile: lagoon/php.dockerfile
args:
CLI_IMAGE: *cli-image
depends_on:
bnfredis:
condition: service_started
bnfmariadb:
condition: service_healthy
# loads the defined volumes and user from the top
<<: [*default-volumes, *default-user]
environment:
<< : *default-environment # loads the defined environment variables from the top
XDEBUG_ENABLE: ${XDEBUG_ENABLE:-false}

bnfmariadb: # Main site database.
# https://docs.lagoon.sh/lagoon/docker-images/mariadb/mariadb-drupal
image: uselagoon/mariadb-10.6-drupal:latest
# Do a periodic healthcheck. This is mainly used to block the php service
# from starting up before we have a healthy database.
healthcheck:
test: "/usr/share/container-scripts/mysql/readiness-probe.sh"
interval: 10s
ports:
- "3306" # exposes the port 3306 with a random local port, find it with `docker-compose port mariadb 3306`
<< : *default-user # uses the defined user from top
environment:
MARIADB_CHARSET: 'utf8mb4'
MARIADB_COLLATION: 'utf8mb4_danish_ci'
<< : *default-environment

bnfredis: # In-memory key-value database used as the Drupal Core cache backend.
# https://docs.lagoon.sh/lagoon/docker-images/redis
image: uselagoon/redis-6:latest
<< : *default-user # uses the defined user from top
environment:
<< : *default-environment
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ x-volumes:
x-user:
&default-user
# The default user under which the containers should run. Change this if you are on linux and run with another user than id `1000`
user: '1000'
user: ${UID:-1000}

x-environment:
&default-environment
Expand Down
6 changes: 6 additions & 0 deletions web/sites/bnf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/all.settings.php
/development.settings.php
/default.development.services.yml
/default.services.yml
/local.settings.php
/local.services.yml
Loading

0 comments on commit 984daf1

Please sign in to comment.