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

121 строка
4.8 KiB

  1. ---
  2. # Pre-checks
  3. - name: Fail if Postgres not enabled
  4. 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. 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. 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. 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. 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. set_fact:
  26. postgres_import_wait_time: "{{ 7 * 86400 }}"
  27. when: "postgres_import_wait_time|default('') == ''"
  28. # Actual import work
  29. - name: Ensure matrix-postgres is started
  30. service:
  31. name: matrix-postgres
  32. state: started
  33. daemon_reload: yes
  34. - name: Wait a bit, so that Postgres can start
  35. wait_for:
  36. timeout: "{{ postgres_start_wait_time }}"
  37. delegate_to: 127.0.0.1
  38. become: false
  39. - import_tasks: tasks/util/detect_existing_postgres_version.yml
  40. - name: Abort, if no existing Postgres version detected
  41. fail:
  42. msg: "Could not find existing Postgres installation"
  43. when: "not matrix_postgres_detected_existing|bool"
  44. # Starting the database container had automatically created the default
  45. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`).
  46. # The dump most likely contains those same entries and would try to re-create them, leading to errors.
  47. # We need to skip over those lines.
  48. - name: Generate Postgres database import command
  49. set_fact:
  50. matrix_postgres_import_command: >-
  51. {{ matrix_host_command_docker }} run --rm --name matrix-postgres-import
  52. --log-driver=none
  53. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  54. --cap-drop=ALL
  55. --network={{ matrix_docker_network }}
  56. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  57. --mount type=bind,src={{ server_path_postgres_dump }},dst=/{{ server_path_postgres_dump|basename }},ro
  58. --entrypoint=/bin/sh
  59. {{ matrix_postgres_docker_image_latest }}
  60. -c "export PGPASSWORD='{{ matrix_postgres_connection_password }}'; cat /{{ server_path_postgres_dump|basename }} |
  61. {{ 'gunzip |' if server_path_postgres_dump.endswith('.gz') else '' }}
  62. grep -vE '^CREATE ROLE {{ matrix_postgres_connection_username }}' |
  63. grep -vE '^CREATE DATABASE {{ matrix_postgres_db_name }}' |
  64. psql -v ON_ERROR_STOP=1 -h matrix-postgres"
  65. when: (matrix_postgres_connection_password is defined) and (matrix_postgres_connection_password|length > 0)
  66. - name: Generate Postgres database import command
  67. set_fact:
  68. matrix_postgres_import_command: >-
  69. {{ matrix_host_command_docker }} run --rm --name matrix-postgres-import
  70. --log-driver=none
  71. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  72. --cap-drop=ALL
  73. --network={{ matrix_docker_network }}
  74. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  75. --mount type=bind,src={{ server_path_postgres_dump }},dst=/{{ server_path_postgres_dump|basename }},ro
  76. --entrypoint=/bin/sh
  77. {{ matrix_postgres_docker_image_latest }}
  78. -c "cat /{{ server_path_postgres_dump|basename }} |
  79. {{ 'gunzip |' if server_path_postgres_dump.endswith('.gz') else '' }}
  80. grep -vE '^CREATE ROLE {{ matrix_postgres_connection_username }}' |
  81. grep -vE '^CREATE DATABASE {{ matrix_postgres_db_name }}' |
  82. psql -v ON_ERROR_STOP=1 -h matrix-postgres"
  83. when: matrix_postgres_connection_password is not defined
  84. # This is a hack.
  85. # See: https://ansibledaily.com/print-to-standard-output-without-escaping/
  86. #
  87. # We want to run `debug: msg=".."`, but that dumps it as JSON and escapes double quotes within it,
  88. # which ruins the command (`matrix_postgres_import_command`)
  89. - name: Note about Postgres importing alternative
  90. set_fact:
  91. dummy: true
  92. with_items:
  93. - >-
  94. Importing Postgres database using the following command: `{{ matrix_postgres_import_command }}`.
  95. If this crashes, you can stop Postgres (`systemctl stop matrix-postgres`),
  96. delete its existing data (`rm -rf {{ matrix_postgres_data_path }}/*`), start it again (`systemctl start matrix-postgres`)
  97. and manually run the above import command directly on the server.
  98. - name: Perform Postgres database import
  99. command: "{{ matrix_postgres_import_command }}"
  100. async: "{{ postgres_import_wait_time }}"
  101. poll: 10