Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres: create databases for all services #740

Merged
merged 83 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
d9f4914
WIP: postgres: create databases for all services
jdreichmann Nov 29, 2020
77a5c7c
Merge branch 'master' into postgres-per-default
spantaleev Dec 13, 2020
dac0d3a
Add default matrix_postgres_additional_databases
spantaleev Dec 13, 2020
527d5f5
Relocate Postgres additional database creation logic
spantaleev Dec 13, 2020
0641106
Allow username of additional Postgres databases to be different
spantaleev Dec 13, 2020
d251764
Fix syntax issues in matrix_postgres_additional_databases
spantaleev Dec 13, 2020
f47e8a9
Make use of matrix_host_command_docker instead of hardcoding
spantaleev Dec 13, 2020
e2952f1
Determine matrix-postgres IP address without relying on jq
spantaleev Dec 13, 2020
c765ceb
Prevent weird loop error
spantaleev Dec 13, 2020
bbc09d0
Do not execute additional databases creation code if not necessary
spantaleev Dec 13, 2020
da4cb2f
Do not use the postgresql_user/postgresql_db modules
spantaleev Dec 13, 2020
3a037a5
Ensure additional databases contain all the keys that we expect
spantaleev Dec 13, 2020
46a4034
Use "password" for additional Postgres databases, not "pass"
spantaleev Dec 13, 2020
a374d30
Make appservice-discord support both SQLite and Postgres
spantaleev Dec 13, 2020
183d2a1
Ensure matrix-postgres.service is started before creating additional …
spantaleev Dec 13, 2020
dd99499
Simplify password for additional Postgres databases
spantaleev Dec 13, 2020
b87b754
Fail if appservice-discord wants Postgres, but has leftover SQLite data
spantaleev Dec 13, 2020
c66c084
Merge branch 'master' into postgres-per-default
spantaleev Dec 13, 2020
cb969c6
Add --tags=import-generic-sqlite-db (pgloader import)
spantaleev Dec 14, 2020
4617984
Add (SQLite -> Postgres) migration instructions
spantaleev Dec 14, 2020
b217a2e
postgres: set default in playbook to postgres but fallback to sqlite
jdreichmann Dec 14, 2020
f1e85f7
Don't mention Postgres roles, just say users
spantaleev Dec 14, 2020
d91aa5a
Do not introduce sub-variables exposing implementation details
spantaleev Dec 14, 2020
c0edacb
Fix matrix_postgres_additional_databases syntax problems
spantaleev Dec 14, 2020
2a99e84
Rename Postgres-related variables (`matrix*database_*` -> `matrix*pos…
spantaleev Dec 14, 2020
5dba0c0
Make --tags=import-generic-sqlite-db commands not pass a sensitive co…
spantaleev Dec 14, 2020
2d99ade
Add (Postgres + SQLite) support to matrix-mautrix-facebook bridge
spantaleev Dec 14, 2020
a3406a1
Move some things around
spantaleev Dec 14, 2020
9bf8ce8
Add (Postgres + SQLite) support to matrix-mautrix-telegram bridge
spantaleev Dec 14, 2020
05dd091
Rename `_database_hostname` to `_postgres_hostname`
spantaleev Dec 14, 2020
6a2dd80
Fix typo (matrix_telegram_facebook_postgres_hostname -> matrix_mautri…
spantaleev Dec 14, 2020
2848322
Remove some remaining `_database_password` usage in favor of `_postgr…
spantaleev Dec 14, 2020
6c77eae
Add (Postgres + SQLite) support to matrix-mautrix-whatsapp bridge
spantaleev Dec 14, 2020
43d6ff2
Fix sqlite usage for mautrix-facebook/mautrix-telegram
spantaleev Dec 14, 2020
ce21ea3
Add (Postgres + SQLite) support to matrix-mautrix-hangouts bridge
spantaleev Dec 14, 2020
087dbe4
Rename `matrix_*_postgres_*` back to `matrix_*_database_*`
spantaleev Dec 14, 2020
5d70bc1
add postgres support for mx-puppet-* with fallback to sqlite in role …
jdreichmann Dec 14, 2020
b9a04a7
Rename some remaining `matrix_*_postgres_*` vars back to `matrix_*_da…
spantaleev Dec 14, 2020
aa828ff
Separate matrix-reminder-bot install/uninstall tasks
spantaleev Dec 14, 2020
dde1c9f
Fix indentation causing YAML syntax error
spantaleev Dec 14, 2020
e3a0c9a
Add (Postgres + SQLite) support to matrix-reminder-bot
spantaleev Dec 14, 2020
54da61f
add postgres support mx-appservice-[slack|irc] with fallback to nedb …
jdreichmann Dec 14, 2020
bc376c2
Add database migration utility to matrix-postgres role
spantaleev Dec 14, 2020
dc7850e
Fix wording and variable names a bit
spantaleev Dec 14, 2020
ac37091
Enable automatic (SQLite -> Postgres) migration for matrix-reminder-bot
spantaleev Dec 14, 2020
c1431b2
Make use of matrix_postgres_db_migration_request.caller
spantaleev Dec 14, 2020
bbc0872
Enable automatic (SQLite -> Postgres) migration for matrix-mautrix-fa…
spantaleev Dec 14, 2020
3ba8520
Enable automatic (SQLite -> Postgres) migration for matrix-mautrix-ha…
spantaleev Dec 14, 2020
86a8091
Enable automatic (SQLite -> Postgres) migration for matrix-mautrix-te…
spantaleev Dec 14, 2020
13f84e2
Enable automatic (SQLite -> Postgres) migration for matrix-mautrix-wh…
spantaleev Dec 14, 2020
cba973d
Enable automatic (SQLite -> Postgres) migration for matrix-appservice…
spantaleev Dec 14, 2020
7248eb3
Fix syntax error in roles/matrix-bridge-appservice-irc/defaults/main.yml
spantaleev Dec 14, 2020
0ca48f3
Separate matrix-ma1sd install/uninstall tasks
spantaleev Dec 14, 2020
af3ea67
Add (SQLite + Postgres) support and automatic migration to matrix-ma1sd
spantaleev Dec 14, 2020
3ab7dd1
Potentially add matrix-postgres.service as required for bridges, etc.
spantaleev Dec 14, 2020
13d8a9b
hint supported automatic migration nedb->postgres
jdreichmann Dec 14, 2020
516ccb2
Separate matrix-registration install/uninstall tasks
spantaleev Dec 14, 2020
8d74593
Prepare matrix-registation for (SQLite + Postgres) support
spantaleev Dec 14, 2020
374f437
Separate matrix-dimension install/uninstall tasks
spantaleev Dec 14, 2020
0790a7b
Add support for matrix_dimension_systemd_{required,wanted}_services_list
spantaleev Dec 14, 2020
2a502db
Add (SQLite + Postgres) support and automatic migration to matrix-dim…
spantaleev Dec 14, 2020
dd797ba
Fix Postgres database importing/upgrading conflicts
spantaleev Dec 14, 2020
e2ba46b
Fix Jinja2 syntax error (else if -> elif)
spantaleev Dec 14, 2020
3289298
Merge branch 'master' into postgres-per-default
spantaleev Dec 15, 2020
1bd5c24
Add support for executing additional DB migration statements
spantaleev Dec 15, 2020
a197968
Make matrix-registration use Postgres by default
spantaleev Dec 15, 2020
0f4649a
Merge branch 'master' into postgres-per-default
spantaleev Dec 16, 2020
6488e11
Relocate some tasks
spantaleev Dec 22, 2020
9b4bf73
Fix undefined variable reference
spantaleev Dec 22, 2020
149872e
Add support for automatic (Postgres -> SQLite) migration to mx-puppet…
spantaleev Dec 22, 2020
e64758c
Add missing restart task
spantaleev Dec 22, 2020
44c9f4d
Add support for automatic (Postgres -> SQLite) migration to mx-puppet…
spantaleev Dec 22, 2020
d135cd9
Ensure mx-puppet-discord directories are created before attempting mi…
spantaleev Dec 22, 2020
e49eb07
Add support for automatic (Postgres -> SQLite) migration to mx-puppet…
spantaleev Dec 22, 2020
262a25f
Add support for automatic (Postgres -> SQLite) migration to mx-puppet…
spantaleev Dec 22, 2020
69cc214
Add support for automatic (Postgres -> SQLite) migration to mx-puppet…
spantaleev Dec 22, 2020
ab6563c
Add support for automatic (Postgres -> SQLite) migration to mx-puppet…
spantaleev Dec 22, 2020
15f4cc9
Rename variables (_database_db_name -> _database_name)
spantaleev Dec 22, 2020
815a80c
Fix alignment issues
spantaleev Dec 22, 2020
715bdf2
Add support for automatic (nedb -> Postgres) migration to mx-appservi…
spantaleev Dec 22, 2020
9b95e19
Auto-restart matrix-appservice-irc after (nedb -> Postgres) migration
spantaleev Dec 22, 2020
8675ded
Add support for automatic (nedb -> Postgres) migration to matrix-apps…
spantaleev Dec 22, 2020
ad1425e
Add pgloader self-building support (for ARM)
spantaleev Dec 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions group_vars/matrix_servers

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions roles/matrix-bridge-appservice-discord/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ matrix_appservice_discord_bridge_homeserverUrl: "http://matrix-synapse:8008"
matrix_appservice_discord_bridge_disablePresence: false
matrix_appservice_discord_bridge_enableSelfServiceBridging: false

# Database-related configuration fields.
#
# To use SQLite, stick to these defaults.
#
# To use Postgres:
# - change the engine (`matrix_appservice_discord_database_engine: 'postgres'`)
# - adjust your database credentials via the `matrix_appservice_discord_postgres_*` variables
matrix_appservice_discord_database_engine: 'sqlite'

matrix_appservice_discord_sqlite_database_path_local: "{{ matrix_appservice_discord_data_path }}/discord.db"
matrix_appservice_discord_sqlite_database_path_in_container: "/data/discord.db"

matrix_appservice_discord_database_username: 'matrix_appservice_discord'
matrix_appservice_discord_database_password: 'some-password'
matrix_appservice_discord_postgres_hostname: 'matrix-postgres'
matrix_appservice_discord_database_port: 5432
matrix_appservice_discord_database_db_name: 'matrix_appservice_discord'

# These 2 variables are what actually ends up in the bridge configuration.
# It's best if you don't change them directly, but rather redefine the sub-variables that constitute them.
matrix_appservice_discord_database_filename: "{{ matrix_appservice_discord_sqlite_database_path_in_container }}"
matrix_appservice_discord_database_connString: 'postgresql://{{ matrix_appservice_discord_database_username }}:{{ matrix_appservice_discord_database_password }}@{{ matrix_appservice_discord_postgres_hostname }}:{{ matrix_appservice_discord_database_port }}/{{ matrix_appservice_discord_database_db_name }}'


# Tells whether the bot should make use of "Privileged Gateway Intents".
#
# Enabling this means that you need to enable it for the bot (Discord application) as well,
Expand Down
20 changes: 20 additions & 0 deletions roles/matrix-bridge-appservice-discord/tasks/setup_install.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
---

- block:
- name: Check if an SQLite database already exists
stat:
path: "{{ matrix_appservice_discord_sqlite_database_path_local }}"
register: matrix_appservice_discord_sqlite_database_path_local_stat_result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could do sth here with import_tasks for an automigration

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto-migration would be ideal. At first I started on that, but then I figured it might be somewhat dangerous.

For example, importing the Appservice Discord database always gives me at least a few errors ("... cannot have multiple primary keys in emoji table", etc). As of yet, I'm still not sure how perfectly that migration went.

So it makes me see it as something a bit dangerous and a somewhat big change, that users should do explicitly (similar to how we don't auto-upgrade between Postgres versions -- although that's even more dangerous).

Doing auto-migration also seemed more difficult to do, as it involves:

  • (already done) detecting whether we need to migrate
  • stopping the bridge service, so that we can work with the database exclusively
  • doing the migration via import_tasks (should be easy). This likely involves doing the same file.db -> file.db.backup rename
  • then doing all the configuration tasks -- we need to rebuild a bridge's config.yml after all, as there's no file.db database anymore and it's all in Postgres already. If the bridge were to be started at this moment, it would create a new empty database (likely not too catastrophic though).
  • then starting the bridge again if it had been stopped in step 2, above. Is this to be done in the role itself or postponed until the very end of playbook execution (matrix-common-after)? If someone does just --tags=setup-appservice-discord, then we won't even make it to where we expect to in matrix-common-after. Well, that's rare, but ..

It should be possible to have auto-migration though. What we have now is a little annoying to use.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative to both auto-migration and erroring-out like we do now is to: detect that there's leftover SQLite data and automatically switch to using SQLite when generating the configuration.

We could leave the matrix_postgres_additional_databases definitions in place and have these empty databases get created and left unused.

Whenever we detect that leftover data and auto-switch to SQLite, we could push a warning (notice) message to be displayed at the end of playbook execution. We do such a thing for major Postgres upgrades. We automatically keep people on the old version (instead of trying to auto-upgrade them), and we tell them with a message, letting them run the upgrade whenever they feel comfortable. Code is here:

- name: Inject warning if on an old version of Postgres
set_fact:
matrix_playbook_runtime_results: |
{{
matrix_playbook_runtime_results|default([])
+
[
"NOTE: Your setup is on an old Postgres version ({{ matrix_postgres_docker_image_to_use }}), while {{ matrix_postgres_docker_image_latest }} is supported. You can upgrade using --tags=upgrade-postgres"
]
}}
when: "matrix_postgres_enabled|bool and matrix_postgres_docker_image_to_use != matrix_postgres_docker_image_latest"

What do you think? To me, it sounds reasonable and simple to implement.. It's also not dangerous.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That adds an extra layer of "let the user choose", and i think it becomes harder and harder to keep the change small. ideally, we would automigrate everything and have the roles themselves with sqlite as default, supporting delaying, external postgres, etc etc blows up the scope, and users would except that a merged feature was tested with every combination.
TL;DR: could do, but might bite us later, and we should encourage users to migrate to postgres by making the path to migrate easy (encourage users to actually do it and not perpetually delay it)

- name: Fail if an SQLite database already exists when using Postgres
fail:
msg: >-
matrix_appservice_discord_database_engine has been set to `postgres` (which is our new default now).
However, we've discovered an existing SQLite database in {{ matrix_appservice_discord_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_appservice_discord_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_appservice_discord_sqlite_database_path_local }} postgres_connection_string_variable_name=matrix_appservice_discord_database_connString'`)
3. Re-run the playbook (`ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start`)
when: "matrix_appservice_discord_sqlite_database_path_local_stat_result.stat.exists"
when: "matrix_appservice_discord_database_engine == 'postgres'"

