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.
 
 

85 lines
2.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 `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: Fail if playbook called incorrectly
  12. fail:
  13. msg: >-
  14. The `postgres_db_connection_string` variable needs to be provided to this playbook, via `--extra-vars`.
  15. Example: `--extra-vars="postgres_db_connection_string=postgresql://username:password@localhost:<port>/database_name`"
  16. when: "postgres_db_connection_string is not defined or not postgres_db_connection_string.startswith('postgresql://')"
  17. - name: Check if the provided SQLite database file exists
  18. stat:
  19. path: "{{ sqlite_database_path }}"
  20. register: sqlite_database_path_stat_result
  21. - name: Fail if provided SQLite database file doesn't exist
  22. fail:
  23. msg: "File cannot be found on the server at {{ sqlite_database_path }}"
  24. when: "not sqlite_database_path_stat_result.stat.exists"
  25. # Defaults
  26. - name: Set postgres_start_wait_time, if not provided
  27. set_fact:
  28. postgres_start_wait_time: 15
  29. when: "postgres_start_wait_time|default('') == ''"
  30. # Actual import work
  31. - name: Ensure matrix-postgres is started
  32. service:
  33. name: matrix-postgres
  34. state: started
  35. daemon_reload: yes
  36. register: matrix_postgres_service_start_result
  37. - name: Wait a bit, so that Postgres can start
  38. wait_for:
  39. timeout: "{{ postgres_start_wait_time }}"
  40. delegate_to: 127.0.0.1
  41. become: false
  42. when: "matrix_postgres_service_start_result.changed|bool"
  43. - name: Import SQLite database from {{ sqlite_database_path }} into Postgres
  44. command:
  45. cmd: >-
  46. {{ matrix_host_command_docker }} run
  47. --rm
  48. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  49. --cap-drop=ALL
  50. --network={{ matrix_docker_network }}
  51. --mount type=bind,src={{ sqlite_database_path }},dst=/in.db,ro
  52. --entrypoint=/bin/sh
  53. {{ matrix_postgres_pgloader_docker_image }}
  54. -c
  55. 'pgloader /in.db {{ postgres_db_connection_string }}'
  56. - name: Archive SQLite database ({{ sqlite_database_path }} -> {{ sqlite_database_path }}.backup)
  57. command:
  58. cmd: "mv {{ sqlite_database_path }} {{ sqlite_database_path }}.backup"
  59. - name: Inject result
  60. set_fact:
  61. matrix_playbook_runtime_results: |
  62. {{
  63. matrix_playbook_runtime_results|default([])
  64. +
  65. [
  66. "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."
  67. ]
  68. }}