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.
 
 

203 line
7.7 KiB

  1. ---
  2. #
  3. # Tasks related to setting up an internal postgres server
  4. #
  5. - import_tasks: "{{ role_path }}/tasks/migrate_postgres_data_directory.yml"
  6. when: matrix_postgres_enabled|bool
  7. - import_tasks: "{{ role_path }}/tasks/util/detect_existing_postgres_version.yml"
  8. when: matrix_postgres_enabled|bool
  9. # If we have found an existing version (installed from before), we use its corresponding Docker image.
  10. # If not, we install using the latest Postgres.
  11. #
  12. # Upgrading is supposed to be performed separately and explicitly (see `upgrade_postgres.yml`).
  13. - set_fact:
  14. matrix_postgres_docker_image_to_use: "{{ matrix_postgres_docker_image_latest if matrix_postgres_detected_version_corresponding_docker_image == '' else matrix_postgres_detected_version_corresponding_docker_image }}"
  15. when: matrix_postgres_enabled|bool
  16. - name: Abort if on an unsupported Postgres version
  17. fail:
  18. msg: "You're on Postgres {{ matrix_postgres_detected_version }}, which is no longer supported. To upgrade, see docs/maintenance-postgres.md"
  19. when: "matrix_postgres_enabled|bool and matrix_postgres_detected_version.startswith('9.')"
  20. - name: Inject warning if on an old version of Postgres
  21. set_fact:
  22. matrix_playbook_runtime_results: |
  23. {{
  24. matrix_playbook_runtime_results|default([])
  25. +
  26. [
  27. "NOTE: Your setup is on an old Postgres version ({{ matrix_postgres_docker_image_to_use }}), while {{ matrix_postgres_docker_image_latest }} is supported. You can upgrade using --tags=upgrade-postgres"
  28. ]
  29. }}
  30. when: "matrix_postgres_enabled|bool and matrix_postgres_docker_image_to_use != matrix_postgres_docker_image_latest"
  31. # Even if we don't run the internal server, we still need this for running the CLI
  32. - name: Ensure postgres Docker image is pulled
  33. docker_image:
  34. name: "{{ matrix_postgres_docker_image_to_use }}"
  35. source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
  36. force_source: "{{ matrix_postgres_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  37. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_postgres_docker_image_force_pull }}"
  38. when: matrix_postgres_enabled|bool
  39. - name: Ensure Postgres paths exist
  40. file:
  41. path: "{{ item }}"
  42. state: directory
  43. mode: 0700
  44. owner: "{{ matrix_user_username }}"
  45. group: "{{ matrix_user_groupname }}"
  46. with_items:
  47. - "{{ matrix_postgres_base_path }}"
  48. - "{{ matrix_postgres_data_path }}"
  49. when: matrix_postgres_enabled|bool
  50. # We do this as a separate task, because:
  51. # - we'd like to do it for the data path only, not for the base path (which contains root-owned environment variable files we'd like to leave as-is)
  52. # - we need to do it without `mode`, or we risk making certain `.conf` and other files's executable bit to flip to true
  53. - name: Ensure Postgres data path ownership is correct
  54. file:
  55. path: "{{ matrix_postgres_data_path }}"
  56. state: directory
  57. owner: "{{ matrix_user_username }}"
  58. group: "{{ matrix_user_groupname }}"
  59. recurse: yes
  60. when: matrix_postgres_enabled|bool
  61. - name: Ensure Postgres environment variables file created
  62. template:
  63. src: "{{ role_path }}/templates/{{ item }}.j2"
  64. dest: "{{ matrix_postgres_base_path }}/{{ item }}"
  65. mode: 0640
  66. with_items:
  67. - "env-postgres-psql"
  68. - "env-postgres-server"
  69. when: matrix_postgres_enabled|bool
  70. - name: Ensure matrix-postgres-cli script created
  71. template:
  72. src: "{{ role_path }}/templates/usr-local-bin/matrix-postgres-cli.j2"
  73. dest: "{{ matrix_local_bin_path }}/matrix-postgres-cli"
  74. mode: 0755
  75. when: matrix_postgres_enabled|bool
  76. - name: Ensure matrix-change-user-admin-status script created
  77. template:
  78. src: "{{ role_path }}/templates/usr-local-bin/matrix-change-user-admin-status.j2"
  79. dest: "{{ matrix_local_bin_path }}/matrix-change-user-admin-status"
  80. mode: 0755
  81. when: matrix_postgres_enabled|bool
  82. - name: (Migration) Ensure old matrix-make-user-admin script deleted
  83. file:
  84. path: "{{ matrix_local_bin_path }}/matrix-make-user-admin"
  85. state: absent
  86. when: matrix_postgres_enabled|bool
  87. - name: Ensure matrix-postgres-update-user-password-hash script created
  88. template:
  89. src: "{{ role_path }}/templates/usr-local-bin/matrix-postgres-update-user-password-hash.j2"
  90. dest: "{{ matrix_local_bin_path }}/matrix-postgres-update-user-password-hash"
  91. mode: 0755
  92. when: matrix_postgres_enabled|bool
  93. - name: Ensure matrix-postgres.service installed
  94. template:
  95. src: "{{ role_path }}/templates/systemd/matrix-postgres.service.j2"
  96. dest: "{{ matrix_systemd_path }}/matrix-postgres.service"
  97. mode: 0644
  98. register: matrix_postgres_systemd_service_result
  99. when: matrix_postgres_enabled|bool
  100. - name: Ensure systemd reloaded after matrix-postgres.service installation
  101. service:
  102. daemon_reload: yes
  103. when: "matrix_postgres_enabled|bool and matrix_postgres_systemd_service_result.changed"
  104. - include_tasks:
  105. file: "{{ role_path }}/tasks/util/create_additional_databases.yml"
  106. apply:
  107. tags:
  108. - always
  109. when: "matrix_postgres_enabled|bool and matrix_postgres_additional_databases|length > 0"
  110. - name: Check existence of matrix-postgres backup data path
  111. stat:
  112. path: "{{ matrix_postgres_data_path }}-auto-upgrade-backup"
  113. register: matrix_postgres_data_backup_path_stat
  114. when: "matrix_postgres_enabled|bool"
  115. - name: Inject warning if backup data remains
  116. set_fact:
  117. matrix_playbook_runtime_results: |
  118. {{
  119. matrix_playbook_runtime_results|default([])
  120. +
  121. [
  122. "NOTE: You have some Postgres backup data in `{{ matrix_postgres_data_path }}-auto-upgrade-backup`, which was created during the last major Postgres update you ran. If your setup works well after this upgrade, feel free to delete this whole directory."
  123. ]
  124. }}
  125. when: "matrix_postgres_enabled|bool and matrix_postgres_data_backup_path_stat.stat.exists"
  126. #
  127. # Tasks related to getting rid of the internal postgres server (if it was previously enabled)
  128. #
  129. - name: Check existence of matrix-postgres service
  130. stat:
  131. path: "{{ matrix_systemd_path }}/matrix-postgres.service"
  132. register: matrix_postgres_service_stat
  133. when: "not matrix_postgres_enabled|bool"
  134. - name: Ensure matrix-postgres is stopped
  135. service:
  136. name: matrix-postgres
  137. state: stopped
  138. daemon_reload: yes
  139. when: "not matrix_postgres_enabled|bool and matrix_postgres_service_stat.stat.exists"
  140. - name: Ensure matrix-postgres.service doesn't exist
  141. file:
  142. path: "{{ matrix_systemd_path }}/matrix-postgres.service"
  143. state: absent
  144. when: "not matrix_postgres_enabled|bool and matrix_postgres_service_stat.stat.exists"
  145. - name: Ensure systemd reloaded after matrix-postgres.service removal
  146. service:
  147. daemon_reload: yes
  148. when: "not matrix_postgres_enabled|bool and matrix_postgres_service_stat.stat.exists"
  149. - name: Check existence of matrix-postgres local data path
  150. stat:
  151. path: "{{ matrix_postgres_data_path }}"
  152. register: matrix_postgres_data_path_stat
  153. when: "not matrix_postgres_enabled|bool"
  154. # We just want to notify the user. Deleting data is too destructive.
  155. - name: Inject warning if matrix-postgres local data remains
  156. set_fact:
  157. matrix_playbook_runtime_results: |
  158. {{
  159. matrix_playbook_runtime_results|default([])
  160. +
  161. [
  162. "NOTE: You are not using a local PostgreSQL database, but some old data remains from before in `{{ matrix_postgres_data_path }}`. Feel free to delete it."
  163. ]
  164. }}
  165. when: "not matrix_postgres_enabled|bool and matrix_postgres_data_path_stat.stat.exists"
  166. - name: Remove Postgres scripts
  167. file:
  168. path: "{{ matrix_local_bin_path }}/{{ item }}"
  169. state: absent
  170. with_items:
  171. - matrix-postgres-cli
  172. - matrix-change-user-admin-status
  173. - matrix-postgres-update-user-password-hash
  174. when: "not matrix_postgres_enabled|bool"