Bläddra i källkod

Add matrix-registration-bot

This adds an install and uninstall task plus helpers. The bot is disabled by default.
This commit does not include documentation, yet. In short, the bot can be enabled by adding
matrix_bot_matrix_registration_bot_enabled: true
matrix_bot_matrix_registration_bot_matrix_user_password: "verysecret"
matrix_bot_matrix_registration_bot_matrix_admin_token: "supersecret"
to the host_vars
pull/1771/head
Julian-Samuel Gebühr 3 år sedan
förälder
incheckning
8e2b3d9027
9 ändrade filer med 253 tillägg och 0 borttagningar
  1. +9
    -0
      group_vars/matrix_servers
  2. +62
    -0
      roles/matrix-bot-matrix-registration-bot/defaults/main.yml
  3. +5
    -0
      roles/matrix-bot-matrix-registration-bot/tasks/init.yml
  4. +23
    -0
      roles/matrix-bot-matrix-registration-bot/tasks/main.yml
  5. +69
    -0
      roles/matrix-bot-matrix-registration-bot/tasks/setup_install.yml
  6. +36
    -0
      roles/matrix-bot-matrix-registration-bot/tasks/setup_uninstall.yml
  7. +10
    -0
      roles/matrix-bot-matrix-registration-bot/tasks/validate_config.yml
  8. +38
    -0
      roles/matrix-bot-matrix-registration-bot/templates/systemd/matrix-bot-matrix-registration-bot.service.j2
  9. +1
    -0
      setup.yml

+ 9
- 0
group_vars/matrix_servers Visa fil

@@ -987,6 +987,15 @@ matrix_bot_matrix_reminder_bot_container_image_self_build: "{{ matrix_architectu
#
######################################################################


######################################################################
#
# matrix-bot-matrix-registration-bot
#
######################################################################
# We don't enable bots by default.
matrix_bot_matrix_registration_bot_enabled: false

######################################################################
#
# matrix-bot-honoroit


+ 62
- 0
roles/matrix-bot-matrix-registration-bot/defaults/main.yml Visa fil

@@ -0,0 +1,62 @@
---
# matrix-registration-bot creates and manages registration tokens for a matrix server
# See: https://github.com/moan0s/matrix-registration-bot

matrix_bot_matrix_registration_bot_enabled: true
matrix_bot_matrix_registration_bot_container_image_self_build: false
matrix_bot_matrix_registration_bot_docker_repo: "https://github.com/moan0s/matrix-registration-bot.git"
matrix_bot_matrix_registration_bot_docker_src_files_path: "{{ matrix_base_data_path }}/matrix-registration-bot/"

matrix_bot_matrix_registration_bot_version: latest
matrix_bot_matrix_registration_bot_docker_image: "{{ matrix_container_global_registry_prefix }}moanos/matrix-registration-bot:{{ matrix_bot_matrix_registration_bot_version }}"
matrix_bot_matrix_registration_bot_docker_image_force_pull: "{{ matrix_bot_matrix_registration_bot_docker_image.endswith(':latest') }}"

matrix_bot_matrix_registration_bot_base_path: "{{ matrix_base_data_path }}/matrix-registration-bot"
matrix_bot_matrix_registration_bot_config_path: "{{ matrix_bot_matrix_registration_bot_base_path }}/config"

# A list of extra arguments to pass to the container
matrix_bot_matrix_registration_bot_container_extra_arguments: []

# List of systemd services that matrix-bot-matrix-registration-bot.service depends on
matrix_bot_matrix_registration_bot_systemd_required_services_list: ['docker.service']

# List of systemd services that matrix-bot-matrix-registration-bot.service wants
matrix_bot_matrix_registration_bot_systemd_wanted_services_list: []

# The bot's username. This user needs to be created manually beforehand.
# Also see `matrix_bot_matrix_registration_bot_user_password`.
matrix_bot_matrix_registration_bot_matrix_user_id_localpart: "matrix-registration-bot"

matrix_bot_matrix_registration_bot_matrix_user_id: '@{{ matrix_bot_matrix_registration_bot_matrix_user_id_localpart }}:{{ matrix_domain }}'

# The password that the bot uses to authenticate.
matrix_bot_matrix_registration_bot_matrix_user_password: ''

matrix_bot_matrix_registration_bot_matrix_homeserver_url: "{{ matrix_homeserver_container_url }}"

# Default configuration template which covers the generic use case.
# You can customize it by controlling the various variables inside it.
#
# For a more advanced customization, you can extend the default (see `matrix_bot_matrix_registration_bot_configuration_extension_yaml`)
# or completely replace this variable with your own template.
matrix_bot_matrix_registration_bot_configuration_yaml: "{{ lookup('template', 'templates/config.yml.j2') }}"

