Matrix Docker Ansible eploy
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

171 líneas
6.6 KiB

  1. ---
  2. - name: Set default postgres_dump_dir, if not provided
  3. set_fact:
  4. postgres_dump_dir: "/tmp"
  5. when: "postgres_dump_dir|default('') == ''"
  6. - name: Set postgres_dump_name, if not provided
  7. set_fact:
  8. postgres_dump_name: "matrix-postgres-dump.sql.gz"
  9. when: "postgres_dump_name|default('') == ''"
  10. - name: Set postgres_auto_upgrade_backup_data_path, if not provided
  11. set_fact:
  12. postgres_auto_upgrade_backup_data_path: "{{ matrix_postgres_data_path }}-auto-upgrade-backup"
  13. when: "postgres_auto_upgrade_backup_data_path|default('') == ''"
  14. - name: Set postgres_start_wait_time, if not provided
  15. set_fact:
  16. postgres_start_wait_time: 15
  17. when: "postgres_start_wait_time|default('') == ''"
  18. - name: Set postgres_force_upgrade, if not provided
  19. set_fact:
  20. postgres_force_upgrade: false
  21. when: "postgres_force_upgrade|default('') == ''"
  22. - name: Fail, if trying to upgrade external Postgres database
  23. fail:
  24. msg: "Your configuration indicates that you're not using Postgres from this role. There is nothing to upgrade."
  25. when: "not matrix_postgres_enabled|bool"
  26. - name: Check Postgres auto-upgrade backup data directory
  27. stat:
  28. path: "{{ postgres_auto_upgrade_backup_data_path }}"
  29. register: result_auto_upgrade_path
  30. - name: Abort, if existing Postgres auto-upgrade data path detected
  31. fail:
  32. msg: "Detected that a left-over {{ postgres_auto_upgrade_backup_data_path }} exists. You should rename it to {{ matrix_postgres_data_path }} if the previous upgrade went wrong, or delete it if it went well."
  33. when: "result_auto_upgrade_path.stat.exists"
  34. - import_tasks: tasks/util/detect_existing_postgres_version.yml
  35. - name: Abort, if no existing Postgres version detected
  36. fail:
  37. msg: "Could not find existing Postgres installation"
  38. when: "not matrix_postgres_detected_existing|bool"
  39. - name: Abort, if already at latest Postgres version
  40. fail:
  41. msg: "You are already running the latest Postgres version supported ({{ matrix_postgres_docker_image_latest }}). Nothing to do"
  42. when: "matrix_postgres_detected_version_corresponding_docker_image == matrix_postgres_docker_image_latest and not postgres_force_upgrade"
  43. - debug:
  44. msg: "Upgrading database from {{ matrix_postgres_detected_version_corresponding_docker_image }} to {{ matrix_postgres_docker_image_latest }}"
  45. - name: Ensure matrix-synapse is stopped
  46. service:
  47. name: matrix-synapse
  48. state: stopped
  49. - name: Ensure matrix-postgres is started
  50. service:
  51. name: matrix-postgres
  52. state: started
  53. daemon_reload: yes
  54. - name: Wait a bit, so that Postgres can start
  55. wait_for:
  56. timeout: "{{ postgres_start_wait_time }}"
  57. delegate_to: 127.0.0.1
  58. become: false
  59. # We dump all databases, roles, etc.
  60. #
  61. # Because we'll be importing into a new container which initializes the default
  62. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`) by itself on startup,
  63. # we need to remove these from the dump, or we'll get errors saying these already exist.
  64. - name: Perform Postgres database dump
  65. command: >-
  66. /usr/bin/docker run --rm --name matrix-postgres-dump
  67. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  68. --network={{ matrix_docker_network }}
  69. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  70. --entrypoint=/bin/sh
  71. -v {{ postgres_dump_dir }}:/out
  72. {{ matrix_postgres_detected_version_corresponding_docker_image }}
  73. -c "pg_dumpall -h matrix-postgres
  74. {{ '| gzip -c ' if postgres_dump_name.endswith('.gz') else '' }}
  75. > /out/{{ postgres_dump_name }}"
  76. - name: Ensure matrix-postgres is stopped
  77. service:
  78. name: matrix-postgres
  79. state: stopped
  80. - name: Rename existing Postgres data directory
  81. command: "mv {{ matrix_postgres_data_path }} {{ postgres_auto_upgrade_backup_data_path }}"
  82. - debug:
  83. msg: "NOTE: Your Postgres data directory has been moved from `{{ matrix_postgres_data_path }}` to `{{ postgres_auto_upgrade_backup_data_path }}`. In the event of failure, you can move it back and run the playbook with --tags=setup-postgres to restore operation."
  84. - import_tasks: tasks/setup_postgres.yml
  85. - name: Ensure matrix-postgres autoruns and is restarted
  86. service:
  87. name: matrix-postgres
  88. enabled: yes
  89. state: restarted
  90. daemon_reload: yes
  91. - name: Wait a bit, so that Postgres can start
  92. wait_for:
  93. timeout: "{{ postgres_start_wait_time }}"
  94. delegate_to: 127.0.0.1
  95. become: false
  96. # Starting the database container had automatically created the default
  97. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`).
  98. # The dump most likely contains those same entries and would try to re-create them, leading to errors.
  99. # We need to skip over those lines.
  100. - name: Generate Postgres database import command
  101. set_fact:
  102. matrix_postgres_import_command: >-
  103. /usr/bin/docker run --rm --name matrix-postgres-import
  104. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  105. --cap-drop=ALL
  106. --network={{ matrix_docker_network }}
  107. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  108. --entrypoint=/bin/sh
  109. -v {{ postgres_dump_dir }}:/in:ro
  110. {{ matrix_postgres_docker_image_latest }}
  111. -c "cat /in/{{ postgres_dump_name }} |
  112. {{ 'gunzip |' if postgres_dump_name.endswith('.gz') else '' }}
  113. grep -vE '^CREATE ROLE {{ matrix_postgres_connection_username }}' |
  114. grep -vE '^CREATE DATABASE {{ matrix_postgres_db_name }}' |
  115. psql -v ON_ERROR_STOP=1 -h matrix-postgres"
  116. # This is a hack.
  117. # See: https://ansibledaily.com/print-to-standard-output-without-escaping/
  118. #
  119. # We want to run `debug: msg=".."`, but that dumps it as JSON and escapes double quotes within it,
  120. # which ruins the command (`matrix_postgres_import_command`)
  121. - name: Note about Postgres importing
  122. set_fact:
  123. dummy: true
  124. with_items:
  125. - >-
  126. Importing Postgres database using the following command: `{{ matrix_postgres_import_command }}`.
  127. If this crashes, you can stop Postgres (`systemctl stop matrix-postgres`),
  128. delete the new database data (`rm -rf {{ matrix_postgres_data_path }}`)
  129. and restore the automatically-made backup (`mv {{ postgres_auto_upgrade_backup_data_path }} {{ matrix_postgres_data_path }}`).
  130. - name: Perform Postgres database import
  131. command: "{{ matrix_postgres_import_command }}"
  132. - name: Delete Postgres database dump file
  133. file:
  134. path: "{{ postgres_dump_dir }}/{{ postgres_dump_name }}"
  135. state: absent
  136. - name: Ensure matrix-synapse is started
  137. service:
  138. name: matrix-synapse
  139. state: started
  140. daemon_reload: yes
  141. - debug:
  142. msg: "NOTE: Your old Postgres data directory is preserved at `{{ postgres_auto_upgrade_backup_data_path }}`. You might want to get rid of it once you've confirmed that all is well."