Matrix Docker Ansible eploy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

151 lines
6.5 KiB

  1. ---
  2. # This will throw a Permission Denied error if already mounted using fuse
  3. - name: Check Synapse media store path
  4. ansible.builtin.stat:
  5. path: "{{ matrix_synapse_media_store_path }}"
  6. register: local_path_media_store_stat
  7. ignore_errors: true
  8. # This is separate and conditional, to ensure we don't execute it
  9. # if the path already exists or we failed to check, because it's mounted using fuse.
  10. - name: Ensure Synapse media store path exists
  11. ansible.builtin.file:
  12. path: "{{ matrix_synapse_media_store_path }}"
  13. state: directory
  14. mode: 0750
  15. owner: "{{ matrix_synapse_uid }}"
  16. group: "{{ matrix_synapse_gid }}"
  17. when: "not local_path_media_store_stat.failed and not local_path_media_store_stat.stat.exists"
  18. - when: "matrix_synapse_container_image_self_build | bool"
  19. block:
  20. - name: Ensure Synapse repository is present on self-build
  21. ansible.builtin.git:
  22. repo: "{{ matrix_synapse_container_image_self_build_repo }}"
  23. dest: "{{ matrix_synapse_docker_src_files_path }}"
  24. version: "{{ matrix_synapse_docker_image.split(':')[1] }}"
  25. force: "yes"
  26. become: true
  27. become_user: "{{ matrix_synapse_username }}"
  28. register: matrix_synapse_git_pull_results
  29. - name: Check if Synapse Docker image exists
  30. ansible.builtin.command: "{{ devture_systemd_docker_base_host_command_docker }} images --quiet --filter 'reference={{ matrix_synapse_docker_image }}'"
  31. register: matrix_synapse_docker_image_check_result
  32. changed_when: false
  33. # Invoking the `docker build` command here, instead of calling the `docker_image` Ansible module,
  34. # because the latter does not support BuildKit.
  35. # See: https://github.com/ansible-collections/community.general/issues/514
  36. - name: Ensure Synapse Docker image is built
  37. ansible.builtin.shell:
  38. chdir: "{{ matrix_synapse_docker_src_files_path }}"
  39. cmd: |
  40. {{ devture_systemd_docker_base_host_command_docker }} build \
  41. -t "{{ matrix_synapse_docker_image }}" \
  42. -f docker/Dockerfile \
  43. .
  44. environment:
  45. DOCKER_BUILDKIT: 1
  46. when: "matrix_synapse_git_pull_results.changed | bool or matrix_synapse_docker_image_check_result.stdout == ''"
  47. - name: Ensure Synapse Docker image is pulled
  48. community.docker.docker_image:
  49. name: "{{ matrix_synapse_docker_image }}"
  50. source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
  51. force_source: "{{ matrix_synapse_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  52. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_synapse_docker_image_force_pull }}"
  53. when: "not matrix_synapse_container_image_self_build"
  54. register: result
  55. retries: "{{ devture_playbook_help_container_retries_count }}"
  56. delay: "{{ devture_playbook_help_container_retries_delay }}"
  57. until: result is not failed
  58. - when: "matrix_synapse_container_image_customizations_enabled | bool"
  59. block:
  60. - name: Ensure customizations Dockerfile is created
  61. ansible.builtin.template:
  62. src: "{{ role_path }}/templates/synapse/customizations/Dockerfile.j2"
  63. dest: "{{ matrix_synapse_customized_docker_src_files_path }}/Dockerfile"
  64. owner: "{{ matrix_synapse_uid }}"
  65. group: "{{ matrix_synapse_gid }}"
  66. mode: 0640
  67. - name: Ensure customized Docker image for Synapse is built
  68. community.docker.docker_image:
  69. name: "{{ matrix_synapse_docker_image_customized }}"
  70. source: build
  71. force_source: "{{ matrix_synapse_docker_image_customized_force_source }}"
  72. build:
  73. dockerfile: Dockerfile
  74. path: "{{ matrix_synapse_customized_docker_src_files_path }}"
  75. nocache: "{{ matrix_synapse_docker_image_customized_build_nocache }}"
  76. - name: Check if a Synapse signing key exists
  77. ansible.builtin.stat:
  78. path: "{{ matrix_synapse_config_dir_path }}/{{ matrix_server_fqn_matrix }}.signing.key"
  79. register: matrix_synapse_signing_key_stat
  80. # We do this so that the signing key would get generated.
  81. #
  82. # This will also generate a default homeserver.yaml configuration file and a log configuration file.
  83. # We don't care about those configuration files, as we replace them with our own anyway (see below).
  84. #
  85. # We don't use the `docker_container` module, because using it with `cap_drop` requires
  86. # a very recent docker-py version, which is not available for a lot of people yet.
  87. - name: Generate initial Synapse config and signing key
  88. ansible.builtin.command: |
  89. docker run
  90. --rm
  91. --name=matrix-config
  92. --user={{ matrix_synapse_uid }}:{{ matrix_synapse_gid }}
  93. --cap-drop=ALL
  94. --mount type=bind,src={{ matrix_synapse_config_dir_path }},dst=/data
  95. -e SYNAPSE_CONFIG_PATH=/data/homeserver.yaml
  96. -e SYNAPSE_SERVER_NAME={{ matrix_server_fqn_matrix }}
  97. -e SYNAPSE_REPORT_STATS=no
  98. {{ matrix_synapse_docker_image }}
  99. generate
  100. when: "not matrix_synapse_signing_key_stat.stat.exists"
  101. - name: Ensure Synapse homeserver config installed
  102. ansible.builtin.copy:
  103. content: "{{ matrix_synapse_configuration | to_nice_yaml(indent=2, width=999999) }}"
  104. dest: "{{ matrix_synapse_config_dir_path }}/homeserver.yaml"
  105. mode: 0644
  106. owner: "{{ matrix_synapse_uid }}"
  107. group: "{{ matrix_synapse_gid }}"
  108. - name: Ensure Synapse log config installed
  109. ansible.builtin.template:
  110. src: "{{ matrix_synapse_template_synapse_log }}"
  111. dest: "{{ matrix_synapse_config_dir_path }}/{{ matrix_server_fqn_matrix }}.log.config"
  112. mode: 0644
  113. - name: Ensure Synapse container network is created
  114. community.general.docker_network:
  115. name: "{{ matrix_synapse_container_network }}"
  116. driver: bridge
  117. - name: Ensure matrix-synapse.service installed
  118. ansible.builtin.template:
  119. src: "{{ role_path }}/templates/synapse/systemd/matrix-synapse.service.j2"
  120. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-synapse.service"
  121. mode: 0644
  122. - name: Ensure register-user script created
  123. ansible.builtin.template:
  124. src: "{{ role_path }}/templates/synapse/bin/register-user.j2"
  125. dest: "{{ matrix_synapse_bin_path }}/register-user"
  126. mode: 0755
  127. - name: Generate sample prometheus.yml for external scraping
  128. ansible.builtin.template:
  129. src: "{{ role_path }}/templates/synapse/prometheus/external_prometheus.yml.example.j2"
  130. dest: "{{ matrix_synapse_base_path }}/external_prometheus.yml.example"
  131. owner: "{{ matrix_synapse_uid }}"
  132. group: "{{ matrix_synapse_gid }}"
  133. mode: 0644
  134. when: matrix_synapse_metrics_proxying_enabled | bool