Skip to content

Commit

Permalink
feat(linux): alternatives support & updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noelmcloughlin committed Jun 17, 2019
1 parent b074bd3 commit 36b3e62
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 104 deletions.
5 changes: 5 additions & 0 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ prometheus:
archive:
source: https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheus-2.10.0.linux-amd64.tar.gz
source_hash: f4233783826f18606b79e5cef0686e4a9c2030146a3c7ce134f0add09f5adcb7
archive_format: tar
enforce_toplevel: False
config_file: /etc/prometheus/prometheus.yml
environ_file: /etc/default/prometheus.sh
Expand All @@ -16,6 +17,10 @@ prometheus:
args:
web.listen-address: 0.0.0.0:9090

linux:
#'Alternatives system' priority: zero disables (default)
altpriority: 0

tofs:
# The files_switch key serves as a selector for alternative
# directories under the formula files directory. See TOFS pattern
Expand Down
34 changes: 34 additions & 0 deletions prometheus/config/alternatives/clean.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
# vim: ft=sls

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import prometheus with context %}
{%- set sls_archive_clean = tplroot ~ '.archive.clean' %}
{%- set sls_package_clean = tplroot ~ '.package.clean' %}
{%- if grains.kernel|lower == 'linux' and prometheus.linux.altpriority|int > 0 %}
include:
- {{ sls_archive_clean if prometheus.pkg.use_upstream_archive else sls_package_clean }}
prometheus-package-archive-remove-home-alternative-remove:
alternatives.remove:
- name: prometheus-home
- path: {{ prometheus.base_dir }}
- onlyif: update-alternatives --get-selections |grep ^prometheus-home
- require:
- sls: {{ sls_archive_clean if prometheus.pkg.use_upstream_archive else sls_package_clean }}
{% for i in ['prometheus', 'promtool'] %}
prometheus-package-archive-remove-{{ i }}-alternative-remove:
alternatives.remove:
- name: link-{{ i }}
- path: {{ prometheus.base_dir }}/{{ i }}
- onlyif: update-alternatives --get-selections |grep ^link-{{ i }}
- require:
- sls: {{ sls_archive_clean if prometheus.pkg.use_upstream_archive else sls_package_clean }}
{% endfor %}
{%- endif %}
5 changes: 5 additions & 0 deletions prometheus/config/alternatives/init.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# vim: ft=sls