matrix_bot_matrix_registration_bot_configuration_extension_yaml: |
# Your custom YAML configuration goes here.
# This configuration extends the default starting configuration (`matrix_bot_matrix_registration_bot_configuration_yaml`).
#
# You can override individual variables from the default configuration, or introduce new ones.
#
# If you need something more special, you can take full control by
# completely redefining `matrix_bot_matrix_registration_bot_configuration_yaml`.
#
# Example configuration extension follows:
#
# matrix:
# device_name: My-Registration-Bot

matrix_bot_matrix_registration_bot_configuration_extension: "{{ matrix_bot_matrix_registration_bot_configuration_extension_yaml|from_yaml if matrix_bot_matrix_registration_bot_configuration_extension_yaml|from_yaml is mapping else {} }}"

# Holds the final configuration (a combination of the default and its extension).
# You most likely don't need to touch this variable. Instead, see `matrix_bot_matrix_registration_bot_configuration_yaml`.
matrix_bot_matrix_registration_bot_configuration: "{{ matrix_bot_matrix_registration_bot_configuration_yaml|from_yaml|combine(matrix_bot_matrix_registration_bot_configuration_extension, recursive=True) }}"

+ 5
- 0
roles/matrix-bot-matrix-registration-bot/tasks/init.yml Visa fil

@@ -0,0 +1,5 @@
---

- set_fact:
matrix_systemd_services_list: "{{ matrix_systemd_services_list + ['matrix-bot-matrix-registration-bot.service'] }}"
when: matrix_bot_matrix_registration_bot_enabled|bool

+ 23
- 0
roles/matrix-bot-matrix-registration-bot/tasks/main.yml Visa fil

@@ -0,0 +1,23 @@
---

- import_tasks: "{{ role_path }}/tasks/init.yml"
tags:
- always

- import_tasks: "{{ role_path }}/tasks/validate_config.yml"
when: "run_setup|bool and matrix_bot_matrix_registration_bot_enabled|bool"
tags:
- setup-all
- setup-bot-matrix-registration-bot

- import_tasks: "{{ role_path }}/tasks/setup_install.yml"
when: "run_setup|bool and matrix_bot_matrix_registration_bot_enabled|bool"
tags:
- setup-all
- setup-bot-matrix-registration-bot

- import_tasks: "{{ role_path }}/tasks/setup_uninstall.yml"
when: "run_setup|bool and not matrix_bot_matrix_registration_bot_enabled|bool"
tags:
- setup-all
- setup-bot-matrix-registration-bot

+ 69
- 0
roles/matrix-bot-matrix-registration-bot/tasks/setup_install.yml Visa fil

@@ -0,0 +1,69 @@
---

- set_fact:
matrix_bot_matrix_registration_bot_requires_restart: false


- name: Ensure matrix-registration-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_registration_bot_config_path }}", when: true}
- {path: "{{ matrix_bot_matrix_registration_bot_docker_src_files_path }}", when: true}
when: "item.when|bool"

- name: Ensure matrix-registration-bot image is pulled
docker_image:
name: "{{ matrix_bot_matrix_registration_bot_docker_image }}"
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
force_source: "{{ matrix_bot_matrix_registration_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_registration_bot_docker_image_force_pull }}"
when: "not matrix_bot_matrix_registration_bot_container_image_self_build|bool"
register: result
retries: "{{ matrix_container_retries_count }}"
delay: "{{ matrix_container_retries_delay }}"
until: result is not failed

- name: Ensure matrix-registration-bot repository is present on self-build
git:
repo: "{{ matrix_bot_matrix_registration_bot_docker_repo }}"
dest: "{{ matrix_bot_matrix_registration_bot_docker_src_files_path }}"
force: "yes"
become: true
become_user: "{{ matrix_user_username }}"
register: matrix_bot_matrix_registration_bot_git_pull_results
when: "matrix_bot_matrix_registration_bot_container_image_self_build|bool"

- name: Ensure matrix-registration-bot image is built
docker_image:
name: "{{ matrix_bot_matrix_registration_bot_docker_image }}"
source: build
force_source: "{{ matrix_bot_matrix_registration_bot_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: docker/Dockerfile
path: "{{ matrix_bot_matrix_registration_bot_docker_src_files_path }}"
pull: true
when: "matrix_bot_matrix_registration_bot_container_image_self_build|bool"

- name: Ensure matrix-bot-matrix-registration-bot.service installed
template:
src: "{{ role_path }}/templates/systemd/matrix-bot-matrix-registration-bot.service.j2"
dest: "{{ matrix_systemd_path }}/matrix-bot-matrix-registration-bot.service"
mode: 0644
register: matrix_bot_matrix_registration_bot_systemd_service_result

