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.
 
 

99 lines
3.9 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 `nedb_database_path` variable needs to be provided to this playbook, via --extra-vars"
  10. when: "nedb_database_path is not defined or nedb_database_path.startswith('<')"
  11. - name: Check if the provided nedb database file exists
  12. stat:
  13. path: "{{ nedb_database_path }}"
  14. register: nedb_database_path_stat_result
  15. - name: Fail if provided SQLite database file doesn't exist
  16. fail:
  17. msg: "File cannot be found on the server at {{ nedb_database_path }}"
  18. when: "not nedb_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. 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. 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. 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. 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. service:
  43. name: matrix-postgres
  44. state: started
  45. daemon_reload: yes
  46. register: matrix_postgres_service_start_result
  47. - name: Wait a bit, so that Postgres can start
  48. 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. # No migration.sh available, but found this:
  54. # https://github.com/matrix-org/matrix-appservice-slack/blob/develop/src/scripts/migrateToPostgres.ts
  55. # Usage should be similar to appservice_irc
  56. - name: Import appservice_slack NeDB database from {{ sqlite_database_path }} into Postgres
  57. when: database == 'appservice_slack'
  58. command:
  59. cmd: >-
  60. {{ matrix_host_command_docker }} run
  61. --rm
  62. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  63. --cap-drop=ALL
  64. --network={{ matrix_docker_network }}
  65. --mount type=bind,src={{ matrix_appservice_irc_data_path }},dst=/data,ro
  66. --entrypoint=/bin/sh
  67. {{ matrix_appservice_slack_docker_image }}
  68. -c
  69. 'node /lib/scripts/migrate-db-to-pgres.js -d /data -p passkey.pem -c {{ postgres_db_connection_string }}'
  70. - name: Inject result
  71. set_fact:
  72. matrix_playbook_runtime_results: |
  73. {{
  74. matrix_playbook_runtime_results|default([])
  75. +
  76. [
  77. "NOTE: Your NeDB database file has been imported into Postgres. The original directory has been moved from `{{ nedb_database_path }}` to `{{ nedb_database_path }}.backup`. When you've confirmed that the import went well and everything works, you should be able to safely delete this file."
  78. ]
  79. }}