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.
 
 

125 lines
4.5 KiB

  1. ---
  2. - name: Set default postgres_dump_dir, if not provided
  3. set_fact:
  4. postgres_dump_dir: "/tmp"
  5. when: "postgres_dump_dir|default('') == ''"
  6. - name: Set postgres_dump_name, if not provided
  7. set_fact:
  8. postgres_dump_name: "matrix-postgres.out"
  9. when: "postgres_dump_name|default('') == ''"
  10. - name: Set postgres_auto_upgrade_backup_data_path, if not provided
  11. set_fact:
  12. postgres_auto_upgrade_backup_data_path: "{{ matrix_postgres_data_path }}-auto-upgrade-backup"
  13. when: "postgres_auto_upgrade_backup_data_path|default('') == ''"
  14. - name: Set postgres_start_wait_time, if not provided
  15. set_fact:
  16. postgres_start_wait_time: 15
  17. when: "postgres_start_wait_time|default('') == ''"
  18. - name: Fail, if trying to upgrade external Postgres database
  19. fail:
  20. msg: "Your configuration indicates that you're not using Postgres from this role. There is nothing to upgrade."
  21. when: "not matrix_postgres_enabled"
  22. - name: Check Postgres auto-upgrade backup data directory
  23. stat:
  24. path: "{{ postgres_auto_upgrade_backup_data_path }}"
  25. register: result_auto_upgrade_path
  26. - name: Abort, if existing Postgres auto-upgrade data path detected
  27. fail:
  28. msg: "Detected that a left-over {{ postgres_auto_upgrade_backup_data_path }} exists. You should rename it to {{ matrix_postgres_data_path }} if the previous upgrade went wrong, or delete it if it went well."
  29. when: "result_auto_upgrade_path.stat.exists"
  30. - import_tasks: tasks/util/detect_existing_postgres_version.yml
  31. - name: Abort, if no existing Postgres version detected
  32. fail:
  33. msg: "Could not find existing Postgres installation"
  34. when: "not matrix_postgres_detected_existing"
  35. - name: Abort, if already at latest Postgres version
  36. fail:
  37. msg: "You are already running the latest Postgres version supported ({{ matrix_postgres_docker_image_latest }}). Nothing to do"
  38. when: "matrix_postgres_detected_version_corresponding_docker_image == matrix_postgres_docker_image_latest"
  39. - debug:
  40. msg: "Upgrading database from {{ matrix_postgres_detected_version_corresponding_docker_image }} to {{ matrix_postgres_docker_image_latest }}"
  41. - name: Ensure matrix-synapse is stopped
  42. service:
  43. name: matrix-synapse
  44. state: stopped
  45. - name: Ensure matrix-postgres is started
  46. service:
  47. name: matrix-postgres
  48. state: started
  49. daemon_reload: yes
  50. - name: Wait a bit, so that Postgres can start
  51. wait_for:
  52. timeout: "{{ postgres_start_wait_time }}"
  53. delegate_to: 127.0.0.1
  54. become: false
  55. - name: Perform Postgres database dump
  56. command: |
  57. /usr/bin/docker run --rm --name matrix-postgres-dump \
  58. --network={{ matrix_docker_network }} \
  59. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql \
  60. -v {{ postgres_dump_dir }}:/out \
  61. {{ matrix_postgres_detected_version_corresponding_docker_image }} pg_dump -h matrix-postgres {{ matrix_postgres_db_name }} -f /out/{{ postgres_dump_name }}
  62. - name: Ensure matrix-postgres is stopped
  63. service:
  64. name: matrix-postgres
  65. state: stopped
  66. - name: Rename existing Postgres data directory
  67. command: "mv {{ matrix_postgres_data_path }} {{ postgres_auto_upgrade_backup_data_path }}"
  68. - debug:
  69. msg: "NOTE: Your Postgres data directory has been moved from `{{ matrix_postgres_data_path }}` to `{{ postgres_auto_upgrade_backup_data_path }}`. In the event of failure, you can move it back and run the playbook with --tags=setup-postgres to restore operation."
  70. - import_tasks: tasks/setup_postgres.yml
  71. - name: Ensure matrix-postgres autoruns and is restarted
  72. service:
  73. name: matrix-postgres
  74. enabled: yes
  75. state: restarted
  76. daemon_reload: yes
  77. - name: Wait a bit, so that Postgres can start
  78. wait_for:
  79. timeout: "{{ postgres_start_wait_time }}"
  80. delegate_to: 127.0.0.1
  81. become: false
  82. - name: Perform Postgres database import
  83. command: |
  84. /usr/bin/docker run --rm --name matrix-postgres-import \
  85. --network={{ matrix_docker_network }} \
  86. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql \
  87. -v {{ postgres_dump_dir }}:/in:ro \
  88. {{ matrix_postgres_docker_image_latest }} psql -v ON_ERROR_STOP=1 -h matrix-postgres -f /in/{{ postgres_dump_name }}
  89. - name: Delete Postgres database dump file
  90. file:
  91. path: "{{ postgres_dump_dir }}/{{ postgres_dump_name }}"
  92. state: absent
  93. - name: Ensure matrix-synapse is started
  94. service:
  95. name: matrix-synapse
  96. state: started
  97. daemon_reload: yes
  98. - debug:
  99. msg: "NOTE: Your old Postgres data directory is preserved at `{{ postgres_auto_upgrade_backup_data_path }}`. You might want to get rid of it once you've confirmed that all is well."