diff --git a/app-install-ssl.sh b/app-install-ssl.sh new file mode 100755 index 0000000..418271b --- /dev/null +++ b/app-install-ssl.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# Overrides the official image's /etc/runit/1.d/install-ssl. +# +# The stock install-ssl always runs configure-ssl (and configure-letsencrypt), +# which empties the default `listen 80` nginx outlet and switches to `listen 443 +# ssl` against a cert that does not exist here — nginx then crash-loops, or the +# image tries to obtain its own Let's Encrypt cert. Under Co-op Cloud, Traefik +# terminates TLS and proxies plain HTTP to port 80, so we skip the image's SSL +# setup entirely and let nginx keep its default HTTP-on-80 config. +echo "install-ssl overridden by recipe: serving plain HTTP on :80 behind Traefik" +exit 0 diff --git a/cc-app-entrypoint.sh b/cc-app-entrypoint.sh new file mode 100755 index 0000000..17ab37f --- /dev/null +++ b/cc-app-entrypoint.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# Co-op Cloud wrapper around the official image's /sbin/boot. +# discourse/discourse reads passwords from the process env (pups/Ruby; it has no +# *_FILE support), so inject them from the docker secrets before booting. +set -e + +if [ -f /run/secrets/db_password ]; then + export DISCOURSE_DB_PASSWORD="$(cat /run/secrets/db_password)" +fi + +if [ -f /run/secrets/smtp_password ]; then + export DISCOURSE_SMTP_PASSWORD="$(cat /run/secrets/smtp_password)" +fi + +exec /sbin/boot diff --git a/compose.smtpauth.yaml b/compose.smtpauth.yaml index 61bdcfb..eaa1f5e 100644 --- a/compose.smtpauth.yaml +++ b/compose.smtpauth.yaml @@ -1,13 +1,7 @@ services: app: - environment: - - DISCOURSE_SMTP_PASSWORD_FILE=/var/run/secrets/smtp_password - secrets: - - smtp_password - - sidekiq: - environment: - - DISCOURSE_SMTP_PASSWORD_FILE=/var/run/secrets/smtp_password + # the wrapper entrypoint reads /run/secrets/smtp_password and exports + # DISCOURSE_SMTP_PASSWORD (the official image has no *_FILE support) secrets: - smtp_password diff --git a/compose.yaml b/compose.yaml index c900fd2..533b7a8 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,110 +1,132 @@ + services: app: - image: bitnami/discourse:3.2.2 + image: discourse/discourse:2026.7.4 networks: - proxy - internal - #entrypoint: ['tail', '-f', '/dev/null'] + # official image CMD is /sbin/boot; wrapper injects the DB password secret first + entrypoint: /usr/local/bin/cc-app-entrypoint.sh environment: - - ALLOW_EMPTY_PASSWORD=yes - - DISCOURSE_DATABASE_HOST=${STACK_NAME}_db - - DISCOURSE_DATABASE_NAME=discourse - - DISCOURSE_DATABASE_PASSWORD_FILE=/run/secrets/db_password - - DISCOURSE_DATABASE_USER=discourse - - DISCOURSE_HOST=${DOMAIN} - - DISCOURSE_NOTIFICATION_EMAIL - - DISCOURSE_SMTP_AUTH - - DISCOURSE_SMTP_HOST + - DISCOURSE_HOSTNAME=${DOMAIN} + - DISCOURSE_DEVELOPER_EMAILS=${DISCOURSE_DEVELOPER_EMAILS} + - DISCOURSE_DB_HOST=${STACK_NAME}_db + - DISCOURSE_DB_PORT=5432 + - DISCOURSE_DB_NAME=discourse + - DISCOURSE_DB_USERNAME=discourse + - DISCOURSE_REDIS_HOST=${STACK_NAME}_redis + - DISCOURSE_REDIS_PORT=6379 + - DISCOURSE_SMTP_ADDRESS - DISCOURSE_SMTP_PORT - - DISCOURSE_SMTP_PROTOCOL - - DISCOURSE_SMTP_USER - - PASSENGER_COMPILE_NATIVE_SUPPORT_BINARY=0 + - DISCOURSE_SMTP_USER_NAME + - DISCOURSE_SMTP_PASSWORD + - DISCOURSE_SMTP_AUTHENTICATION + - DISCOURSE_SMTP_ENABLE_START_TLS + - DISCOURSE_NOTIFICATION_EMAIL volumes: - - 'discourse_data:/bitnami/discourse' + - 'discourse_shared:/shared' + # transition only: legacy bitnami volume, read-only, for one-time upload migration + - 'discourse_data:/legacy:ro' secrets: - db_password + configs: + - source: app_entrypoint + target: /usr/local/bin/cc-app-entrypoint.sh + mode: 0555 + - source: app_install_ssl + target: /etc/runit/1.d/install-ssl + mode: 0555 + - source: app_migrate_uploads + target: /etc/runit/1.d/02-migrate-bitnami-uploads + mode: 0555 depends_on: - db - redis deploy: update_config: failure_action: rollback - order: start-first + order: stop-first labels: - "traefik.enable=true" - - "traefik.http.services.${STACK_NAME}.loadbalancer.server.port=3000" + - "traefik.http.services.${STACK_NAME}.loadbalancer.server.port=80" - "traefik.http.routers.${STACK_NAME}.rule=Host(`${DOMAIN}`${EXTRA_DOMAINS})" - "traefik.http.routers.${STACK_NAME}.entrypoints=web-secure" - "traefik.http.routers.${STACK_NAME}.tls.certresolver=${LETS_ENCRYPT_ENV}" - "caddy=${DOMAIN}" - - "caddy.reverse_proxy={{upstreams 3000}}" + - "caddy.reverse_proxy={{upstreams 80}}" - "caddy.tls.on_demand=" - # healthcheck: - # test: ["CMD", "curl", "-f", "http://localhost:3000"] - # interval: 30s - # timeout: 10s - # retries: 10 - # start_period: 1m + healthcheck: + test: "curl -fsS http://localhost/srv/status || exit 1" + interval: 30s + timeout: 10s + retries: 6 + start_period: 25m db: - image: postgres:13 + # discourse/postgres = pgvector + discourse's postgres management layer, which + # auto-upgrades an older cluster in place on boot (pg_upgrade into the versioned + # PGDATA /var/lib/postgresql/${MAJOR}/docker); everything is driven by the env below. + image: discourse/postgres:pg18 networks: - internal secrets: - db_password volumes: - - 'postgresql_data:/var/lib/postgresql/data' + # the image expects the whole cluster tree mounted here (not the data subdir); + # an existing pg17 cluster at the volume root is found and upgraded into /18/docker + - 'postgresql_data:/var/lib/postgresql' configs: - - source: db_entrypoint - target: /docker-entrypoint.sh + - source: pg_backup + target: /pg_backup.sh mode: 0555 - entrypoint: /docker-entrypoint.sh + entrypoint: + - /bin/bash + - -c + - | + if [ -f /run/secrets/db_password ]; then + DB_PASSWORD="$$(cat /run/secrets/db_password)" + export DB_PASSWORD POSTGRES_PASSWORD="$$DB_PASSWORD" + fi + exec run-postgres.sh postgres environment: + # internal-only overlay network; keep all-trust so the app and the + # backup/restore hooks connect without juggling the superuser password - POSTGRES_HOST_AUTH_METHOD=trust - - POSTGRES_USER=discourse - POSTGRES_DB=discourse - - POSTGRES_PASSWORD_FILE=/run/secrets/db_password + - DB_USER=discourse + # pg_upgrade runs as this role and initdb's the new cluster with it; it must + # match the OLD cluster's bootstrap superuser (oid 10). The image default + # `postgres` matches fresh installs and bitnami-origin clusters. Override in + # the app .env (POSTGRES_USER=...) only for a cluster bootstrapped differently. + - POSTGRES_USER=${POSTGRES_USER:-postgres} + # pg18's initdb enables data checksums by default, but pg13-17 clusters here + # have them off and pg_upgrade requires a match -> initialise without them. + - POSTGRES_INITDB_ARGS=--no-data-checksums + healthcheck: + test: "pg_isready -U discourse -d discourse" + interval: 30s + timeout: 10s + retries: 5 + start_period: 15m deploy: labels: backupbot.backup: "true" - backupbot.backup.pre-hook: "bash -c 'PGPASSWORD=$$(cat $${POSTGRES_PASSWORD_FILE}) pg_dump -U $${POSTGRES_USER} $${POSTGRES_DB} > /tmp/backup.sql'" - backupbot.backup.post-hook: "rm -rf /tmp/backup.sql" - backupbot.backup.path: "/tmp/backup.sql" + backupbot.backup.pre-hook: "/pg_backup.sh backup" + backupbot.backup.volumes.postgresql_data.path: "backup.sql" + backupbot.restore.post-hook: "/pg_backup.sh restore" redis: - image: redis:7.2-alpine + image: redis:8.10-alpine networks: - internal volumes: - 'redis_data:/data' - - sidekiq: - image: bitnami/discourse:3.2.2 - networks: - - internal - depends_on: - - discourse - volumes: - - 'discourse_data:/bitnami/discourse' - command: /opt/bitnami/scripts/discourse-sidekiq/run.sh - secrets: - - db_password - environment: - - ALLOW_EMPTY_PASSWORD=yes - - DISCOURSE_DATABASE_HOST=db - - DISCOURSE_DATABASE_NAME=discourse - - DISCOURSE_DATABASE_PASSWORD_FILE=/run/secrets/db_password - - DISCOURSE_DATABASE_PORT_NUMBER=5432 - - DISCOURSE_DATABASE_USER=discourse - - DISCOURSE_HOST=${DOMAIN} - - DISCOURSE_REDIS_HOST=redis - - DISCOURSE_REDIS_PORT_NUMBER=6379 - - DISCOURSE_SMTP_HOST - - DISCOURSE_SMTP_PORT - - DISCOURSE_SMTP_PROTOCOL - - DISCOURSE_SMTP_USER - - PASSENGER_COMPILE_NATIVE_SUPPORT_BINARY=0 - - DISCOURSE_SMTP_AUTH + healthcheck: + test: "redis-cli ping | grep -q PONG" + interval: 30s + timeout: 5s + retries: 5 + start_period: 30s secrets: db_password: @@ -114,6 +136,7 @@ secrets: volumes: postgresql_data: redis_data: + discourse_shared: discourse_data: networks: @@ -122,7 +145,15 @@ networks: internal: configs: - db_entrypoint: - name: ${STACK_NAME}_db_entrypoint - file: entrypoint.postgres.sh.tmpl - template_driver: golang + app_entrypoint: + name: ${STACK_NAME}_app_entrypoint + file: cc-app-entrypoint.sh + app_install_ssl: + name: ${STACK_NAME}_app_install_ssl + file: app-install-ssl.sh + app_migrate_uploads: + name: ${STACK_NAME}_app_migrate_uploads + file: migrate-uploads.sh + pg_backup: + name: ${STACK_NAME}_pg_backup + file: pg_backup.sh diff --git a/entrypoint.postgres.sh.tmpl b/entrypoint.postgres.sh.tmpl deleted file mode 100644 index fc69a6e..0000000 --- a/entrypoint.postgres.sh.tmpl +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -set -e - -MIGRATION_MARKER=$PGDATA/migration_in_progress -OLDDATA=$PGDATA/old_data -NEWDATA=$PGDATA/new_data - -echo "Running as $(id)" - -if [ -e $MIGRATION_MARKER ]; then - echo "FATAL: migration was started but did not complete in a previous run. manual recovery necessary" - exit 1 -fi - -if [ -f $PGDATA/PG_VERSION ]; then - DATA_VERSION=$(cat $PGDATA/PG_VERSION) - - if [ -n "$DATA_VERSION" -a "$PG_MAJOR" != "$DATA_VERSION" ]; then - echo "postgres data version $DATA_VERSION found, but need $PG_MAJOR. Starting migration" - echo "Installing postgres $DATA_VERSION" - sed -i "s/$/ $DATA_VERSION/" /etc/apt/sources.list.d/pgdg.list - apt-get update && apt-get install -y --no-install-recommends \ - postgresql-$DATA_VERSION \ - && rm -rf /var/lib/apt/lists/* - echo "shuffling around" - gosu postgres mkdir $OLDDATA $NEWDATA - chmod 700 $OLDDATA $NEWDATA - mv $PGDATA/* $OLDDATA/ || true - touch $MIGRATION_MARKER - echo "running initdb" - # abuse entrypoint script for initdb by making server error out - gosu postgres bash -c "export PGDATA=$NEWDATA ; /usr/local/bin/docker-entrypoint.sh --invalid-arg || true" - echo "running pg_upgrade" - cd /tmp - gosu postgres pg_upgrade --link -b /usr/lib/postgresql/$DATA_VERSION/bin -d $OLDDATA -D $NEWDATA -U $POSTGRES_USER - cp $OLDDATA/pg_hba.conf $NEWDATA/ - mv $NEWDATA/* $PGDATA - rm -rf $OLDDATA - rmdir $NEWDATA - rm $MIGRATION_MARKER - echo "migration complete" - fi -fi - -/usr/local/bin/docker-entrypoint.sh postgres diff --git a/migrate-uploads.sh b/migrate-uploads.sh new file mode 100755 index 0000000..3189be5 --- /dev/null +++ b/migrate-uploads.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# One-time, idempotent, NON-destructive migration of uploads + backups from a +# legacy bitnami discourse volume into the official image's /shared. +# +# Runs on every boot as a runit 1.d hook but no-ops after the first success +# (sentinel) and when there is no legacy volume mounted (fresh installs). It only +# ever COPIES from the read-only /legacy mount, so an interruption just re-copies +# on the next boot — there is no move/delete to leave the data half-migrated. +set -e + +SENTINEL=/shared/.bitnami-uploads-migrated +[ -e "$SENTINEL" ] && exit 0 + +if [ -d /legacy/public/uploads ]; then + echo "[migrate-uploads] copying bitnami uploads/backups -> /shared" + mkdir -p /shared/uploads /shared/backups + cp -a /legacy/public/uploads/. /shared/uploads/ 2>/dev/null || true + cp -a /legacy/public/backups/. /shared/backups/ 2>/dev/null || true + # discourse runs as uid 1000; the official boot also chowns /shared, but be explicit + chown -R discourse:discourse /shared/uploads /shared/backups 2>/dev/null || true + echo "[migrate-uploads] done" +fi + +touch "$SENTINEL" diff --git a/pg_backup.sh b/pg_backup.sh new file mode 100755 index 0000000..9f26058 --- /dev/null +++ b/pg_backup.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# Postgres backup/restore hook for the discourse `db` service (discourse/postgres image). + +set -e + +# dump goes at the volume root so backupbot's backup.sql label finds it +BACKUP_FILE='/var/lib/postgresql/backup.sql' +DATADIR="${PGDATA:-/var/lib/postgresql/18/docker}" +DB_NAME="${POSTGRES_DB:-discourse}" + +# bootstrap superuser for the dump/drop/recreate; same POSTGRES_USER the db service sets +SU="${POSTGRES_USER:-postgres}" + +function backup { + pg_dump -U "$SU" "$DB_NAME" | gzip > "$BACKUP_FILE" +} + +function restore { + cd "$DATADIR" + + # Block all non-local connections so the running discourse app + sidekiq cannot reconnect and + # interfere with the drop/recreate/reimport. Restored on exit. + restore_hba() { + cat pg_hba.conf.bak > pg_hba.conf + rm -f pg_hba.conf.bak + su postgres -c "pg_ctl -D '$DATADIR' reload" + } + cp pg_hba.conf pg_hba.conf.bak + echo 'local all all trust' > pg_hba.conf + su postgres -c "pg_ctl -D '$DATADIR' reload" + trap restore_hba EXIT INT TERM + + # terminate any lingering local sessions before recreate + # see https://stackoverflow.com/questions/5108876/kill-a-postgresql-session-connection + psql -U "$SU" -d postgres -c \ + "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='${DB_NAME}' AND pid<>pg_backend_pid();" + + # drop database and then recreate it + psql -U "$SU" -d postgres -c "DROP DATABASE ${DB_NAME} WITH (FORCE);" + createdb -U "$SU" "$DB_NAME" + + # reimport data + gunzip -c "$BACKUP_FILE" | psql -U "$SU" -d "$DB_NAME" -1 -v ON_ERROR_STOP=1 -f - +} + +$@