DEV: allow for 2 container data services to setup on boot

allow postgres templates to assign themselves a DB password
allows 2 container setups to be able to create db on boot and
setup passwords

move missing folder check to data services when running images without unicorn

standalone just needs to wait for db to setup via the postgres
service start script

exit if migrate or precompile on boot fail so services retry gracefully when db
is not yet up.
This commit is contained in:
Jeff Wong 2024-09-22 10:50:10 -07:00
parent 64f31dbbe3
commit d47aad22cb
No known key found for this signature in database
GPG Key ID: D4EEB78E484F8A83
5 changed files with 52 additions and 22 deletions

View File

@ -9,8 +9,10 @@ templates:
# any extra arguments for Docker? # any extra arguments for Docker?
# docker_args: # docker_args:
# TODO: SOME_SECRET to a password for the discourse user
params: params:
db_default_text_search_config: "pg_catalog.english" db_default_text_search_config: "pg_catalog.english"
db_password: SOME_SECRET
## Set db_shared_buffers to a max of 25% of the total memory. ## Set db_shared_buffers to a max of 25% of the total memory.
## will be set automatically by bootstrap based on detected RAM, or you can override ## will be set automatically by bootstrap based on detected RAM, or you can override
@ -24,6 +26,7 @@ env:
LC_ALL: en_US.UTF-8 LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8 LANG: en_US.UTF-8
LANGUAGE: en_US.UTF-8 LANGUAGE: en_US.UTF-8
DISCOURSE_DB_PASSWORD: $db_password
volumes: volumes:
- volume: - volume:
@ -33,12 +36,11 @@ volumes:
host: /var/discourse/shared/data/log/var-log host: /var/discourse/shared/data/log/var-log
guest: /var/log guest: /var/log
# TODO: SOME_SECRET to a password for the discourse user
hooks: hooks:
after_postgres: after_postgres:
- exec: - exec:
stdin: | stdin: |
alter user discourse with password 'SOME_SECRET'; alter user discourse with password '$db_password';
cmd: su - postgres -c 'psql discourse' cmd: su - postgres -c 'psql discourse'
raise_on_fail: false raise_on_fail: false

View File

@ -14,20 +14,35 @@ hooks:
- replace: - replace:
filename: /etc/service/unicorn/run filename: /etc/service/unicorn/run
from: "# postgres" from: "# postgres"
to: | to: sv start postgres || exit 1
if [ -f /root/install_postgres ]; then
/root/install_postgres
rm /root/install_postgres
fi
sv start postgres || exit 1
run: run:
- file:
path: /etc/service/config-postgres/run
chmod: "+x"
contents: |
#!/bin/sh
if [ -f /usr/local/bin/create_db ] && [ "$CREATE_DB_ON_BOOT" = "1" ]; then
sleep 5
/usr/local/bin/create_db
if [ ! -z "$DISCOURSE_DB_PASSWORD" ]; then
echo "alter user discourse with password '$DISCOURSE_DB_PASSWORD';" | su - postgres -c 'psql $db_name'
fi
fi
- file: - file:
path: /etc/service/postgres/run path: /etc/service/postgres/run
chmod: "+x" chmod: "+x"
contents: | contents: |
#!/bin/sh #!/bin/sh
exec 2>&1 exec 2>&1
if [ -f /root/install_postgres ]; then
/root/install_postgres
rm /root/install_postgres
fi
if [ "$CREATE_DB_ON_BOOT" = "1" ]; then
sv once config-postgres
fi
HOME=/var/lib/postgresql USER=postgres exec thpoff chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/15/bin/postmaster -D /etc/postgresql/15/main HOME=/var/lib/postgresql USER=postgres exec thpoff chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/15/bin/postmaster -D /etc/postgresql/15/main
- file: - file:

View File

