Move roles/matrix* to roles/custom/matrix*

This paves the way for installing other roles into `roles/galaxy` using `ansible-galaxy`,
similar to how it's done in:

- https://github.com/spantaleev/gitea-docker-ansible-deploy
- https://github.com/spantaleev/nextcloud-docker-ansible-deploy

In the near future, we'll be removing a lot of the shared role code from here
and using upstream roles for it. Some of the core `matrix-*` roles have
already been extracted out into other reusable roles:

- https://github.com/devture/com.devture.ansible.role.postgres
- https://github.com/devture/com.devture.ansible.role.systemd_docker_base
- https://github.com/devture/com.devture.ansible.role.timesync
- https://github.com/devture/com.devture.ansible.role.vars_preserver
- https://github.com/devture/com.devture.ansible.role.playbook_runtime_messages
- https://github.com/devture/com.devture.ansible.role.playbook_help

We just need to migrate to those.
This commit is contained in:
Slavi Pantaleev
2022-11-03 09:11:29 +02:00
parent 6c131138ad
commit 410a915a8a
722 changed files with 148 additions and 145 deletions

View File

@ -0,0 +1,52 @@
---
- name: Fail if playbook called incorrectly
ansible.builtin.fail:
msg: "The `one_time` variable needs to be provided to this playbook, via --extra-vars"
when: "one_time is not defined or one_time not in ['yes', 'no']"
- name: Fail if playbook called incorrectly
ansible.builtin.fail:
msg: "The `ex_date` variable (expiration date) needs to be provided to this playbook, via --extra-vars"
when: "ex_date is not defined or ex_date == '<date>'"
- name: Call matrix-registration token creation API
ansible.builtin.uri:
url: "{{ matrix_registration_api_token_endpoint }}"
follow_redirects: none
validate_certs: "{{ matrix_registration_api_validate_certs }}"
headers:
Content-Type: application/json
Authorization: "SharedSecret {{ matrix_registration_admin_secret }}"
method: POST
body_format: json
body: |
{
"one_time": {{ 'true' if one_time == 'yes' else 'false' }},
"ex_date": {{ ex_date | to_json }}
}
check_mode: false
register: matrix_registration_api_result
- ansible.builtin.set_fact:
matrix_registration_api_result_message: >-
matrix-registration result:
Direct registration link (with the token prefilled):
{{ matrix_registration_api_register_endpoint }}?token={{ matrix_registration_api_result.json.name }}
Full token details are:
{{ matrix_registration_api_result.json }}
check_mode: false
- name: Inject result message into matrix_playbook_runtime_results
ansible.builtin.set_fact:
matrix_playbook_runtime_results: |
{{
matrix_playbook_runtime_results | default([])
+
[matrix_registration_api_result_message]
}}
check_mode: false

View File

