Skip to content

Commit

Permalink
Add support for automatic (nedb -> Postgres) migration to matrix-apps…
Browse files Browse the repository at this point in the history
…ervice-slack
  • Loading branch information
spantaleev committed Dec 22, 2020
1 parent 9b95e19 commit 8675ded
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 144 deletions.
1 change: 0 additions & 1 deletion roles/matrix-base/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ matrix_docker_package_name: docker-ce
run_postgres_import: true
run_postgres_upgrade: true
run_postgres_import_sqlite_db: true
run_postgres_import_nedb: true
run_postgres_vacuum: true
run_synapse_register_user: true
run_synapse_update_user_password: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@
with_items:
- rooms.db
- users.db

- name: Inject result
set_fact:
matrix_playbook_runtime_results: |
{{
matrix_playbook_runtime_results|default([])
+
[
"NOTE: Your appservice-irc database files have been imported into Postgres. The original database files have been moved from `{{ matrix_appservice_irc_data_path }}/*.db` to `{{ matrix_appservice_irc_data_path }}/*.db.backup`. When you've confirmed that the import went well and everything works, you should be able to safely delete these files."
]
}}
19 changes: 12 additions & 7 deletions roles/matrix-bridge-appservice-slack/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ matrix_appservice_slack_database_password: ~
matrix_appservice_slack_database_hostname: 'matrix-postgres'
matrix_appservice_slack_database_port: 5432
matrix_appservice_slack_database_name: matrix_appservice_slack
matrix_appservice_slack_database_file: /data
matrix_appservice_slack_database_connString: >-2
{%- if matrix_appservice_slack_database_engine == 'postgres' -%}
postgresql://{{ matrix_appservice_slack_database_username }}:{{ matrix_appservice_slack_database_password }}@{{ matrix_appservice_slack_database_hostname }}:{{ matrix_appservice_slack_database_port }}/{{ matrix_appservice_slack_database_name }}?sslmode=disable
{%- elif matrix_appservice_slack_database_engine == 'nedb' -%}
{{ matrix_appservice_slack_database_engine }}://{{ matrix_appservice_slack_database_file }}
{%- endif -%}

# This is just the Postgres connection string, if Postgres is used.
# Naming clashes with `matrix_appservice_slack_database_connectionString` somewhat.
matrix_appservice_slack_database_connection_string: 'postgresql://{{ matrix_appservice_slack_database_username }}:{{ matrix_appservice_slack_database_password }}@{{ matrix_appservice_slack_database_hostname }}:{{ matrix_appservice_slack_database_port }}/{{ matrix_appservice_slack_database_name }}?sslmode=disable'

# This is what actually goes into `database.connectionString` for the bridge.
matrix_appservice_slack_database_connectionString: "{{
{
'nedb': 'nedb:///data',
'postgres': matrix_appservice_slack_database_connection_string,
}[matrix_appservice_slack_database_engine]
}}"


matrix_appservice_slack_configuration_yaml: "{{ lookup('template', 'templates/config.yaml.j2') }}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
- name: Fail if Postgres not enabled
fail:
msg: "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot migrate."
when: "not matrix_postgres_enabled|bool"

# Defaults

- name: Set postgres_start_wait_time, if not provided
set_fact:
postgres_start_wait_time: 15
when: "postgres_start_wait_time|default('') == ''"

# Actual import work

- name: Ensure matrix-postgres is started
service:
name: matrix-postgres
state: started
daemon_reload: yes
register: matrix_postgres_service_start_result

- name: Wait a bit, so that Postgres can start
wait_for:
timeout: "{{ postgres_start_wait_time }}"
delegate_to: 127.0.0.1
become: false
when: "matrix_postgres_service_start_result.changed|bool"

- name: Ensure matrix-appservice-slack is stopped
service:
name: matrix-appservice-slack
state: stopped

- name: Import appservice-slack NeDB database into Postgres
command:
cmd: >-
{{ matrix_host_command_docker }} run
--rm
--user={{ matrix_user_uid }}:{{ matrix_user_gid }}
--cap-drop=ALL
--network={{ matrix_docker_network }}
--mount type=bind,src={{ matrix_appservice_slack_data_path }},dst=/data
--entrypoint=/bin/sh
{{ matrix_appservice_slack_docker_image }}
-c
'/usr/local/bin/node /usr/src/app/lib/scripts/migrateToPostgres.js --dbdir /data --connectionString {{ matrix_appservice_slack_database_connection_string }}'
- name: Archive NeDB database files
command:
cmd: "mv {{ matrix_appservice_slack_data_path }}/{{ item }} {{ matrix_appservice_slack_data_path }}/{{ item }}.backup"
with_items:
- teams.db
- room-store.db
- user-store.db
- event-store.db

- name: Inject result
set_fact:
matrix_playbook_runtime_results: |
{{
matrix_playbook_runtime_results|default([])
+
[
"NOTE: Your appservice-slack database files have been imported into Postgres. The original database files have been moved from `{{ matrix_appservice_slack_data_path }}/*.db` to `{{ matrix_appservice_slack_data_path }}/*.db.backup`. When you've confirmed that the import went well and everything works, you should be able to safely delete these files."
]
}}
37 changes: 30 additions & 7 deletions roles/matrix-bridge-appservice-slack/tasks/setup_install.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
---

- name: Ensure Appservice Slack image is pulled
docker_image:
name: "{{ matrix_appservice_slack_docker_image }}"
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
force_source: "{{ matrix_appservice_slack_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_appservice_slack_docker_image_force_pull }}"

- name: Ensure AppService Slack paths exist
file:
path: "{{ item }}"
Expand All @@ -19,6 +12,30 @@
- "{{ matrix_appservice_slack_config_path }}"
- "{{ matrix_appservice_slack_data_path }}"

- set_fact:
matrix_appservice_slack_requires_restart: false

- block:
- name: Check if a nedb database already exists
stat:
path: "{{ matrix_appservice_slack_data_path }}/teams.db"
register: matrix_appservice_slack_nedb_database_path_local_stat_result

- block:
- import_tasks: "{{ role_path }}/tasks/migrate_nedb_to_postgres.yml"

- set_fact:
matrix_appservice_slack_requires_restart: true
when: "matrix_appservice_slack_nedb_database_path_local_stat_result.stat.exists|bool"
when: "matrix_appservice_slack_database_engine == 'postgres'"

- name: Ensure Appservice Slack image is pulled
docker_image:
name: "{{ matrix_appservice_slack_docker_image }}"
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
force_source: "{{ matrix_appservice_slack_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_appservice_slack_docker_image_force_pull }}"

- name: Ensure Matrix Appservice Slack config installed
copy:
content: "{{ matrix_appservice_slack_configuration|to_nice_yaml }}"
Expand Down Expand Up @@ -46,3 +63,9 @@
service:
daemon_reload: yes
when: "matrix_appservice_slack_systemd_service_result.changed"

- name: Ensure matrix-appservice-slack.service restarted, if necessary
service:
name: "matrix-appservice-slack.service"
state: restarted
when: "matrix_appservice_slack_requires_restart|bool"
20 changes: 0 additions & 20 deletions roles/matrix-bridge-appservice-slack/tasks/validate_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,3 @@
- "matrix_appservice_slack_appservice_token"
- "matrix_appservice_slack_homeserver_token"
- "matrix_appservice_slack_id_token"

- block:
- name: Check if a neDB database already exists
stat:
path: "{{ matrix_appservice_slack_data_path }}/"
register: matrix_appservice_slack_nedb_stat_result

- name: Fail if an neDB database already exists when using Postgres
fail:
msg: >-2
matrix_appservice_slack_database_engine has been set to `postgres` (which is our new default now).
However, we've discovered an existing neDB database in {{ matrix_appservice_slack_data_path }}/.
It appears that you've been using this bridge with the neDB engine until now.
To continue using neDB, opt into it explicitly: add `matrix_appservice_slack_database_engine: nedb` to your vars.yml file and re-run this same command.
Alternatively, to migrate your existing neDB database to Postgres:
1. Stop all services (`ansible-playbook -i inventory/hosts setup.yml --tags=stop`)
2. Import the neDB database into Postgres (`ansible-playbook -v -i inventory/hosts setup.yml --tags=import-generic-sqlite-db --extra-vars='sqlite_database_path={{ matrix_appservice_slack_data_path }} postgres_connection_string_variable_name=matrix_appservice_slack_database_connString'`)
3. Re-run the playbook (`ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start`)
when: "matrix_appservice_slack_nedb_stat_result.stat.exists"
when: "matrix_appservice_slack_database_engine == 'postgres'"
7 changes: 4 additions & 3 deletions roles/matrix-bridge-appservice-slack/templates/config.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ homeserver:

{% if matrix_appservice_slack_database_engine == 'nedb' %}
dbdir: "/data"
{% endif %}
{% else %}
db:
engine: "{{ matrix_appservice_slack_database_engine }}"
connectionString: {{ matrix_appservice_slack_database_connString | to_json }}
engine: {{ matrix_appservice_slack_database_engine|to_json }}
connectionString: {{ matrix_appservice_slack_database_connectionString|to_json }}
{% endif %}

matrix_admin_room: "{{ matrix_appservice_slack_control_room_id }}"
98 changes: 0 additions & 98 deletions roles/matrix-postgres/tasks/import_nedb.yml

This file was deleted.

8 changes: 0 additions & 8 deletions roles/matrix-postgres/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@
tags:
- import-generic-sqlite-db

# Imports slacks neDB to postgres.
- import_tasks: "{{ role_path }}/tasks/import_nedb.yml"
vars:
database: appservice_slack
when: run_postgres_import_nedb|bool
tags:
- import-slack-nedb

- import_tasks: "{{ role_path }}/tasks/upgrade_postgres.yml"
when: run_postgres_upgrade|bool
tags:
Expand Down

0 comments on commit 8675ded

Please sign in to comment.