- name: Ensure systemd reloaded after matrix-bot-matrix-registration-bot.service installation
service:
daemon_reload: true
when: "matrix_bot_matrix_registration_bot_systemd_service_result.changed|bool"

- name: Ensure matrix-bot-matrix-registration-bot.service restarted, if necessary
service:
name: "matrix-bot-matrix-registration-bot.service"
state: restarted
when: "matrix_bot_matrix_registration_bot_requires_restart|bool"

+ 36
- 0
roles/matrix-bot-matrix-registration-bot/tasks/setup_uninstall.yml Visa fil

@@ -0,0 +1,36 @@
---

- name: Check existence of matrix-matrix-registration-bot service
stat:
path: "{{ matrix_systemd_path }}/matrix-bot-matrix-registration-bot.service"
register: matrix_bot_matrix_registration_bot_service_stat

- name: Ensure matrix-matrix-registration-bot is stopped
service:
name: matrix-bot-matrix-registration-bot
state: stopped
enabled: false
daemon_reload: true
register: stopping_result
when: "matrix_bot_matrix_registration_bot_service_stat.stat.exists|bool"

- name: Ensure matrix-bot-matrix-registration-bot.service doesn't exist
file:
path: "{{ matrix_systemd_path }}/matrix-bot-matrix-registration-bot.service"
state: absent
when: "matrix_bot_matrix_registration_bot_service_stat.stat.exists|bool"

- name: Ensure systemd reloaded after matrix-bot-matrix-registration-bot.service removal
service:
daemon_reload: true
when: "matrix_bot_matrix_registration_bot_service_stat.stat.exists|bool"

- name: Ensure Matrix matrix-registration-bot paths don't exist
file:
path: "{{ matrix_bot_matrix_registration_bot_base_path }}"
state: absent

- name: Ensure matrix-registration-bot Docker image doesn't exist
docker_image:
name: "{{ matrix_bot_matrix_registration_bot_docker_image }}"
state: absent

+ 10
- 0
roles/matrix-bot-matrix-registration-bot/tasks/validate_config.yml Visa fil

@@ -0,0 +1,10 @@
---

- name: Fail if required settings not defined
fail:
msg: >-
You need to define a required configuration setting (`{{ item }}`).
when: "vars[item] == ''"
with_items:
- "matrix_bot_matrix_registration_bot_matrix_user_password"
- "matrix_bot_matrix_registration_bot_matrix_admin_token"

+ 38
- 0
roles/matrix-bot-matrix-registration-bot/templates/systemd/matrix-bot-matrix-registration-bot.service.j2 Visa fil

@@ -0,0 +1,38 @@
#jinja2: lstrip_blocks: "True"
[Unit]
Description=Matrix registration bot
{% for service in matrix_bot_matrix_registration_bot_systemd_required_services_list %}
Requires={{ service }}
After={{ service }}
{% endfor %}
{% for service in matrix_bot_matrix_registration_bot_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-matrix-registration-bot 2>/dev/null || true'
ExecStartPre=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} rm matrix-bot-matrix-registration-bot 2>/dev/null || true'

ExecStart={{ matrix_host_command_docker }} run --rm --name matrix-bot-matrix-registration-bot \
--log-driver=none \
--cap-drop=ALL \
--network={{ matrix_docker_network }} \
-e 'BOT_SERVER=https://{{ matrix_server_fqn_matrix }}' \
-e 'BOT_USERNAME={{ matrix_bot_matrix_registration_bot_matrix_user_id_localpart }}' \
-e 'BOT_PASSWORD={{ matrix_bot_matrix_registration_bot_matrix_user_password }}' \
-e 'API_BASE_URL=https://{{ matrix_server_fqn_matrix }}' \
-e 'API_TOKEN={{ matrix_bot_matrix_registration_bot_matrix_admin_token }}' \
-e 'LOGGING_LEVEL=info' \
{{ matrix_bot_matrix_registration_bot_docker_image }}

ExecStop=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} kill matrix-bot-matrix-registration-bot 2>/dev/null || true'
ExecStop=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} rm matrix-bot-matrix-registration-bot 2>/dev/null || true'
Restart=always
RestartSec=30
SyslogIdentifier=matrix-bot-matrix-registration-bot

[Install]
WantedBy=multi-user.target

+ 1
- 0
setup.yml Visa fil

@@ -63,3 +63,4 @@
- matrix-postgres-backup
- matrix-prometheus-postgres-exporter
- matrix-common-after
- matrix-bot-matrix-registration-bot

Laddar…
Avbryt
Spara