- name: Ensure Appservice Discord image is pulled
docker_image:
name: "{{ matrix_appservice_discord_docker_image }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
when: "item.old in vars"
with_items:
- {'old': 'matrix_appservice_discord_container_expose_client_server_api_port', 'new': '<superseded by matrix_appservice_discord_container_http_host_bind_port>'}

- name: Require a valid database engine
fail: msg="`matrix_appservice_discord_database_engine` needs to be either 'sqlite' or 'postgres'"
when: "matrix_appservice_discord_database_engine not in ['sqlite', 'postgres']"
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ database:
# If you are migrating, see https://github.com/Half-Shot/matrix-appservice-discord/blob/master/docs/howto.md#migrate-to-postgres-from-sqlite
# WARNING: You will almost certainly be fine with sqlite unless your bridge
# is in heavy demand and you suffer from IO slowness.
filename: "/data/discord.db"
# connString: "postgresql://user:password@localhost/database_name"
{% if matrix_appservice_discord_database_engine == 'sqlite' %}
filename: {{ matrix_appservice_discord_database_filename|to_json }}
{% else %}
connString: {{ matrix_appservice_discord_database_connString|to_json }}
jdreichmann marked this conversation as resolved.
Show resolved Hide resolved
{% endif %}
room:
# Set the default visibility of alias rooms, defaults to "public".
# One of: "public", "private"
Expand Down
4 changes: 2 additions & 2 deletions roles/matrix-bridge-appservice-irc/templates/config.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ advanced:
# Use an external database to store bridge state.
database:
# database engine (must be 'postgres' or 'nedb'). Default: nedb
engine: "nedb"
engine: "postgres"
# Either a PostgreSQL connection string, or a path to the NeDB storage directory.
# For postgres, it must start with postgres://
# For NeDB, it must start with nedb://. The path is relative to the project directory.
connectionString: "nedb:///data"
connectionString: "postgres://matrix_appservice_irc:{{ matrix_addtional_databases | selectattr('name', 'equalto', 'matrix_appservice_irc') | map(attribute='pass') | first }}@{{ matrix_postgres_connection_hostname }}/matrix_appservice_irc"
3 changes: 3 additions & 0 deletions roles/matrix-bridge-appservice-slack/templates/config.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ homeserver:
server_name: "{{ matrix_domain }}"

