Skip to content

Commit 156a6d7

Browse files
authored
feat: delegated-init support for Fly projects (#997)
* feat: delegated-init support for Fly projects * chore: add a salt minion config * chore: always extract the archive to / * chore: extract newer archive only based on touch time in GHA * chore: track salt version in aio, not always latest * chore: bump image version
1 parent 438a454 commit 156a6d7

File tree

4 files changed

+174
-42
lines changed

4 files changed

+174
-42
lines changed

common.vars.pkr.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
postgres-version = "15.1.1.61"
1+
postgres-version = "15.1.1.62"

docker/all-in-one/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
154154
&& mkdir -p /dist \
155155
&& mkdir -p /data/opt && chmod go+rwx /data/opt
156156

157+
####################
158+
# Install salt
159+
####################
160+
ENV DEBIAN_FRONTEND noninteractive
161+
ENV SALT_VERSION 3006
162+
163+
# Install one-dir salt
164+
RUN mkdir /etc/apt/keyrings \
165+
&& curl -fsSL -o /etc/apt/keyrings/salt-archive-keyring-2023.gpg https://repo.saltproject.io/salt/py3/ubuntu/20.04/arm64/SALT-PROJECT-GPG-PUBKEY-2023.gpg \
166+
&& echo "deb [signed-by=/etc/apt/keyrings/salt-archive-keyring-2023.gpg arch=arm64] https://repo.saltproject.io/salt/py3/ubuntu/20.04/arm64/$SALT_VERSION focal main" | tee /etc/apt/sources.list.d/salt.list \
167+
&& apt-get clean && apt-get update \
168+
&& apt-get install -y salt-minion
169+
170+
ADD docker/all-in-one/etc/salt/minion /etc/salt/minion
171+
172+
157173
# Copy single binary dependencies
158174
COPY --from=pgrst /bin/postgrest /dist/
159175
COPY --from=gotrue /usr/local/bin/auth /dist/gotrue

docker/all-in-one/entrypoint.sh

Lines changed: 86 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/bash
22
set -eou pipefail
33

4+
START_TIME=$(date +%s%N)
5+
46
PG_CONF=/etc/postgresql/postgresql.conf
57
SUPERVISOR_CONF=/etc/supervisor/supervisord.conf
68

@@ -9,6 +11,16 @@ export CONFIGURED_FLAG_PATH=${CONFIGURED_FLAG_PATH:-$DATA_VOLUME_MOUNTPOINT/mach
911

1012
export MAX_IDLE_TIME_MINUTES=${MAX_IDLE_TIME_MINUTES:-5}
1113

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+
1224
# Ref: https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746
1325
function retry {
1426
# Pass 0 for unlimited retries
@@ -53,12 +65,12 @@ function enable_swap {
5365
}
5466

5567
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
6072

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"
6274
}
6375

6476
function graceful_shutdown {
@@ -71,46 +83,33 @@ function graceful_shutdown {
7183
}
7284

7385
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
7587
}
7688

7789
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
8092
}
8193

8294
function disable_fail2ban {
83-
sed -i "s/command=.*/command=sleep 5/" /etc/supervisor/services/fail2ban.conf
8495
sed -i "s/autostart=.*/autostart=false/" /etc/supervisor/services/fail2ban.conf
8596
sed -i "s/autorestart=.*/autorestart=false/" /etc/supervisor/services/fail2ban.conf
8697
}
8798

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-
10099
function setup_postgres {
101100
tar -xzvf "$INIT_PAYLOAD_PATH" -C / ./etc/postgresql.schema.sql
102101
mv /etc/postgresql.schema.sql /docker-entrypoint-initdb.d/migrations/99-schema.sql
103102

104103
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
106105

107106
# TODO (darora): walg enablement is temporarily performed here until changes from https://github.com/supabase/postgres/pull/639 get picked up
108107
# other things will still be needed in the future (auth_delay config)
109108
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}"
114113

115114
# Setup ssl certs
116115
mkdir -p /etc/ssl/certs/postgres
@@ -169,6 +168,8 @@ function setup_postgres {
169168
chmod g+rx "/etc/wal-g"
170169
chmod g+rx "${WALG_CONF_DIR}"
171170
fi
171+
DURATION=$(calculate_duration "$START_TIME" "$(date +%s%N)")
172+
echo "E: Execution time to setting up postgresql: $DURATION milliseconds"
172173
}
173174

174175
function setup_credentials {
@@ -178,6 +179,8 @@ function setup_credentials {
178179
export SERVICE_ROLE_KEY=${SERVICE_ROLE_KEY:-$(jq -r '.["service_key"]' /tmp/init.json)}
179180
export ADMIN_API_KEY=${ADMIN_API_KEY:-$(jq -r '.["supabase_admin_key"]' /tmp/init.json)}
180181
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"
181184
}
182185

183186
function report_health {
@@ -193,9 +196,9 @@ function report_health {
193196
}
194197

195198
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
199202
}
200203

201204
function start_supervisor {
@@ -206,19 +209,57 @@ function start_supervisor {
206209
/usr/bin/supervisord -c $SUPERVISOR_CONF
207210
}
208211

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+
209250
# Increase max number of open connections
210251
ulimit -n 65536
211252

212253
# Update pgsodium root key
213254
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
215256
fi
216257

217258
# Update pgdata directory
218259
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}"
222263
fi
223264

224265
if [ "${PGDATA:-}" ]; then
@@ -239,7 +280,7 @@ export INIT_PAYLOAD_PATH=${INIT_PAYLOAD_PATH:-/tmp/payload.tar.gz}
239280

240281
if [ "${INIT_PAYLOAD_PRESIGNED_URL:-}" ]; then
241282
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
243284
mv "/tmp/payload.tar.gz" "$INIT_PAYLOAD_PATH"
244285
fi
245286
fi
@@ -298,11 +339,8 @@ if [ "${FAIL2BAN_DISABLED:-}" == "true" ]; then
298339
fi
299340

300341
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
306344
fi
307345

308346
if [ "${PLATFORM_DEPLOYMENT:-}" == "true" ]; then
@@ -316,5 +354,12 @@ fi
316354

317355
touch "$CONFIGURED_FLAG_PATH"
318356
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

docker/all-in-one/etc/salt/minion

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Minions can connect to multiple masters simultaneously (all masters
2+
# are "hot"), or can be configured to failover if a master becomes
3+
# unavailable. Multiple hot masters are configured by setting this
4+
# value to "str". Failover masters can be requested by setting
5+
# to "failover". MAKE SURE TO SET master_alive_interval if you are
6+
# using failover.
7+
# Setting master_type to 'disable' lets you have a running minion (with engines and
8+
# beacons) without a master connection
9+
master_type: disable
10+
11+
# The minion can locally cache the return data from jobs sent to it, this
12+
# can be a good way to keep track of jobs the minion has executed
13+
# (on the minion side). By default this feature is disabled, to enable, set
14+
# cache_jobs to True.
15+
cache_jobs: True
16+
17+
# The minion can take a while to start up when lspci and/or dmidecode is used
18+
# to populate the grains for the minion. Set this to False if you do not need
19+
# GPU hardware grains for your minion.
20+
enable_gpu_grains: False
21+
22+
# Backup files that are replaced by file.managed and file.recurse under
23+
# 'cachedir'/file_backup relative to their original location and appended
24+
# with a timestamp. The only valid setting is "minion". Disabled by default.
25+
#
26+
# Alternatively this can be specified for each file in state files:
27+
# /etc/ssh/sshd_config:
28+
# file.managed:
29+
# - source: salt://ssh/sshd_config
30+
# - backup: minion
31+
#
32+
backup_mode: minion
33+
34+
##### File Directory Settings #####
35+
##########################################
36+
# The Salt Minion can redirect all file server operations to a local directory,
37+
# this allows for the same state tree that is on the master to be used if
38+
# copied completely onto the minion. This is a literal copy of the settings on
39+
# the master but used to reference a local directory on the minion.
40+
41+
# Set the file client. The client defaults to looking on the master server for
42+
# files, but can be directed to look at the local file directory setting
43+
# defined below by setting it to "local". Setting a local file_client runs the
44+
# minion in masterless mode.
45+
file_client: local
46+
47+
# The file directory works on environments passed to the minion, each environment
48+
# can have multiple root directories, the subdirectories in the multiple file
49+
# roots cannot match, otherwise the downloaded files will not be able to be
50+
# reliably ensured. A base environment is required to house the top file.
51+
# Example:
52+
# file_roots:
53+
# base:
54+
# - /srv/salt/
55+
# dev:
56+
# - /srv/salt/dev/services
57+
# - /srv/salt/dev/states
58+
# prod:
59+
# - /srv/salt/prod/services
60+
# - /srv/salt/prod/states
61+
#
62+
file_roots:
63+
base:
64+
- /opt/salt/state
65+
66+
# The Salt pillar is searched for locally if file_client is set to local. If
67+
# this is the case, and pillar data is defined, then the pillar_roots need to
68+
# also be configured on the minion:
69+
pillar_roots:
70+
base:
71+
- /opt/salt/pillar

0 commit comments

Comments
 (0)