Skip to content

Commit e149265

Browse files
authored
chore: Fly refactoring; fixes; extra LSN shipping coverage (#800)
* chore: fly fixes * chore: configure autoshutdown using remote init payload * chore: more lsn checkpoint handling * chore: bump admin-api and admin-mgr * chore: remove supervisord event listener; use built-in admin-mgr watcher * chore: rename `db-only` to `base-services` * chore: logrotate, more tests
1 parent 82c6562 commit e149265

26 files changed

+208
-65
lines changed

.github/workflows/dockerhub-release-aio.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ on:
1313
- develop
1414
types:
1515
- completed
16-
16+
1717
jobs:
1818
settings:
1919
runs-on: ubuntu-latest

.github/workflows/dockerhub-release.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
paths:
88
- ".github/workflows/dockerhub-release.yml"
99
- "common.vars*"
10-
10+
1111
jobs:
1212
settings:
1313
runs-on: ubuntu-latest
@@ -25,6 +25,7 @@ jobs:
2525
with:
2626
cmd: yq 'to_entries | map(select(.value|type == "!!str")) | map(.key + "=" + .value) | join("\n")' 'ansible/vars.yml'
2727

28+
2829
build_image:
2930
needs: settings
3031
strategy:

.github/workflows/testinfra.yml

+14-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- runner: arm-runner
1515
arch: arm64
1616
runs-on: ${{ matrix.runner }}
17-
timeout-minutes: 60
17+
timeout-minutes: 30
1818
steps:
1919
- uses: actions/checkout@v3
2020

@@ -27,7 +27,17 @@ jobs:
2727
run: |
2828
# TODO: use poetry for pkg mgmt
2929
pip3 install boto3 boto3-stubs[essential] docker ec2instanceconnectcli pytest pytest-testinfra[paramiko,docker] requests
30-
pytest -vv testinfra/test_all_in_one.py
30+
31+
32+
if ! pytest -vv testinfra/test_all_in_one.py; then
33+
# display container logs if the test fails
34+
35+
if [ -f testinfra-aio-container-logs.log ]; then
36+
echo "AIO container logs:"
37+
cat testinfra-aio-container-logs.log
38+
fi
39+
exit 1
40+
fi
3141
3242
test-ami:
3343
strategy:
@@ -132,9 +142,9 @@ jobs:
132142
- name: Cleanup resources on build cancellation
133143
if: ${{ cancelled() }}
134144
run: |
135-
aws ec2 --region ap-southeast-1 describe-instances --filters "Name=tag:packerExecutionId,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -I {} aws ec2 terminate-instances --instance-ids {}
145+
aws ec2 --region ap-southeast-1 describe-instances --filters "Name=tag:packerExecutionId,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -I {} aws ec2 terminate-instances --region ap-southeast-1 --instance-ids {}
136146
137147
- name: Cleanup resources on build cancellation
138148
if: ${{ always() }}
139149
run: |
140-
aws ec2 --region ap-southeast-1 describe-instances --filters "Name=tag:testinfra-run-id,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -I {} aws ec2 terminate-instances --instance-ids {} || true
150+
aws ec2 --region ap-southeast-1 describe-instances --filters "Name=tag:testinfra-run-id,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -I {} aws ec2 terminate-instances --region ap-southeast-1 --instance-ids {} || true

ansible/vars.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ postgres_exporter_release_checksum:
4646
arm64: sha256:29ba62d538b92d39952afe12ee2e1f4401250d678ff4b354ff2752f4321c87a0
4747
amd64: sha256:cb89fc5bf4485fb554e0d640d9684fae143a4b2d5fa443009bd29c59f9129e84
4848

49-
adminapi_release: 0.58.0
50-
adminmgr_release: 0.13.1
49+
adminapi_release: 0.58.1
50+
adminmgr_release: 0.14.0
5151

5252
# Postgres Extensions
5353
postgis_release: "3.3.2"

docker/all-in-one/Dockerfile

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
ARG postgres_version=15.1.0.83
1+
ARG postgres_version=15.1.0.148
22

33
ARG pgbouncer_release=1.18.0
44
ARG postgrest_release=10.1.2
55
ARG gotrue_release=2.47.0
6-
ARG adminapi_release=0.58.0
7-
ARG adminmgr_release=0.9.0
6+
ARG adminapi_release=0.58.1
7+
ARG adminmgr_release=0.14.0
88
ARG vector_release=0.22.3
99
ARG postgres_exporter_release=0.9.0
1010
ARG envoy_release=1.28.0
@@ -144,6 +144,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
144144
sudo \
145145
vim-tiny \
146146
less \
147+
libnuma1 \
148+
logrotate \
147149
# pg_egress_collect deps
148150
tcpdump libio-async-perl \
149151
&& rm -rf /var/lib/apt/lists/* /tmp/* \
@@ -197,6 +199,10 @@ COPY --chown=postgrest:postgrest docker/all-in-one/etc/postgrest/bootstrap.sh /e
197199
COPY --chown=postgrest:postgrest docker/all-in-one/etc/postgrest/base.conf /etc/postgrest/base.conf
198200
COPY --chown=postgrest:postgrest docker/all-in-one/etc/postgrest/generated.conf /etc/postgrest/generated.conf
199201

202+
# Customizations for logrotate
203+
COPY docker/all-in-one/etc/logrotate.d/walg.conf /etc/logrotate.d/walg.conf
204+
COPY docker/all-in-one/etc/logrotate.d/postgresql.conf /etc/logrotate.d/postgresql.conf
205+
200206
# Customizations for gotrue
201207
COPY docker/all-in-one/etc/gotrue.env /etc/gotrue.env
202208

@@ -264,7 +270,7 @@ RUN mkdir -p /var/log/wal-g \
264270
&& chmod ug+s /dist/admin-mgr \
265271
&& touch /etc/wal-g/config.json \
266272
&& chown adminapi:adminapi /etc/wal-g/config.json \
267-
&& echo '{}' > /etc/wal-g/config.json
273+
&& echo '{"WALG_S3_PREFIX": "s3://foo/bar/"}' > /etc/wal-g/config.json
268274
RUN chown -R adminapi:adminapi /etc/adminapi
269275

270276
# Add healthcheck and entrypoint scripts
@@ -275,4 +281,6 @@ COPY docker/all-in-one/init /init
275281
COPY docker/all-in-one/entrypoint.sh /usr/local/bin/
276282
COPY docker/all-in-one/postgres-entrypoint.sh /usr/local/bin/
277283
COPY docker/all-in-one/shutdown.sh /usr/local/bin/supa-shutdown.sh
284+
COPY docker/all-in-one/run-logrotate.sh /usr/local/bin/run-logrotate.sh
285+
278286
ENTRYPOINT [ "entrypoint.sh" ]

docker/all-in-one/entrypoint.sh

+48-14
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ set -eou pipefail
44
PG_CONF=/etc/postgresql/postgresql.conf
55
SUPERVISOR_CONF=/etc/supervisor/supervisord.conf
66

7-
DATA_VOLUME_MOUNTPOINT=${DATA_VOLUME_MOUNTPOINT:-/data}
7+
export DATA_VOLUME_MOUNTPOINT=${DATA_VOLUME_MOUNTPOINT:-/data}
88
export CONFIGURED_FLAG_PATH=${CONFIGURED_FLAG_PATH:-$DATA_VOLUME_MOUNTPOINT/machine.configured}
99

10+
export MAX_IDLE_TIME_MINUTES=${MAX_IDLE_TIME_MINUTES:-5}
11+
1012
# Ref: https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746
1113
function retry {
1214
# Pass 0 for unlimited retries
@@ -50,14 +52,40 @@ function enable_swap {
5052
swapon /mnt/swapfile
5153
}
5254

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
5978
}
6079

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+
}
6189

6290
function setup_postgres {
6391
tar -xzvf "$INIT_PAYLOAD_PATH" -C / ./etc/postgresql.schema.sql
@@ -210,6 +238,8 @@ if [ "${DATA_VOLUME_MOUNTPOINT}" ]; then
210238
done
211239

212240
chown -R postgres:postgres "${BASE_LOGS_FOLDER}"
241+
242+
mkdir -p "${DATA_VOLUME_MOUNTPOINT}/etc/logrotate"
213243
fi
214244

215245
# Process init payload
@@ -229,16 +259,16 @@ find /etc/supervisor/ -type f -exec chmod 0660 {} +
229259
# Start services in the background
230260
if [ "${POSTGRES_ONLY:-}" == "true" ]; then
231261
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
233263
/init/configure-adminapi.sh
234264
else
235265
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
237267
configure_services
238268
fi
239269

240270
if [ "${AUTOSHUTDOWN_ENABLED:-}" == "true" ]; then
241-
sed -i "s/autostart=.*/autostart=true/" /etc/supervisor/db-only/supa-shutdown.conf
271+
enable_autoshutdown
242272
fi
243273

244274
if [ "${ENVOY_ENABLED:-}" == "true" ]; then
@@ -248,8 +278,7 @@ if [ "${ENVOY_ENABLED:-}" == "true" ]; then
248278
fi
249279

250280
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
253282
fi
254283

255284
if [ "${GOTRUE_DISABLED:-}" == "true" ]; then
@@ -258,9 +287,14 @@ if [ "${GOTRUE_DISABLED:-}" == "true" ]; then
258287
fi
259288

260289
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
263296
fi
264297

265298
touch "$CONFIGURED_FLAG_PATH"
266299
start_supervisor
300+
push_lsn_checkpoint_file

docker/all-in-one/etc/adminapi/adminapi.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ metric_collectors:
99
- loadavg
1010
- cpu
1111
- diskstats
12+
- vmstat
1213
node_exporter_additional_args:
1314
- "--collector.filesystem.ignored-mount-points=^/(boot|sys|dev|run).*"
1415
- "--collector.netdev.device-exclude=lo"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/var/log/postgresql/postgresql.csv {
2+
size 50M
3+
rotate 4
4+
compress
5+
delaycompress
6+
notifempty
7+
missingok
8+
postrotate
9+
sudo -u postgres /usr/lib/postgresql/15/bin/pg_ctl -D /var/lib/postgresql/data logrotate
10+
endscript
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/var/log/wal-g/*.log {
2+
size 50M
3+
rotate 3
4+
copytruncate
5+
delaycompress
6+
compress
7+
notifempty
8+
missingok
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[program:logrotate]
2+
command=/usr/local/bin/run-logrotate.sh
3+
autostart=true
4+
autorestart=true
5+
user=root
6+
stdout_logfile=/var/log/services/logrotate.log
7+
redirect_stderr=true
8+
stdout_logfile_maxbytes=10MB
9+
priority=50
10+
environment=DATA_VOLUME_MOUNTPOINT="%(ENV_DATA_VOLUME_MOUNTPOINT)s"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[program:lsn-checkpoint-push]
2+
command=/usr/bin/admin-mgr lsn-checkpoint-push --watch
3+
user=root
4+
autorestart=false
5+
autostart=false
6+
stdout_logfile=/var/log/services/lsn-push.log
7+
redirect_stderr=true
8+
stdout_logfile_maxbytes=10MB
9+
priority=50

docker/all-in-one/etc/supervisor/db-only/supa-shutdown.conf renamed to docker/all-in-one/etc/supervisor/base-services/supa-shutdown.conf

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ stdout_logfile=/var/log/services/supa-shutdown.log
77
redirect_stderr=true
88
stdout_logfile_maxbytes=10MB
99
priority=50
10+
environment=MAX_IDLE_TIME_MINUTES="%(ENV_MAX_IDLE_TIME_MINUTES)s"

docker/all-in-one/etc/supervisor/supervisord.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,4 @@ serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
167167
; include files themselves.
168168

169169
[include]
170-
files = db-only/*.conf
170+
files = base-services/*.conf
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
22
set -eou pipefail
33

4+
touch "/var/log/wal-g/pitr.log"
5+
chown postgres:postgres "/var/log/wal-g/pitr.log"
6+
chmod 0666 "/var/log/wal-g/pitr.log"
7+
48
/usr/local/bin/configure-shim.sh /dist/admin-mgr /usr/bin/admin-mgr

docker/all-in-one/init/configure-adminapi.sh

+15-15
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,6 @@ ADMINAPI_CUSTOM_DIR="${DATA_VOLUME_MOUNTPOINT}/etc/adminapi"
88

99
/usr/local/bin/configure-shim.sh /dist/supabase-admin-api /opt/supabase-admin-api
1010

11-
if [ "${DATA_VOLUME_MOUNTPOINT}" ]; then
12-
mkdir -p "${ADMINAPI_CUSTOM_DIR}"
13-
if [ ! -f "${CONFIGURED_FLAG_PATH}" ]; then
14-
echo "Copying existing custom adminapi config from /etc/adminapi to ${ADMINAPI_CUSTOM_DIR}"
15-
cp -R "/etc/adminapi/." "${ADMINAPI_CUSTOM_DIR}/"
16-
fi
17-
18-
rm -rf "/etc/adminapi"
19-
ln -s "${ADMINAPI_CUSTOM_DIR}" "/etc/adminapi"
20-
chown -R adminapi:adminapi "/etc/adminapi"
21-
22-
chown -R adminapi:adminapi "${ADMINAPI_CUSTOM_DIR}"
23-
chmod g+rx "${ADMINAPI_CUSTOM_DIR}"
24-
fi
25-
2611
if [ -f "${INIT_PAYLOAD_PATH:-}" ]; then
2712
echo "init adminapi payload"
2813
tar -xzvf "$INIT_PAYLOAD_PATH" -C / ./etc/adminapi/adminapi.yaml
@@ -53,3 +38,18 @@ chmod -R 0775 /etc/postgresql-custom
5338

5439
# Update api port
5540
sed -i "s|^port: .*$|port: ${ADMIN_API_PORT:-8085}|g" $ADMIN_API_CONF
41+
42+
if [ "${DATA_VOLUME_MOUNTPOINT}" ]; then
43+
mkdir -p "${ADMINAPI_CUSTOM_DIR}"
44+
if [ ! -f "${CONFIGURED_FLAG_PATH}" ]; then
45+
echo "Copying existing custom adminapi config from /etc/adminapi to ${ADMINAPI_CUSTOM_DIR}"
46+
cp -R "/etc/adminapi/." "${ADMINAPI_CUSTOM_DIR}/"
47+
fi
48+
49+
rm -rf "/etc/adminapi"
50+
ln -s "${ADMINAPI_CUSTOM_DIR}" "/etc/adminapi"
51+
chown -R adminapi:adminapi "/etc/adminapi"
52+
53+
chown -R adminapi:adminapi "${ADMINAPI_CUSTOM_DIR}"
54+
chmod g+rx "${ADMINAPI_CUSTOM_DIR}"
55+
fi

docker/all-in-one/init/configure-autoshutdown.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -eou pipefail
33

44
mkdir -p /etc/supa-shutdown
55

6-
AUTOSHUTDOWN_CUSTOM_DIR="${DATA_VOLUME_MOUNTPOINT}/supa-shutdown"
6+
AUTOSHUTDOWN_CUSTOM_DIR="${DATA_VOLUME_MOUNTPOINT}/etc/supa-shutdown"
77
if [ "${DATA_VOLUME_MOUNTPOINT}" ]; then
88
mkdir -p "${AUTOSHUTDOWN_CUSTOM_DIR}"
99

docker/all-in-one/init/configure-gotrue.sh

+12-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ set -eou pipefail
33

44
touch /var/log/services/gotrue.log
55

6-
/usr/local/bin/configure-shim.sh /dist/gotrue /opt/gotrue/gotrue
6+
GOTRUE_CUSTOM_DIR="${DATA_VOLUME_MOUNTPOINT}/etc/gotrue"
7+
GOTRUE_CUSTOM_CONFIG_FILE_PATH="${DATA_VOLUME_MOUNTPOINT}/etc/gotrue/gotrue.env"
78

8-
sed -i "s|api_external_url|${API_EXTERNAL_URL:-http://localhost}|g" /etc/gotrue.env
9-
sed -i "s|gotrue_api_host|${GOTRUE_API_HOST:-0.0.0.0}|g" /etc/gotrue.env
10-
sed -i "s|gotrue_site_url|$GOTRUE_SITE_URL|g" /etc/gotrue.env
11-
sed -i "s|gotrue_jwt_secret|$JWT_SECRET|g" /etc/gotrue.env
9+
/usr/local/bin/configure-shim.sh /dist/gotrue /opt/gotrue/gotrue
1210

1311
if [ "${DATA_VOLUME_MOUNTPOINT}" ]; then
14-
GOTRUE_CUSTOM_CONFIG_FILE_PATH="${DATA_VOLUME_MOUNTPOINT}/etc/gotrue.env"
12+
mkdir -p "${GOTRUE_CUSTOM_DIR}"
13+
chown adminapi:adminapi "${GOTRUE_CUSTOM_DIR}"
14+
1515
if [ ! -f "${CONFIGURED_FLAG_PATH}" ]; then
1616
echo "Copying existing GoTrue config from /etc/gotrue.env to ${GOTRUE_CUSTOM_CONFIG_FILE_PATH}"
1717
cp "/etc/gotrue.env" "${GOTRUE_CUSTOM_CONFIG_FILE_PATH}"
@@ -27,6 +27,11 @@ fi
2727

2828
if [ -f "${INIT_PAYLOAD_PATH:-}" ]; then
2929
echo "init gotrue payload"
30-
tar -xzvf "$INIT_PAYLOAD_PATH" -C / ./etc/gotrue.env
30+
tar -h --overwrite -xzvf "$INIT_PAYLOAD_PATH" -C / ./etc/gotrue.env
3131
chown -R adminapi:adminapi /etc/gotrue.env
32+
else
33+
sed -i "s|api_external_url|${API_EXTERNAL_URL:-http://localhost}|g" /etc/gotrue.env
34+
sed -i "s|gotrue_api_host|${GOTRUE_API_HOST:-0.0.0.0}|g" /etc/gotrue.env
35+
sed -i "s|gotrue_site_url|$GOTRUE_SITE_URL|g" /etc/gotrue.env
36+
sed -i "s|gotrue_jwt_secret|$JWT_SECRET|g" /etc/gotrue.env
3237
fi

0 commit comments

Comments
 (0)