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

102 строки
4.3 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 `sqlite_database_path` variable needs to be provided to this playbook, via --extra-vars"
  10. when: "sqlite_database_path is not defined or sqlite_database_path.startswith('<')"
  11. - name: Check if the provided SQLite database file exists
  12. ansible.builtin.stat:
  13. path: "{{ sqlite_database_path }}"
  14. register: sqlite_database_path_stat_result
  15. - name: Fail if provided SQLite database file doesn't exist
  16. ansible.builtin.fail:
  17. msg: "File cannot be found on the server at {{ sqlite_database_path }}"
  18. when: "not sqlite_database_path_stat_result.stat.exists"
  19. # We either expect `postgres_db_connection_string` specifying a full Postgres database connection string,
  20. # or `postgres_connection_string_variable_name`, specifying a name of a variable, which contains a valid connection string.
  21. - block:
  22. - name: Fail if postgres_connection_string_variable_name points to an undefined variable
  23. ansible.builtin.fail: msg="postgres_connection_string_variable_name is defined, but there is no variable with the name `{{ postgres_connection_string_variable_name }}`"
  24. when: "postgres_connection_string_variable_name not in vars"
  25. - name: Get Postgres connection string from variable
  26. ansible.builtin.set_fact:
  27. postgres_db_connection_string: "{{ lookup('vars', postgres_connection_string_variable_name) }}"
  28. when: 'postgres_connection_string_variable_name is defined'
  29. - name: Fail if playbook called incorrectly
  30. ansible.builtin.fail:
  31. msg: >-
  32. Either a `postgres_db_connection_string` variable or a `postgres_connection_string_variable_name` needs to be provided to this playbook, via `--extra-vars`.
  33. Example: `--extra-vars="postgres_db_connection_string=postgresql://username:password@localhost:<port>/database_name"` or `--extra-vars="postgres_connection_string_variable_name=matrix_appservice_discord_database_connString"`
  34. when: "postgres_db_connection_string is not defined or not postgres_db_connection_string.startswith('postgresql://')"
  35. # Defaults
  36. - name: Set postgres_start_wait_time, if not provided
  37. ansible.builtin.set_fact:
  38. postgres_start_wait_time: 15
  39. when: "postgres_start_wait_time | default('') == ''"
  40. # Actual import work
  41. - name: Ensure matrix-postgres is started
  42. ansible.builtin.service:
  43. name: matrix-postgres
  44. state: started
  45. daemon_reload: true
  46. register: matrix_postgres_service_start_result
  47. - name: Wait a bit, so that Postgres can start
  48. ansible.builtin.wait_for:
  49. timeout: "{{ postgres_start_wait_time }}"
  50. delegate_to: 127.0.0.1
  51. become: false
  52. when: "matrix_postgres_service_start_result.changed | bool"
  53. - name: Import SQLite database from {{ sqlite_database_path }} into Postgres
  54. ansible.builtin.command:
  55. cmd: >-
  56. {{ matrix_host_command_docker }} run
  57. --rm
  58. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  59. --cap-drop=ALL
  60. --network={{ matrix_docker_network }}
  61. --mount type=bind,src={{ sqlite_database_path }},dst=/in.db,ro
  62. --entrypoint=/bin/sh
  63. {{ matrix_postgres_pgloader_docker_image }}
  64. -c
  65. 'pgloader /in.db {{ postgres_db_connection_string }}'
  66. register: matrix_postgres_import_generic_sqlite_db_import_result
  67. changed_when: matrix_postgres_import_generic_sqlite_db_import_result.rc == 0
  68. - name: Archive SQLite database ({{ sqlite_database_path }} -> {{ sqlite_database_path }}.backup)
  69. ansible.builtin.command:
  70. cmd: "mv {{ sqlite_database_path }} {{ sqlite_database_path }}.backup"
  71. register: matrix_postgres_import_generic_sqlite_db_move_result
  72. changed_when: matrix_postgres_import_generic_sqlite_db_move_result.rc == 0
  73. - name: Inject result
  74. ansible.builtin.set_fact:
  75. matrix_playbook_runtime_results: |
  76. {{
  77. matrix_playbook_runtime_results | default([])
  78. +
  79. [
  80. "NOTE: Your SQLite database file has been imported into Postgres. The original file has been moved from `{{ sqlite_database_path }}` to `{{ sqlite_database_path }}.backup`. When you've confirmed that the import went well and everything works, you should be able to safely delete this file."
  81. ]
  82. }}