@ -0,0 +1,69 @@
---
# See https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/1070
# and https://github.com/spantaleev/matrix-docker-ansible-deploy/commit/1ab507349c752042d26def3e95884f6df8886b74#commitcomment-51108407
- name: Fail if trying to self-build on Ansible < 2.8
ansible.builtin.fail:
msg: "To self-build the Matrix Registration image, you should use Ansible 2.8 or higher. See docs/ansible.md"
when: "ansible_version.major == 2 and ansible_version.minor < 8 and matrix_registration_container_image_self_build and matrix_registration_enabled"
- ansible.builtin.set_fact:
matrix_systemd_services_list: "{{ matrix_systemd_services_list + ['matrix-registration.service'] }}"
when: matrix_registration_enabled | bool
- when: matrix_registration_enabled | bool
tags:
- always
block:
- name: Fail if matrix-nginx-proxy role already executed
ansible.builtin.fail:
msg: >-
Trying to append matrix-registration's reverse-proxying configuration to matrix-nginx-proxy,
but it's pointless since the matrix-nginx-proxy role had already executed.
To fix this, please change the order of roles in your playbook,
so that the matrix-nginx-proxy role would run after the matrix-registration role.
when: matrix_nginx_proxy_role_executed | default(False) | bool
- name: Generate matrix-registration proxying configuration for matrix-nginx-proxy
ansible.builtin.set_fact:
matrix_registration_matrix_nginx_proxy_configuration: |
rewrite ^{{ matrix_registration_public_endpoint }}$ {{ matrix_nginx_proxy_x_forwarded_proto_value }}://$server_name{{ matrix_registration_public_endpoint }}/ permanent;
rewrite ^{{ matrix_registration_public_endpoint }}/$ {{ matrix_nginx_proxy_x_forwarded_proto_value }}://$server_name{{ matrix_registration_public_endpoint }}/register redirect;
location ~ ^{{ matrix_registration_public_endpoint }}/(.*) {
{% if matrix_nginx_proxy_enabled | default(False) %}
{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "matrix-registration:5000";
proxy_pass http://$backend/$1;
{% else %}
{# Generic configuration for use outside of our container setup #}
proxy_pass http://127.0.0.1:8767/$1;
{% endif %}
{#
Workaround matrix-registration serving the background image at /static
(see https://github.com/ZerataX/matrix-registration/issues/47)
#}
sub_filter_once off;
sub_filter_types text/css;
sub_filter "/static/" "{{ matrix_registration_public_endpoint }}/static/";
}
- name: Register matrix-registration proxying configuration with matrix-nginx-proxy
ansible.builtin.set_fact:
matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks: |
{{
matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks | default([])
+
[matrix_registration_matrix_nginx_proxy_configuration]
}}
- name: Warn about reverse-proxying if matrix-nginx-proxy not used
ansible.builtin.debug:
msg: >-
NOTE: You've enabled the matrix-registration tool but are not using the matrix-nginx-proxy
reverse proxy.
Please make sure that you're proxying the `{{ matrix_registration_public_endpoint }}`
URL endpoint to the matrix-registration container.
You can expose the container's port using the `matrix_registration_container_http_host_bind_port` variable.
when: "matrix_registration_enabled | bool and not matrix_nginx_proxy_enabled | default(False) | bool"

View File

@ -0,0 +1,31 @@
---
- name: Call matrix-registration list all tokens API
ansible.builtin.uri:
url: "{{ matrix_registration_api_token_endpoint }}"
follow_redirects: none
validate_certs: "{{ matrix_registration_api_validate_certs }}"
headers:
Content-Type: application/json
Authorization: "SharedSecret {{ matrix_registration_admin_secret }}"
method: GET
body_format: json
check_mode: false
register: matrix_registration_api_result
- ansible.builtin.set_fact:
matrix_registration_api_result_message: >-
matrix-registration result:
{{ matrix_registration_api_result.json | to_nice_json }}
check_mode: false
- name: Inject result message into matrix_playbook_runtime_results
ansible.builtin.set_fact:
matrix_playbook_runtime_results: |
{{
matrix_playbook_runtime_results | default([])
+
[matrix_registration_api_result_message]
}}
check_mode: false

View File

@ -0,0 +1,33 @@
---
- 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_registration_enabled | bool"
tags:
- setup-all
- setup-matrix-registration
- ansible.builtin.import_tasks: "{{ role_path }}/tasks/setup_install.yml"
when: "run_setup | bool and matrix_registration_enabled | bool"
tags:
- setup-all
- setup-matrix-registration
- ansible.builtin.import_tasks: "{{ role_path }}/tasks/setup_uninstall.yml"
when: "run_setup | bool and not matrix_registration_enabled | bool"
tags:
- setup-all
- setup-matrix-registration
- ansible.builtin.import_tasks: "{{ role_path }}/tasks/generate_token.yml"
when: "run_setup | bool and matrix_registration_enabled | bool"
tags:
- generate-matrix-registration-token
- ansible.builtin.import_tasks: "{{ role_path }}/tasks/list_tokens.yml"
when: "run_setup | bool and matrix_registration_enabled | bool"
tags:
- list-matrix-registration-tokens

View File

@ -0,0 +1,117 @@
---
- ansible.builtin.set_fact:
matrix_registration_requires_restart: false
- when: "matrix_registration_database_engine == 'postgres'"
block:
- name: Check if an SQLite database already exists
ansible.builtin.stat:
path: "{{ matrix_registration_sqlite_database_path_local }}"
register: matrix_registration_sqlite_database_path_local_stat_result
- when: "matrix_registration_sqlite_database_path_local_stat_result.stat.exists | bool"
block:
- ansible.builtin.set_fact:
matrix_postgres_db_migration_request:
src: "{{ matrix_registration_sqlite_database_path_local }}"
dst: "{{ matrix_registration_database_connection_string }}"
caller: "{{ role_path | basename }}"
engine_variable_name: 'matrix_registration_database_engine'
engine_old: 'sqlite'
systemd_services_to_stop: ['matrix-registration.service']
# pgloader makes `ex_date` of type `TIMESTAMP WITH TIMEZONE`,
# which makes matrix-registration choke on it later on when comparing dates.
additional_psql_statements_list:
- ALTER TABLE tokens ALTER COLUMN ex_date TYPE TIMESTAMP WITHOUT TIME ZONE;
additional_psql_statements_db_name: "{{ matrix_registration_database_name }}"
- ansible.builtin.import_role:
name: custom/matrix-postgres
tasks_from: migrate_db_to_postgres
- ansible.builtin.set_fact:
matrix_registration_requires_restart: true
- name: Ensure matrix-registration paths exist
ansible.builtin.file:
path: "{{ item.path }}"
state: directory
mode: 0750
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
with_items:
- {path: "{{ matrix_registration_base_path }}", when: true}
- {path: "{{ matrix_registration_config_path }}", when: true}
- {path: "{{ matrix_registration_data_path }}", when: true}
- {path: "{{ matrix_registration_docker_src_files_path }}", when: "{{ matrix_registration_container_image_self_build }}"}
when: "item.when | bool"
- name: Ensure matrix-registration image is pulled
community.docker.docker_image:
name: "{{ matrix_registration_docker_image }}"
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
force_source: "{{ matrix_registration_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_registration_docker_image_force_pull }}"
when: "not matrix_registration_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 repository is present when self-building
ansible.builtin.git:
repo: "{{ matrix_registration_container_image_self_build_repo }}"
dest: "{{ matrix_registration_docker_src_files_path }}"
version: "{{ matrix_registration_container_image_self_build_branch }}"
force: "yes"
become: true
become_user: "{{ matrix_user_username }}"
register: matrix_registration_git_pull_results
when: "matrix_registration_container_image_self_build | bool"
# See: https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/1864
- name: Patch setup.py to allow self-built version to work
ansible.builtin.lineinfile:
path: "{{ matrix_registration_docker_src_files_path }}/setup.py"
regexp: 'flask-limiter'
line: '"flask-limiter~=1.1.0", "Markupsafe<2.1",'
when: "matrix_registration_container_image_self_build | bool and matrix_registration_container_image_self_build_python_dependencies_patch_enabled | bool"
- name: Ensure matrix-registration Docker image is built
community.docker.docker_image:
name: "{{ matrix_registration_docker_image }}"
source: build
force_source: "{{ matrix_registration_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_registration_git_pull_results.changed }}"
build:
dockerfile: Dockerfile
path: "{{ matrix_registration_docker_src_files_path }}"
pull: true
when: "matrix_registration_container_image_self_build | bool"
- name: Ensure matrix-registration config installed
ansible.builtin.copy:
content: "{{ matrix_registration_configuration | to_nice_yaml(indent=2, width=999999) }}"
dest: "{{ matrix_registration_config_path }}/config.yaml"
mode: 0644
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
- name: Ensure matrix-registration.service installed
ansible.builtin.template:
src: "{{ role_path }}/templates/systemd/matrix-registration.service.j2"
dest: "{{ matrix_systemd_path }}/matrix-registration.service"
mode: 0644
register: matrix_registration_systemd_service_result
- name: Ensure systemd reloaded after matrix-registration.service installation
ansible.builtin.service:
daemon_reload: true
when: "matrix_registration_systemd_service_result.changed | bool"
- name: Ensure matrix-registration.service restarted, if necessary
ansible.builtin.service:
name: "matrix-registration.service"
state: restarted
when: "matrix_registration_requires_restart | bool"

View File

@ -0,0 +1,31 @@
---
- name: Check existence of matrix-registration service
ansible.builtin.stat:
path: "{{ matrix_systemd_path }}/matrix-registration.service"
register: matrix_registration_service_stat
- name: Ensure matrix-registration is stopped
ansible.builtin.service:
name: matrix-registration
state: stopped
enabled: false
daemon_reload: true
register: stopping_result
when: "matrix_registration_service_stat.stat.exists | bool"
- name: Ensure matrix-registration.service doesn't exist
ansible.builtin.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
ansible.builtin.service:
daemon_reload: true
when: "matrix_registration_service_stat.stat.exists | bool"
- name: Ensure matrix-registration Docker image doesn't exist
community.docker.docker_image:
name: "{{ matrix_registration_docker_image }}"
state: absent

View File

@ -0,0 +1,20 @@
---
- name: Fail if required matrix-registration settings not defined
ansible.builtin.fail:
msg: >
You need to define a required configuration setting (`{{ item }}`) for using matrix-registration.
when: "vars[item] == ''"
with_items:
- "matrix_registration_shared_secret"
- "matrix_registration_admin_secret"
- "matrix_registration_server_location"
- name: (Deprecation) Catch and report renamed settings
ansible.builtin.fail:
msg: >-
Your configuration contains a variable, which now has a different name.
Please change your configuration to rename the variable (`{{ item.old }}` -> `{{ item.new }}`).
when: "item.old in vars"
with_items:
- {'old': 'matrix_registration_docker_repo', 'new': 'matrix_registration_container_image_self_build_repo'}