include:
- .install
70 changes: 70 additions & 0 deletions prometheus/config/alternatives/install.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
# vim: ft=sls

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import prometheus with context %}
{%- set sls_archive_install = tplroot ~ '.archive.install' %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
{%- if grains.kernel|lower == 'linux' and prometheus.linux.altpriority|int > 0 %}
include:
- {{ sls_archive_install }}
prometheus-package-archive-install-home-alternative-install:
cmd.run:
- name: update-alternatives --install {{ prometheus.dir }} prometheus-home {{ prometheus.base_dir }} {{prometheus.linux.altpriority}}
- watch:
- archive: prometheus-package-archive-install-archive-extracted
- require:
- sls: {{ sls_archive_install }}
- onlyif: {{ grains.os_family in ('Suse',) }}
alternatives.install:
- name: prometheus-home
- link: {{ prometheus.dir }}
- path: {{ prometheus.base_dir }}
- priority: {{ prometheus.linux.altpriority }}
- order: 10
- watch:
- archive: prometheus-package-archive-install-archive-extracted
- unless: {{ grains.os_family in ('Suse',) }}
- require:
- sls: {{ sls_archive_install }}
prometheus-package-archive-install-home-alternative-set:
alternatives.set:
- name: prometheus-home
- path: {{ prometheus.base_dir }}
- require:
- alternatives: prometheus-package-archive-install-home-alternative-install
- unless: {{ grains.os_family in ('Suse',) }}
{% for i in ['prometheus', 'promtool'] %}
prometheus-package-archive-install-{{ i }}-alternative-install:
cmd.run:
- name: update-alternatives --install /usr/bin/{{i}} link-{{i}} {{ prometheus.base_dir }}/{{i}} {{prometheus.linux.altpriority}}
- require:
- cmd: prometheus-package-archive-install-home-alternative-install
- onlyif: {{ grains.os_family in ('Suse',) }}
alternatives.install:
- name: link-{{ i }}
- link: /usr/bin/{{ i }}
- path: {{ prometheus.base_dir }}/{{ i }}
- priority: {{ prometheus.linux.altpriority }}
- order: 10
- require:
- alternatives: prometheus-package-archive-install-home-alternative-install
- unless: {{ grains.os_family in ('Suse',) }}
prometheus-package-archive-install-{{ i }}-alternative-set:
alternatives.set:
- name: link-{{ i }}
- path: {{ prometheus.base_dir }}/{{ i }}
- require:
- alternatives: prometheus-package-archive-install-{{ i }}-alternative-install
- unless: {{ grains.os_family in ('Suse',) }}
{% endfor %}
{%- endif %}
30 changes: 10 additions & 20 deletions prometheus/config/clean.sls
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,23 @@

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import golang with context %}
{%- from tplroot ~ "/map.jinja" import prometheus with context %}
{%- set sls_archive_clean = tplroot ~ '.archive.clean' %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
{%- set sls_alternatives_clean = tplroot ~ '.config.alternatives.clean' %}
{%- if grains.kernel|lower == 'linux' and golang.linux.altpriority|int > 0 %}
{%- if grains.kernel|lower == 'linux' and prometheus.linux.altpriority|int > 0 %}
include:
- {{ sls_archive_clean }}
- {{ sls_alternatives_clean }}
golang-package-archive-remove-home-alternative-remove:
alternatives.remove:
- name: golang-home
- path: {{ golang.base_dir }}/go
- onlyif: update-alternatives --get-selections |grep ^golang-home
- require:
- sls: {{ sls_archive_clean }}
{% for i in ['go', 'godoc', 'gofmt'] %}
golang-package-archive-remove-{{ i }}-alternative-remove:
alternatives.remove:
- name: link-{{ i }}
- path: {{ golang.base_dir }}/go/bin/{{ i }}
- onlyif: update-alternatives --get-selections |grep ^link-{{ i }}
prometheus-config-clean-file-absent:
file.absent:
- names:
- {{ prometheus.config_file }}
- {{ prometheus.environ_file }}
- require:
- sls: {{ sls_archive_clean }}
- sls: {{ sls_alternatives_clean }}
{% endfor %}
{%- endif %}
6 changes: 4 additions & 2 deletions prometheus/config/environ.sls
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import prometheus with context %}
{%- set sls_archive_install = tplroot ~ '.archive.install' %}
{%- set sls_package_install = tplroot ~ '.package.install' %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
include:
- {{ sls_archive_install }}
- {{ sls_archive_install if prometheus.pkg.use_upstream_archive else sls_package_install }}
prometheus-config-file-file-managed-environ_file:
file.managed:
Expand All @@ -25,4 +26,5 @@ prometheus-config-file-file-managed-environ_file:
- context:
prometheus: {{ prometheus|json }}
- require:
- sls: {{ sls_archive_install }}
- sls: {{ sls_archive_install if prometheus.pkg.use_upstream_archive else sls_package_install }}
70 changes: 4 additions & 66 deletions prometheus/config/init.sls
Original file line number Diff line number Diff line change
@@ -1,70 +1,8 @@
# -*- coding: utf-8 -*-
# vim: ft=sls

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import golang with context %}
{%- set sls_archive_install = tplroot ~ '.archive.install' %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
{%- if grains.kernel|lower == 'linux' and golang.linux.altpriority|int > 0 %}
include:
- {{ sls_archive_install }}
golang-package-archive-install-home-alternative-install:
cmd.run:
- name: update-alternatives --install {{ golang.go_root }} golang-home {{ golang.base_dir }}/go {{ golang.linux.altpriority }}
- watch:
- archive: golang-package-archive-install-archive-extracted
- require:
- sls: {{ sls_archive_install }}
- onlyif: {{ grains.os_family in ('Suse',) }}
alternatives.install:
- name: golang-home
- link: {{ golang.go_root }}
- path: {{ golang.base_dir }}/go
- priority: {{ golang.linux.altpriority }}
- order: 10
- watch:
- archive: golang-package-archive-install-archive-extracted
- unless: {{ grains.os_family in ('Suse',) }}
- require:
- sls: {{ sls_archive_install }}
golang-package-archive-install-home-alternative-set:
alternatives.set:
- name: golang-home
- path: {{ golang.base_dir }}/go
- require:
- alternatives: golang-package-archive-install-home-alternative-install
- unless: {{ grains.os_family in ('Suse',) }}
{% for i in ['go', 'godoc', 'gofmt'] %}
golang-package-archive-install-{{ i }}-alternative-install:
cmd.run:
- name: update-alternatives --install /usr/bin/{{i}} link-{{i}} {{ golang.base_dir }}/go/bin/{{i}} {{golang.linux.altpriority}}
- require:
- cmd: golang-package-archive-install-home-alternative-install
- onlyif: {{ grains.os_family in ('Suse',) }}
alternatives.install:
- name: link-{{ i }}
- link: /usr/bin/{{ i }}
- path: {{ golang.base_dir }}/go/bin/{{ i }}
- priority: {{ golang.linux.altpriority }}
- order: 10
- require:
- alternatives: golang-package-archive-install-home-alternative-install
- unless: {{ grains.os_family in ('Suse',) }}
golang-package-archive-install-{{ i }}-alternative-set:
alternatives.set:
- name: link-{{ i }}
- path: {{ golang.base_dir }}/go/bin/{{ i }}
- require:
- alternatives: golang-package-archive-install-{{ i }}-alternative-install
- unless: {{ grains.os_family in ('Suse',) }}
{% endfor %}
{%- endif %}
- .args
- .file
- .environ
- .alternatives
14 changes: 9 additions & 5 deletions prometheus/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# vim: ft=yaml
---
prometheus:
dir:
root: /opt
dir: /opt/prometheus
base_dir: /opt/prometheus-2.10.0
version: '2.10.0'
pkg:
name: prometheus
Expand All @@ -13,11 +13,11 @@ prometheus:
name: /opt
uri: https://github.com/prometheus/prometheus/releases/download/
source: None
# linux amd64 source hash
source_hash: f4233783826f18606b79e5cef0686e4a9c2030146a3c7ce134f0add09f5adcb7
source_hash: f4233783826f18606b79e5cef0686e4a9c2030146a3c7ce134f0add09f5adcb7 #linux amd64
trim_output: True
enforce_toplevel: True
archive_format: tar.gz
archive_suffix: tar.gz
archive_format: tar
rootgroup: root
kernel: {{ grains.kernel | lower }}
config_file: /etc/prometheus/prometheus.yml
Expand All @@ -33,3 +33,7 @@ prometheus:
pkg:
name: prometheus-node-exporter
service: prometheus-node-exporter

linux:
#'Alternatives system' priority: zero disables (default)
altpriority: 0
9 changes: 5 additions & 4 deletions prometheus/map.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
{#- Merge the prometheus pillar #}
{%- set prometheus = salt['pillar.get']('prometheus', default=defaults, merge=True) %}

{#- Update archive details #}
{#- archive jinja #}
{%- if prometheus.pkg.use_upstream_archive %}
{%- set name = 'prometheus-%s.%s-%s'|format(prometheus.version, prometheus.kernel, prometheus.arch) %}
{%- set uri = prometheus.pkg.archive.uri + '/v' + prometheus.version + '/' + name %}
{%- do prometheus.pkg.archive.update({
'source': prometheus.pkg.archive.uri + 'v' + prometheus.version + '/' + name
+ '.' + prometheus.pkg.archive.archive_format,
'archive_format': prometheus.pkg.archive.archive_format.split('.')[0]
'source': uri + '.' + prometheus.pkg.archive.archive_suffix,
'archive_format': prometheus.pkg.archive.archive_format
}) %}
{%- do prometheus.update({'base_dir': prometheus.pkg.archive.name + '/' + name}) %}
{%- do prometheus.environ.append('export PATH=${PATH}:' + prometheus.pkg.archive.name + '/' + name) %}
{%- endif %}


{#- Contactenate arguments #}
{%- macro concat_args(args) %}
{%- set args = args|dictsort %}
Expand Down
20 changes: 16 additions & 4 deletions prometheus/osfamilymap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,35 @@ FreeBSD:
pkg:
name: node_exporter
service: node_exporter
pkg:
archive:
source_hash: 94a63f14baeadab2f17b5ae0bbeda6688e6d06f964ef4e32c2954a0ecf3996a1

OpenBSD:
rootgroup: wheel
kernel: openbsd
pkg:
archive:
source_hash: c3c69919b359f00a84ef12f7ed6a956111790d64a71bd94990572baaf63377ce


NetBSD:
rootgroup: wheel
kernel: netbsd
pkg:
archive:
source_hash: fbfed9922eb8f1045952e0e8b93406028e123a5adb233a0ba7d4fc4e5c537b2c

Solaris: {}

Windows:
pkg:
archive:
name: C:\\Program Files
source: https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheus-2.10.0.windows-amd64.tar.gz
source_hash: eb138082a4d5e4d5b1e3ca838fa508f053474d46bca76e87ab0834f0d8b110db

MacOS:
rootgroup: {{ macos_group | d('') }}
pkg:
archive:
name: /opt
source: https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheus-2.10.0.darwin-amd64.tar.gz
source_hash: 740e36bcacc0c5d4495f5341fcfa8b7e0dc623d12e8b07ac291052ea0a681325
rootgroup: {{ macos_group | d('') }}
2 changes: 2 additions & 0 deletions prometheus/package/clean.sls
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ include:
prometheus-package-clean-pkg-removed:
pkg.removed:
- name: {{ prometheus.pkg.name }}
{%- if 'config' in prometheus and prometheus.config %}
- require:
- sls: {{ sls_config_clean }}
{%- endif %}
3 changes: 2 additions & 1 deletion prometheus/service/running.sls
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include:
prometheus-service-running-service-unmasked:
service.unmasked:
- name: {{ prometheus.service.name }}
- onlyif: systemctl >/dev/null 2>&1
- onlyif: systemctl list-unit-files | grep {{ prometheus.service.name }} >/dev/null 2>&1
prometheus-service-running-service-running:
service.running:
Expand All @@ -28,3 +28,4 @@ prometheus-service-running-service-running:
- sls: {{ sls_config_args }}
- sls: {{ sls_config_file }}
{%- endif %}
- onlyif: systemctl list-unit-files | grep {{ prometheus.service.name }} >/dev/null 2>&1
2 changes: 1 addition & 1 deletion test/integration/default/controls/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
it { should be_grouped_into 'root' }
its('mode') { should cmp '0644' }
its('content') { should include 'File managed by Salt' }
its('content') { should include 'Your changes will be overwritten.' }
its('content') { should include 'Your changes may be overwritten.' }
its('content') { should include 'global:' }
its('content') { should include 'alerting:' }
end
Expand Down
Loading

0 comments on commit 36b3e62

Please sign in to comment.