Add (SQLite + Postgres) support and automatic migration to matrix-dimension

This commit is contained in:
Slavi Pantaleev 2020-12-14 21:01:47 +02:00
parent 0790a7b2a8
commit 2a502db239
5 changed files with 67 additions and 7 deletions

View File

@ -716,7 +716,7 @@ matrix_dimension_systemd_required_services_list: |
# Postgres is the default, except if not using `matrix_postgres` (internal postgres)
matrix_dimension_database_engine: "{{ 'postgres' if matrix_postgres_enabled else 'sqlite' }}"
matrix_dimension_database_password: "{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'as.dimension.db') | to_uuid }}"
matrix_dimension_database_password: "{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'dimension.db') | to_uuid }}"
######################################################################
#
@ -1092,13 +1092,12 @@ matrix_postgres_additional_databases: |
}] if (matrix_mx_puppet_steam_enabled
and matrix_mx_puppet_steam_database_engine == 'postgres'
and matrix_mx_puppet_steam_database_hostname == 'matrix-postgres') else [])
+ ([{
+
([{
'name': matrix_dimension_database_db_name,
'username': matrix_dimension_database_username,
'password': matrix_dimension_database_password,
}] if (matrix_dimension_enabled
and matrix_dimension_database_engine == 'postgres'
and matrix_dimension_database_hostname == 'matrix-postgres') else [])
}] if (matrix_dimension_enabled and matrix_dimension_database_engine == 'postgres' and matrix_dimension_database_hostname == 'matrix-postgres') else [])
}}
######################################################################

View File

@ -40,6 +40,28 @@ matrix_dimension_integrations_jitsi_widget_url: "https://{{ matrix_server_fqn_di
matrix_dimension_homeserver_federationUrl: "http://matrix-synapse:8048"
# Database-related configuration fields.
#
# To use SQLite, stick to these defaults.
#
# To use Postgres:
# - change the engine (`matrix_dimension_database_engine: 'postgres'`)
# - adjust your database credentials via the `matrix_dimension_postgres_*` variables
matrix_dimension_database_engine: 'sqlite'
matrix_dimension_sqlite_database_path_local: "{{ matrix_dimension_base_path }}/dimension.db"
matrix_dimension_sqlite_database_path_in_container: "dimension.db"
matrix_dimension_database_username: 'matrix_dimension'
matrix_dimension_database_password: 'some-password'
matrix_dimension_database_hostname: 'matrix-postgres'
matrix_dimension_database_port: 5432
matrix_dimension_database_db_name: 'matrix_dimension'
matrix_dimension_database_connection_string: 'postgres://{{ matrix_dimension_database_username }}:{{ matrix_dimension_database_password }}@{{ matrix_dimension_database_hostname }}:{{ matrix_dimension_database_port }}/{{ matrix_dimension_database_db_name }}'
# Default Dimension configuration template which covers the generic use case.
# You can customize it by controlling the various variables inside it.
#

View File

@ -1,5 +1,32 @@
---
- set_fact:
matrix_dimension_requires_restart: false
- block:
- name: Check if an SQLite database already exists
stat:
path: "{{ matrix_dimension_sqlite_database_path_local }}"
register: matrix_dimension_sqlite_database_path_local_stat_result
- block:
- set_fact:
matrix_postgres_db_migration_request:
src: "{{ matrix_dimension_sqlite_database_path_local }}"
dst: "{{ matrix_dimension_database_connection_string }}"
caller: "{{ role_path|basename }}"
engine_variable_name: 'matrix_dimension_database_engine'
engine_old: 'sqlite'
systemd_services_to_stop: ['matrix-dimension.service']
pgloader_options: ['--with "quote identifiers"']
- import_tasks: "roles/matrix-postgres/tasks/util/migrate_db_to_postgres.yml"
- set_fact:
matrix_dimension_requires_restart: true
when: "matrix_dimension_sqlite_database_path_local_stat_result.stat.exists|bool"
when: "matrix_dimension_database_engine == 'postgres'"
- name: Ensure Dimension base path exists
file:
path: "{{ matrix_dimension_base_path }}"
@ -34,3 +61,9 @@
service:
daemon_reload: yes
when: "matrix_dimension_systemd_service_result.changed|bool"
- name: Ensure matrix-dimension.service restarted, if necessary
service:
name: "matrix-dimension.service"
state: restarted
when: "matrix_dimension_requires_restart|bool"

View File

@ -44,7 +44,11 @@ widgetBlacklist:
# Where the database for Dimension is
database:
uri: "postgres://matrix_dimension:{{ matrix_additional_databases | selectattr('name', 'equalto', 'matrix_dimension') | map(attribute='pass') | first }}@{{ matrix_postgres_connection_hostname }}/matrix_dimension"
{% if matrix_dimension_database_engine == 'sqlite' %}
file: {{ matrix_dimension_sqlite_database_path_in_container|to_json }}
{% elif matrix_dimension_database_engine == 'postgres' %}
uri: {{ matrix_dimension_database_connection_string|to_json }}
{% endif %}
# Display settings that apply to self-hosted go-neb instances
goneb:

View File

@ -16,7 +16,9 @@ ExecStartPre=-{{ matrix_host_command_docker }} kill matrix-dimension
ExecStartPre=-{{ matrix_host_command_docker }} rm matrix-dimension
# Fixup database ownership if it got changed somehow (during a server migration, etc.)
ExecStartPre=-{{ matrix_host_command_chown }} {{ matrix_dimension_user_uid }}:{{ matrix_dimension_user_gid }} {{ matrix_dimension_base_path }}/dimension.db
{% if matrix_dimension_database_engine == 'sqlite' %}
ExecStartPre=-{{ matrix_host_command_chown }} {{ matrix_dimension_user_uid }}:{{ matrix_dimension_user_gid }} {{ matrix_dimension_sqlite_database_path_local }}
{% endif %}
ExecStart={{ matrix_host_command_docker }} run --rm --name matrix-dimension \
--log-driver=none \