dbdir: "/data"
db:
engine: "postgres"
connectionString: "postgresql://matrix_appservice_slack:{{ matrix_addtional_databases | selectattr('name', 'equalto', 'matrix_appservice_slack') | map(attribute='pass') | first }}@{{ matrix_postgres_connection_hostname }}/matrix_appservice_slack"

matrix_admin_room: "{{ matrix_appservice_slack_control_room_id }}"
29 changes: 29 additions & 0 deletions roles/matrix-bridge-mautrix-facebook/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ matrix_mautrix_facebook_systemd_wanted_services_list: []
matrix_mautrix_facebook_appservice_token: ''
matrix_mautrix_facebook_homeserver_token: ''


# Database-related configuration fields.
#
# To use SQLite, stick to these defaults.
#
# To use Postgres:
# - change the engine (`matrix_mautrix_facebook_database_engine: 'postgres'`)
# - adjust your database credentials via the `matrix_mautrix_facebook_postgres_*` variables
matrix_mautrix_facebook_database_engine: 'sqlite'

matrix_mautrix_facebook_sqlite_database_path_local: "{{ matrix_mautrix_facebook_data_path }}/mautrix-facebook.db"
matrix_mautrix_facebook_sqlite_database_path_in_container: "/data/mautrix-facebook.db"

