add prometheus-nginxlog-exporter role (#2315)

* add prometheus-nginxlog-exporter role

* Rename matrix_prometheus_nginxlog_exporter_container_url to matrix_prometheus_nginxlog_exporter_container_hostname

* avoid referencing variables from other roles, handover info using group_vars/matrix_servers

* fix: stop service when uninstalling

fix: typo

move available arch's into a var

fix: text

* fix: prometheus enabled condition

Co-authored-by: ikkemaniac <ikkemaniac@localhost>
This commit is contained in:
ikkemaniac
2022-12-07 15:58:36 +01:00
committed by GitHub
parent 48d601008c
commit 8ebf18a885
18 changed files with 2139 additions and 2 deletions

View File

@ -0,0 +1,31 @@
---
- name: Fail if matrix-nginx-proxy role already executed
ansible.builtin.fail:
msg: >-
Trying to append prometheus-nginxlog-exporter's configuration to matrix-nginx-proxy,
but it's pointless since the matrix-nginx-proxy role is already executed.
To fix this, please change the order of roles in your playbook to make sure that
the matrix-nginx-proxy role is run after the prometheus-nginxlog-exporter role.
when: matrix_nginx_proxy_role_executed | default(False) | bool
- name: Generate prometheus-nginxlog-exporter's metrics proxying configuration for matrix-nginx-proxy (matrix.DOMAIN/metrics/nginxlog)
ansible.builtin.set_fact:
matrix_prometheus_nginxlog_exporter_matrix_nginx_proxy_metrics_configuration_matrix_domain: |
location /metrics/nginxlog {
{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "{{ matrix_prometheus_nginxlog_exporter_container_hostname }}:{{ matrix_prometheus_nginxlog_exporter_container_metrics_port }}";
proxy_pass http://$backend/metrics;
}
when: matrix_prometheus_nginxlog_exporter_metrics_proxying_enabled | bool and matrix_nginx_proxy_enabled | bool
- name: Register prometheus-nginxlog-exporter metrics proxying configuration with matrix-nginx-proxy (matrix.DOMAIN/metrics/nginxlog)
ansible.builtin.set_fact:
matrix_nginx_proxy_proxy_matrix_metrics_additional_system_location_configuration_blocks: |
{{
matrix_nginx_proxy_proxy_matrix_metrics_additional_system_location_configuration_blocks | default([])
+
[matrix_prometheus_nginxlog_exporter_matrix_nginx_proxy_metrics_configuration_matrix_domain]
}}
when: matrix_prometheus_nginxlog_exporter_metrics_proxying_enabled | bool

View File

@ -0,0 +1,29 @@
---
- block:
- when: matrix_prometheus_nginxlog_exporter_enabled | bool
ansible.builtin.include_tasks: "{{ role_path }}/tasks/inject_into_nginx_proxy.yml"
tags:
- setup-all
- setup-prometheus-nginxlog-exporter
- install-all
- install-prometheus-nginxlog-exporter
- block:
- when: matrix_prometheus_nginxlog_exporter_enabled | bool
ansible.builtin.include_tasks: "{{ role_path }}/tasks/validate_config.yml"
- when: matrix_prometheus_nginxlog_exporter_enabled | bool
ansible.builtin.include_tasks: "{{ role_path }}/tasks/setup_install.yml"
tags:
- setup-all
- setup-prometheus-nginxlog-exporter
- install-all
- install-prometheus-nginxlog-exporter
- block:
- when: not matrix_prometheus_nginxlog_exporter_enabled | bool
ansible.builtin.include_tasks: "{{ role_path }}/tasks/setup_uninstall.yml"
tags:
- setup-all
- setup-prometheus-nginxlog-exporter

View File

@ -0,0 +1,47 @@
---
- name: Ensure matrix-prometheus-nginxlog-exporter image is pulled
community.docker.docker_image:
name: "{{ matrix_prometheus_nginxlog_exporter_docker_image }}"
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
force_source: "{{ matrix_prometheus_nginxlog_exporter_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_prometheus_nginxlog_exporter_docker_image_force_pull }}"
register: result
retries: "{{ devture_playbook_help_container_retries_count }}"
delay: "{{ devture_playbook_help_container_retries_delay }}"
until: result is not failed
- name: Ensure prometheus-nginxlog-exporter paths exists
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: 0750
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
with_items:
- "{{ matrix_prometheus_nginxlog_exporter_base_path }}"
- "{{ matrix_prometheus_nginxlog_exporter_config_path }}"
- name: Ensure prometheus-nginxlog-exporter.yaml present
ansible.builtin.template:
src: "{{ role_path }}/templates/prometheus-nginxlog-exporter.yaml.j2"
dest: "{{ matrix_prometheus_nginxlog_exporter_config_path }}/prometheus-nginxlog-exporter.yaml"
mode: 0644
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
- name: Ensure grafana dashboard is installed
ansible.builtin.template:
src: "{{ role_path }}/templates/nginx-proxy.json"
dest: "{{ matrix_grafana_config_path }}/dashboards/"
mode: 0644
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
when: matrix_grafana_enabled
- name: Ensure matrix-prometheus-nginxlog-exporter.service installed
ansible.builtin.template:
src: "{{ role_path }}/templates/systemd/matrix-prometheus-nginxlog-exporter.service.j2"
dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-prometheus-nginxlog-exporter.service"
mode: 0644
register: matrix_prometheus_nginxlog_exporter_systemd_service_result

View File

@ -0,0 +1,39 @@
---
- name: Check existence of matrix-prometheus-nginxlog-exporter service
ansible.builtin.stat:
path: "{{ devture_systemd_docker_base_systemd_path }}/matrix-prometheus-nginxlog-exporter.service"
register: matrix_prometheus_nginxlog_exporter_service_stat
- when: matrix_prometheus_nginxlog_exporter_service_stat.stat.exists | bool
block:
- name: Fail when not cleaning up nginx and prometheus configs
ansible.builtin.fail:
msg: >
This role has added to configs in 'matrix-nginx-proxy', 'matrix-synapse-reverse-proxy-companion' and 'matrix-prometheus'.
Running 'setup-synapse-reverse-proxy-companion' WILL NOT remove those settings from those roles.
Run the playbook again with the `setup-all` tag or all three 'setup-nginx-proxy,setup-synapse-reverse-proxy-companion,setup-prometheus' tags while
'matrix_prometheus_enabled: false' to rebuild their configs.
when: not ('setup-all' in ansible_run_tags or ('setup-nginx-proxy' in ansible_run_tags and 'setup-synapse-reverse-proxy-companion' in ansible_run_tags and 'setup-prometheus' in ansible_run_tags))
- name: Ensure matrix-prometheus-nginxlog-exporter is stopped
ansible.builtin.service:
name: matrix-prometheus-nginxlog-exporter
state: stopped
enabled: false
daemon_reload: true
- name: Ensure matrix-prometheus-nginxlog-exporter.service doesn't exist
ansible.builtin.file:
path: "{{ devture_systemd_docker_base_systemd_path }}/matrix-prometheus-nginxlog-exporter.service"
state: absent
- name: Ensure matrix-prometheus-nginxlog-exporter paths don't exist
ansible.builtin.file:
path: "{{ matrix_prometheus_nginxlog_exporter_base_path }}"
state: absent
- name: Ensure matrix-prometheus-nginxlog-exporter grafana dashboard doesn't exist
ansible.builtin.file:
path: "{{ matrix_grafana_config_path }}/dashboards/nginx-proxy.json"
state: absent

View File

@ -0,0 +1,32 @@
---
- name: Fail if Prometheus not enabled
ansible.builtin.fail:
msg: >
You need to set `matrix_prometheus_enabled: true` or configure an external Prometheus database
as this roles requires Prometheus for data storage.
when: not matrix_prometheus_enabled and not matrix_prometheus_nginxlog_exporter_metrics_proxying_enabled
- name: Fail if docker image not availble for arch
ansible.builtin.fail:
msg: >
'prometheus-nginxlog-exporter' docker image is not available for your arch '{{ matrix_architecture }}'.
We currently do not support building an image using this playbook.
You can use a custom-build image by setting
'matrix_prometheus_nginxlog_exporter_docker_image_arch_check_enabled: false'
'matrix_prometheus_nginxlog_exporter_docker_image: path/to/docker/image:tag'
or disable 'prometheus-nginxlog-exporter' by setting
'matrix_prometheus_nginxlog_exporter: false'
in vars.yml
when: matrix_prometheus_nginxlog_exporter_docker_image_arch_check_enabled and matrix_architecture not in matrix_prometheus_nginxlog_exporter_docker_image_arch
- name: Fail if nginx-proxy is not set to proxy metrics while prometheus-nginxlog-exporter is
ansible.builtin.fail:
msg: >
'matrix_prometheus_nginxlog_exporter_metrics_proxying_enabled' is set but 'matrix_nginx_proxy_proxy_matrix_metrics_enabled' is not
when: matrix_prometheus_nginxlog_exporter_metrics_proxying_enabled | bool and not matrix_nginx_proxy_proxy_matrix_metrics_enabled | bool