Matrix Docker Ansible eploy
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

110 строки
4.5 KiB

  1. ---
  2. # Pre-checks
  3. - name: Fail if Postgres not enabled
  4. ansible.builtin.fail:
  5. msg: "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot import."
  6. when: "not matrix_postgres_enabled | bool"
  7. - name: Fail if playbook called incorrectly
  8. ansible.builtin.fail:
  9. msg: "The `server_path_postgres_dump` variable needs to be provided to this playbook, via --extra-vars"
  10. when: "server_path_postgres_dump is not defined or server_path_postgres_dump.startswith('<')"
  11. - name: Check if the provided Postgres dump file exists
  12. ansible.builtin.stat:
  13. path: "{{ server_path_postgres_dump }}"
  14. register: result_server_path_postgres_dump_stat
  15. - name: Fail if provided Postgres dump file doesn't exists
  16. ansible.builtin.fail:
  17. msg: "File cannot be found on the server at {{ server_path_postgres_dump }}"
  18. when: "not result_server_path_postgres_dump_stat.stat.exists"
  19. # Defaults
  20. - name: Set postgres_start_wait_time, if not provided
  21. ansible.builtin.set_fact:
  22. postgres_start_wait_time: 15
  23. when: "postgres_start_wait_time | default('') == ''"
  24. - name: Set postgres_import_wait_time, if not provided
  25. ansible.builtin.set_fact:
  26. postgres_import_wait_time: "{{ 7 * 86400 }}"
  27. when: "postgres_import_wait_time | default('') == ''"
  28. # By default, we connect and import into the main (`matrix`) database.
  29. # Single-database dumps for Synapse may wish to import into `synapse` instead.
  30. - name: Set postgres_default_import_database, if not provided
  31. ansible.builtin.set_fact:
  32. postgres_default_import_database: "{{ matrix_postgres_db_name }}"
  33. when: "postgres_default_import_database | default('') == ''"
  34. # Actual import work
  35. - name: Ensure matrix-postgres is started
  36. ansible.builtin.service:
  37. name: matrix-postgres
  38. state: started
  39. daemon_reload: true
  40. - name: Wait a bit, so that Postgres can start
  41. ansible.builtin.wait_for:
  42. timeout: "{{ postgres_start_wait_time }}"
  43. delegate_to: 127.0.0.1
  44. become: false
  45. - ansible.builtin.import_tasks: tasks/util/detect_existing_postgres_version.yml
  46. - name: Abort, if no existing Postgres version detected
  47. ansible.builtin.fail:
  48. msg: "Could not find existing Postgres installation"
  49. when: "not matrix_postgres_detected_existing | bool"
  50. # Starting the database container had automatically created the default
  51. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`).
  52. # The dump most likely contains those same entries and would try to re-create them, leading to errors.
  53. # We need to skip over those lines.
  54. - name: Generate Postgres database import command
  55. ansible.builtin.set_fact:
  56. matrix_postgres_import_command: >-
  57. {{ matrix_host_command_docker }} run --rm --name matrix-postgres-import
  58. --log-driver=none
  59. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  60. --cap-drop=ALL
  61. --network={{ matrix_docker_network }}
  62. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  63. --mount type=bind,src={{ server_path_postgres_dump }},dst=/{{ server_path_postgres_dump | basename }},ro
  64. --entrypoint=/bin/sh
  65. {{ matrix_postgres_docker_image_latest }}
  66. -c "cat /{{ server_path_postgres_dump | basename }} |
  67. {{ 'gunzip |' if server_path_postgres_dump.endswith('.gz') else '' }}
  68. grep -vE '{{ matrix_postgres_import_roles_ignore_regex }}' |
  69. grep -vE '{{ matrix_postgres_import_databases_ignore_regex }}' |
  70. psql -v ON_ERROR_STOP=1 -h matrix-postgres --dbname={{ postgres_default_import_database }}"
  71. # This is a hack.
  72. # See: https://ansibledaily.com/print-to-standard-output-without-escaping/
  73. #
  74. # We want to run `debug: msg=".."`, but that dumps it as JSON and escapes double quotes within it,
  75. # which ruins the command (`matrix_postgres_import_command`)
  76. - name: Note about Postgres importing alternative
  77. ansible.builtin.set_fact:
  78. dummy: true
  79. with_items:
  80. - >-
  81. Importing Postgres database using the following command: `{{ matrix_postgres_import_command }}`.
  82. If this crashes, you can stop Postgres (`systemctl stop matrix-postgres`),
  83. delete its existing data (`rm -rf {{ matrix_postgres_data_path }}/*`), start it again (`systemctl start matrix-postgres`)
  84. and manually run the above import command directly on the server.
  85. - name: Perform Postgres database import
  86. ansible.builtin.command:
  87. cmd: "{{ matrix_postgres_import_command }}"
  88. async: "{{ postgres_import_wait_time }}"
  89. poll: 10
  90. register: matrix_postgres_import_postgres_command_result
  91. changed_when: matrix_postgres_import_postgres_command_result.rc == 0