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.
 
 

103 lines
4.4 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. - when: 'postgres_connection_string_variable_name is defined'
  22. block:
  23. - name: Fail if postgres_connection_string_variable_name points to an undefined variable
  24. ansible.builtin.fail:
  25. msg: "postgres_connection_string_variable_name is defined, but there is no variable with the name `{{ postgres_connection_string_variable_name }}`"
  26. when: "postgres_connection_string_variable_name not in vars"
  27. - name: Get Postgres connection string from variable
  28. ansible.builtin.set_fact:
  29. postgres_db_connection_string: "{{ lookup('vars', postgres_connection_string_variable_name) }}"
  30. - name: Fail if playbook called incorrectly
  31. ansible.builtin.fail:
  32. msg: >-
  33. Either a `postgres_db_connection_string` variable or a `postgres_connection_string_variable_name` needs to be provided to this playbook, via `--extra-vars`.
  34. 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"`
  35. when: "postgres_db_connection_string is not defined or not postgres_db_connection_string.startswith('postgresql://')"
  36. # Defaults
  37. - name: Set postgres_start_wait_time, if not provided
  38. ansible.builtin.set_fact:
  39. postgres_start_wait_time: 15
  40. when: "postgres_start_wait_time | default('') == ''"
  41. # Actual import work
  42. - name: Ensure matrix-postgres is started
  43. ansible.builtin.service:
  44. name: matrix-postgres
  45. state: started
  46. daemon_reload: true
  47. register: matrix_postgres_service_start_result
  48. - name: Wait a bit, so that Postgres can start
  49. ansible.builtin.wait_for:
  50. timeout: "{{ postgres_start_wait_time }}"
  51. delegate_to: 127.0.0.1
  52. become: false
  53. when: "matrix_postgres_service_start_result.changed | bool"
  54. - name: Import SQLite database from {{ sqlite_database_path }} into Postgres # noqa name[template]
  55. ansible.builtin.command:
  56. cmd: >-
  57. {{ matrix_host_command_docker }} run
  58. --rm
  59. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  60. --cap-drop=ALL
  61. --network={{ matrix_docker_network }}
  62. --mount type=bind,src={{ sqlite_database_path }},dst=/in.db,ro
  63. --entrypoint=/bin/sh
  64. {{ matrix_postgres_pgloader_docker_image }}
  65. -c
  66. 'pgloader /in.db {{ postgres_db_connection_string }}'
  67. register: matrix_postgres_import_generic_sqlite_db_import_result
  68. changed_when: matrix_postgres_import_generic_sqlite_db_import_result.rc == 0
  69. - name: Archive SQLite database ({{ sqlite_database_path }} -> {{ sqlite_database_path }}.backup) # noqa name[template]
  70. ansible.builtin.command:
  71. cmd: "mv {{ sqlite_database_path }} {{ sqlite_database_path }}.backup"
  72. register: matrix_postgres_import_generic_sqlite_db_move_result
  73. changed_when: matrix_postgres_import_generic_sqlite_db_move_result.rc == 0
  74. - name: Inject result
  75. ansible.builtin.set_fact:
  76. matrix_playbook_runtime_results: |
  77. {{
  78. matrix_playbook_runtime_results | default([])
  79. +
  80. [
  81. "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."
  82. ]
  83. }}