Merge pull request #740 from jdreichmann/postgres-per-default
postgres: create databases for all servicesdevelopment
commit
f19b29846d
@ -1,88 +0,0 @@
|
||||
---
|
||||
|
||||
#
|
||||
# Tasks related to setting up matrix-reminder-bot
|
||||
#
|
||||
|
||||
- name: Ensure matrix-reminder-bot paths exist
|
||||
file:
|
||||
path: "{{ item.path }}"
|
||||
state: directory
|
||||
mode: 0750
|
||||
owner: "{{ matrix_user_username }}"
|
||||
group: "{{ matrix_user_groupname }}"
|
||||
with_items:
|
||||
- { path: "{{ matrix_bot_matrix_reminder_bot_config_path }}", when: true }
|
||||
- { path: "{{ matrix_bot_matrix_reminder_bot_data_path }}", when: true }
|
||||
- { path: "{{ matrix_bot_matrix_reminder_bot_data_store_path }}", when: true }
|
||||
when: matrix_bot_matrix_reminder_bot_enabled|bool and item.when
|
||||
|
||||
- name: Ensure matrix-reminder-bot image is pulled
|
||||
docker_image:
|
||||
name: "{{ matrix_bot_matrix_reminder_bot_docker_image }}"
|
||||
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
|
||||
force_source: "{{ matrix_bot_matrix_reminder_bot_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_bot_matrix_reminder_bot_docker_image_force_pull }}"
|
||||
when: matrix_bot_matrix_reminder_bot_enabled|bool
|
||||
|
||||
- name: Ensure matrix-reminder-bot config installed
|
||||
copy:
|
||||
content: "{{ matrix_bot_matrix_reminder_bot_configuration|to_nice_yaml }}"
|
||||
dest: "{{ matrix_bot_matrix_reminder_bot_config_path }}/config.yaml"
|
||||
mode: 0644
|
||||
owner: "{{ matrix_user_username }}"
|
||||
group: "{{ matrix_user_groupname }}"
|
||||
when: matrix_bot_matrix_reminder_bot_enabled|bool
|
||||
|
||||
- name: Ensure matrix-matrix-reminder-bot.service installed
|
||||
template:
|
||||
src: "{{ role_path }}/templates/systemd/matrix-bot-matrix-reminder-bot.service.j2"
|
||||
dest: "{{ matrix_systemd_path }}/matrix-bot-matrix-reminder-bot.service"
|
||||
mode: 0644
|
||||
register: matrix_bot_matrix_reminder_bot_systemd_service_result
|
||||
when: matrix_bot_matrix_reminder_bot_enabled|bool
|
||||
|
||||
- name: Ensure systemd reloaded after matrix-matrix-reminder-bot.service installation
|
||||
service:
|
||||
daemon_reload: yes
|
||||
when: "matrix_bot_matrix_reminder_bot_enabled|bool and matrix_bot_matrix_reminder_bot_systemd_service_result.changed"
|
||||
|
||||
#
|
||||
# Tasks related to getting rid of matrix-reminder-bot (if it was previously enabled)
|
||||
#
|
||||
|
||||
- name: Check existence of matrix-matrix-reminder-bot service
|
||||
stat:
|
||||
path: "{{ matrix_systemd_path }}/matrix-matrix-reminder-bot.service"
|
||||
register: matrix_bot_matrix_reminder_bot_service_stat
|
||||
|
||||
- name: Ensure matrix-matrix-reminder-bot is stopped
|
||||
service:
|
||||
name: matrix-matrix-reminder-bot
|
||||
state: stopped
|
||||
daemon_reload: yes
|
||||
register: stopping_result
|
||||
when: "not matrix_bot_matrix_reminder_bot_enabled|bool and matrix_bot_matrix_reminder_bot_service_stat.stat.exists"
|
||||
|
||||
- name: Ensure matrix-matrix-reminder-bot.service doesn't exist
|
||||
file:
|
||||
path: "{{ matrix_systemd_path }}/matrix-matrix-reminder-bot.service"
|
||||
state: absent
|
||||
when: "not matrix_bot_matrix_reminder_bot_enabled|bool and matrix_bot_matrix_reminder_bot_service_stat.stat.exists"
|
||||
|
||||
- name: Ensure systemd reloaded after matrix-matrix-reminder-bot.service removal
|
||||
service:
|
||||
daemon_reload: yes
|
||||
when: "not matrix_bot_matrix_reminder_bot_enabled|bool and matrix_bot_matrix_reminder_bot_service_stat.stat.exists"
|
||||
|
||||
- name: Ensure Matrix matrix-reminder-bot paths don't exist
|
||||
file:
|
||||
path: "{{ matrix_bot_matrix_reminder_bot_base_path }}"
|
||||
state: absent
|
||||
when: "not matrix_bot_matrix_reminder_bot_enabled|bool"
|
||||
|
||||
- name: Ensure matrix-reminder-bot Docker image doesn't exist
|
||||
docker_image:
|
||||
name: "{{ matrix_bot_matrix_reminder_bot_docker_image }}"
|
||||
state: absent
|
||||
when: "not matrix_bot_matrix_reminder_bot_enabled|bool"
|
@ -0,0 +1,73 @@
|
||||
---
|
||||
|
||||
- set_fact:
|
||||
matrix_bot_matrix_reminder_bot_requires_restart: false
|
||||
|
||||
- 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
|
||||
|
||||
- block:
|
||||
- set_fact:
|
||||
matrix_postgres_db_migration_request:
|
||||
src: "{{ matrix_bot_matrix_reminder_bot_sqlite_database_path_local }}"
|
||||
dst: "{{ matrix_bot_matrix_reminder_bot_database_connection_string }}"
|
||||
caller: "{{ role_path|basename }}"
|
||||
engine_variable_name: 'matrix_bot_matrix_reminder_bot_database_engine'
|
||||
engine_old: 'sqlite'
|
||||
systemd_services_to_stop: ['matrix-bot-matrix-reminder-bot.service']
|
||||
|
||||
- import_tasks: "roles/matrix-postgres/tasks/util/migrate_db_to_postgres.yml"
|
||||
|
||||
- set_fact:
|
||||
matrix_bot_matrix_reminder_bot_requires_restart: true
|
||||
when: "matrix_bot_matrix_reminder_bot_sqlite_database_path_local_stat_result.stat.exists|bool"
|
||||
when: "matrix_bot_matrix_reminder_bot_database_engine == 'postgres'"
|
||||
|
||||
- name: Ensure matrix-reminder-bot paths exist
|
||||
file:
|
||||
path: "{{ item.path }}"
|
||||
state: directory
|
||||
mode: 0750
|
||||
owner: "{{ matrix_user_username }}"
|
||||
group: "{{ matrix_user_groupname }}"
|
||||
with_items:
|
||||
- { path: "{{ matrix_bot_matrix_reminder_bot_config_path }}", when: true }
|
||||
- { path: "{{ matrix_bot_matrix_reminder_bot_data_path }}", when: true }
|
||||
- { path: "{{ matrix_bot_matrix_reminder_bot_data_store_path }}", when: true }
|
||||
when: "item.when|bool"
|
||||
|
||||
- name: Ensure matrix-reminder-bot image is pulled
|
||||
docker_image:
|
||||
name: "{{ matrix_bot_matrix_reminder_bot_docker_image }}"
|
||||
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
|
||||
force_source: "{{ matrix_bot_matrix_reminder_bot_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_bot_matrix_reminder_bot_docker_image_force_pull }}"
|
||||
|
||||
- name: Ensure matrix-reminder-bot config installed
|
||||
copy:
|
||||
content: "{{ matrix_bot_matrix_reminder_bot_configuration|to_nice_yaml }}"
|
||||
dest: "{{ matrix_bot_matrix_reminder_bot_config_path }}/config.yaml"
|
||||
mode: 0644
|
||||
owner: "{{ matrix_user_username }}"
|
||||
group: "{{ matrix_user_groupname }}"
|
||||
|
||||
- name: Ensure matrix-bot-matrix-reminder-bot.service installed
|
||||
template:
|
||||
src: "{{ role_path }}/templates/systemd/matrix-bot-matrix-reminder-bot.service.j2"
|
||||
dest: "{{ matrix_systemd_path }}/matrix-bot-matrix-reminder-bot.service"
|
||||
mode: 0644
|
||||
register: matrix_bot_matrix_reminder_bot_systemd_service_result
|
||||
|
||||
- name: Ensure systemd reloaded after matrix-bot-matrix-reminder-bot.service installation
|
||||
service:
|
||||
daemon_reload: yes
|
||||
when: "matrix_bot_matrix_reminder_bot_systemd_service_result.changed|bool"
|
||||
|
||||
- name: Ensure matrix-bot-matrix-reminder-bot.service restarted, if necessary
|
||||
service:
|
||||
name: "matrix-bot-matrix-reminder-bot.service"
|
||||
state: restarted
|
||||
when: "matrix_bot_matrix_reminder_bot_requires_restart|bool"
|
@ -0,0 +1,35 @@
|
||||
---
|
||||
|
||||
- name: Check existence of matrix-matrix-reminder-bot service
|
||||
stat:
|
||||
path: "{{ matrix_systemd_path }}/matrix-bot-matrix-reminder-bot.service"
|
||||
register: matrix_bot_matrix_reminder_bot_service_stat
|
||||
|
||||
- name: Ensure matrix-matrix-reminder-bot is stopped
|
||||
service:
|
||||
name: matrix-matrix-reminder-bot
|
||||
state: stopped
|
||||
daemon_reload: yes
|
||||
register: stopping_result
|
||||
when: "matrix_bot_matrix_reminder_bot_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure matrix-bot-matrix-reminder-bot.service doesn't exist
|
||||
file:
|
||||
path: "{{ matrix_systemd_path }}/matrix-bot-matrix-reminder-bot.service"
|
||||
state: absent
|
||||
when: "matrix_bot_matrix_reminder_bot_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure systemd reloaded after matrix-bot-matrix-reminder-bot.service removal
|
||||
service:
|
||||
daemon_reload: yes
|
||||
when: "matrix_bot_matrix_reminder_bot_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure Matrix matrix-reminder-bot paths don't exist
|
||||
file:
|
||||
path: "{{ matrix_bot_matrix_reminder_bot_base_path }}"
|
||||
state: absent
|
||||
|
||||
- name: Ensure matrix-reminder-bot Docker image doesn't exist
|
||||
docker_image:
|
||||
name: "{{ matrix_bot_matrix_reminder_bot_docker_image }}"
|
||||
state: absent
|
@ -0,0 +1,64 @@
|
||||
- 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-irc is stopped
|
||||
service:
|
||||
name: matrix-appservice-irc
|
||||
state: stopped
|
||||
|
||||
- name: Import appservice-irc 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_irc_data_path }},dst=/data
|
||||
--entrypoint=/bin/sh
|
||||
{{ matrix_appservice_irc_docker_image }}
|
||||
-c
|
||||
'/usr/local/bin/node /app/lib/scripts/migrate-db-to-pgres.js --dbdir /data --privateKey /data/passkey.pem --connectionString {{ matrix_appservice_irc_database_connection_string }}'
|
||||
|
||||
- name: Archive NeDB database files
|
||||
command:
|
||||
cmd: "mv {{ matrix_appservice_irc_data_path }}/{{ item }} {{ matrix_appservice_irc_data_path }}/{{ item }}.backup"
|
||||
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."
|
||||
]
|
||||
}}
|
@ -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."
|
||||
]
|
||||
}}
|
@ -1,85 +0,0 @@
|
||||
---
|
||||
|
||||
#
|
||||
# Tasks related to setting up the dimension
|
||||
#
|
||||
|
||||
- name: Ensure Dimension base path exists
|
||||
file:
|
||||
path: "{{ matrix_dimension_base_path }}"
|
||||
state: directory
|
||||
mode: 0770
|
||||
owner: "{{ matrix_user_username }}"
|
||||
group: "{{ matrix_dimension_user_gid }}"
|
||||
when: matrix_dimension_enabled|bool
|
||||
|
||||
- name: Ensure Dimension config installed
|
||||
copy:
|
||||
content: "{{ matrix_dimension_configuration|to_nice_yaml }}"
|
||||
dest: "{{ matrix_dimension_base_path }}/config.yaml"
|
||||
mode: 0640
|
||||
owner: "{{ matrix_user_username }}"
|
||||
group: "{{ matrix_dimension_user_gid }}"
|
||||
when: matrix_dimension_enabled|bool
|
||||
|
||||
- name: Ensure Dimension image is pulled
|
||||
docker_image:
|
||||
name: "{{ matrix_dimension_docker_image }}"
|
||||
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
|
||||
force_source: "{{ matrix_dimension_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_dimension_docker_image_force_pull }}"
|
||||
when: matrix_dimension_enabled|bool
|
||||
|
||||
- name: Ensure matrix-dimension.service installed
|
||||
template:
|
||||
src: "{{ role_path }}/templates/systemd/matrix-dimension.service.j2"
|
||||
dest: "{{ matrix_systemd_path }}/matrix-dimension.service"
|
||||
mode: 0644
|
||||
register: matrix_dimension_systemd_service_result
|
||||
when: matrix_dimension_enabled|bool
|
||||
|
||||
- name: Ensure systemd reloaded after matrix-dimension.service installation
|
||||
service:
|
||||
daemon_reload: yes
|
||||
when: "matrix_dimension_enabled|bool and matrix_dimension_systemd_service_result.changed"
|
||||
|
||||
#
|
||||
# Tasks related to getting rid of the dimension (if it was previously enabled)
|
||||
#
|
||||
|
||||
- name: Check existence of matrix-dimension service
|
||||
stat:
|
||||
path: "{{ matrix_systemd_path }}/matrix-dimension.service"
|
||||
register: matrix_dimension_service_stat
|
||||
when: "not matrix_dimension_enabled|bool"
|
||||
|
||||
- name: Ensure matrix-dimension is stopped
|
||||
service:
|
||||
name: matrix-dimension
|
||||
state: stopped
|
||||
daemon_reload: yes
|
||||
register: stopping_result
|
||||
when: "not matrix_dimension_enabled|bool and matrix_dimension_service_stat.stat.exists"
|
||||
|
||||
- name: Ensure matrix-dimension.service doesn't exist
|
||||
file:
|
||||
path: "{{ matrix_systemd_path }}/matrix-dimension.service"
|
||||
state: absent
|
||||
when: "not matrix_dimension_enabled|bool and matrix_dimension_service_stat.stat.exists"
|
||||
|
||||
- name: Ensure systemd reloaded after matrix-dimension.service removal
|
||||
service:
|
||||
daemon_reload: yes
|
||||
when: "not matrix_dimension_enabled|bool and matrix_dimension_service_stat.stat.exists"
|
||||
|
||||
- name: Ensure Dimension environment variables path doesn't exist
|
||||
file:
|
||||
path: "{{ matrix_dimension_base_path }}"
|
||||
state: absent
|
||||
when: "not matrix_dimension_enabled|bool"
|
||||
|
||||
- name: Ensure Dimension Docker image doesn't exist
|
||||
docker_image:
|
||||
name: "{{ matrix_dimension_docker_image }}"
|
||||
state: absent
|
||||
when: "not matrix_dimension_enabled|bool"
|
@ -0,0 +1,69 @@
|
||||
---
|
||||
|
||||
- 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 }}"
|
||||
state: directory
|
||||
mode: 0770
|
||||
owner: "{{ matrix_user_username }}"
|
||||
group: "{{ matrix_dimension_user_gid }}"
|
||||
|
||||
- name: Ensure Dimension config installed
|
||||
copy:
|
||||
content: "{{ matrix_dimension_configuration|to_nice_yaml }}"
|
||||
dest: "{{ matrix_dimension_base_path }}/config.yaml"
|
||||
mode: 0640
|
||||
owner: "{{ matrix_user_username }}"
|
||||
group: "{{ matrix_dimension_user_gid }}"
|
||||
|
||||
- name: Ensure Dimension image is pulled
|
||||
docker_image:
|
||||
name: "{{ matrix_dimension_docker_image }}"
|
||||
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
|
||||
force_source: "{{ matrix_dimension_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_dimension_docker_image_force_pull }}"
|
||||
|
||||
- name: Ensure matrix-dimension.service installed
|
||||
template:
|
||||
src: "{{ role_path }}/templates/systemd/matrix-dimension.service.j2"
|
||||
dest: "{{ matrix_systemd_path }}/matrix-dimension.service"
|
||||
mode: 0644
|
||||
register: matrix_dimension_systemd_service_result
|
||||
|
||||
- name: Ensure systemd reloaded after matrix-dimension.service installation
|
||||
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"
|
@ -0,0 +1,35 @@
|
||||
---
|
||||
|
||||
- name: Check existence of matrix-dimension service
|
||||
stat:
|
||||
path: "{{ matrix_systemd_path }}/matrix-dimension.service"
|
||||
register: matrix_dimension_service_stat
|
||||
|
||||
- name: Ensure matrix-dimension is stopped
|
||||
service:
|
||||
name: matrix-dimension
|
||||
state: stopped
|
||||
daemon_reload: yes
|
||||
register: stopping_result
|
||||
when: "matrix_dimension_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure matrix-dimension.service doesn't exist
|
||||
file:
|
||||
path: "{{ matrix_systemd_path }}/matrix-dimension.service"
|
||||
state: absent
|
||||
when: "matrix_dimension_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure systemd reloaded after matrix-dimension.service removal
|
||||
service:
|
||||
daemon_reload: yes
|
||||
when: "matrix_dimension_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure Dimension base directory doesn't exist
|
||||
file:
|
||||
path: "{{ matrix_dimension_base_path }}"
|
||||
state: absent
|
||||
|
||||
- name: Ensure Dimension Docker image doesn't exist
|
||||
docker_image:
|
||||
name: "{{ matrix_dimension_docker_image }}"
|
||||
state: absent
|
@ -0,0 +1,35 @@
|
||||
---
|
||||
|
||||
- name: Check existence of matrix-ma1sd service
|
||||
stat:
|
||||
path: "{{ matrix_systemd_path }}/matrix-ma1sd.service"
|
||||
register: matrix_ma1sd_service_stat
|
||||
|
||||
- name: Ensure matrix-ma1sd is stopped
|
||||
service:
|
||||
name: matrix-ma1sd
|
||||
state: stopped
|
||||
daemon_reload: yes
|
||||
register: stopping_result
|
||||
when: "matrix_ma1sd_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure matrix-ma1sd.service doesn't exist
|
||||
file:
|
||||
path: "{{ matrix_systemd_path }}/matrix-ma1sd.service"
|
||||
state: absent
|
||||
when: "matrix_ma1sd_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure systemd reloaded after matrix-ma1sd.service removal
|
||||
service:
|
||||
daemon_reload: yes
|
||||
when: "matrix_ma1sd_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure Matrix ma1sd paths don't exist
|
||||
file:
|
||||
path: "{{ matrix_ma1sd_base_path }}"
|
||||
state: absent
|
||||
|
||||
- name: Ensure ma1sd Docker image doesn't exist
|
||||
docker_image:
|
||||
name: "{{ matrix_ma1sd_docker_image }}"
|
||||
state: absent
|
@ -0,0 +1,97 @@
|
||||
---
|
||||
|
||||
# Pre-checks
|
||||
|
||||
- name: Fail if Postgres not enabled
|
||||
fail:
|
||||
msg: "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot import."
|
||||
when: "not matrix_postgres_enabled|bool"
|
||||
|
||||
- name: Fail if playbook called incorrectly
|
||||
fail:
|
||||
msg: "The `sqlite_database_path` variable needs to be provided to this playbook, via --extra-vars"
|
||||
when: "sqlite_database_path is not defined or sqlite_database_path.startswith('<')"
|
||||
|
||||
- name: Check if the provided SQLite database file exists
|
||||
stat:
|
||||
path: "{{ sqlite_database_path }}"
|
||||
register: sqlite_database_path_stat_result
|
||||
|
||||
- name: Fail if provided SQLite database file doesn't exist
|
||||
fail:
|
||||
msg: "File cannot be found on the server at {{ sqlite_database_path }}"
|
||||
when: "not sqlite_database_path_stat_result.stat.exists"
|
||||
|
||||
# We either expect `postgres_db_connection_string` specifying a full Postgres database connection string,
|
||||
# or `postgres_connection_string_variable_name`, specifying a name of a variable, which contains a valid connection string.
|
||||
|
||||
- block:
|
||||
- name: Fail if postgres_connection_string_variable_name points to an undefined variable
|
||||
fail: msg="postgres_connection_string_variable_name is defined, but there is no variable with the name `{{ postgres_connection_string_variable_name }}`"
|
||||
when: "postgres_connection_string_variable_name not in vars"
|
||||
|
||||
- name: Get Postgres connection string from variable
|
||||
set_fact:
|
||||
postgres_db_connection_string: "{{ lookup('vars', postgres_connection_string_variable_name) }}"
|
||||
when: 'postgres_connection_string_variable_name is defined'
|
||||
|
||||
- name: Fail if playbook called incorrectly
|
||||
fail:
|
||||
msg: >-
|
||||
Either a `postgres_db_connection_string` variable or a `postgres_connection_string_variable_name` needs to be provided to this playbook, via `--extra-vars`.
|
||||
Example: `--extra-vars="postgres_db_connection_string=postgresql://username:password@localhost:<port>/database_name"` or `--extra-vars="postgres_connection_string_variable_name=matrix_appservice_discord_database_connString"`
|
||||
when: "postgres_db_connection_string is not defined or not postgres_db_connection_string.startswith('postgresql://')"
|
||||
|
||||
|
||||
# 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: Import SQLite database from {{ sqlite_database_path }} 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={{ sqlite_database_path }},dst=/in.db,ro
|
||||
--entrypoint=/bin/sh
|
||||
{{ matrix_postgres_pgloader_docker_image }}
|
||||
-c
|
||||
'pgloader /in.db {{ postgres_db_connection_string }}'
|
||||
|
||||
- name: Archive SQLite database ({{ sqlite_database_path }} -> {{ sqlite_database_path }}.backup)
|
||||
command:
|
||||
cmd: "mv {{ sqlite_database_path }} {{ sqlite_database_path }}.backup"
|
||||
|
||||
- name: Inject result
|
||||
set_fact:
|
||||
matrix_playbook_runtime_results: |
|
||||
{{
|
||||
matrix_playbook_runtime_results|default([])
|
||||
+
|
||||
[
|
||||
"NOTE: Your SQLite database file has been imported into Postgres. The original file has been moved from `{{ sqlite_database_path }}` to `{{ sqlite_database_path }}.backup`. When you've confirmed that the import went well and everything works, you should be able to safely delete this file."
|
||||
]
|
||||
}}
|
@ -0,0 +1,40 @@
|
||||
---
|
||||
|
||||
# It'd be better if this is belonged to `validate_config.yml`, but it would have to be some loop-within-a-loop there,
|
||||
# and that's ugly. We also don't expect this to catch errors often. It's more of a defensive last-minute check.
|
||||
- name: Fail if additional database data appears invalid
|
||||
fail:
|
||||
msg: "Additional database definition ({{ additional_db }} lacks a required key: {{ item }}"
|
||||
when: "item not in additional_db"
|
||||
with_items: "{{ ['name', 'username', 'password'] }}"
|
||||
|
||||
# The SQL statements that we'll run against Postgres are stored in a file that others can't read.
|
||||
# This file will be mounted into the container and fed to Postgres.
|
||||
# This way, we avoid passing sensitive data around in CLI commands that other users on the system can see.
|
||||
- name: Create additional database initialization SQL file for {{ additional_db.name }}
|
||||
template:
|
||||
src: "{{ role_path }}/templates/init-additional-db-user-and-role.sql.j2"
|
||||
dest: "/tmp/matrix-postgres-init-additional-db-user-and-role.sql"
|
||||
mode: 0600
|
||||
owner: "{{ matrix_user_uid }}"
|
||||
group: "{{ matrix_user_gid }}"
|
||||
|
||||
- name: Execute Postgres additional database initialization SQL file for {{ additional_db.name }}
|
||||
command:
|
||||
cmd: >-
|
||||
{{ matrix_host_command_docker }} run
|
||||
--rm
|
||||
--user={{ matrix_user_uid }}:{{ matrix_user_gid }}
|
||||
--cap-drop=ALL
|
||||
--env-file={{ matrix_postgres_base_path }}/env-postgres-psql
|
||||
--network {{ matrix_docker_network }}
|
||||
--mount type=bind,src=/tmp/matrix-postgres-init-additional-db-user-and-role.sql,dst=/matrix-postgres-init-additional-db-user-and-role.sql,ro
|
||||
--entrypoint=/bin/sh
|
||||
{{ matrix_postgres_docker_image_to_use }}
|
||||
-c
|
||||
'psql -h {{ matrix_postgres_connection_hostname }} --file=/matrix-postgres-init-additional-db-user-and-role.sql'
|
||||
|
||||
- name: Delete additional database initialization SQL file for {{ additional_db.name }}
|
||||
file:
|
||||
path: /tmp/matrix-postgres-init-additional-db-user-and-role.sql
|
||||
state: absent
|
@ -0,0 +1,23 @@
|
||||
---
|
||||
|
||||
- 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: "{{ matrix_postgres_additional_databases_postgres_start_wait_timeout_seconds }}"
|
||||
delegate_to: 127.0.0.1
|
||||
become: false
|
||||
when: "matrix_postgres_service_start_result.changed|bool"
|
||||
|
||||
- name: Create additional Postgres user and database
|
||||
include_tasks: "{{ role_path }}/tasks/util/create_additional_database.yml"
|
||||
with_items: "{{ matrix_postgres_additional_databases }}"
|
||||
loop_control:
|
||||
loop_var: additional_db
|
||||
# Suppress logging to avoid dumping the credentials to the shell
|
||||
no_log: true
|
@ -0,0 +1,167 @@
|
||||
---
|
||||
|
||||
- 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"
|
||||
|
||||
- name: Fail if util called incorrectly (missing matrix_postgres_db_migration_request)
|
||||
fail:
|
||||
msg: "The `matrix_postgres_db_migration_request` variable needs to be provided to this util."
|
||||
when: "matrix_postgres_db_migration_request is not defined"
|
||||
|
||||
- name: Fail if util called incorrectly (invalid matrix_postgres_db_migration_request)
|
||||
fail:
|
||||
msg: "The `matrix_postgres_db_migration_request` variable needs to contain `{{ item }}`."
|
||||
with_items:
|
||||
- src
|
||||
- dst
|
||||
- caller
|
||||
- engine_variable_name
|
||||
- systemd_services_to_stop
|
||||
when: "item not in matrix_postgres_db_migration_request"
|
||||
|
||||
- name: Check if the provided source database file exists
|
||||
stat:
|
||||
path: "{{ matrix_postgres_db_migration_request.src }}"
|
||||
register: matrix_postgres_db_migration_request_src_stat_result
|
||||
|
||||
- name: Fail if provided source database file doesn't exist
|
||||
fail:
|
||||
msg: "File cannot be found on the server at {{ matrix_postgres_db_migration_request.src }}"
|
||||
when: "not matrix_postgres_db_migration_request_src_stat_result.stat.exists"
|
||||
|
||||
- block:
|
||||
- name: Ensure pgloader repository is present on self-build
|
||||
git:
|
||||
repo: "{{ matrix_postgres_pgloader_container_image_self_build_repo }}"
|
||||
dest: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}"
|
||||
version: "{{ matrix_postgres_pgloader_container_image_self_build_repo_branch }}"
|
||||
force: "yes"
|
||||
register: matrix_postgres_pgloader_git_pull_results
|
||||
|
||||
# If `stable` is used, we hit an error when processing /opt/src/pgloader/build/quicklisp/dists/quicklisp/software/uax-15-20201220-git/data/CompositionExclusions.txt:
|
||||
# > the octet sequence #(194) cannot be decoded
|
||||
#
|
||||
# The issue is described here and is not getting fixed for months: https://github.com/dimitri/pgloader/pull/1179
|
||||
#
|
||||
# Although we're not using the dimitri/pgloader image, the one we're using suffers from the same problem.
|
||||
- name: Switch pgloader base image from Debian stable (likely 10.x/Buster) to Bullseye
|
||||
lineinfile:
|
||||
path: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}/Dockerfile"
|
||||
regexp: "{{ item.match }}"
|
||||
line: "{{ item.replace }}"
|
||||
with_items:
|
||||
- match: '^FROM debian:stable-slim as builder$'
|
||||
replace: 'FROM debian:bullseye-slim as builder'
|
||||
- match: '^FROM debian:stable-slim$'
|
||||
replace: 'FROM debian:bullseye-slim'
|
||||
|
||||
- name: Ensure pgloader Docker image is built
|
||||
docker_image:
|
||||
name: "{{ matrix_postgres_pgloader_docker_image }}"
|
||||
source: build
|
||||
force_source: "{{ matrix_postgres_pgloader_git_pull_results.changed }}"
|
||||
build:
|
||||
dockerfile: Dockerfile
|
||||
path: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}"
|
||||
pull: yes
|
||||
when: "matrix_postgres_pgloader_container_image_self_build|bool"
|
||||
|
||||
- name: Ensure pgloader Docker image is pulled
|
||||
docker_image:
|
||||
name: "{{ matrix_postgres_pgloader_docker_image }}"
|
||||
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
|
||||
force_source: "{{ matrix_postgres_pgloader_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_postgres_pgloader_docker_image_force_pull }}"
|
||||
when: "not matrix_postgres_pgloader_container_image_self_build"
|
||||
|
||||
# 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
|
||||
|
||||
# matrix-postgres is most likely started already
|
||||
- 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"
|
||||
|
||||
# We only stop services here, leaving it to the caller to start them later.
|
||||
#
|
||||
# We can't start them, because they probably need to be reconfigured too (changing the configuration from using SQLite to Postgres, etc.),
|
||||
# before starting.
|
||||
#
|
||||
# Since the caller will be starting them, it might make sense to leave stopping to it as well.
|
||||
# However, we don't do it, because it's simpler having it here, and it also gets to happen only if we'll be doing an import.
|
||||
# If we bailed out (somewhere above), nothing would have gotten stopped. It's nice to leave this running in such cases.
|
||||
- name: Ensure systemd services blocking the database import are stopped
|
||||
service:
|
||||
name: "{{ item }}"
|
||||
state: stopped
|
||||
with_items: "{{ matrix_postgres_db_migration_request.systemd_services_to_stop }}"
|
||||
|
||||
- name: Import {{ matrix_postgres_db_migration_request.engine_old }} database from {{ matrix_postgres_db_migration_request.src }} 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_postgres_db_migration_request.src }},dst=/in.db,ro
|
||||
--entrypoint=/bin/sh
|
||||
{{ matrix_postgres_pgloader_docker_image }}
|
||||
-c
|
||||
'pgloader {{ matrix_postgres_db_migration_request.pgloader_options|default([])|join(' ') }} /in.db {{ matrix_postgres_db_migration_request.dst }}'
|
||||
|
||||
- name: Archive {{ matrix_postgres_db_migration_request.engine_old }} database ({{ matrix_postgres_db_migration_request.src }} -> {{ matrix_postgres_db_migration_request.src }}.backup)
|
||||
command:
|
||||
cmd: "mv {{ matrix_postgres_db_migration_request.src }} {{ matrix_postgres_db_migration_request.src }}.backup"
|
||||
|
||||
- block:
|
||||
# We can't use `{{ role_path }}` here, neither with `import_tasks`, nor with `include_tasks`,
|
||||
# because it refers to the role that included this util, and not to the role this file belongs to.
|
||||
- import_tasks: "roles/matrix-postgres/tasks/util/detect_existing_postgres_version.yml"
|
||||
|
||||
- set_fact:
|
||||
matrix_postgres_docker_image_to_use: "{{ matrix_postgres_docker_image_latest if matrix_postgres_detected_version_corresponding_docker_image == '' else matrix_postgres_detected_version_corresponding_docker_image }}"
|
||||
|
||||
- name: Execute additional Postgres SQL migration statements
|
||||
command:
|
||||
cmd: >-
|
||||
{{ matrix_host_command_docker }} run
|
||||
--rm
|
||||
--user={{ matrix_user_uid }}:{{ matrix_user_gid }}
|
||||
--cap-drop=ALL
|
||||
--env-file={{ matrix_postgres_base_path }}/env-postgres-psql
|
||||
--network={{ matrix_docker_network }}
|
||||
{{ matrix_postgres_docker_image_to_use }}
|
||||
psql --host=matrix-postgres --dbname={{ matrix_postgres_db_migration_request.additional_psql_statements_db_name }} --command='{{ item }}'
|
||||
with_items: "{{ matrix_postgres_db_migration_request.additional_psql_statements_list }}"
|
||||
|
||||
when: "matrix_postgres_db_migration_request.additional_psql_statements_list|default([])|length > 0"
|
||||
|
||||
- name: Inject result
|
||||
set_fact:
|
||||
matrix_playbook_runtime_results: |
|
||||
{{
|
||||
matrix_playbook_runtime_results|default([])
|
||||
+
|
||||
[
|
||||
"NOTE: Your {{ matrix_postgres_db_migration_request.engine_old }} database file has been imported into Postgres. The original database file has been moved from `{{ matrix_postgres_db_migration_request.src }}` to `{{ matrix_postgres_db_migration_request.src }}.backup`. When you've confirmed that the import went well and everything works, you should be able to safely delete this file."
|
||||
]
|
||||
}}
|
@ -0,0 +1,19 @@
|
||||
-- `CREATE USER` does not support `IF NOT EXISTS`, so we use this workaround to prevent an error and raise a notice instead.
|
||||
-- Seen here: https://stackoverflow.com/a/49858797
|
||||
DO $$
|
||||
BEGIN
|
||||
CREATE USER {{ additional_db.username }};
|
||||
EXCEPTION WHEN DUPLICATE_OBJECT THEN
|
||||
RAISE NOTICE 'not creating user {{ additional_db.username }}, since it already exists';
|
||||
END
|
||||
$$;
|
||||
|
||||
-- This is useful for initial user creation (since we don't assign a password above) and for handling subsequent password changes
|
||||
-- TODO - we should escape quotes in the password.
|
||||
ALTER ROLE {{ additional_db.username }} PASSWORD '{{ additional_db.password }}';
|
||||
|
||||
-- This will generate an error on subsequent execution
|
||||
CREATE DATABASE {{ additional_db.name }} WITH LC_CTYPE 'C' LC_COLLATE 'C' OWNER {{ additional_db.username }};
|
||||
|
||||
-- This is useful for changing the database owner subsequently
|
||||
ALTER DATABASE {{ additional_db.name }} OWNER TO {{ additional_db.username }};
|
@ -0,0 +1,30 @@
|
||||
---
|
||||
|
||||
- name: Check existence of matrix-registration service
|
||||
stat:
|
||||
path: "{{ matrix_systemd_path }}/matrix-registration.service"
|
||||
register: matrix_registration_service_stat
|
||||
|
||||
- name: Ensure matrix-registration is stopped
|
||||
service:
|
||||
name: matrix-registration
|
||||
state: stopped
|
||||
daemon_reload: yes
|
||||
register: stopping_result
|
||||
when: "matrix_registration_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure matrix-registration.service doesn't exist
|
||||
file:
|
||||
path: "{{ matrix_systemd_path }}/matrix-registration.service"
|
||||
state: absent
|
||||
when: "matrix_registration_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure systemd reloaded after matrix-registration.service removal
|
||||
service:
|
||||
daemon_reload: yes
|
||||
when: "matrix_registration_service_stat.stat.exists|bool"
|
||||
|
||||
- name: Ensure matrix-registration Docker image doesn't exist
|
||||
docker_image:
|
||||
name: "{{ matrix_registration_docker_image }}"
|
||||
state: absent
|
Loading…
Reference in new issue