Skip to content

Commit e1a2d42

Browse files
committed
Use multi-stage Dockerfile for building customized Synape
This also reverts e5574a4 because: - it was causing issues on some servers (not clear why) - such workarounds are no longer necessary when doing multi-stage building.
1 parent e5574a4 commit e1a2d42

File tree

2 files changed

+40
-63
lines changed

2 files changed

+40
-63
lines changed

roles/custom/matrix-synapse/defaults/main.yml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,6 @@ matrix_synapse_container_image_customizations_s3_storage_provider_installation_e
5252
# https://github.com/aws/aws-cli/issues/9214
5353
matrix_synapse_container_image_customizations_s3_storage_provider_installation_old_boto_workaround_enabled: true
5454

55-
# Controls whether to install libldapXX explicitly when installing s3-storage-provider.
56-
# This is to work around it potentially not being there (after `autoremove` invoked by other scripts cleans it up, etc.)
57-
#
58-
# Some scripts in the custom Dockerfile may install and then autoremove git/ssh/openssh-client.
59-
# This has the side-effect of removing the libldap library that s3-storage-provider depends upon indirectly (via psycopg2).
60-
#
61-
# If this should happen, s3-storage-provider (the `s3_media_upload` script) will fail to start with an error like this:
62-
# > Traceback (most recent call last):
63-
# > File "/usr/local/bin/s3_media_upload", line 10, in <module>
64-
# > import psycopg2
65-
# > File "/usr/local/lib/python3.12/site-packages/psycopg2/__init__.py", line 51, in <module>
66-
# > from psycopg2._psycopg import ( # noqa
67-
# > ImportError: libldap-2.5.so.0: cannot open shared object file: No such file or directory
68-
#
69-
# The library (e.g. `/usr/lib/x86_64-linux-gnu/libldap-2.5.so.0`) appears to be available by default in the upstream Synapse image for some reason,
70-
# but it doesn't seem to be installed through a Debian package. Autoremoval would remove it, causing s3-storage-provider to fail.
71-
# Given that this is a dependency for s3-storage-provider (psycopg2), we prefer to install it explicitly.
72-
matrix_synapse_container_image_customizations_s3_storage_provider_installation_explicit_libldap_installation_enabled: true
73-
matrix_synapse_container_image_customizations_s3_storage_provider_installation_explicit_libldap_installation_package_name: libldap-2.5
74-
7555
# Controls whether custom build steps will be added to the Dockerfile for installing auto-accept-invite module.
7656
# The version that will be installed is specified in `matrix_synapse_ext_synapse_auto_accept_invite_version`.
7757
matrix_synapse_container_image_customizations_auto_accept_invite_installation_enabled: "{{ matrix_synapse_ext_synapse_auto_accept_invite_enabled }}"
Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
#jinja2: lstrip_blocks: "True"
2+
{% if matrix_synapse_container_image_customizations_templates_enabled %}
3+
FROM {{ matrix_synapse_docker_image }} AS templates-builder
4+
5+
{#
6+
This ugly script below does quite a lot:
7+
- installs git and other dependencies temporarily, just so we could do a shallow-clone
8+
- prepare the SSH config: keyscanning (if enabled), private key (if enabled)
9+
- performs a git shallow clone with just the branch we need
10+
- makes sure the files are owned by the user that will actually run the container later
11+
#}
12+
{% set dependencies = ['git', 'ssh', 'openssh-client'] %}
13+
{% if matrix_synapse_container_image_customizations_templates_git_repository_ssh_private_key %}
14+
RUN echo '{{ matrix_synapse_container_image_customizations_templates_git_repository_ssh_private_key | b64encode }}' | base64 -d > /custom-templates-private-key
15+
RUN chmod 400 /custom-templates-private-key
16+
{% endif %}
17+
18+
RUN apt-get update && apt-get install --no-install-recommends -y {{ dependencies | join(' ') }}
19+
20+
{% if matrix_synapse_container_image_customizations_templates_git_repository_keyscan_enabled %}
21+
RUN mkdir ~/.ssh
22+
RUN chmod 700 ~/.ssh
23+
RUN ssh-keyscan -t rsa {{ matrix_synapse_container_image_customizations_templates_git_repository_keyscan_hostname }} >> ~/.ssh/known_hosts
24+
{% endif %}
25+
26+
RUN {% if matrix_synapse_container_image_customizations_templates_git_repository_ssh_private_key %}GIT_SSH_COMMAND='ssh -i /custom-templates-private-key'{% endif %} git \
27+
clone \
28+
--branch={{ matrix_synapse_container_image_customizations_templates_git_repository_branch }} \
29+
--depth=1 \
30+
--single-branch \
31+
--no-tags \
32+
{{ matrix_synapse_container_image_customizations_templates_git_repository_url }} \
33+
{{ matrix_synapse_container_image_customizations_templates_in_container_base_path }}
34+
35+
RUN /bin/sh -c 'cd {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }} && git rev-parse HEAD > git-revision.txt'
36+
RUN rm -rf {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }}/.git
37+
38+
RUN chown -R {{ matrix_synapse_uid }}:{{ matrix_synapse_gid }} {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }}
39+
{% endif %}
40+
241
FROM {{ matrix_synapse_docker_image }}
342

