Add (Postgres + SQLite) support to matrix-mautrix-whatsapp bridge

I don't use this bridge, so this is completely untested.
This commit is contained in:
Slavi Pantaleev 2020-12-14 12:24:37 +02:00
parent 2848322461
commit 6c77eae969
3 changed files with 61 additions and 5 deletions

View file

@ -27,6 +27,42 @@ matrix_mautrix_whatsapp_systemd_wanted_services_list: []
matrix_mautrix_whatsapp_appservice_token: '' matrix_mautrix_whatsapp_appservice_token: ''
matrix_mautrix_whatsapp_homeserver_token: '' matrix_mautrix_whatsapp_homeserver_token: ''
# Database-related configuration fields.
#
# To use SQLite, stick to these defaults.
#
# To use Postgres:
# - change the engine (`matrix_mautrix_whatsapp_database_engine: 'postgres'`)
# - adjust your database credentials via the `matrix_mautrix_whatsapp_postgres_*` variables
matrix_mautrix_whatsapp_database_engine: 'sqlite'
matrix_mautrix_whatsapp_sqlite_database_path_local: "{{ matrix_mautrix_whatsapp_data_path }}/mautrix-whatsapp.db"
matrix_mautrix_whatsapp_sqlite_database_path_in_container: "/data/mautrix-whatsapp.db"
matrix_mautrix_whatsapp_postgres_username: 'matrix_mautrix_whatsapp'
matrix_mautrix_whatsapp_postgres_password: 'some-password'
matrix_mautrix_whatsapp_postgres_hostname: 'matrix-postgres'
matrix_mautrix_whatsapp_postgres_port: 5432
matrix_mautrix_whatsapp_postgres_db_name: 'matrix_mautrix_whatsapp'
matrix_mautrix_whatsapp_postgres_connection_string: 'postgresql://{{ matrix_mautrix_whatsapp_postgres_username }}:{{ matrix_mautrix_whatsapp_postgres_password }}@{{ matrix_mautrix_whatsapp_postgres_hostname }}:{{ matrix_mautrix_whatsapp_postgres_port }}/{{ matrix_mautrix_whatsapp_postgres_db_name }}'
matrix_mautrix_whatsapp_appservice_database_type: "{{
{
'sqlite': 'sqlite3',
'postgres':'postgres',
}[matrix_mautrix_whatsapp_database_engine]
}}"
matrix_mautrix_whatsapp_appservice_database_uri: "{{
{
'sqlite': matrix_mautrix_whatsapp_sqlite_database_path_in_container,
'postgres': matrix_mautrix_whatsapp_postgres_connection_string,
}[matrix_mautrix_whatsapp_database_engine]
}}"
# Can be set to enable automatic double-puppeting via Shared Secret Auth (https://github.com/devture/matrix-synapse-shared-secret-auth). # Can be set to enable automatic double-puppeting via Shared Secret Auth (https://github.com/devture/matrix-synapse-shared-secret-auth).
matrix_mautrix_whatsapp_login_shared_secret: '' matrix_mautrix_whatsapp_login_shared_secret: ''

View file

@ -8,6 +8,26 @@
The matrix-bridge-mautrix-whatsapp role needs to execute before the matrix-synapse role. The matrix-bridge-mautrix-whatsapp role needs to execute before the matrix-synapse role.
when: "matrix_synapse_role_executed|default(False)" when: "matrix_synapse_role_executed|default(False)"
- block:
- name: Check if an SQLite database already exists
stat:
path: "{{ matrix_mautrix_whatsapp_sqlite_database_path_local }}"
register: matrix_mautrix_whatsapp_sqlite_database_path_local_stat_result
- name: Fail if an SQLite database already exists when using Postgres
fail:
msg: >-
matrix_mautrix_whatsapp_database_engine has been set to `postgres` (which is our new default now).
However, we've discovered an existing SQLite database in {{ matrix_mautrix_whatsapp_sqlite_database_path_local }}.
It appears that you've been using this bridge with the SQLite engine until now.
To continue using SQLite, opt into it explicitly: add `matrix_mautrix_whatsapp_database_engine: sqlite` to your vars.yml file and re-run this same command.
Alternatively, to migrate your existing SQLite database to Postgres:
1. Stop all services (`ansible-playbook -i inventory/hosts setup.yml --tags=stop`)
2. Import the SQLite database into Postgres (`ansible-playbook -v -i inventory/hosts setup.yml --tags=import-generic-sqlite-db --extra-vars='sqlite_database_path={{ matrix_mautrix_whatsapp_sqlite_database_path_local }} postgres_connection_string_variable_name=matrix_mautrix_whatsapp_postgres_connection_string'`)
3. Re-run the playbook (`ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start`)
when: "matrix_mautrix_whatsapp_sqlite_database_path_local_stat_result.stat.exists"
when: "matrix_mautrix_whatsapp_database_engine == 'postgres'"
- name: Ensure Mautrix Whatsapp image is pulled - name: Ensure Mautrix Whatsapp image is pulled
docker_image: docker_image:
name: "{{ matrix_mautrix_whatsapp_docker_image }}" name: "{{ matrix_mautrix_whatsapp_docker_image }}"

View file

@ -19,11 +19,11 @@ appservice:
# Database config. # Database config.
database: database:
# The database type. "sqlite3" and "postgres" are supported. # The database type. "sqlite3" and "postgres" are supported.
type: sqlite3 type: {{ matrix_mautrix_whatsapp_appservice_database_type|to_json }}
# The database URI. # The database URI.
# SQLite: File name is enough. https://github.com/mattn/go-sqlite3#connection-string # SQLite: File name is enough. https://github.com/mattn/go-sqlite3#connection-string
# Postgres: Connection string. For example, postgres://user:password@host/database # Postgres: Connection string. For example, postgres://user:password@host/database
uri: postgres://matrix_bridge_whatsapp@{{ matrix_additional_databases | selectattr('name', 'equalto', 'matrix_bridge_whatsapp') | map(attribute='pass') | first }}@{{ matrix_postgres_connection_hostname }}/matrix_bridge_whatsapp uri: {{ matrix_mautrix_whatsapp_appservice_database_uri|to_json }}
# Maximum number of connections. Mostly relevant for Postgres. # Maximum number of connections. Mostly relevant for Postgres.
max_open_conns: 20 max_open_conns: 20
max_idle_conns: 2 max_idle_conns: 2