feat(args): handle service arguments the same way

This commit is contained in:
Alexander Weidinger 2019-04-26 14:02:30 +02:00
parent 34ada49bf7
commit 94078fed74
10 changed files with 87 additions and 35 deletions

View File

@ -6,7 +6,8 @@ prometheus:
config_file: /etc/prometheus/prometheus.yml
service:
name: prometheus
flags: --web.listen-address="0.0.0.0:9090"
args:
web.listen-address: 0.0.0.0:9090
tofs:
# The files_switch key serves as a selector for alternative

View File

@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import prometheus with context %}
{%- from tplroot ~ "/map.jinja" import concat_args %}
{%- if 'args' in prometheus.service %}
{%- set args = prometheus.service.get('args', {}) -%}
{%- if 'storage.tsdb.path' in args.keys() %}
prometheus-data-dir:
file.directory:
- name: {{ args['storage.tsdb.path'] }}
- owner: {{ prometheus.service.user }}
- group: {{ prometheus.service.group }}
- makedirs: True
- watch_in:
- service: prometheus-service-running-service-running
{%- endif %}
{# FreeBSD #}
{%- if salt['grains.get']('os_family') == 'FreeBSD' %}
{%- if 'storage.tsdb.path' in args.keys() %}
{%- set value = args.pop('storage.tsdb.path') %}
prometheus-config-args-storage-tsdb-path:
sysrc.managed:
- name: prometheus_data_dir
- value: {{ value }}
- watch_in:
- service: prometheus-service-running-service-running
{%- endif %}
prometheus-config-args-all:
sysrc.managed:
- name: prometheus_args
# service prometheus restart tended to hang on FreeBSD
# https://github.com/saltstack/salt/issues/44848#issuecomment-487016414
- value: "{{ concat_args(args) }} >/dev/null 2>&1"
- watch_in:
- service: prometheus-service-running-service-running
{# Debian #}
{%- elif salt['grains.get']('os_family') == 'Debian'%}
prometheus-config-args-file-managed:
file.managed:
- name: {{ prometheus.args_file }}
- contents: |
ARGS="{{ concat_args(args) }}"
- watch_in:
- service: prometheus-service-running-service-running
{%- endif %}
{%- endif %}

View File

@ -14,3 +14,13 @@ prometheus-config-clean-file-absent:
- name: {{ prometheus.config_file }}
- require:
- sls: {{ sls_service_clean }}
{%- if salt['grains.get']('os_family') == 'FreeBSD' %}
{%- for parameter in ['args', 'data_dir'] %}
prometheus-service-args-{{ parameter }}:
sysrc.absent:
- name: prometheus_{{ parameter }}
- require:
- service: prometheus-service-clean-service-dead
{%- endfor %}
{%- endif %}

View File

@ -2,4 +2,5 @@
# vim: ft=sls
include:
- .args
- .file

View File

@ -8,7 +8,8 @@ prometheus:
config: {}
service:
name: prometheus
sysrc: False
user: prometheus
group: prometheus
exporters:
node:
pkg: prometheus-node-exporter

View File

@ -4,16 +4,7 @@
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import prometheus with context %}
{%- macro concat_args(args) %}
{%- if args|length > 0 %}
{%- for k,v in args -%}
{%- if not k or not v %}{% continue %}{% endif -%}
--{{ k }}={{ v }}
{%- if not loop.last %} {% endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro %}
{%- from tplroot ~ "/map.jinja" import concat_args %}
prometheus-exporters-node-pkg-installed:
pkg.installed:
@ -49,7 +40,7 @@ prometheus-exporters-node-args:
- name: node_exporter_args
# service node_exporter restart tended to hang on FreeBSD
# https://github.com/saltstack/salt/issues/44848#issuecomment-487016414
- value: "{{ concat_args(args|dictsort) }} >/dev/null 2>&1"
- value: "{{ concat_args(args) }} >/dev/null 2>&1"
- watch_in:
- service: prometheus-exporters-node-service-running

View File

@ -22,3 +22,15 @@
{#- Merge the prometheus pillar #}
{%- set prometheus = salt['pillar.get']('prometheus', default=defaults, merge=True) %}
{#- Contactenate arguments #}
{%- macro concat_args(args) %}
{%- set args = args|dictsort %}
{%- if args|length > 0 %}
{%- for k,v in args -%}
{%- if not k or not v %}{% continue %}{% endif -%}
--{{ k }}={{ v }}
{%- if not loop.last %} {% endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro %}

View File

@ -11,6 +11,7 @@
# osfamilymap: {}
---
Debian:
args_file: /etc/default/prometheus
exporters:
node:
config_file: /etc/default/prometheus-node-exporter
@ -28,8 +29,6 @@ Alpine: {}
FreeBSD:
rootgroup: wheel
config_file: /usr/local/etc/prometheus.yml
service:
sysrc: True
exporters:
node:
pkg: node_exporter
@ -37,8 +36,6 @@ FreeBSD:
OpenBSD:
rootgroup: wheel
service:
sysrc: True
Solaris: {}

View File

@ -9,10 +9,3 @@ prometheus-service-clean-service-dead:
service.dead:
- name: {{ prometheus.service.name }}
- enable: False
{%- if prometheus.service.use_sysrc %}
prometheus_flags:
sysrc.absent:
- require:
- service: prometheus-service-clean-service-dead
{%- endif %}

View File

@ -3,28 +3,20 @@
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_config_args = tplroot ~ '.config.args' %}
{%- set sls_config_file = tplroot ~ '.config.file' %}
{%- from tplroot ~ "/map.jinja" import prometheus with context %}
include:
- {{ sls_config_args }}
- {{ sls_config_file }}
{%- if prometheus.service.sysrc %}
prometheus_args:
sysrc.managed:
# service prometheus restart tended to hang on FreeBSD
# https://github.com/saltstack/salt/issues/44848#issuecomment-487016414
- value: "{{ prometheus.service.flags }} >/dev/null 2>&1"
{%- endif %}
prometheus-service-running-service-running:
service.running:
- name: {{ prometheus.service.name }}
- enable: True
- watch:
- file: prometheus-config-file-file-managed
{%- if prometheus.service.sysrc %}
- sysrc: prometheus_args
{%- endif %}
- require:
- sls: {{ sls_config_args }}
- sls: {{ sls_config_file }}