443
{% if matrix_synapse_container_image_customizations_auto_accept_invite_installation_enabled %}
@@ -11,52 +50,10 @@ RUN pip install 'boto3<1.36.0' 'botocore<1.36.0' synapse-s3-storage-provider=={{
1150
{% else %}
1251
RUN pip install synapse-s3-storage-provider=={{ matrix_synapse_ext_synapse_s3_storage_provider_version }}
1352
{% endif %}
14-
{% if matrix_synapse_container_image_customizations_s3_storage_provider_installation_explicit_libldap_installation_enabled %}
15-
RUN apt-get update && apt-get install --no-install-recommends -y {{ matrix_synapse_container_image_customizations_s3_storage_provider_installation_explicit_libldap_installation_package_name }}
16-
{% endif %}
1753
{% endif %}
1854
1955
{% if matrix_synapse_container_image_customizations_templates_enabled %}
20-
{#
21-
This ugly script below does quite a lot:
22-
- installs git and other dependencies temporarily, just so we could do a shallow-clone
23-
- prepare the SSH config: keyscanning (if enabled), private key (if enabled)
24-
- performs a git shallow clone with just the branch we need
25-
- makes sure the files are owned by the user that will actually run the container later
26-
- removes the `.git` directory to save space, but keeps git revision in `git-revision.txt`, should we need it for debugging
27-
- finally, verifies that the templates path can indeed be found within the base path (sanity check)
28-
#}
29-
{% set dependencies = ['git', 'ssh', 'openssh-client'] %}
30-
RUN \
31-
{% if matrix_synapse_container_image_customizations_templates_git_repository_ssh_private_key %}
32-
echo '{{ matrix_synapse_container_image_customizations_templates_git_repository_ssh_private_key | b64encode }}' | base64 -d > /custom-templates-private-key && \
33-
chmod 400 /custom-templates-private-key && \
34-
{% endif %}
35-
apt-get update && \
36-
apt-get install --no-install-recommends -y {{ dependencies | join(' ') }} && \
37-
{% if matrix_synapse_container_image_customizations_templates_git_repository_keyscan_enabled %}
38-
mkdir ~/.ssh && \
39-
chmod 700 ~/.ssh && \
40-
ssh-keyscan -t rsa {{ matrix_synapse_container_image_customizations_templates_git_repository_keyscan_hostname }} >> ~/.ssh/known_hosts && \
41-
{% endif %}
42-
{% if matrix_synapse_container_image_customizations_templates_git_repository_ssh_private_key %}GIT_SSH_COMMAND='ssh -i /custom-templates-private-key'{% endif %} git \
43-
clone \
44-
--branch={{ matrix_synapse_container_image_customizations_templates_git_repository_branch }} \
45-
--depth=1 \
46-
--single-branch \
47-
--no-tags \
48-
{{ matrix_synapse_container_image_customizations_templates_git_repository_url }} \
49-
{{ matrix_synapse_container_image_customizations_templates_in_container_base_path }} && \
50-
/bin/sh -c 'cd {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }} && git rev-parse HEAD > git-revision.txt' && \
51-
rm -rf {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }}/.git && \
52-
chown -R {{ matrix_synapse_uid }}:{{ matrix_synapse_gid }} {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }} && \
53-
apt-get autoremove -y {{ dependencies | join(' ') }} && \
54-
{% if matrix_synapse_container_image_customizations_templates_git_repository_ssh_private_key %}
55-
rm /custom-templates-private-key && \
56-
{% endif %}
57-
true
58-
59-
RUN /bin/sh -c 'stat {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }}/{{ matrix_synapse_container_image_customizations_templates_in_container_template_files_relative_path }} || exit 1'
56+
COPY --from=templates-builder {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }} {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }}
6057
{% endif %}
6158
6259
{{ matrix_synapse_container_image_customizations_dockerfile_body_custom }}

0 commit comments

Comments
 (0)