1
1
#! /bin/bash
2
2
set -eou pipefail
3
3
4
+ START_TIME=$( date +%s%N)
5
+
4
6
PG_CONF=/etc/postgresql/postgresql.conf
5
7
SUPERVISOR_CONF=/etc/supervisor/supervisord.conf
6
8
@@ -9,6 +11,16 @@ export CONFIGURED_FLAG_PATH=${CONFIGURED_FLAG_PATH:-$DATA_VOLUME_MOUNTPOINT/mach
9
11
10
12
export MAX_IDLE_TIME_MINUTES=${MAX_IDLE_TIME_MINUTES:- 5}
11
13
14
+ function calculate_duration {
15
+ local start_time=$1
16
+ local end_time=$2
17
+
18
+ local duration=$(( end_time - start_time))
19
+ local milliseconds=$(( duration / 1000000 ))
20
+
21
+ echo " $milliseconds "
22
+ }
23
+
12
24
# Ref: https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746
13
25
function retry {
14
26
# Pass 0 for unlimited retries
@@ -53,12 +65,12 @@ function enable_swap {
53
65
}
54
66
55
67
function push_lsn_checkpoint_file {
56
- if [ " ${PLATFORM_DEPLOYMENT:- } " != " true" ]; then
57
- echo " Skipping push of LSN checkpoint file"
58
- return
59
- fi
68
+ if [ " ${PLATFORM_DEPLOYMENT:- } " != " true" ]; then
69
+ echo " Skipping push of LSN checkpoint file"
70
+ return
71
+ fi
60
72
61
- /usr/bin/admin-mgr lsn-checkpoint-push --immediately || echo " Failed to push LSN checkpoint"
73
+ /usr/bin/admin-mgr lsn-checkpoint-push --immediately || echo " Failed to push LSN checkpoint"
62
74
}
63
75
64
76
function graceful_shutdown {
@@ -71,46 +83,33 @@ function graceful_shutdown {
71
83
}
72
84
73
85
function enable_autoshutdown {
74
- sed -i " s/autostart=.*/autostart=true/" /etc/supervisor/base-services/supa-shutdown.conf
86
+ sed -i " s/autostart=.*/autostart=true/" /etc/supervisor/base-services/supa-shutdown.conf
75
87
}
76
88
77
89
function enable_lsn_checkpoint_push {
78
- sed -i " s/autostart=.*/autostart=true/" /etc/supervisor/base-services/lsn-checkpoint-push.conf
79
- sed -i " s/autorestart=.*/autorestart=true/" /etc/supervisor/base-services/lsn-checkpoint-push.conf
90
+ sed -i " s/autostart=.*/autostart=true/" /etc/supervisor/base-services/lsn-checkpoint-push.conf
91
+ sed -i " s/autorestart=.*/autorestart=true/" /etc/supervisor/base-services/lsn-checkpoint-push.conf
80
92
}
81
93
82
94
function disable_fail2ban {
83
- sed -i " s/command=.*/command=sleep 5/" /etc/supervisor/services/fail2ban.conf
84
95
sed -i " s/autostart=.*/autostart=false/" /etc/supervisor/services/fail2ban.conf
85
96
sed -i " s/autorestart=.*/autorestart=false/" /etc/supervisor/services/fail2ban.conf
86
97
}
87
98
88
- function disable_gotrue {
89
- sed -i " s/command=.*/command=sleep 5/" /etc/supervisor/services/gotrue.conf
90
- sed -i " s/autostart=.*/autostart=false/" /etc/supervisor/services/gotrue.conf
91
- sed -i " s/autorestart=.*/autorestart=false/" /etc/supervisor/services/gotrue.conf
92
- }
93
-
94
- function disable_pgbouncer {
95
- sed -i " s/command=.*/command=sleep 5/" /etc/supervisor/services/pgbouncer.conf
96
- sed -i " s/autostart=.*/autostart=false/" /etc/supervisor/services/pgbouncer.conf
97
- sed -i " s/autorestart=.*/autorestart=false/" /etc/supervisor/services/pgbouncer.conf
98
- }
99
-
100
99
function setup_postgres {
101
100
tar -xzvf " $INIT_PAYLOAD_PATH " -C / ./etc/postgresql.schema.sql
102
101
mv /etc/postgresql.schema.sql /docker-entrypoint-initdb.d/migrations/99-schema.sql
103
102
104
103
tar -xzvf " $INIT_PAYLOAD_PATH " -C / ./etc/postgresql-custom/pgsodium_root.key
105
- echo " include = '/etc/postgresql-custom/postgresql-platform-defaults.conf'" >> $PG_CONF
104
+ echo " include = '/etc/postgresql-custom/postgresql-platform-defaults.conf'" >> $PG_CONF
106
105
107
106
# TODO (darora): walg enablement is temporarily performed here until changes from https://github.com/supabase/postgres/pull/639 get picked up
108
107
# other things will still be needed in the future (auth_delay config)
109
108
sed -i \
110
- -e " s|#include = '/etc/postgresql-custom/wal-g.conf'|include = '/etc/postgresql-custom/wal-g.conf'|g" \
111
- -e " s|shared_preload_libraries = '\(.*\)'|shared_preload_libraries = '\1, auth_delay'|" \
112
- -e " /# Automatically generated optimizations/i auth_delay.milliseconds = '3000'" \
113
- " ${PG_CONF} "
109
+ -e " s|#include = '/etc/postgresql-custom/wal-g.conf'|include = '/etc/postgresql-custom/wal-g.conf'|g" \
110
+ -e " s|shared_preload_libraries = '\(.*\)'|shared_preload_libraries = '\1, auth_delay'|" \
111
+ -e " /# Automatically generated optimizations/i auth_delay.milliseconds = '3000'" \
112
+ " ${PG_CONF} "
114
113
115
114
# Setup ssl certs
116
115
mkdir -p /etc/ssl/certs/postgres
@@ -169,6 +168,8 @@ function setup_postgres {
169
168
chmod g+rx " /etc/wal-g"
170
169
chmod g+rx " ${WALG_CONF_DIR} "
171
170
fi
171
+ DURATION=$( calculate_duration " $START_TIME " " $( date +%s%N) " )
172
+ echo " E: Execution time to setting up postgresql: $DURATION milliseconds"
172
173
}
173
174
174
175
function setup_credentials {
@@ -178,6 +179,8 @@ function setup_credentials {
178
179
export SERVICE_ROLE_KEY=${SERVICE_ROLE_KEY:- $(jq -r ' .["service_key"]' / tmp/ init.json)}
179
180
export ADMIN_API_KEY=${ADMIN_API_KEY:- $(jq -r ' .["supabase_admin_key"]' / tmp/ init.json)}
180
181
export JWT_SECRET=${JWT_SECRET:- $(jq -r ' .["jwt_secret"]' / tmp/ init.json)}
182
+ DURATION=$( calculate_duration " $START_TIME " " $( date +%s%N) " )
183
+ echo " E: Execution time to setting up credentials: $DURATION milliseconds"
181
184
}
182
185
183
186
function report_health {
@@ -193,9 +196,9 @@ function report_health {
193
196
}
194
197
195
198
function run_prelaunch_hooks {
196
- if [ -f " /etc/postgresql-custom/supautils.conf" ]; then
197
- sed -i -e ' s/dblink, //' " /etc/postgresql-custom/supautils.conf"
198
- fi
199
+ if [ -f " /etc/postgresql-custom/supautils.conf" ]; then
200
+ sed -i -e ' s/dblink, //' " /etc/postgresql-custom/supautils.conf"
201
+ fi
199
202
}
200
203
201
204
function start_supervisor {
@@ -206,19 +209,57 @@ function start_supervisor {
206
209
/usr/bin/supervisord -c $SUPERVISOR_CONF
207
210
}
208
211
212
+ DELEGATED_ARCHIVE_PATH=/data/delegated-init.tar.gz
213
+ DELEGATED_ENTRY_PATH=/data/delegated-entry.sh
214
+
215
+ function fetch_and_execute_delegated_payload {
216
+ curl -s --time-cond $DELEGATED_ARCHIVE_PATH -o $DELEGATED_ARCHIVE_PATH " $DELEGATED_INIT_LOCATION "
217
+
218
+ if [ ! -f $DELEGATED_ARCHIVE_PATH ]; then
219
+ echo " No delegated payload found, bailing"
220
+ return
221
+ fi
222
+
223
+ # only extract a valid archive
224
+ if tar -tzf " $DELEGATED_ARCHIVE_PATH " & > /dev/null; then
225
+ TAR_MTIME_EPOCH=$( tar -tvzf " $DELEGATED_ARCHIVE_PATH " data/delegated-entry.sh | awk ' {print $4, $5}' | xargs -I {} date -d {} +%s)
226
+
227
+ if [ -f $DELEGATED_ENTRY_PATH ]; then
228
+ FILE_MTIME_EPOCH=$( stat -c %Y " $DELEGATED_ENTRY_PATH " )
229
+
230
+ if [ " $TAR_MTIME_EPOCH " -gt " $FILE_MTIME_EPOCH " ]; then
231
+ tar -xvzf " $DELEGATED_ARCHIVE_PATH " -C /
232
+ else
233
+ echo " TAR archive is not newer, skipping extraction"
234
+ fi
235
+ else
236
+ tar -xvzf " $DELEGATED_ARCHIVE_PATH " -C /
237
+ fi
238
+ else
239
+ echo " Invalid TAR archive"
240
+ return
241
+ fi
242
+
243
+ # Run our delegated entry script here
244
+ if [ -f " $DELEGATED_ENTRY_PATH " ]; then
245
+ chmod +x $DELEGATED_ENTRY_PATH
246
+ bash -c " $DELEGATED_ENTRY_PATH $START_TIME "
247
+ fi
248
+ }
249
+
209
250
# Increase max number of open connections
210
251
ulimit -n 65536
211
252
212
253
# Update pgsodium root key
213
254
if [ " ${PGSODIUM_ROOT_KEY:- } " ]; then
214
- echo " ${PGSODIUM_ROOT_KEY} " > /etc/postgresql-custom/pgsodium_root.key
255
+ echo " ${PGSODIUM_ROOT_KEY} " > /etc/postgresql-custom/pgsodium_root.key
215
256
fi
216
257
217
258
# Update pgdata directory
218
259
if [ " ${PGDATA_REAL:- } " ]; then
219
- mkdir -p " ${PGDATA_REAL} "
220
- chown -R postgres:postgres " ${PGDATA_REAL} "
221
- chmod -R g+rx " ${PGDATA_REAL} "
260
+ mkdir -p " ${PGDATA_REAL} "
261
+ chown -R postgres:postgres " ${PGDATA_REAL} "
262
+ chmod -R g+rx " ${PGDATA_REAL} "
222
263
fi
223
264
224
265
if [ " ${PGDATA:- } " ]; then
@@ -239,7 +280,7 @@ export INIT_PAYLOAD_PATH=${INIT_PAYLOAD_PATH:-/tmp/payload.tar.gz}
239
280
240
281
if [ " ${INIT_PAYLOAD_PRESIGNED_URL:- } " ]; then
241
282
curl -fsSL " $INIT_PAYLOAD_PRESIGNED_URL " -o " /tmp/payload.tar.gz" || true
242
- if [ -f " /tmp/payload.tar.gz" ] && [ " /tmp/payload.tar.gz" != " $INIT_PAYLOAD_PATH " ] ; then
283
+ if [ -f " /tmp/payload.tar.gz" ] && [ " /tmp/payload.tar.gz" != " $INIT_PAYLOAD_PATH " ]; then
243
284
mv " /tmp/payload.tar.gz" " $INIT_PAYLOAD_PATH "
244
285
fi
245
286
fi
@@ -298,11 +339,8 @@ if [ "${FAIL2BAN_DISABLED:-}" == "true" ]; then
298
339
fi
299
340
300
341
if [ " ${GOTRUE_DISABLED:- } " == " true" ]; then
301
- disable_gotrue
302
- fi
303
-
304
- if [ " ${PGBOUNCER_DISABLED:- } " == " true" ]; then
305
- disable_pgbouncer
342
+ sed -i " s/autostart=.*/autostart=false/" /etc/supervisor/services/gotrue.conf
343
+ sed -i " s/autorestart=.*/autorestart=false/" /etc/supervisor/services/gotrue.conf
306
344
fi
307
345
308
346
if [ " ${PLATFORM_DEPLOYMENT:- } " == " true" ]; then
316
354
317
355
touch " $CONFIGURED_FLAG_PATH "
318
356
run_prelaunch_hooks
319
- start_supervisor
320
- push_lsn_checkpoint_file
357
+
358
+ if [ -n " ${DELEGATED_INIT_LOCATION:- } " ]; then
359
+ fetch_and_execute_delegated_payload
360
+ else
361
+ DURATION=$( calculate_duration " $START_TIME " " $( date +%s%N) " )
362
+ echo " E: Execution time to starting supervisor: $DURATION milliseconds"
363
+ start_supervisor
364
+ push_lsn_checkpoint_file
365
+ fi
0 commit comments