From 8ef23a655d414df57a15ecff417ac0e60a200c7f Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Tue, 2 May 2023 13:32:35 +0300 Subject: [PATCH] Restore --tags=import-synapse-sqlite-db support Fixes https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/2669 Removed in 04b9483f0d9e562398e (2022-11-28) when switching from matrix-postgres to the devture-postgres external Ansible role. More details: https://github.com/spantaleev/matrix-docker-ansible-deploy/blob/master/CHANGELOG.md#matrix-postgres-has-been-replaced-by-the-comdevtureansiblerolepostgres-external-role The `import_synapse_sqlite_db.yml` file and documentation has been adapted somewhat compared to before, so that: - it doesn't try to start Postgres automatically. You need to handle this part manually - it doesn't rely on the integrated Postgres and may potentially work with external Postgres instances just the same - it doesn't wipe out the whole database anymore. By default, we assume it's empty anyway and there's no need for such things. If it's not, then it's also probably dangerous to be so destructive. This is all completely untested, but will hopefully work. --- docs/importing-synapse-sqlite.md | 16 +++++--- .../tasks/import_synapse_sqlite_db.yml | 40 +++++++++++++++++++ roles/custom/matrix-synapse/tasks/main.yml | 6 +++ 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 roles/custom/matrix-synapse/tasks/import_synapse_sqlite_db.yml diff --git a/docs/importing-synapse-sqlite.md b/docs/importing-synapse-sqlite.md index aade22261..b5aa9f218 100644 --- a/docs/importing-synapse-sqlite.md +++ b/docs/importing-synapse-sqlite.md @@ -3,24 +3,28 @@ Run this if you'd like to import your database from a previous default installation of Synapse. (don't forget to import your `media_store` files as well - see [the importing-synapse-media-store guide](importing-synapse-media-store.md)). -While this playbook always sets up PostgreSQL, by default a Synapse installation would run -using an SQLite database. +While this playbook only supports running Synapse in combination with PostgreSQL, a Synapse instance installed manually usually defaults to using an SQLite database. -If you have such a Synapse setup and wish to migrate it here (and over to PostgreSQL), this command is for you. +If you have such a Synapse setup and wish to migrate it to one managed by the playbook (and over to PostgreSQL), this documentation page is for you. ## Prerequisites -Before doing the actual import, **you need to upload your SQLite database file to the server** (any path is okay). +Before doing the actual import: +- **ensure you have NOT started Synapse yet**. That is, make sure you have followed the [Installing step](installing.md), but haven't run the playbook's `start` tag yet. If you had started your new Synapse instance, it may have already initialized your Postgres database and importing onto it may not work. In such cases, you may need to clean up the `synapse` database first. +- **ensure you have uploaded your SQLite database file to the server** (any path is okay) +- if you're using the integrated Postgres server (**by default, you are** using it, unless you've explicitly switched to [Using an external PostgreSQL server](configuring-playbook-external-postgres.md)), **make sure Postgres is started** by running `just start-group postgres` ## Importing Run this command (make sure to replace `` with a file path on your server): - ansible-playbook -i inventory/hosts setup.yml --extra-vars='server_path_homeserver_db=' --tags=import-synapse-sqlite-db +```sh +just run-tags import-synapse-sqlite-db --extra-vars=server_path_homeserver_db= +``` **Notes**: -- `` must be a file path to a `homeserver.db` **file on the server** (not on your local machine!). +- `` must be replaced with a file path to a `homeserver.db` **file on the server** (not on your local machine!). - if the SQLite database is from an older version of Synapse, the **importing procedure may run migrations on it to bring it up to date**. That is, your SQLite database file may get modified and become unusable with your older Synapse version. Keeping a copy of the original is probably wise. diff --git a/roles/custom/matrix-synapse/tasks/import_synapse_sqlite_db.yml b/roles/custom/matrix-synapse/tasks/import_synapse_sqlite_db.yml new file mode 100644 index 000000000..92bd36b42 --- /dev/null +++ b/roles/custom/matrix-synapse/tasks/import_synapse_sqlite_db.yml @@ -0,0 +1,40 @@ +--- + +- name: Fail if playbook called incorrectly + ansible.builtin.fail: + msg: "The `server_path_homeserver_db` variable needs to be provided to this playbook, via --extra-vars" + when: "server_path_homeserver_db is not defined or server_path_homeserver_db.startswith('<')" + +- name: Check if the provided SQLite homeserver.db file exists + ansible.builtin.stat: + path: "{{ server_path_homeserver_db }}" + register: result_server_path_homeserver_db_stat + +- name: Fail if provided SQLite homeserver.db file doesn't exist + ansible.builtin.fail: + msg: "File cannot be found on the server at {{ server_path_homeserver_db }}" + when: "not result_server_path_homeserver_db_stat.stat.exists" + +# We don't use the `docker_container` module, because using it with `cap_drop` requires +# a very recent version, which is not available for a lot of people yet. +# +# Also, some old `docker_container` versions were buggy and would leave containers behind +# on failure, which we had to work around to allow retries (by re-running the playbook). +- name: Import SQLite database into Postgres + ansible.builtin.command: + cmd: | + docker run + --rm + --name=matrix-synapse-migrate + --log-driver=none + --user={{ matrix_user_uid }}:{{ matrix_user_gid }} + --cap-drop=ALL + --network={{ matrix_synapse_container_network }} + --entrypoint=python + --mount type=bind,src={{ matrix_synapse_config_dir_path }},dst=/data + --mount type=bind,src={{ matrix_synapse_config_dir_path }},dst=/matrix-media-store-parent/media-store + --mount type=bind,src={{ server_path_homeserver_db }},dst=/{{ server_path_homeserver_db | basename }} + {{ matrix_synapse_docker_image_final }} + /usr/local/bin/synapse_port_db --sqlite-database /{{ server_path_homeserver_db | basename }} --postgres-config /data/homeserver.yaml + register: matrix_postgres_import_synapse_sqlite_db_result + changed_when: matrix_postgres_import_synapse_sqlite_db_result.rc == 0 diff --git a/roles/custom/matrix-synapse/tasks/main.yml b/roles/custom/matrix-synapse/tasks/main.yml index 88a54afc5..743dab5fe 100644 --- a/roles/custom/matrix-synapse/tasks/main.yml +++ b/roles/custom/matrix-synapse/tasks/main.yml @@ -45,6 +45,12 @@ - when: matrix_synapse_enabled | bool ansible.builtin.include_tasks: "{{ role_path }}/tasks/import_media_store.yml" +- tags: + - import-synapse-sqlite-db + block: + - when: matrix_synapse_enabled | bool + ansible.builtin.include_tasks: "{{ role_path }}/tasks/import_synapse_sqlite_db.yml" + - tags: - register-user block: