From 9c2a8addee93910cb9079f856bc3fb3932592c91 Mon Sep 17 00:00:00 2001 From: Aine Date: Mon, 22 Aug 2022 20:10:35 +0300 Subject: [PATCH] add postmoogle --- docs/configuring-playbook-bot-postmoogle.md | 54 ++++++++++ docs/configuring-playbook-email2matrix.md | 2 + group_vars/matrix_servers | 35 +++++++ roles/matrix-bot-postmoogle/defaults/main.yml | 96 ++++++++++++++++++ roles/matrix-bot-postmoogle/tasks/init.yml | 5 + roles/matrix-bot-postmoogle/tasks/main.yml | 23 +++++ .../tasks/setup_install.yml | 99 +++++++++++++++++++ .../tasks/setup_uninstall.yml | 36 +++++++ .../tasks/validate_config.yml | 9 ++ roles/matrix-bot-postmoogle/templates/env.j2 | 13 +++ .../systemd/matrix-bot-postmoogle.service.j2 | 40 ++++++++ setup.yml | 1 + 12 files changed, 413 insertions(+) create mode 100644 docs/configuring-playbook-bot-postmoogle.md create mode 100644 roles/matrix-bot-postmoogle/defaults/main.yml create mode 100644 roles/matrix-bot-postmoogle/tasks/init.yml create mode 100644 roles/matrix-bot-postmoogle/tasks/main.yml create mode 100644 roles/matrix-bot-postmoogle/tasks/setup_install.yml create mode 100644 roles/matrix-bot-postmoogle/tasks/setup_uninstall.yml create mode 100644 roles/matrix-bot-postmoogle/tasks/validate_config.yml create mode 100644 roles/matrix-bot-postmoogle/templates/env.j2 create mode 100644 roles/matrix-bot-postmoogle/templates/systemd/matrix-bot-postmoogle.service.j2 diff --git a/docs/configuring-playbook-bot-postmoogle.md b/docs/configuring-playbook-bot-postmoogle.md new file mode 100644 index 00000000..bbbfa405 --- /dev/null +++ b/docs/configuring-playbook-bot-postmoogle.md @@ -0,0 +1,54 @@ +# Setting up Postmoogle (optional) + +**Note**: email bridging can also happen via the [email2matrix](configuring-playbook-email2matrix.md) bridge supported by the playbook. + +The playbook can install and configure [Postmoogle](https://gitlab.com/etke.cc/postmoogle) for you. + +It's a bot/bridge you can use to forward emails to Matrix rooms + +See the project's [documentation](https://gitlab.com/etke.cc/postmoogle) to learn what it does and why it might be useful to you. + + +## Registering the bot user + +By default, the playbook will set up the bot with a username like this: `@postmoogle:DOMAIN`. + +(to use a different username, adjust the `matrix_bot_postmoogle_login` variable). + +You **need to register the bot user manually** before setting up the bot. You can use the playbook to [register a new user](registering-users.md): + +``` +ansible-playbook -i inventory/hosts setup.yml --extra-vars='username=postmoogle password=PASSWORD_FOR_THE_BOT admin=no' --tags=register-user +``` + +Choose a strong password for the bot. You can generate a good password with a command like this: `pwgen -s 64 1`. + + +## Adjusting the playbook configuration + +Add the following configuration to your `inventory/host_vars/matrix.DOMAIN/vars.yml` file: + +```yaml +matrix_bot_postmoogle_enabled: true + +# Adjust this to whatever password you chose when registering the bot user +matrix_bot_postmoogle_password: PASSWORD_FOR_THE_BOT +``` + + +## Installing + +After configuring the playbook, run the [installation](installing.md) command again: + +``` +ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start +``` + + +## Usage + +To use the bot, invite the `@postmoogle:DOMAIN` into a room you want to use as a mailbox, after that send `mailbox NAME` to enable email address `NAME@matrix.domain` and start receiving emails + +Send `help` to the room to see the bot's help menu for additional commands. + +You can also refer to the upstream [documentation](https://gitlab.com/etke.cc/postmoogle). diff --git a/docs/configuring-playbook-email2matrix.md b/docs/configuring-playbook-email2matrix.md index 510a9dcc..bc3fba8c 100644 --- a/docs/configuring-playbook-email2matrix.md +++ b/docs/configuring-playbook-email2matrix.md @@ -1,5 +1,7 @@ # Setting up Email2Matrix (optional) +**Note**: email bridging can also happen via the [postmoogle](configuring-playbook-bot-postmoogle.md) bot supported by the playbook. + The playbook can install and configure [email2matrix](https://github.com/devture/email2matrix) for you. See the project's [documentation](https://github.com/devture/email2matrix/blob/master/docs/README.md) to learn what it does and why it might be useful to you. diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index 90482810..91bfddc6 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -1198,6 +1198,35 @@ matrix_bot_buscarron_container_image_self_build: "{{ matrix_architecture not in # ###################################################################### +###################################################################### +# +# matrix-bot-postmoogle +# +###################################################################### + +# We don't enable bots by default. +matrix_bot_postmoogle_enabled: false + +matrix_bot_postmoogle_systemd_required_services_list: | + {{ + ['docker.service'] + + + (['matrix-postgres.service'] if matrix_postgres_enabled else []) + + + (['matrix-synapse.service'] if matrix_synapse_enabled else []) + }} + +# Postgres is the default, except if not using `matrix_postgres` (internal postgres) +matrix_bot_postmoogle_database_engine: "{{ 'postgres' if matrix_postgres_enabled else 'sqlite' }}" +matrix_bot_postmoogle_database_password: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'postmoogle.db') | to_uuid }}" +matrix_bot_postmoogle_container_image_self_build: "{{ matrix_architecture not in ['amd64', 'arm32', 'arm64'] }}" + +###################################################################### +# +# /matrix-bot-postmoogle +# +###################################################################### + ###################################################################### # @@ -1850,6 +1879,12 @@ matrix_postgres_additional_databases: | 'password': matrix_bot_honoroit_database_password, }] if (matrix_bot_honoroit_enabled and matrix_bot_honoroit_database_engine == 'postgres' and matrix_bot_honoroit_database_hostname == 'matrix-postgres') else []) + + ([{ + 'name': matrix_bot_postmoogle_database_name, + 'username': matrix_bot_postmoogle_database_username, + 'password': matrix_bot_postmoogle_database_password, + }] if (matrix_bot_postmoogle_enabled and matrix_bot_postmoogle_database_engine == 'postgres' and matrix_bot_postmoogle_database_hostname == 'matrix-postgres') else []) + + ([{ 'name': matrix_bot_maubot_database_name, 'username': matrix_bot_maubot_database_username, diff --git a/roles/matrix-bot-postmoogle/defaults/main.yml b/roles/matrix-bot-postmoogle/defaults/main.yml new file mode 100644 index 00000000..7d5818ce --- /dev/null +++ b/roles/matrix-bot-postmoogle/defaults/main.yml @@ -0,0 +1,96 @@ +--- +# postmoogle is an email to matrix bot +# Project source code URL: https://gitlab.com/etke.cc/postmoogle + +matrix_bot_postmoogle_enabled: true + +matrix_bot_postmoogle_container_image_self_build: false +matrix_bot_postmoogle_docker_repo: "https://gitlab.com/etke.cc/postmoogle.git" +matrix_bot_postmoogle_docker_repo_version: "{{ matrix_bot_postmoogle_version }}" +matrix_bot_postmoogle_docker_src_files_path: "{{ matrix_base_data_path }}/postmoogle/docker-src" + +matrix_bot_postmoogle_version: latest +matrix_bot_postmoogle_docker_image: "{{ matrix_bot_postmoogle_docker_image_name_prefix }}postmoogle:{{ matrix_bot_postmoogle_version }}" +matrix_bot_postmoogle_docker_image_name_prefix: "{{ 'localhost/' if matrix_bot_postmoogle_container_image_self_build else 'registry.gitlab.com/etke.cc/' }}" +matrix_bot_postmoogle_docker_image_force_pull: "{{ matrix_bot_postmoogle_docker_image.endswith(':latest') }}" + +matrix_bot_postmoogle_base_path: "{{ matrix_base_data_path }}/postmoogle" +matrix_bot_postmoogle_config_path: "{{ matrix_bot_postmoogle_base_path }}/config" +matrix_bot_postmoogle_data_path: "{{ matrix_bot_postmoogle_base_path }}/data" + +# A list of extra arguments to pass to the container +matrix_bot_postmoogle_container_extra_arguments: [] + +# List of systemd services that matrix-bot-postmoogle.service depends on +matrix_bot_postmoogle_systemd_required_services_list: ['docker.service'] + +# List of systemd services that matrix-bot-postmoogle.service wants +matrix_bot_postmoogle_systemd_wanted_services_list: [] + + +# Database-related configuration fields. +# +# To use SQLite, stick to these defaults. +# +# To use Postgres: +# - change the engine (`matrix_bot_postmoogle_database_engine: 'postgres'`) +# - adjust your database credentials via the `matrix_bot_postmoogle_database_*` variables +matrix_bot_postmoogle_database_engine: 'sqlite' + +matrix_bot_postmoogle_sqlite_database_path_local: "{{ matrix_bot_postmoogle_data_path }}/bot.db" +matrix_bot_postmoogle_sqlite_database_path_in_container: "/data/bot.db" + +matrix_bot_postmoogle_database_username: 'postmoogle' +matrix_bot_postmoogle_database_password: 'some-password' +matrix_bot_postmoogle_database_hostname: 'matrix-postgres' +matrix_bot_postmoogle_database_port: 5432 +matrix_bot_postmoogle_database_name: 'postmoogle' + +matrix_bot_postmoogle_database_connection_string: 'postgres://{{ matrix_bot_postmoogle_database_username }}:{{ matrix_bot_postmoogle_database_password }}@{{ matrix_bot_postmoogle_database_hostname }}:{{ matrix_bot_postmoogle_database_port }}/{{ matrix_bot_postmoogle_database_name }}?sslmode=disable' + +matrix_bot_postmoogle_storage_database: "{{ + { + 'sqlite': matrix_bot_postmoogle_sqlite_database_path_in_container, + 'postgres': matrix_bot_postmoogle_database_connection_string, + }[matrix_bot_postmoogle_database_engine] +}}" + +matrix_bot_postmoogle_database_dialect: "{{ + { + 'sqlite': 'sqlite3', + 'postgres': 'postgres', + }[matrix_bot_postmoogle_database_engine] +}}" + + +# The bot's username. This user needs to be created manually beforehand. +# Also see `matrix_bot_postmoogle_password`. +matrix_bot_postmoogle_login: "postmoogle" + +# The password that the bot uses to authenticate. +matrix_bot_postmoogle_password: '' + +matrix_bot_postmoogle_homeserver: "{{ matrix_homeserver_container_url }}" + +# Command prefix +matrix_bot_postmoogle_prefix: '' + +# Sentry DSN +matrix_bot_postmoogle_sentry: '' + +# Log level +matrix_bot_postmoogle_loglevel: '' + +# Disable encryption +matrix_bot_postmoogle_noencryption: false + +matrix_bot_postmoogle_domain: "{{ matrix_server_fqn_matrix }}" + +matrix_bot_postmoogle_port: "25" + +# Additional environment variables to pass to the postmoogle container +# +# Example: +# matrix_bot_postmoogle_environment_variables_extension: | +# postmoogle_TEXT_DONE=Done +matrix_bot_postmoogle_environment_variables_extension: '' diff --git a/roles/matrix-bot-postmoogle/tasks/init.yml b/roles/matrix-bot-postmoogle/tasks/init.yml new file mode 100644 index 00000000..16b78171 --- /dev/null +++ b/roles/matrix-bot-postmoogle/tasks/init.yml @@ -0,0 +1,5 @@ +--- + +- ansible.builtin.set_fact: + matrix_systemd_services_list: "{{ matrix_systemd_services_list + ['matrix-bot-postmoogle.service'] }}" + when: matrix_bot_postmoogle_enabled | bool diff --git a/roles/matrix-bot-postmoogle/tasks/main.yml b/roles/matrix-bot-postmoogle/tasks/main.yml new file mode 100644 index 00000000..cbe590e1 --- /dev/null +++ b/roles/matrix-bot-postmoogle/tasks/main.yml @@ -0,0 +1,23 @@ +--- + +- ansible.builtin.import_tasks: "{{ role_path }}/tasks/init.yml" + tags: + - always + +- ansible.builtin.import_tasks: "{{ role_path }}/tasks/validate_config.yml" + when: "run_setup | bool and matrix_bot_postmoogle_enabled | bool" + tags: + - setup-all + - setup-bot-postmoogle + +- ansible.builtin.import_tasks: "{{ role_path }}/tasks/setup_install.yml" + when: "run_setup | bool and matrix_bot_postmoogle_enabled | bool" + tags: + - setup-all + - setup-bot-postmoogle + +- ansible.builtin.import_tasks: "{{ role_path }}/tasks/setup_uninstall.yml" + when: "run_setup | bool and not matrix_bot_postmoogle_enabled | bool" + tags: + - setup-all + - setup-bot-postmoogle diff --git a/roles/matrix-bot-postmoogle/tasks/setup_install.yml b/roles/matrix-bot-postmoogle/tasks/setup_install.yml new file mode 100644 index 00000000..d0885698 --- /dev/null +++ b/roles/matrix-bot-postmoogle/tasks/setup_install.yml @@ -0,0 +1,99 @@ +--- +- block: + - name: Check if an SQLite database already exists + ansible.builtin.stat: + path: "{{ matrix_bot_postmoogle_sqlite_database_path_local }}" + register: matrix_bot_postmoogle_sqlite_database_path_local_stat_result + + - block: + - ansible.builtin.set_fact: + matrix_postgres_db_migration_request: + src: "{{ matrix_bot_postmoogle_sqlite_database_path_local }}" + dst: "{{ matrix_bot_postmoogle_database_connection_string }}" + caller: "{{ role_path | basename }}" + engine_variable_name: 'matrix_bot_postmoogle_database_engine' + engine_old: 'sqlite' + systemd_services_to_stop: ['matrix-bot-postmoogle.service'] + + - ansible.builtin.import_role: + name: matrix-postgres + tasks_from: migrate_db_to_postgres + + - ansible.builtin.set_fact: + matrix_bot_postmoogle_requires_restart: true + when: "matrix_bot_postmoogle_sqlite_database_path_local_stat_result.stat.exists | bool" + when: "matrix_bot_postmoogle_database_engine == 'postgres'" + +- name: Ensure postmoogle paths exist + ansible.builtin.file: + path: "{{ item.path }}" + state: directory + mode: 0750 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + with_items: + - {path: "{{ matrix_bot_postmoogle_config_path }}", when: true} + - {path: "{{ matrix_bot_postmoogle_data_path }}", when: true} + - {path: "{{ matrix_bot_postmoogle_docker_src_files_path }}", when: true} + when: "item.when | bool" + +- name: Ensure postmoogle environment variables file created + ansible.builtin.template: + src: "{{ role_path }}/templates/env.j2" + dest: "{{ matrix_bot_postmoogle_config_path }}/env" + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + mode: 0640 + +- name: Ensure postmoogle image is pulled + docker_image: + name: "{{ matrix_bot_postmoogle_docker_image }}" + source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}" + force_source: "{{ matrix_bot_postmoogle_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_postmoogle_docker_image_force_pull }}" + when: "not matrix_bot_postmoogle_container_image_self_build | bool" + register: result + retries: "{{ matrix_container_retries_count }}" + delay: "{{ matrix_container_retries_delay }}" + until: result is not failed + +- name: Ensure postmoogle repository is present on self-build + ansible.builtin.git: + repo: "{{ matrix_bot_postmoogle_docker_repo }}" + version: "{{ matrix_bot_postmoogle_docker_repo_version }}" + dest: "{{ matrix_bot_postmoogle_docker_src_files_path }}" + force: "yes" + become: true + become_user: "{{ matrix_user_username }}" + register: matrix_bot_postmoogle_git_pull_results + when: "matrix_bot_postmoogle_container_image_self_build | bool" + +- name: Ensure postmoogle image is built + docker_image: + name: "{{ matrix_bot_postmoogle_docker_image }}" + source: build + force_source: "{{ matrix_bot_postmoogle_git_pull_results.changed 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_mailer_git_pull_results.changed }}" + build: + dockerfile: Dockerfile + path: "{{ matrix_bot_postmoogle_docker_src_files_path }}" + pull: true + when: "matrix_bot_postmoogle_container_image_self_build | bool" + +- name: Ensure matrix-bot-postmoogle.service installed + ansible.builtin.template: + src: "{{ role_path }}/templates/systemd/matrix-bot-postmoogle.service.j2" + dest: "{{ matrix_systemd_path }}/matrix-bot-postmoogle.service" + mode: 0644 + register: matrix_bot_postmoogle_systemd_service_result + +- name: Ensure systemd reloaded after matrix-bot-postmoogle.service installation + ansible.builtin.service: + daemon_reload: true + when: "matrix_bot_postmoogle_systemd_service_result.changed | bool" + +- name: Ensure matrix-bot-postmoogle.service restarted, if necessary + ansible.builtin.service: + name: "matrix-bot-postmoogle.service" + state: restarted + when: "matrix_bot_postmoogle_systemd_service_result.changed | bool" diff --git a/roles/matrix-bot-postmoogle/tasks/setup_uninstall.yml b/roles/matrix-bot-postmoogle/tasks/setup_uninstall.yml new file mode 100644 index 00000000..64164a86 --- /dev/null +++ b/roles/matrix-bot-postmoogle/tasks/setup_uninstall.yml @@ -0,0 +1,36 @@ +--- + +- name: Check existence of matrix-postmoogle service + ansible.builtin.stat: + path: "{{ matrix_systemd_path }}/matrix-bot-postmoogle.service" + register: matrix_bot_postmoogle_service_stat + +- name: Ensure matrix-postmoogle is stopped + ansible.builtin.service: + name: matrix-bot-postmoogle + state: stopped + enabled: false + daemon_reload: true + register: stopping_result + when: "matrix_bot_postmoogle_service_stat.stat.exists | bool" + +- name: Ensure matrix-bot-postmoogle.service doesn't exist + ansible.builtin.file: + path: "{{ matrix_systemd_path }}/matrix-bot-postmoogle.service" + state: absent + when: "matrix_bot_postmoogle_service_stat.stat.exists | bool" + +- name: Ensure systemd reloaded after matrix-bot-postmoogle.service removal + ansible.builtin.service: + daemon_reload: true + when: "matrix_bot_postmoogle_service_stat.stat.exists | bool" + +- name: Ensure Matrix postmoogle paths don't exist + ansible.builtin.file: + path: "{{ matrix_bot_postmoogle_base_path }}" + state: absent + +- name: Ensure postmoogle Docker image doesn't exist + docker_image: + name: "{{ matrix_bot_postmoogle_docker_image }}" + state: absent diff --git a/roles/matrix-bot-postmoogle/tasks/validate_config.yml b/roles/matrix-bot-postmoogle/tasks/validate_config.yml new file mode 100644 index 00000000..b5d9d1ed --- /dev/null +++ b/roles/matrix-bot-postmoogle/tasks/validate_config.yml @@ -0,0 +1,9 @@ +--- + +- name: Fail if required settings not defined + ansible.builtin.fail: + msg: >- + You need to define a required configuration setting (`{{ item }}`). + when: "vars[item] == ''" + with_items: + - "matrix_bot_postmoogle_password" diff --git a/roles/matrix-bot-postmoogle/templates/env.j2 b/roles/matrix-bot-postmoogle/templates/env.j2 new file mode 100644 index 00000000..f9f49e79 --- /dev/null +++ b/roles/matrix-bot-postmoogle/templates/env.j2 @@ -0,0 +1,13 @@ +POSTMOOGLE_LOGIN={{ matrix_bot_postmoogle_login }} +POSTMOOGLE_PASSWORD={{ matrix_bot_postmoogle_password }} +POSTMOOGLE_HOMESERVER={{ matrix_bot_postmoogle_homeserver }} +POSTMOOGLE_DOMAIN={{ matrix_bot_postmoogle_domain }} +POSTMOOGLE_PORT={{ matrix_bot_postmoogle_port }} +POSTMOOGLE_DB_DSN={{ matrix_bot_postmoogle_database_connection_string }} +POSTMOOGLE_DB_DIALECT={{ matrix_bot_postmoogle_database_dialect }} +POSTMOOGLE_PREFIX={{ matrix_bot_postmoogle_prefix }} +POSTMOOGLE_SENTRY={{ matrix_bot_postmoogle_sentry }} +POSTMOOGLE_LOGLEVEL={{ matrix_bot_postmoogle_loglevel }} +POSTMOOGLE_NOENCRYPTION={{ matrix_bot_postmoogle_noencryption }} + +{{ matrix_bot_postmoogle_environment_variables_extension }} diff --git a/roles/matrix-bot-postmoogle/templates/systemd/matrix-bot-postmoogle.service.j2 b/roles/matrix-bot-postmoogle/templates/systemd/matrix-bot-postmoogle.service.j2 new file mode 100644 index 00000000..d5368603 --- /dev/null +++ b/roles/matrix-bot-postmoogle/templates/systemd/matrix-bot-postmoogle.service.j2 @@ -0,0 +1,40 @@ +#jinja2: lstrip_blocks: "True" +[Unit] +Description=Matrix helpdesk bot +{% for service in matrix_bot_postmoogle_systemd_required_services_list %} +Requires={{ service }} +After={{ service }} +{% endfor %} +{% for service in matrix_bot_postmoogle_systemd_wanted_services_list %} +Wants={{ service }} +{% endfor %} +DefaultDependencies=no + +[Service] +Type=simple +Environment="HOME={{ matrix_systemd_unit_home_path }}" +ExecStartPre=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} kill matrix-bot-postmoogle 2>/dev/null || true' +ExecStartPre=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} rm matrix-bot-postmoogle 2>/dev/null || true' + +ExecStart={{ matrix_host_command_docker }} run --rm --name matrix-bot-postmoogle \ + --log-driver=none \ + --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ + --cap-drop=ALL \ + --read-only \ + --network={{ matrix_docker_network }} \ + --env-file={{ matrix_bot_postmoogle_config_path }}/env \ + -p {{ matrix_bot_postmoogle_port }}:{{ matrix_bot_postmoogle_port }} \ + --mount type=bind,src={{ matrix_bot_postmoogle_data_path }},dst=/data \ + {% for arg in matrix_bot_postmoogle_container_extra_arguments %} + {{ arg }} \ + {% endfor %} + {{ matrix_bot_postmoogle_docker_image }} + +ExecStop=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} kill matrix-bot-postmoogle 2>/dev/null || true' +ExecStop=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} rm matrix-bot-postmoogle 2>/dev/null || true' +Restart=always +RestartSec=30 +SyslogIdentifier=matrix-bot-postmoogle + +[Install] +WantedBy=multi-user.target diff --git a/setup.yml b/setup.yml index 18fbdf10..a4ef8c35 100755 --- a/setup.yml +++ b/setup.yml @@ -43,6 +43,7 @@ - matrix-bot-maubot - matrix-bot-buscarron - matrix-bot-honoroit + - matrix-bot-postmoogle - matrix-bot-go-neb - matrix-bot-mjolnir - matrix-synapse