Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit cf4a8fe

Browse files
reivilibreMathieu Velten
authored andcommitted
Add --editable flag to complement.sh which uses an editable install of Synapse for faster turn-around times whilst developing iteratively. (#14548)
Co-authored-by: Mathieu Velten <[email protected]>
1 parent 8b733d3 commit cf4a8fe

File tree

5 files changed

+162
-20
lines changed

5 files changed

+162
-20
lines changed

changelog.d/14548.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `--editable` flag to `complement.sh` which uses an editable install of Synapse for faster turn-around times whilst developing iteratively.

docker/Dockerfile-workers

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# syntax=docker/dockerfile:1
22

33
ARG SYNAPSE_VERSION=latest
4+
ARG FROM=matrixdotorg/synapse:$SYNAPSE_VERSION
45

56
# first of all, we create a base image with an nginx which we can copy into the
67
# target image. For repeated rebuilds, this is much faster than apt installing
@@ -23,7 +24,7 @@ FROM debian:bullseye-slim AS deps_base
2324
FROM redis:6-bullseye AS redis_base
2425

2526
# now build the final image, based on the the regular Synapse docker image
26-
FROM matrixdotorg/synapse:$SYNAPSE_VERSION
27+
FROM $FROM
2728

2829
# Install supervisord with pip instead of apt, to avoid installing a second
2930
# copy of python.

docker/complement/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
# https://github.com/matrix-org/synapse/blob/develop/docker/README-testing.md#testing-with-postgresql-and-single-or-multi-process-synapse
88

99
ARG SYNAPSE_VERSION=latest
10+
ARG FROM=matrixdotorg/synapse-workers:$SYNAPSE_VERSION
1011

11-
FROM matrixdotorg/synapse-workers:$SYNAPSE_VERSION
12+
FROM $FROM
1213
# First of all, we copy postgres server from the official postgres image,
1314
# since for repeated rebuilds, this is much faster than apt installing
1415
# postgres each time.

docker/editable.Dockerfile

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# syntax=docker/dockerfile:1
2+
# This dockerfile builds an editable install of Synapse.
3+
#
4+
# Used by `complement.sh`. Not suitable for production use.
5+
6+
ARG PYTHON_VERSION=3.9
7+
8+
###
9+
### Stage 0: generate requirements.txt
10+
###
11+
# We hardcode the use of Debian bullseye here because this could change upstream
12+
# and other Dockerfiles used for testing are expecting bullseye.
13+
FROM docker.io/python:${PYTHON_VERSION}-slim-bullseye
14+
15+
# Install Rust and other dependencies (stolen from normal Dockerfile)
16+
# install the OS build deps
17+
RUN \
18+
--mount=type=cache,target=/var/cache/apt,sharing=locked \
19+
--mount=type=cache,target=/var/lib/apt,sharing=locked \
20+
apt-get update -qq && apt-get install -yqq \
21+
build-essential \
22+
libffi-dev \
23+
libjpeg-dev \
24+
libpq-dev \
25+
libssl-dev \
26+
libwebp-dev \
27+
libxml++2.6-dev \
28+
libxslt1-dev \
29+
openssl \
30+
zlib1g-dev \
31+
git \
32+
curl \
33+
gosu \
34+
libjpeg62-turbo \
35+
libpq5 \
36+
libwebp6 \
37+
xmlsec1 \
38+
libjemalloc2 \
39+
&& rm -rf /var/lib/apt/lists/*
40+
ENV RUSTUP_HOME=/rust
41+
ENV CARGO_HOME=/cargo
42+
ENV PATH=/cargo/bin:/rust/bin:$PATH
43+
RUN mkdir /rust /cargo
44+
RUN curl -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --default-toolchain stable --profile minimal
45+
46+
47+
# Make a base copy of the editable source tree, so that we have something to
48+
# install and build now — even though it's going to be covered up by a mount
49+
# at runtime.
50+
COPY synapse /editable-src/synapse/
51+
COPY rust /editable-src/rust/
52+
# ... and what we need to `pip install`.
53+
COPY pyproject.toml poetry.lock README.rst build_rust.py Cargo.toml Cargo.lock /editable-src/
54+
55+
RUN pip install poetry
56+
RUN poetry config virtualenvs.create false
57+
RUN cd /editable-src && poetry install --extras all
58+
59+
# Make copies of useful things for inspection:
60+
# - the Rust module (must be copied to the editable source tree before startup)
61+
# - poetry.lock is useful for checking if dependencies have changed.
62+
RUN cp /editable-src/synapse/synapse_rust.abi3.so /synapse_rust.abi3.so.bak
63+
RUN cp /editable-src/poetry.lock /poetry.lock.bak
64+
65+
66+
### Extra setup from original Dockerfile
67+
COPY ./docker/start.py /start.py
68+
COPY ./docker/conf /conf
69+
70+
EXPOSE 8008/tcp 8009/tcp 8448/tcp
71+
72+
ENTRYPOINT ["/start.py"]
73+
74+
HEALTHCHECK --start-period=5s --interval=15s --timeout=5s \
75+
CMD curl -fSs http://localhost:8008/health || exit 1

scripts-dev/complement.sh

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ Run the complement test suite on Synapse.
5353
Only build the Docker images. Don't actually run Complement.
5454
Conflicts with -f/--fast.
5555
56+
-e, --editable
57+
Use an editable build of Synapse, rebuilding the image if necessary.
58+
This is suitable for use in development where a fast turn-around time
59+
is important.
60+
Not suitable for use in CI in case the editable environment is impure.
61+
5662
For help on arguments to 'go test', run 'go help testflag'.
5763
EOF
5864
}
@@ -73,6 +79,9 @@ while [ $# -ge 1 ]; do
7379
"--build-only")
7480
skip_complement_run=1
7581
;;
82+
"-e"|"--editable")
83+
use_editable_synapse=1
84+
;;
7685
*)
7786
# unknown arg: presumably an argument to gotest. break the loop.
7887
break
@@ -96,25 +105,76 @@ if [[ -z "$COMPLEMENT_DIR" ]]; then
96105
echo "Checkout available at 'complement-${COMPLEMENT_REF}'"
97106
fi
98107

108+
if [ -n "$use_editable_synapse" ]; then
109+
if [[ -e synapse/synapse_rust.abi3.so ]]; then
110+
# In an editable install, back up the host's compiled Rust module to prevent
111+
# inconvenience; the container will overwrite the module with its own copy.
112+
mv -n synapse/synapse_rust.abi3.so synapse/synapse_rust.abi3.so~host
113+
# And restore it on exit:
114+
synapse_pkg=`realpath synapse`
115+
trap "mv -f '$synapse_pkg/synapse_rust.abi3.so~host' '$synapse_pkg/synapse_rust.abi3.so'" EXIT
116+
fi
117+
118+
editable_mount="$(realpath .):/editable-src:z"
119+
if docker inspect complement-synapse-editable &>/dev/null; then
120+
# complement-synapse-editable already exists: see if we can still use it:
121+
# - The Rust module must still be importable; it will fail to import if the Rust source has changed.
122+
# - The Poetry lock file must be the same (otherwise we assume dependencies have changed)
123+
124+
# First set up the module in the right place for an editable installation.
125+
docker run --rm -v $editable_mount --entrypoint 'cp' complement-synapse-editable -- /synapse_rust.abi3.so.bak /editable-src/synapse/synapse_rust.abi3.so
126+
127+
if (docker run --rm -v $editable_mount --entrypoint 'python' complement-synapse-editable -c 'import synapse.synapse_rust' \
128+
&& docker run --rm -v $editable_mount --entrypoint 'diff' complement-synapse-editable --brief /editable-src/poetry.lock /poetry.lock.bak); then
129+
skip_docker_build=1
130+
else
131+
echo "Editable Synapse image is stale. Will rebuild."
132+
unset skip_docker_build
133+
fi
134+
fi
135+
fi
136+
99137
if [ -z "$skip_docker_build" ]; then
100-
# Build the base Synapse image from the local checkout
101-
echo_if_github "::group::Build Docker image: matrixdotorg/synapse"
102-
docker build -t matrixdotorg/synapse \
103-
--build-arg TEST_ONLY_SKIP_DEP_HASH_VERIFICATION \
104-
--build-arg TEST_ONLY_IGNORE_POETRY_LOCKFILE \
105-
-f "docker/Dockerfile" .
106-
echo_if_github "::endgroup::"
107-
108-
# Build the workers docker image (from the base Synapse image we just built).
109-
echo_if_github "::group::Build Docker image: matrixdotorg/synapse-workers"
110-
docker build -t matrixdotorg/synapse-workers -f "docker/Dockerfile-workers" .
111-
echo_if_github "::endgroup::"
112-
113-
# Build the unified Complement image (from the worker Synapse image we just built).
114-
echo_if_github "::group::Build Docker image: complement/Dockerfile"
115-
docker build -t complement-synapse \
116-
-f "docker/complement/Dockerfile" "docker/complement"
117-
echo_if_github "::endgroup::"
138+
if [ -n "$use_editable_synapse" ]; then
139+
140+
# Build a special image designed for use in development with editable
141+
# installs.
142+
docker build -t synapse-editable \
143+
-f "docker/editable.Dockerfile" .
144+
145+
docker build -t synapse-workers-editable \
146+
--build-arg FROM=synapse-editable \
147+
-f "docker/Dockerfile-workers" .
148+
149+
docker build -t complement-synapse-editable \
150+
--build-arg FROM=synapse-workers-editable \
151+
-f "docker/complement/Dockerfile" "docker/complement"
152+
153+
# Prepare the Rust module
154+
docker run --rm -v $editable_mount --entrypoint 'cp' complement-synapse-editable -- /synapse_rust.abi3.so.bak /editable-src/synapse/synapse_rust.abi3.so
155+
156+
else
157+
158+
# Build the base Synapse image from the local checkout
159+
echo_if_github "::group::Build Docker image: matrixdotorg/synapse"
160+
docker build -t matrixdotorg/synapse \
161+
--build-arg TEST_ONLY_SKIP_DEP_HASH_VERIFICATION \
162+
--build-arg TEST_ONLY_IGNORE_POETRY_LOCKFILE \
163+
-f "docker/Dockerfile" .
164+
echo_if_github "::endgroup::"
165+
166+
# Build the workers docker image (from the base Synapse image we just built).
167+
echo_if_github "::group::Build Docker image: matrixdotorg/synapse-workers"
168+
docker build -t matrixdotorg/synapse-workers -f "docker/Dockerfile-workers" .
169+
echo_if_github "::endgroup::"
170+
171+
# Build the unified Complement image (from the worker Synapse image we just built).
172+
echo_if_github "::group::Build Docker image: complement/Dockerfile"
173+
docker build -t complement-synapse \
174+
-f "docker/complement/Dockerfile" "docker/complement"
175+
echo_if_github "::endgroup::"
176+
177+
fi
118178
fi
119179

120180
if [ -n "$skip_complement_run" ]; then
@@ -123,6 +183,10 @@ if [ -n "$skip_complement_run" ]; then
123183
fi
124184

125185
export COMPLEMENT_BASE_IMAGE=complement-synapse
186+
if [ -n "$use_editable_synapse" ]; then
187+
export COMPLEMENT_BASE_IMAGE=complement-synapse-editable
188+
export COMPLEMENT_HOST_MOUNTS="$editable_mount"
189+
fi
126190

127191
extra_test_args=()
128192

0 commit comments

Comments
 (0)