matrix_mautrix_facebook_database_username: 'matrix_mautrix_facebook'
matrix_mautrix_facebook_database_password: 'some-password'
matrix_mautrix_facebook_postgres_hostname: 'matrix-postgres'
matrix_mautrix_facebook_database_port: 5432
matrix_mautrix_facebook_database_db_name: 'matrix_mautrix_facebook'

matrix_mautrix_facebook_postgres_connection_string: 'postgresql://{{ matrix_mautrix_facebook_database_username }}:{{ matrix_mautrix_facebook_database_password }}@{{ matrix_mautrix_facebook_postgres_hostname }}:{{ matrix_mautrix_facebook_database_port }}/{{ matrix_mautrix_facebook_database_db_name }}'

matrix_mautrix_facebook_appservice_database: "{{
{
'sqlite': ('sqlite://' + matrix_mautrix_facebook_sqlite_database_path_in_container),
'postgres': matrix_mautrix_facebook_postgres_connection_string,
}[matrix_mautrix_facebook_database_engine]
}}"


# Can be set to enable automatic double-puppeting via Shared Secret Auth (https://github.com/devture/matrix-synapse-shared-secret-auth).
matrix_mautrix_facebook_login_shared_secret: ''

Expand Down
20 changes: 20 additions & 0 deletions roles/matrix-bridge-mautrix-facebook/tasks/setup_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
The matrix-bridge-mautrix-facebook role needs to execute before the matrix-synapse role.
when: "matrix_synapse_role_executed|default(False)"

- block:
- name: Check if an SQLite database already exists
stat:
path: "{{ matrix_mautrix_facebook_sqlite_database_path_local }}"
register: matrix_mautrix_facebook_sqlite_database_path_local_stat_result

- name: Fail if an SQLite database already exists when using Postgres
fail:
msg: >-
matrix_mautrix_facebook_database_engine has been set to `postgres` (which is our new default now).
However, we've discovered an existing SQLite database in {{ matrix_mautrix_facebook_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_facebook_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_facebook_sqlite_database_path_local }} postgres_connection_string_variable_name=matrix_mautrix_facebook_postgres_connection_string'`)
3. Re-run the playbook (`ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start`)
when: "matrix_mautrix_facebook_sqlite_database_path_local_stat_result.stat.exists"
when: "matrix_mautrix_facebook_database_engine == 'postgres'"

- name: Ensure Mautrix Facebook image is pulled
docker_image:
name: "{{ matrix_mautrix_facebook_docker_image }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ appservice:
# Format examples:
# SQLite: sqlite:///filename.db
# Postgres: postgres://username:password@hostname/dbname
database: sqlite:////data/mautrix-facebook.db
database: {{ matrix_mautrix_facebook_appservice_database|to_json }}

# Public part of web server for out-of-Matrix interaction with the bridge.
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ExecStartPre={{ matrix_host_command_docker }} run --rm --name matrix-mautrix-fac
--log-driver=none \
--user={{ matrix_user_uid }}:{{ matrix_user_gid }} \
--cap-drop=ALL \
--network={{ matrix_docker_network }} \
-v {{ matrix_mautrix_facebook_config_path }}:/config:z \
-v {{ matrix_mautrix_facebook_data_path }}:/data:z \
{{ matrix_mautrix_facebook_docker_image }} \
Expand Down
29 changes: 29 additions & 0 deletions roles/matrix-bridge-mautrix-hangouts/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ matrix_mautrix_hangouts_systemd_wanted_services_list: []
matrix_mautrix_hangouts_appservice_token: ''
matrix_mautrix_hangouts_homeserver_token: ''


# Database-related configuration fields.
#
# To use SQLite, stick to these defaults.
#
# To use Postgres:
# - change the engine (`matrix_mautrix_hangouts_database_engine: 'postgres'`)
# - adjust your database credentials via the `matrix_mautrix_hangouts_postgres_*` variables
matrix_mautrix_hangouts_database_engine: 'sqlite'

matrix_mautrix_hangouts_sqlite_database_path_local: "{{ matrix_mautrix_hangouts_data_path }}/mautrix-hangouts.db"
matrix_mautrix_hangouts_sqlite_database_path_in_container: "/data/mautrix-hangouts.db"

matrix_mautrix_hangouts_database_username: 'matrix_mautrix_hangouts'
matrix_mautrix_hangouts_database_password: 'some-password'
matrix_mautrix_hangouts_postgres_hostname: 'matrix-postgres'
matrix_mautrix_hangouts_database_port: 5432
matrix_mautrix_hangouts_database_db_name: 'matrix_mautrix_hangouts'

matrix_mautrix_hangouts_postgres_connection_string: 'postgresql://{{ matrix_mautrix_hangouts_database_username }}:{{ matrix_mautrix_hangouts_database_password }}@{{ matrix_mautrix_hangouts_postgres_hostname }}:{{ matrix_mautrix_hangouts_database_port }}/{{ matrix_mautrix_hangouts_database_db_name }}'

matrix_mautrix_hangouts_appservice_database: "{{
{
'sqlite': ('sqlite://' + matrix_mautrix_hangouts_sqlite_database_path_in_container),
'postgres': matrix_mautrix_hangouts_postgres_connection_string,
}[matrix_mautrix_hangouts_database_engine]
}}"


# Can be set to enable automatic double-puppeting via Shared Secret Auth (https://github.com/devture/matrix-synapse-shared-secret-auth).
matrix_mautrix_hangouts_login_shared_secret: ''

Expand Down
20 changes: 20 additions & 0 deletions roles/matrix-bridge-mautrix-hangouts/tasks/setup_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
The matrix-bridge-mautrix-hangouts role needs to execute before the matrix-synapse role.
when: "matrix_synapse_role_executed|default(False)"

- block:
- name: Check if an SQLite database already exists
stat:
path: "{{ matrix_mautrix_hangouts_sqlite_database_path_local }}"
register: matrix_mautrix_hangouts_sqlite_database_path_local_stat_result

- name: Fail if an SQLite database already exists when using Postgres
fail:
msg: >-
matrix_mautrix_hangouts_database_engine has been set to `postgres` (which is our new default now).
However, we've discovered an existing SQLite database in {{ matrix_mautrix_hangouts_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_hangouts_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_hangouts_sqlite_database_path_local }} postgres_connection_string_variable_name=matrix_mautrix_hangouts_postgres_connection_string'`)
3. Re-run the playbook (`ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start`)
when: "matrix_mautrix_hangouts_sqlite_database_path_local_stat_result.stat.exists"
when: "matrix_mautrix_hangouts_database_engine == 'postgres'"

- name: Ensure Mautrix Hangouts image is pulled
docker_image:
name: "{{ matrix_mautrix_hangouts_docker_image }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ appservice:
# Format examples:
# SQLite: sqlite:///filename.db
# Postgres: postgres://username:password@hostname/dbname
database: sqlite:////data/mautrix-hangouts.db
database: {{ matrix_mautrix_hangouts_appservice_database|to_json }}

# The unique ID of this appservice.
id: hangouts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ExecStartPre={{ matrix_host_command_docker }} run --rm --name matrix-mautrix-han
--log-driver=none \
--user={{ matrix_user_uid }}:{{ matrix_user_gid }} \
--cap-drop=ALL \
--network={{ matrix_docker_network }} \
-v {{ matrix_mautrix_hangouts_config_path }}:/config:z \
-v {{ matrix_mautrix_hangouts_data_path }}:/data:z \
{{ matrix_mautrix_hangouts_docker_image }} \
Expand Down
29 changes: 29 additions & 0 deletions roles/matrix-bridge-mautrix-telegram/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@ matrix_mautrix_telegram_systemd_wanted_services_list: []
matrix_mautrix_telegram_appservice_token: ''
matrix_mautrix_telegram_homeserver_token: ''


# Database-related configuration fields.
#
# To use SQLite, stick to these defaults.
#
# To use Postgres:
# - change the engine (`matrix_mautrix_telegram_database_engine: 'postgres'`)
# - adjust your database credentials via the `matrix_mautrix_telegram_postgres_*` variables
matrix_mautrix_telegram_database_engine: 'sqlite'

