Skip to content

Commit 1f5e7c3

Browse files
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.
1 parent 20e33fb commit 1f5e7c3

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

samples/data.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ templates:
99
# any extra arguments for Docker?
1010
# docker_args:
1111

12+
# TODO: SOME_SECRET to a password for the discourse user
1213
params:
1314
db_default_text_search_config: "pg_catalog.english"
15+
db_password: SOME_SECRET
1416

1517
## Set db_shared_buffers to a max of 25% of the total memory.
1618
## will be set automatically by bootstrap based on detected RAM, or you can override
@@ -24,6 +26,7 @@ env:
2426
LC_ALL: en_US.UTF-8
2527
LANG: en_US.UTF-8
2628
LANGUAGE: en_US.UTF-8
29+
DISCOURSE_DB_PASSWORD: $db_password
2730

2831
volumes:
2932
- volume:
@@ -33,12 +36,11 @@ volumes:
3336
host: /var/discourse/shared/data/log/var-log
3437
guest: /var/log
3538

36-
# TODO: SOME_SECRET to a password for the discourse user
3739
hooks:
3840
after_postgres:
3941
- exec:
4042
stdin: |
41-
alter user discourse with password 'SOME_SECRET';
43+
alter user discourse with password '$db_password';
4244
cmd: su - postgres -c 'psql discourse'
4345

4446
raise_on_fail: false

templates/postgres.15.template.yml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,38 @@ hooks:
1414
- replace:
1515
filename: /etc/service/unicorn/run
1616
from: "# postgres"
17-
to: |
18-
if [ -f /root/install_postgres ]; then
19-
/root/install_postgres
20-
rm /root/install_postgres
21-
fi
22-
sv start postgres || exit 1
17+
to: sv start postgres || exit 1
2318

2419
run:
2520
- exec: DEBIAN_FRONTEND=noninteractive apt-get purge -y postgresql-13 postgresql-client-13 postgresql-contrib-13 postgresql-13-pgvector
2621
- exec: apt-get update && apt-get install -y postgresql-15 postgresql-client-15 postgresql-contrib-15 postgresql-15-pgvector
22+
23+
- file:
24+
path: /etc/service/config-postgres/run
25+
chmod: "+x"
26+
contents: |
27+
#!/bin/sh
28+
if [ -f /usr/local/bin/create_db ] && [ "$CREATE_DB_ON_BOOT" = "1" ]; then
29+
sleep 5
30+
/usr/local/bin/create_db
31+
if [ ! -z "$DISCOURSE_DB_PASSWORD" ]; then
32+
echo "alter user discourse with password '$DISCOURSE_DB_PASSWORD';" | su - postgres -c 'psql $db_name'
33+
fi
34+
fi
35+
2736
- file:
2837
path: /etc/service/postgres/run
2938
chmod: "+x"
3039
contents: |
3140
#!/bin/sh
3241
exec 2>&1
42+
if [ -f /root/install_postgres ]; then
43+
/root/install_postgres
44+
rm /root/install_postgres
45+
fi
46+
if [ "$CREATE_DB_ON_BOOT" = "1" ]; then
47+
sv once config-postgres
48+
fi
3349
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
3450
3551
- file:

templates/postgres.template.yml

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,35 @@ hooks:
1414
- replace:
1515
filename: /etc/service/unicorn/run
1616
from: "# postgres"
17-
to: |
18-
if [ -f /root/install_postgres ]; then
19-
/root/install_postgres
20-
rm /root/install_postgres
21-
fi
22-
sv start postgres || exit 1
17+
to: sv start postgres || exit 1
2318

2419
run:
20+
- file:
21+
path: /etc/service/config-postgres/run
22+
chmod: "+x"
23+
contents: |
24+
#!/bin/sh
25+
if [ -f /usr/local/bin/create_db ] && [ "$CREATE_DB_ON_BOOT" = "1" ]; then
26+
sleep 5
27+
/usr/local/bin/create_db
28+
if [ ! -z "$DISCOURSE_DB_PASSWORD" ]; then
29+
echo "alter user discourse with password '$DISCOURSE_DB_PASSWORD';" | su - postgres -c 'psql $db_name'
30+
fi
31+
fi
32+
2533
- file:
2634
path: /etc/service/postgres/run
2735
chmod: "+x"
2836
contents: |
2937
#!/bin/sh
3038
exec 2>&1
39+
if [ -f /root/install_postgres ]; then
40+
/root/install_postgres
41+
rm /root/install_postgres
42+
fi
43+
if [ "$CREATE_DB_ON_BOOT" = "1" ]; then
44+
sv once config-postgres
45+
fi
3146
HOME=/var/lib/postgresql USER=postgres exec thpoff chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/13/bin/postmaster -D /etc/postgresql/13/main
3247
3348
- file:

templates/redis.template.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ run:
88
contents: |
99
#!/bin/sh
1010
exec 2>&1
11+
if [ ! -d /shared/redis_data ]; then
12+
install -d -m 0755 -o redis -g redis /shared/redis_data
13+
fi
1114
exec thpoff chpst -u redis -U redis /usr/bin/redis-server /etc/redis/redis.conf
1215
- file:
1316
path: /etc/service/redis/log/run
@@ -86,8 +89,4 @@ hooks:
8689
- replace:
8790
filename: /etc/service/unicorn/run
8891
from: "# redis"
89-
to: |
90-
if [ ! -d /shared/redis_data ]; then
91-
install -d -m 0755 -o redis -g redis /shared/redis_data
92-
fi
93-
sv start redis || exit 1
92+
to: sv start redis || exit 1

templates/web.template.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ run:
6060
if [[ -z "$PRECOMPILE_ON_BOOT" ]]; then
6161
PRECOMPILE_ON_BOOT=1
6262
fi
63-
if [ -f /usr/local/bin/create_db ] && [ "$CREATE_DB_ON_BOOT" = "1" ]; then /usr/local/bin/create_db; fi;
64-
if [ "$MIGRATE_ON_BOOT" = "1" ]; then su discourse -c 'bundle exec rake db:migrate'; fi
65-
if [ "$PRECOMPILE_ON_BOOT" = "1" ]; then SKIP_EMBER_CLI_COMPILE=1 su discourse -c 'bundle exec rake assets:precompile'; fi
63+
if [ "$MIGRATE_ON_BOOT" = "1" ]; then su discourse -c 'bundle exec rake db:migrate' || exit 1; fi
64+
if [ "$PRECOMPILE_ON_BOOT" = "1" ]; then SKIP_EMBER_CLI_COMPILE=1 su discourse -c 'bundle exec rake assets:precompile' || exit 1; fi
6665
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
6766
6867
- file:

0 commit comments

Comments
 (0)