@ -15,20 +15,35 @@ hooks:
- replace: - replace:
filename: /etc/service/unicorn/run filename: /etc/service/unicorn/run
from: "# postgres" from: "# postgres"
to: | to: sv start postgres || exit 1
if [ -f /root/install_postgres ]; then
/root/install_postgres
rm /root/install_postgres
fi
sv start postgres || exit 1
run: run:
- file:
path: /etc/service/config-postgres/run
chmod: "+x"
contents: |
#!/bin/sh
if [ -f /usr/local/bin/create_db ] && [ "$CREATE_DB_ON_BOOT" = "1" ]; then
sleep 5
/usr/local/bin/create_db
if [ ! -z "$DISCOURSE_DB_PASSWORD" ]; then
echo "alter user discourse with password '$DISCOURSE_DB_PASSWORD';" | su - postgres -c 'psql $db_name'
fi
fi
- file: - file:
path: /etc/service/postgres/run path: /etc/service/postgres/run
chmod: "+x" chmod: "+x"
contents: | contents: |
#!/bin/sh #!/bin/sh
exec 2>&1 exec 2>&1
if [ -f /root/install_postgres ]; then
/root/install_postgres
rm /root/install_postgres
fi
if [ "$CREATE_DB_ON_BOOT" = "1" ]; then
sv once config-postgres
fi
HOME=/var/lib/postgresql USER=postgres exec thpoff chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/15/bin/postmaster -D /etc/postgresql/15/main HOME=/var/lib/postgresql USER=postgres exec thpoff chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/15/bin/postmaster -D /etc/postgresql/15/main
- file: - file:

View File

@ -9,6 +9,9 @@ run:
contents: | contents: |
#!/bin/sh #!/bin/sh
exec 2>&1 exec 2>&1
if [ ! -d /shared/redis_data ]; then
install -d -m 0755 -o redis -g redis /shared/redis_data
fi
exec thpoff chpst -u redis -U redis /usr/bin/redis-server /etc/redis/redis.conf exec thpoff chpst -u redis -U redis /usr/bin/redis-server /etc/redis/redis.conf
- file: - file:
path: /etc/service/redis/log/run path: /etc/service/redis/log/run
@ -87,8 +90,4 @@ hooks:
- replace: - replace:
filename: /etc/service/unicorn/run filename: /etc/service/unicorn/run
from: "# redis" from: "# redis"
to: | to: sv start redis || exit 1
if [ ! -d /shared/redis_data ]; then
install -d -m 0755 -o redis -g redis /shared/redis_data
fi
sv start redis || exit 1

View File

@ -61,9 +61,8 @@ run:
if [[ -z "$PRECOMPILE_ON_BOOT" ]]; then if [[ -z "$PRECOMPILE_ON_BOOT" ]]; then
PRECOMPILE_ON_BOOT=1 PRECOMPILE_ON_BOOT=1
fi fi
if [ -f /usr/local/bin/create_db ] && [ "$CREATE_DB_ON_BOOT" = "1" ]; then /usr/local/bin/create_db; fi; if [ "$MIGRATE_ON_BOOT" = "1" ]; then su discourse -c 'bundle exec rake db:migrate' || exit 1; fi
if [ "$MIGRATE_ON_BOOT" = "1" ]; then su discourse -c 'bundle exec rake db:migrate'; fi if [ "$PRECOMPILE_ON_BOOT" = "1" ]; then SKIP_EMBER_CLI_COMPILE=1 su discourse -c 'bundle exec rake assets:precompile' || exit 1; fi
if [ "$PRECOMPILE_ON_BOOT" = "1" ]; then SKIP_EMBER_CLI_COMPILE=1 su discourse -c 'bundle exec rake assets:precompile'; fi
LD_PRELOAD=$RUBY_ALLOCATOR HOME=/home/discourse USER=discourse exec thpoff chpst -u discourse:www-data -U discourse:www-data bundle exec config/unicorn_launcher -E production -c config/unicorn.conf.rb LD_PRELOAD=$RUBY_ALLOCATOR HOME=/home/discourse USER=discourse exec thpoff chpst -u discourse:www-data -U discourse:www-data bundle exec config/unicorn_launcher -E production -c config/unicorn.conf.rb
- file: - file: