Add (Postgres + SQLite) support to matrix-reminder-bot

This has been tested and appears to work.
This commit is contained in:
Slavi Pantaleev 2020-12-14 15:02:11 +02:00
parent dde1c9f899
commit e3a0c9adda
4 changed files with 59 additions and 1 deletions

View File

@ -580,6 +580,10 @@ matrix_mx_puppet_steam_database_password: "{{ matrix_synapse_macaroon_secret_key
# We don't enable bots by default. # We don't enable bots by default.
matrix_bot_matrix_reminder_bot_enabled: false matrix_bot_matrix_reminder_bot_enabled: false
# Postgres is the default, except if not using `matrix_postgres` (internal postgres)
matrix_bot_matrix_reminder_bot_database_engine: "{{ 'postgres' if matrix_postgres_enabled else 'sqlite' }}"
matrix_bot_matrix_reminder_bot_database_password: "{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'reminder.bot.db') | to_uuid }}"
###################################################################### ######################################################################
# #
# /matrix-bot-matrix-reminder-bot # /matrix-bot-matrix-reminder-bot
@ -949,6 +953,12 @@ matrix_postgres_db_name: "homeserver"
matrix_postgres_additional_databases: | matrix_postgres_additional_databases: |
{{ {{
([{
'name': matrix_bot_matrix_reminder_bot_database_db_name,
'username': matrix_bot_matrix_reminder_bot_database_username,
'password': matrix_bot_matrix_reminder_bot_database_password,
}] if (matrix_bot_matrix_reminder_bot_enabled and matrix_bot_matrix_reminder_bot_database_engine == 'postgres' and matrix_bot_matrix_reminder_bot_database_hostname == 'matrix-postgres') else [])
+
([{ ([{
'name': matrix_appservice_discord_database_db_name, 'name': matrix_appservice_discord_database_db_name,
'username': matrix_appservice_discord_database_username, 'username': matrix_appservice_discord_database_username,

View File

@ -21,6 +21,34 @@ matrix_bot_matrix_reminder_bot_systemd_required_services_list: ['docker.service'
matrix_bot_matrix_reminder_bot_systemd_wanted_services_list: [] matrix_bot_matrix_reminder_bot_systemd_wanted_services_list: []
# Database-related configuration fields.
#
# To use SQLite, stick to these defaults.
#
# To use Postgres:
# - change the engine (`matrix_bot_matrix_reminder_bot_database_engine: 'postgres'`)
# - adjust your database credentials via the `matrix_bot_matrix_reminder_bot_database_*` variables
matrix_bot_matrix_reminder_bot_database_engine: 'sqlite'
matrix_bot_matrix_reminder_bot_sqlite_database_path_local: "{{ matrix_bot_matrix_reminder_bot_data_path }}/bot.db"
matrix_bot_matrix_reminder_bot_sqlite_database_path_in_container: "/data/bot.db"
matrix_bot_matrix_reminder_bot_database_username: 'matrix_reminder_bot'
matrix_bot_matrix_reminder_bot_database_password: 'some-password'
matrix_bot_matrix_reminder_bot_database_hostname: 'matrix-postgres'
matrix_bot_matrix_reminder_bot_database_port: 5432
matrix_bot_matrix_reminder_bot_database_db_name: 'matrix_reminder_bot'
matrix_bot_matrix_reminder_bot_database_connection_string: 'postgres://{{ matrix_bot_matrix_reminder_bot_database_username }}:{{ matrix_bot_matrix_reminder_bot_database_password }}@{{ matrix_bot_matrix_reminder_bot_database_hostname }}:{{ matrix_bot_matrix_reminder_bot_database_port }}/{{ matrix_bot_matrix_reminder_bot_database_db_name }}'
matrix_bot_matrix_reminder_bot_storage_database: "{{
{
'sqlite': ('sqlite://' + matrix_bot_matrix_reminder_bot_sqlite_database_path_in_container),
'postgres': matrix_bot_matrix_reminder_bot_database_connection_string,
}[matrix_bot_matrix_reminder_bot_database_engine]
}}"
# The bot's username. This user needs to be created manually beforehand. # The bot's username. This user needs to be created manually beforehand.
# Also see `matrix_bot_matrix_reminder_bot_user_password`. # Also see `matrix_bot_matrix_reminder_bot_user_password`.
matrix_bot_matrix_reminder_bot_matrix_user_id_localpart: "bot.matrix-reminder-bot" matrix_bot_matrix_reminder_bot_matrix_user_id_localpart: "bot.matrix-reminder-bot"

View File

@ -1,5 +1,25 @@
--- ---
- block:
- name: Check if an SQLite database already exists
stat:
path: "{{ matrix_bot_matrix_reminder_bot_sqlite_database_path_local }}"
register: matrix_bot_matrix_reminder_bot_sqlite_database_path_local_stat_result
- name: Fail if an SQLite database already exists when using Postgres
fail:
msg: >-
matrix_bot_matrix_reminder_bot_database_engine has been set to `postgres` (which is our new default now).
However, we've discovered an existing SQLite database in {{ matrix_bot_matrix_reminder_bot_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_bot_matrix_reminder_bot_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_bot_matrix_reminder_bot_sqlite_database_path_local }} postgres_connection_string_variable_name=matrix_bot_matrix_reminder_bot_database_connection_string'`)
3. Re-run the playbook (`ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start`)
when: "matrix_bot_matrix_reminder_bot_sqlite_database_path_local_stat_result.stat.exists"
when: "matrix_bot_matrix_reminder_bot_database_engine == 'postgres'"
- name: Ensure matrix-reminder-bot paths exist - name: Ensure matrix-reminder-bot paths exist
file: file:
path: "{{ item.path }}" path: "{{ item.path }}"

View File

@ -23,7 +23,7 @@ storage:
# For Postgres, this would look like: # For Postgres, this would look like:
# database: "postgres://username:password@localhost/dbname?sslmode=disable" # database: "postgres://username:password@localhost/dbname?sslmode=disable"
#database: "postgres://matrix-reminder-bot:remindme@localhost/matrix-reminder-bot?sslmode=disable" #database: "postgres://matrix-reminder-bot:remindme@localhost/matrix-reminder-bot?sslmode=disable"
database: "sqlite:///data/bot.db" database: {{ matrix_bot_matrix_reminder_bot_storage_database|to_json }}
# The path to a directory for internal bot storage # The path to a directory for internal bot storage
# containing encryption keys, sync tokens, etc. # containing encryption keys, sync tokens, etc.
store_path: "/data/store" store_path: "/data/store"