@@ -4,9 +4,11 @@ set -eou pipefail
4
4
PG_CONF=/etc/postgresql/postgresql.conf
5
5
SUPERVISOR_CONF=/etc/supervisor/supervisord.conf
6
6
7
- DATA_VOLUME_MOUNTPOINT=${DATA_VOLUME_MOUNTPOINT:-/ data}
7
+ export DATA_VOLUME_MOUNTPOINT=${DATA_VOLUME_MOUNTPOINT:-/ data}
8
8
export CONFIGURED_FLAG_PATH=${CONFIGURED_FLAG_PATH:- $DATA_VOLUME_MOUNTPOINT / machine.configured}
9
9
10
+ export MAX_IDLE_TIME_MINUTES=${MAX_IDLE_TIME_MINUTES:- 5}
11
+
10
12
# Ref: https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746
11
13
function retry {
12
14
# Pass 0 for unlimited retries
@@ -50,14 +52,40 @@ function enable_swap {
50
52
swapon /mnt/swapfile
51
53
}
52
54
53
- function create_lsn_checkpoint_file {
54
- if [ ! -f " ${DATA_VOLUME_MOUNTPOINT} /latest-lsn-checkpoint" ]; then
55
- echo " 0/0" > " ${DATA_VOLUME_MOUNTPOINT} /latest-lsn-checkpoint"
56
- chown postgres:postgres " ${DATA_VOLUME_MOUNTPOINT} /latest-lsn-checkpoint"
57
- chmod 0300 " ${DATA_VOLUME_MOUNTPOINT} /latest-lsn-checkpoint"
58
- fi
55
+ function push_lsn_checkpoint_file {
56
+ if [ " ${PLATFORM_DEPLOYMENT:- } " != " true" ]; then
57
+ echo " Skipping push of LSN checkpoint file"
58
+ return
59
+ fi
60
+
61
+ /usr/bin/admin-mgr lsn-checkpoint-push --immediately || echo " Failed to push LSN checkpoint"
62
+ }
63
+
64
+ function graceful_shutdown {
65
+ echo " $( date) : Received SIGINT. Shutting down."
66
+ supervisorctl stop postgresql
67
+
68
+ # Postgres ships the latest WAL file using archive_command during shutdown, in a blocking operation
69
+ # This is to ensure that the WAL file is shipped, just in case
70
+ sleep 0.2
71
+ push_lsn_checkpoint_file
72
+
73
+ kill -s TERM " $( supervisorctl pid) "
74
+ }
75
+
76
+ function enable_autoshutdown {
77
+ sed -i " s/autostart=.*/autostart=true/" /etc/supervisor/base-services/supa-shutdown.conf
59
78
}
60
79
80
+ function enable_lsn_checkpoint_push {
81
+ sed -i " s/autostart=.*/autostart=true/" /etc/supervisor/base-services/lsn-checkpoint-push.conf
82
+ sed -i " s/autorestart=.*/autorestart=true/" /etc/supervisor/base-services/lsn-checkpoint-push.conf
83
+ }
84
+
85
+ function disable_fail2ban {
86
+ sed -i " s/autostart=.*/autostart=false/" /etc/supervisor/services/fail2ban.conf
87
+ sed -i " s/autorestart=.*/autorestart=false/" /etc/supervisor/services/fail2ban.conf
88
+ }
61
89
62
90
function setup_postgres {
63
91
tar -xzvf " $INIT_PAYLOAD_PATH " -C / ./etc/postgresql.schema.sql
@@ -210,6 +238,8 @@ if [ "${DATA_VOLUME_MOUNTPOINT}" ]; then
210
238
done
211
239
212
240
chown -R postgres:postgres " ${BASE_LOGS_FOLDER} "
241
+
242
+ mkdir -p " ${DATA_VOLUME_MOUNTPOINT} /etc/logrotate"
213
243
fi
214
244
215
245
# Process init payload
@@ -229,16 +259,16 @@ find /etc/supervisor/ -type f -exec chmod 0660 {} +
229
259
# Start services in the background
230
260
if [ " ${POSTGRES_ONLY:- } " == " true" ]; then
231
261
sed -i " s| - postgrest| # - postgrest|g" /etc/adminapi/adminapi.yaml
232
- sed -i " s|files = services/\*.conf db-only /\*.conf|files = db-only /\*.conf|g" $SUPERVISOR_CONF
262
+ sed -i " s|files = services/\*.conf base-services /\*.conf|files = base-services /\*.conf|g" $SUPERVISOR_CONF
233
263
/init/configure-adminapi.sh
234
264
else
235
265
sed -i " s| # - postgrest| - postgrest|g" /etc/adminapi/adminapi.yaml
236
- sed -i " s|files = db-only /\*.conf|files = services/\*.conf db-only /\*.conf|g" $SUPERVISOR_CONF
266
+ sed -i " s|files = base-services /\*.conf|files = services/\*.conf base-services /\*.conf|g" $SUPERVISOR_CONF
237
267
configure_services
238
268
fi
239
269
240
270
if [ " ${AUTOSHUTDOWN_ENABLED:- } " == " true" ]; then
241
- sed -i " s/autostart=.*/autostart=true/ " /etc/supervisor/db-only/supa-shutdown.conf
271
+ enable_autoshutdown
242
272
fi
243
273
244
274
if [ " ${ENVOY_ENABLED:- } " == " true" ]; then
@@ -248,8 +278,7 @@ if [ "${ENVOY_ENABLED:-}" == "true" ]; then
248
278
fi
249
279
250
280
if [ " ${FAIL2BAN_DISABLED:- } " == " true" ]; then
251
- sed -i " s/autostart=.*/autostart=false/" /etc/supervisor/services/fail2ban.conf
252
- sed -i " s/autorestart=.*/autorestart=false/" /etc/supervisor/services/fail2ban.conf
281
+ disable_fail2ban
253
282
fi
254
283
255
284
if [ " ${GOTRUE_DISABLED:- } " == " true" ]; then
@@ -258,9 +287,14 @@ if [ "${GOTRUE_DISABLED:-}" == "true" ]; then
258
287
fi
259
288
260
289
if [ " ${PLATFORM_DEPLOYMENT:- } " == " true" ]; then
261
- enable_swap
262
- create_lsn_checkpoint_file
290
+ if [ " ${SWAP_DISABLED:- } " != " true" ]; then
291
+ enable_swap
292
+ fi
293
+ enable_lsn_checkpoint_push
294
+
295
+ trap graceful_shutdown SIGINT
263
296
fi
264
297
265
298
touch " $CONFIGURED_FLAG_PATH "
266
299
start_supervisor
300
+ push_lsn_checkpoint_file
0 commit comments