matrix_mautrix_telegram_sqlite_database_path_local: "{{ matrix_mautrix_telegram_data_path }}/mautrix-telegram.db"
matrix_mautrix_telegram_sqlite_database_path_in_container: "/data/mautrix-telegram.db"

matrix_mautrix_telegram_database_username: 'matrix_mautrix_telegram'
matrix_mautrix_telegram_database_password: 'some-password'
matrix_mautrix_telegram_postgres_hostname: 'matrix-postgres'
matrix_mautrix_telegram_database_port: 5432
matrix_mautrix_telegram_database_db_name: 'matrix_mautrix_telegram'

matrix_mautrix_telegram_postgres_connection_string: 'postgresql://{{ matrix_mautrix_telegram_database_username }}:{{ matrix_mautrix_telegram_database_password }}@{{ matrix_mautrix_telegram_postgres_hostname }}:{{ matrix_mautrix_telegram_database_port }}/{{ matrix_mautrix_telegram_database_db_name }}'

matrix_mautrix_telegram_appservice_database: "{{
{
'sqlite': ('sqlite://' + matrix_mautrix_telegram_sqlite_database_path_in_container),
'postgres': matrix_mautrix_telegram_postgres_connection_string,
}[matrix_mautrix_telegram_database_engine]
}}"


# Can be set to enable automatic double-puppeting via Shared Secret Auth (https://github.com/devture/matrix-synapse-shared-secret-auth).
matrix_mautrix_telegram_login_shared_secret: ''

Expand Down
20 changes: 20 additions & 0 deletions roles/matrix-bridge-mautrix-telegram/tasks/setup_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
The matrix-bridge-mautrix-telegram role needs to execute before the matrix-synapse role.
when: "matrix_synapse_role_executed|default(False)"

- block:
- name: Check if an SQLite database already exists
stat:
path: "{{ matrix_mautrix_telegram_sqlite_database_path_local }}"
register: matrix_mautrix_telegram_sqlite_database_path_local_stat_result

- name: Fail if an SQLite database already exists when using Postgres
fail:
msg: >-
matrix_mautrix_telegram_database_engine has been set to `postgres` (which is our new default now).
However, we've discovered an existing SQLite database in {{ matrix_mautrix_telegram_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_telegram_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_telegram_sqlite_database_path_local }} postgres_connection_string_variable_name=matrix_mautrix_telegram_postgres_connection_string'`)
3. Re-run the playbook (`ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start`)
when: "matrix_mautrix_telegram_sqlite_database_path_local_stat_result.stat.exists"
when: "matrix_mautrix_telegram_database_engine == 'postgres'"

- name: Ensure Mautrix Telegram image is pulled
docker_image:
name: "{{ matrix_mautrix_telegram_docker_image }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ appservice:
# Format examples:
# SQLite: sqlite:///filename.db
# Postgres: postgres://username:password@hostname/dbname
database: sqlite:////data/mautrix-telegram.db
database: {{ matrix_mautrix_telegram_appservice_database|to_json }}

# Public part of web server for out-of-Matrix interaction with the bridge.
# Used for things like login if the user wants to make sure the 2FA password isn't stored in
Expand Down
Loading