diff --git a/pillar.example b/pillar.example index 9608538..5ddde08 100644 --- a/pillar.example +++ b/pillar.example @@ -7,11 +7,14 @@ prometheus: - pushgateway - node_explorer + use_upstream_repo: False use_upstream_archive: True + pkg: prometheus: archive_version: 2.10.0 archive_hash: f4233783826f18606b79e5cef0686e4a9c2030146a3c7ce134f0add09f5adcb7 + repo: {} alertmanager: archive_version: '0.17.0' archive_hash: ec171b13976baceace193461f8a1e61021ab9657df5ba45157cd0095aee7d569 @@ -148,4 +151,3 @@ prometheus: linux: #'Alternatives system' priority: zero disables (default) altpriority: 0 - diff --git a/prometheus/defaults.yaml b/prometheus/defaults.yaml index 2c45261..1c26294 100644 --- a/prometheus/defaults.yaml +++ b/prometheus/defaults.yaml @@ -5,6 +5,8 @@ prometheus: rootgroup: root kernel: {{ grains.kernel | lower }} arch: {{ grains.osarch }} + use_upstream_repo: False + use_upstream_archive: False wanted: - prometheus @@ -35,6 +37,13 @@ prometheus: binaries: - prometheus - promtool + repo: + humanname: prometheus + name: prometheus + comments: + - installed by salt + enabled: 1 + gpgcheck: 1 alertmanager: archive_version: '0.17.0' archive_hash: 7c8d2cfeb021c80881ae9904d959131091b8785b6fda9800f84ddef148fe0a4f @@ -87,7 +96,6 @@ prometheus: binaries: - statsd_exporter - use_upstream_archive: False archive: uri: https://github.com/prometheus suffix: tar.gz diff --git a/prometheus/exporters/node/clean.sls b/prometheus/exporters/node/clean.sls new file mode 100644 index 0000000..14401e2 --- /dev/null +++ b/prometheus/exporters/node/clean.sls @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{#- Get the `tplroot` from `tpldir` #} +{%- set tplroot = tpldir.split('/')[0] %} +{%- from tplroot ~ "/map.jinja" import prometheus with context %} + +prometheus-exporters-node-service-dead: + service.dead: + - name: {{ prometheus.exporters.node.service }} + - enable: False + +prometheus-exporters-node-pkg-removed: + pkg.removed: + - name: {{ prometheus.exporters.node.pkg.name }} + - require: + - service: prometheus-exporters-node-service-dead + +{# FreeBSD #} +{%- if salt['grains.get']('os_family') == 'FreeBSD' %} +{%- for parameter in ['args', 'listen_address', 'textfile_dir'] %} +prometheus-exporters-node-args-{{ parameter }}: + sysrc.absent: + - name: node_exporter_{{ parameter }} + - require: + - service: prometheus-exporters-node-service-dead +{%- endfor %} +{%- endif %} diff --git a/prometheus/exporters/node/init.sls b/prometheus/exporters/node/init.sls new file mode 100644 index 0000000..f8f7534 --- /dev/null +++ b/prometheus/exporters/node/init.sls @@ -0,0 +1,64 @@ +# -*- 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 %} + +prometheus-exporters-node-pkg-installed: + pkg.installed: + - name: {{ prometheus.exporters.node.pkg.name }} + +{%- if 'args' in prometheus.exporters.node %} +{%- set args = prometheus.exporters.node.get('args', {}) -%} + +{# FreeBSD #} +{%- if salt['grains.get']('os_family') == 'FreeBSD' %} +{%- if 'web.listen-address' in args.keys() %} +{%- set value = args.pop('web.listen-address') %} +prometheus-exporters-node-args-web-listen-address: + sysrc.managed: + - name: node_exporter_listen_address + - value: {{ value }} + - watch_in: + - service: prometheus-exporters-node-service-running +{%- endif %} + +{%- if 'collector.textfile.directory' in args.keys() %} +{%- set value = args.pop('collector.textfile.directory') %} +prometheus-exporters-node-args-collector-textfile-directory: + sysrc.managed: + - name: node_exporter_textfile_dir + - value: {{ value }} + - watch_in: + - service: prometheus-exporters-node-service-running +{%- endif %} + +prometheus-exporters-node-args: + sysrc.managed: + - 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) }} >/dev/null 2>&1" + - watch_in: + - service: prometheus-exporters-node-service-running + +{# Debian #} +{%- elif salt['grains.get']('os_family') == 'Debian'%} +prometheus-exporters-node-args: + file.managed: + - name: {{ prometheus.exporters.node.config_file }} + - contents: | + ARGS="{{ concat_args(args) }}" + - watch_in: + - service: prometheus-exporters-node-service-running +{%- endif %} +{%- endif %} + +prometheus-exporters-node-service-running: + service.running: + - name: {{ prometheus.exporters.node.service }} + - enable: True + - watch: + - pkg: prometheus-exporters-node-pkg-installed diff --git a/prometheus/jinja/macros.jinja b/prometheus/jinja/macros.jinja index aeeb852..f736111 100644 --- a/prometheus/jinja/macros.jinja +++ b/prometheus/jinja/macros.jinja @@ -4,9 +4,11 @@ # Collection of common macros {%- macro format_kwargs(kwarg) -%} + {%- filter indent(4) %} {%- for k, v in kwarg|dictsort() %} - {{ k }}: {{ v }} {%- endfor %} {%- endfilter %} + {%- endmacro %} diff --git a/prometheus/osfamilymap.yaml b/prometheus/osfamilymap.yaml index 0fc5fc2..09c7867 100644 --- a/prometheus/osfamilymap.yaml +++ b/prometheus/osfamilymap.yaml @@ -11,14 +11,19 @@ # osfamilymap: {} --- {%- if grains.os == 'MacOS' %} + {% set macos_user = salt['cmd.run']("stat -f '%Su' /dev/console") %} {% set macos_group = salt['cmd.run']("stat -f '%Sg' /dev/console") %} {%- endif %} Debian: {} RedHat: - ## remove when https://github.com/saltstack-formulas/prometheus-formula/pull/4 is merged - use_upstream_archive: True + pkg: + prometheus: + repo: + baseurl: 'https://packagecloud.io/prometheus-rpm/release/el/$releasever/$basearch' + gpgkey: 'https://packagecloud.io/prometheus-rpm/release/gpgkey gpgkey2=https://raw.githubusercontent.com/lest/prometheus-rpm/master/RPM-GPG-KEY-prometheus-rpm' + metadata_expire: 300 Suse: {} @@ -109,6 +114,7 @@ Windows: archive_hash: 9362b7482e74792f111c4bb1a372b18a88f6354c78f24713bacfbcb050883556 MacOS: + rootuser: {{ macos_user | d('') }} rootgroup: {{ macos_group | d('') }} kernel: darwin pkg: diff --git a/prometheus/package/install.sls b/prometheus/package/install.sls index cc540aa..dc6caa6 100644 --- a/prometheus/package/install.sls +++ b/prometheus/package/install.sls @@ -4,11 +4,19 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} {%- from tplroot ~ "/map.jinja" import prometheus with context %} + + {%- if prometheus.pkg.use_upstream_repo %} +include: + - .repo + + {%- endif %} {%- for name in prometheus.wanted %} + {%- if name in prometheus.pkg %} prometheus-package-install-{{ name }}-installed: pkg.installed: - name: {{ name }} + {%- endif %} {%- endfor %} diff --git a/prometheus/package/repo/clean.sls b/prometheus/package/repo/clean.sls new file mode 100644 index 0000000..d5f2ee6 --- /dev/null +++ b/prometheus/package/repo/clean.sls @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{#- Get the `tplroot` from `tpldir` #} +{%- set tplroot = tpldir.split('/')[0] %} +{%- from tplroot ~ "/map.jinja" import prometheus with context %} + + {%- for name in prometheus.wanted %} + {%- if name in prometheus.pkg and 'repo' in prometheus.pkg[name] and prometheus.pkg[name]['repo'] %} + +prometheus-package-repo-clean-{{ name }}-pkgrepo-absent: + pkgrepo.absent: + - name: {{ prometheus.pkg[name]['repo']['name'] }} + + {%- endif %} + {%- endfor %} diff --git a/prometheus/package/repo/init.sls b/prometheus/package/repo/init.sls new file mode 100644 index 0000000..d3e5518 --- /dev/null +++ b/prometheus/package/repo/init.sls @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +include: + - .install diff --git a/prometheus/package/repo/install.sls b/prometheus/package/repo/install.sls new file mode 100644 index 0000000..9de6fb8 --- /dev/null +++ b/prometheus/package/repo/install.sls @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{#- Get the `tplroot` from `tpldir` #} +{%- set tplroot = tpldir.split('/')[0] %} +{%- from tplroot ~ "/map.jinja" import prometheus with context %} + + {%- for name in prometheus.wanted %} + {%- if name in prometheus.pkg and 'repo' in prometheus.pkg[name] and prometheus.pkg[name]['repo'] %} + {%- from tplroot ~ "/jinja/macros.jinja" import format_kwargs with context %} + +prometheus-package-repo-install-{{ name }}-pkgrepo-managed: + pkgrepo.managed: + {{- format_kwargs(prometheus.pkg[name]['repo']) }} + +prometheus-package-repo-install-{{ name }}-file-replace: + # redhat workaround for salt issue #51494 + file.replace: + - name: /etc/yum.repos.d/{{ name }}.repo + - pattern: ' gpgkey2=' + - repl: '\n ' + - ignore_if_missing: True + - onlyif: {{ grains.os_family == 'RedHat' }} + + {%- endif %} + {%- endfor %}