Skip to content

Commit e48b505

Browse files
authored
chore: preserve binaries across reboots (#708)
* chore: preserve binaries across reboots * chore: add shims * chore: reference binaries using dangling symlinks
1 parent 56578fb commit e48b505

File tree

10 files changed

+121
-40
lines changed

10 files changed

+121
-40
lines changed

docker/all-in-one/Dockerfile

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,25 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
138138
less \
139139
# pg_egress_collect deps
140140
tcpdump libio-async-perl \
141-
&& rm -rf /var/lib/apt/lists/* /tmp/*
141+
&& rm -rf /var/lib/apt/lists/* /tmp/* \
142+
&& mkdir -p /dist \
143+
&& mkdir -p /data/opt && chmod go+rwx /data/opt
142144

143145
# Copy single binary dependencies
144-
COPY --from=pgrst /bin/postgrest /opt/
145-
COPY --from=gotrue /usr/local/bin/gotrue /opt/gotrue/
146+
COPY --from=pgrst /bin/postgrest /dist/
147+
COPY --from=gotrue /usr/local/bin/gotrue /dist/
146148
COPY --from=gotrue /usr/local/etc/gotrue /opt/gotrue/
147-
COPY --from=adminapi /tmp/supabase-admin-api /opt/
148-
COPY --chown=root:root --from=adminmgr /tmp/admin-mgr /usr/bin/
149+
COPY --from=adminapi /tmp/supabase-admin-api /dist/
150+
COPY --chown=root:root --from=adminmgr /tmp/admin-mgr /dist/
149151
COPY --from=exporter /tmp/postgres_exporter /opt/postgres_exporter/
150152
COPY docker/all-in-one/opt/postgres_exporter /opt/postgres_exporter/
151153

154+
# Configuring dangling symlinks for binaries
155+
RUN ln -s /data/opt/supabase-admin-api /opt/supabase-admin-api \
156+
&& ln -s /data/opt/postgrest /opt/postgrest \
157+
&& ln -s /data/opt/gotrue /opt/gotrue/gotrue \
158+
&& ln -s /data/opt/admin-mgr /usr/bin/admin-mgr
159+
152160
# Scripts for adminapi
153161
COPY ansible/files/admin_api_scripts /root
154162
COPY --chown=adminapi:adminapi docker/all-in-one/etc/adminapi /etc/adminapi
@@ -175,6 +183,7 @@ COPY docker/all-in-one/etc/fail2ban/filter.d /etc/fail2ban/filter.d/
175183
COPY docker/all-in-one/etc/fail2ban/jail.d /etc/fail2ban/jail.d/
176184

177185
# Customizations for postgrest
186+
COPY --chown=postgrest:postgrest docker/all-in-one/etc/postgrest/bootstrap.sh /etc/postgrest/bootstrap.sh
178187
COPY --chown=postgrest:postgrest docker/all-in-one/etc/postgrest/base.conf /etc/postgrest/base.conf
179188
COPY --chown=postgrest:postgrest docker/all-in-one/etc/postgrest/generated.conf /etc/postgrest/generated.conf
180189

@@ -192,7 +201,8 @@ COPY --chown=vector:vector docker/all-in-one/etc/vector/vector.yaml /etc/vector/
192201
COPY docker/all-in-one/etc/supervisor /etc/supervisor
193202

194203
# Customizations for supa-shutdown
195-
COPY --chown=adminapi:adminapi docker/all-in-one/etc/supa-shutdown /etc/supa-shutdown
204+
COPY --chown=adminapi:adminapi docker/all-in-one/etc/supa-shutdown /etc/supa-shutdown
205+
COPY docker/all-in-one/configure-shim.sh /usr/local/bin/configure-shim.sh
196206

197207
# Configure service ports
198208
ENV PGRST_SERVER_PORT=3000
@@ -229,7 +239,13 @@ RUN useradd --create-home --shell /bin/bash postgrest && \
229239
RUN mkdir -p /etc/wal-g && \
230240
chown -R adminapi:adminapi /etc/wal-g && \
231241
chmod g+w /etc/wal-g
232-
RUN mkdir -p /var/log/wal-g && chown -R postgres:postgres /var/log/wal-g && chmod +x /usr/bin/admin-mgr && chmod ug+s /usr/bin/admin-mgr && touch /etc/wal-g/config.json && chown adminapi:adminapi /etc/wal-g/config.json && echo '{}' > /etc/wal-g/config.json
242+
RUN mkdir -p /var/log/wal-g \
243+
&& chown -R postgres:postgres /var/log/wal-g \
244+
&& chmod +x /dist/admin-mgr \
245+
&& chmod ug+s /dist/admin-mgr \
246+
&& touch /etc/wal-g/config.json \
247+
&& chown adminapi:adminapi /etc/wal-g/config.json \
248+
&& echo '{}' > /etc/wal-g/config.json
233249
RUN chown -R adminapi:adminapi /etc/adminapi
234250

235251
# Add healthcheck and entrypoint scripts

docker/all-in-one/configure-shim.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
INITIAL_BINARY_PATH=$1
4+
SYMLINK_PATH=$2
5+
6+
SYMLINK_TARGET=$(readlink -f "$SYMLINK_PATH")
7+
8+
if [ ! -f "$SYMLINK_TARGET" ]; then
9+
cp "$INITIAL_BINARY_PATH" "$SYMLINK_TARGET"
10+
11+
PERMS=$(stat -c "%a" "$INITIAL_BINARY_PATH")
12+
chmod "$PERMS" "$SYMLINK_TARGET"
13+
14+
OWNER_GROUP=$(stat -c "%u:%g" "$INITIAL_BINARY_PATH")
15+
chown "$OWNER_GROUP" "$SYMLINK_TARGET"
16+
fi

docker/all-in-one/entrypoint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ function setup_postgres {
8686
$PG_CONF
8787

8888
if [ "${DATA_VOLUME_MOUNTPOINT}" ]; then
89+
/usr/local/bin/configure-shim.sh /dist/supabase-admin-api /opt/supabase-admin-api
8990
/opt/supabase-admin-api optimize db --destination-config-file-path /etc/postgresql-custom/generated-optimizations.conf
9091

9192
# Preserve postgresql configs across restarts
@@ -184,7 +185,7 @@ export INIT_PAYLOAD_PATH=${INIT_PAYLOAD_PATH:-/tmp/payload.tar.gz}
184185

185186
if [ "${INIT_PAYLOAD_PRESIGNED_URL:-}" ]; then
186187
curl -fsSL "$INIT_PAYLOAD_PRESIGNED_URL" -o "/tmp/payload.tar.gz" || true
187-
if [ -f "/tmp/payload.tar.gz" ]; then
188+
if [ -f "/tmp/payload.tar.gz" ] && [ "/tmp/payload.tar.gz" != "$INIT_PAYLOAD_PATH" ] ; then
188189
mv "/tmp/payload.tar.gz" "$INIT_PAYLOAD_PATH"
189190
fi
190191
fi
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#! /usr/bin/env bash
2+
set -euo pipefail
3+
set -x
4+
5+
cd "$(dirname "$0")"
6+
cat $@ > merged.conf
7+
8+
/opt/postgrest merged.conf

docker/all-in-one/etc/supervisor/services/postgrest.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[program:postgrest]
2-
command=/opt/postgrest /etc/postgrest/generated.conf
2+
command=/etc/postgrest/bootstrap.sh /etc/postgrest/generated.conf /etc/postgrest/base.conf
33
user=postgrest
44
autorestart=true
55
autostart=true
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -eou pipefail
3+
4+
/usr/local/bin/configure-shim.sh /dist/admin-mgr /usr/bin/admin-mgr

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ ADMIN_API_CONF=/etc/adminapi/adminapi.yaml
55
touch /var/log/services/adminapi.log
66

77
ADMINAPI_CUSTOM_DIR="${DATA_VOLUME_MOUNTPOINT}/etc/adminapi"
8-
mkdir -p "${ADMINAPI_CUSTOM_DIR}"
9-
if [ ! -f "${CONFIGURED_FLAG_PATH}" ]; then
10-
echo "Copying existing custom adminapi config from /etc/adminapi to ${ADMINAPI_CUSTOM_DIR}"
11-
cp -R "/etc/adminapi/." "${ADMINAPI_CUSTOM_DIR}/"
12-
fi
138

14-
rm -rf "/etc/adminapi"
15-
ln -s "${ADMINAPI_CUSTOM_DIR}" "/etc/adminapi"
16-
chown -R adminapi:adminapi "/etc/adminapi"
17-
chown -R adminapi:adminapi "${ADMINAPI_CUSTOM_DIR}"
18-
chmod g+rx "${ADMINAPI_CUSTOM_DIR}"
9+
/usr/local/bin/configure-shim.sh /dist/supabase-admin-api /opt/supabase-admin-api
10+
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
1925

2026
if [ -f "${INIT_PAYLOAD_PATH:-}" ]; then
2127
echo "init adminapi payload"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
set -eou pipefail
3+
4+
mkdir -p /etc/supa-shutdown
5+
6+
AUTOSHUTDOWN_CUSTOM_DIR="${DATA_VOLUME_MOUNTPOINT}/supa-shutdown"
7+
if [ "${DATA_VOLUME_MOUNTPOINT}" ]; then
8+
mkdir -p "${AUTOSHUTDOWN_CUSTOM_DIR}"
9+
10+
AUTOSHUTDOWN_CUSTOM_CONFIG_FILE_PATH="${AUTOSHUTDOWN_CUSTOM_DIR}/shutdown.conf"
11+
if [ ! -f "${AUTOSHUTDOWN_CUSTOM_CONFIG_FILE_PATH}" ]; then
12+
echo "Copying existing custom shutdown config from /etc/supa-shutdown to ${AUTOSHUTDOWN_CUSTOM_CONFIG_FILE_PATH}"
13+
cp "/etc/supa-shutdown/shutdown.conf" "${AUTOSHUTDOWN_CUSTOM_CONFIG_FILE_PATH}"
14+
fi
15+
16+
rm -f "/etc/supa-shutdown/shutdown.conf"
17+
ln -s "${AUTOSHUTDOWN_CUSTOM_CONFIG_FILE_PATH}" "/etc/supa-shutdown/shutdown.conf"
18+
chown -R adminapi:adminapi "/etc/supa-shutdown/shutdown.conf"
19+
chown -R adminapi:adminapi "${AUTOSHUTDOWN_CUSTOM_CONFIG_FILE_PATH}"
20+
fi

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@ set -eou pipefail
33

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

6+
/usr/local/bin/configure-shim.sh /dist/gotrue /opt/gotrue/gotrue
7+
68
sed -i "s|gotrue_api_host|${GOTRUE_API_HOST:-0.0.0.0}|g" /etc/gotrue.env
79
sed -i "s|gotrue_site_url|$GOTRUE_SITE_URL|g" /etc/gotrue.env
810
sed -i "s|gotrue_jwt_secret|$JWT_SECRET|g" /etc/gotrue.env
911

10-
GOTRUE_CUSTOM_CONFIG_FILE_PATH="${DATA_VOLUME_MOUNTPOINT}/etc/gotrue.env"
11-
if [ ! -f "${CONFIGURED_FLAG_PATH}" ]; then
12-
echo "Copying existing GoTrue config from /etc/gotrue.env to ${GOTRUE_CUSTOM_CONFIG_FILE_PATH}"
13-
cp "/etc/gotrue.env" "${GOTRUE_CUSTOM_CONFIG_FILE_PATH}"
14-
fi
12+
if [ "${DATA_VOLUME_MOUNTPOINT}" ]; then
13+
GOTRUE_CUSTOM_CONFIG_FILE_PATH="${DATA_VOLUME_MOUNTPOINT}/etc/gotrue.env"
14+
if [ ! -f "${CONFIGURED_FLAG_PATH}" ]; then
15+
echo "Copying existing GoTrue config from /etc/gotrue.env to ${GOTRUE_CUSTOM_CONFIG_FILE_PATH}"
16+
cp "/etc/gotrue.env" "${GOTRUE_CUSTOM_CONFIG_FILE_PATH}"
17+
fi
1518

16-
rm "/etc/gotrue.env"
17-
ln -s "${GOTRUE_CUSTOM_CONFIG_FILE_PATH}" "/etc/gotrue.env"
18-
chown -R adminapi:adminapi "/etc/gotrue.env"
19-
chown -R adminapi:adminapi "${GOTRUE_CUSTOM_CONFIG_FILE_PATH}"
20-
chmod g+rx "${GOTRUE_CUSTOM_CONFIG_FILE_PATH}"
19+
rm -f "/etc/gotrue.env"
20+
ln -s "${GOTRUE_CUSTOM_CONFIG_FILE_PATH}" "/etc/gotrue.env"
21+
chown -R adminapi:adminapi "/etc/gotrue.env"
22+
23+
chown -R adminapi:adminapi "${GOTRUE_CUSTOM_CONFIG_FILE_PATH}"
24+
chmod g+rx "${GOTRUE_CUSTOM_CONFIG_FILE_PATH}"
25+
fi
2126

2227
if [ -f "${INIT_PAYLOAD_PATH:-}" ]; then
2328
echo "init gotrue payload"

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ sed -i "s|pgrst_db_extra_search_path|${PGRST_DB_SCHEMAS:-public,extensions}|g" /
1111
sed -i "s|pgrst_db_anon_role|${PGRST_DB_ANON_ROLE:-anon}|g" /etc/postgrest/base.conf
1212
sed -i "s|pgrst_jwt_secret|$JWT_SECRET|g" /etc/postgrest/base.conf
1313

14-
POSTGREST_CUSTOM_DIR="${DATA_VOLUME_MOUNTPOINT}/etc/postgrest"
15-
mkdir -p "${POSTGREST_CUSTOM_DIR}"
16-
if [ ! -f "${CONFIGURED_FLAG_PATH}" ]; then
17-
echo "Copying existing custom PostgREST config from /etc/postgrest/ to ${POSTGREST_CUSTOM_DIR}"
18-
cp -R "/etc/postgrest/." "${POSTGREST_CUSTOM_DIR}/"
19-
fi
14+
/usr/local/bin/configure-shim.sh /dist/postgrest /opt/postgrest
15+
16+
if [ "${DATA_VOLUME_MOUNTPOINT}" ]; then
17+
POSTGREST_CUSTOM_DIR="${DATA_VOLUME_MOUNTPOINT}/etc/postgrest"
18+
mkdir -p "${POSTGREST_CUSTOM_DIR}"
19+
if [ ! -f "${CONFIGURED_FLAG_PATH}" ]; then
20+
echo "Copying existing custom PostgREST config from /etc/postgrest/ to ${POSTGREST_CUSTOM_DIR}"
21+
cp -R "/etc/postgrest/." "${POSTGREST_CUSTOM_DIR}/"
22+
fi
2023

21-
rm -rf "/etc/postgrest"
22-
ln -s "${POSTGREST_CUSTOM_DIR}" "/etc/postgrest"
23-
chown -R postgrest:postgrest "/etc/postgrest"
24-
chown -R postgrest:postgrest "${POSTGREST_CUSTOM_DIR}"
25-
chmod g+rx "${POSTGREST_CUSTOM_DIR}"
24+
rm -rf "/etc/postgrest"
25+
ln -s "${POSTGREST_CUSTOM_DIR}" "/etc/postgrest"
26+
chown -R postgrest:postgrest "/etc/postgrest"
27+
28+
chown -R postgrest:postgrest "${POSTGREST_CUSTOM_DIR}"
29+
chmod g+rx "${POSTGREST_CUSTOM_DIR}"
30+
fi
2631

2732
if [ -f "${INIT_PAYLOAD_PATH:-}" ]; then
2833
echo "init postgrest payload"

0 commit comments

Comments
 (0)