feat(linux): alternatives support & updated unit tests

This commit is contained in:
N 2019-06-16 01:33:24 +01:00
parent b074bd3dee
commit 36b3e62678
No known key found for this signature in database
GPG Key ID: 55A292EAB4E54067
16 changed files with 232 additions and 104 deletions

View File

@ -8,6 +8,7 @@ prometheus:
archive: archive:
source: https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheus-2.10.0.linux-amd64.tar.gz source: https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheus-2.10.0.linux-amd64.tar.gz
source_hash: f4233783826f18606b79e5cef0686e4a9c2030146a3c7ce134f0add09f5adcb7 source_hash: f4233783826f18606b79e5cef0686e4a9c2030146a3c7ce134f0add09f5adcb7
archive_format: tar
enforce_toplevel: False enforce_toplevel: False
config_file: /etc/prometheus/prometheus.yml config_file: /etc/prometheus/prometheus.yml
environ_file: /etc/default/prometheus.sh environ_file: /etc/default/prometheus.sh
@ -16,6 +17,10 @@ prometheus:
args: args:
web.listen-address: 0.0.0.0:9090 web.listen-address: 0.0.0.0:9090
linux:
#'Alternatives system' priority: zero disables (default)
altpriority: 0
tofs: tofs:
# The files_switch key serves as a selector for alternative # The files_switch key serves as a selector for alternative
# directories under the formula files directory. See TOFS pattern # directories under the formula files directory. See TOFS pattern

View File

@ -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 %}

View File

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
include:
- .install

View File

@ -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 %}

View File

@ -3,33 +3,23 @@
{#- Get the `tplroot` from `tpldir` #} {#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %} {%- 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' %} {%- 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: include:
- {{ sls_archive_clean }} - {{ sls_archive_clean }}
- {{ sls_alternatives_clean }}
prometheus-config-clean-file-absent:
golang-package-archive-remove-home-alternative-remove: file.absent:
alternatives.remove: - names:
- name: golang-home - {{ prometheus.config_file }}
- path: {{ golang.base_dir }}/go - {{ prometheus.environ_file }}
- onlyif: update-alternatives --get-selections |grep ^golang-home
- require: - require:
- sls: {{ sls_archive_clean }} - sls: {{ sls_archive_clean }}
- sls: {{ sls_alternatives_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 }}
- require:
- sls: {{ sls_archive_clean }}
{% endfor %}
{%- endif %} {%- endif %}

View File

@ -5,10 +5,11 @@
{%- set tplroot = tpldir.split('/')[0] %} {%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import prometheus with context %} {%- from tplroot ~ "/map.jinja" import prometheus with context %}
{%- set sls_archive_install = tplroot ~ '.archive.install' %} {%- set sls_archive_install = tplroot ~ '.archive.install' %}
{%- set sls_package_install = tplroot ~ '.package.install' %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %} {%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
include: include:
- {{ sls_archive_install }} - {{ sls_archive_install if prometheus.pkg.use_upstream_archive else sls_package_install }}
prometheus-config-file-file-managed-environ_file: prometheus-config-file-file-managed-environ_file:
file.managed: file.managed:
@ -25,4 +26,5 @@ prometheus-config-file-file-managed-environ_file:
- context: - context:
prometheus: {{ prometheus|json }} prometheus: {{ prometheus|json }}
- require: - require:
- sls: {{ sls_archive_install }} - sls: {{ sls_archive_install if prometheus.pkg.use_upstream_archive else sls_package_install }}

View File

@ -1,70 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vim: ft=sls # 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: include:
- {{ sls_archive_install }} - .args
- .file
golang-package-archive-install-home-alternative-install: - .environ
cmd.run: - .alternatives
- 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 %}

View File

@ -2,8 +2,8 @@
# vim: ft=yaml # vim: ft=yaml
--- ---
prometheus: prometheus:
dir: dir: /opt/prometheus
root: /opt base_dir: /opt/prometheus-2.10.0
version: '2.10.0' version: '2.10.0'
pkg: pkg:
name: prometheus name: prometheus
@ -13,11 +13,11 @@ prometheus:
name: /opt name: /opt
uri: https://github.com/prometheus/prometheus/releases/download/ uri: https://github.com/prometheus/prometheus/releases/download/
source: None source: None
# linux amd64 source hash source_hash: f4233783826f18606b79e5cef0686e4a9c2030146a3c7ce134f0add09f5adcb7 #linux amd64
source_hash: f4233783826f18606b79e5cef0686e4a9c2030146a3c7ce134f0add09f5adcb7
trim_output: True trim_output: True
enforce_toplevel: True enforce_toplevel: True
archive_format: tar.gz archive_suffix: tar.gz
archive_format: tar
rootgroup: root rootgroup: root
kernel: {{ grains.kernel | lower }} kernel: {{ grains.kernel | lower }}
config_file: /etc/prometheus/prometheus.yml config_file: /etc/prometheus/prometheus.yml
@ -33,3 +33,7 @@ prometheus:
pkg: pkg:
name: prometheus-node-exporter name: prometheus-node-exporter
service: prometheus-node-exporter service: prometheus-node-exporter
linux:
#'Alternatives system' priority: zero disables (default)
altpriority: 0

View File

@ -26,18 +26,19 @@
{#- Merge the prometheus pillar #} {#- Merge the prometheus pillar #}
{%- set prometheus = salt['pillar.get']('prometheus', default=defaults, merge=True) %} {%- set prometheus = salt['pillar.get']('prometheus', default=defaults, merge=True) %}
{#- Update archive details #} {#- archive jinja #}
{%- if prometheus.pkg.use_upstream_archive %} {%- if prometheus.pkg.use_upstream_archive %}
{%- set name = 'prometheus-%s.%s-%s'|format(prometheus.version, prometheus.kernel, prometheus.arch) %} {%- 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({ {%- do prometheus.pkg.archive.update({
'source': prometheus.pkg.archive.uri + 'v' + prometheus.version + '/' + name 'source': uri + '.' + prometheus.pkg.archive.archive_suffix,
+ '.' + prometheus.pkg.archive.archive_format, 'archive_format': prometheus.pkg.archive.archive_format
'archive_format': prometheus.pkg.archive.archive_format.split('.')[0]
}) %} }) %}
{%- do prometheus.update({'base_dir': prometheus.pkg.archive.name + '/' + name}) %} {%- do prometheus.update({'base_dir': prometheus.pkg.archive.name + '/' + name}) %}
{%- do prometheus.environ.append('export PATH=${PATH}:' + prometheus.pkg.archive.name + '/' + name) %} {%- do prometheus.environ.append('export PATH=${PATH}:' + prometheus.pkg.archive.name + '/' + name) %}
{%- endif %} {%- endif %}
{#- Contactenate arguments #} {#- Contactenate arguments #}
{%- macro concat_args(args) %} {%- macro concat_args(args) %}
{%- set args = args|dictsort %} {%- set args = args|dictsort %}

View File

@ -38,9 +38,24 @@ FreeBSD:
pkg: pkg:
name: node_exporter name: node_exporter
service: node_exporter service: node_exporter
pkg:
archive:
source_hash: 94a63f14baeadab2f17b5ae0bbeda6688e6d06f964ef4e32c2954a0ecf3996a1
OpenBSD: OpenBSD:
rootgroup: wheel rootgroup: wheel
kernel: openbsd
pkg:
archive:
source_hash: c3c69919b359f00a84ef12f7ed6a956111790d64a71bd94990572baaf63377ce
NetBSD:
rootgroup: wheel
kernel: netbsd
pkg:
archive:
source_hash: fbfed9922eb8f1045952e0e8b93406028e123a5adb233a0ba7d4fc4e5c537b2c
Solaris: {} Solaris: {}
@ -48,13 +63,10 @@ Windows:
pkg: pkg:
archive: archive:
name: C:\\Program Files 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 source_hash: eb138082a4d5e4d5b1e3ca838fa508f053474d46bca76e87ab0834f0d8b110db
MacOS: MacOS:
rootgroup: {{ macos_group | d('') }}
pkg: pkg:
archive: 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 source_hash: 740e36bcacc0c5d4495f5341fcfa8b7e0dc623d12e8b07ac291052ea0a681325
rootgroup: {{ macos_group | d('') }}

View File

@ -12,5 +12,7 @@ include:
prometheus-package-clean-pkg-removed: prometheus-package-clean-pkg-removed:
pkg.removed: pkg.removed:
- name: {{ prometheus.pkg.name }} - name: {{ prometheus.pkg.name }}
{%- if 'config' in prometheus and prometheus.config %}
- require: - require:
- sls: {{ sls_config_clean }} - sls: {{ sls_config_clean }}
{%- endif %}

View File

@ -14,7 +14,7 @@ include:
prometheus-service-running-service-unmasked: prometheus-service-running-service-unmasked:
service.unmasked: service.unmasked:
- name: {{ prometheus.service.name }} - 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: prometheus-service-running-service-running:
service.running: service.running:
@ -28,3 +28,4 @@ prometheus-service-running-service-running:
- sls: {{ sls_config_args }} - sls: {{ sls_config_args }}
- sls: {{ sls_config_file }} - sls: {{ sls_config_file }}
{%- endif %} {%- endif %}
- onlyif: systemctl list-unit-files | grep {{ prometheus.service.name }} >/dev/null 2>&1

View File

@ -7,7 +7,7 @@ control 'Prometheus configuration' do
it { should be_grouped_into 'root' } it { should be_grouped_into 'root' }
its('mode') { should cmp '0644' } its('mode') { should cmp '0644' }
its('content') { should include 'File managed by Salt' } 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 'global:' }
its('content') { should include 'alerting:' } its('content') { should include 'alerting:' }
end end

View File

@ -1,4 +1,4 @@
control 'template configuration environment' do control 'prometheus configuration environment' do
title 'should match desired lines' title 'should match desired lines'
describe file('/etc/default/prometheus.sh') do describe file('/etc/default/prometheus.sh') do

View File

@ -0,0 +1,36 @@
title 'linux alternatives profile'
control 'prometheus linux alternatives' do
impact 1.0
title 'should be installed'
desc "Ensure prometheus linux alternatives are correct"
tag: package: "tarball archive"
describe file('/opt/prometheus') do # prometheus-home alternative
it { should be_symlink }
it { should_not be_file }
it { should_not be_directory }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('shallow_link_path') { should eq '/etc/alternatives/prometheus-home' }
end
describe file('/usr/bin/prometheus') do # prometheus alternative
it { should be_symlink }
it { should_not be_file }
it { should_not be_directory }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('shallow_link_path') { should eq '/etc/alternatives/link-prometheus' }
end
describe file('/usr/bin/promtool') do # promtool alternative
it { should be_symlink }
it { should_not be_file }
it { should_not be_directory }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('shallow_link_path') { should eq '/etc/alternatives/link-promtool' }
end
end

View File

@ -0,0 +1,28 @@
title 'prometheus archives profile'
control 'prometheus tarball archive' do
impact 1.0
title 'should be installed'
desc "Ensure prometheus tarball archive was extracted correctly"
describe file('/opt/prometheus-2.10.0.linux-amd64') do
it { should be_directory }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('mode') { should cmp '0755' }
end
require 'digest'
binary = file('/opt/prometheus-2.10.0.linux-amd64/prometheus').content
sha256sum = Digest::SHA256.hexdigest(binary)
describe file('/opt/prometheus-2.10.0.linux-amd64/prometheus') do
its('sha256sum') { should eq '025a7bb0327e1b2b20efbd6e66b8ef81d9020c66f0d5d077b1388a48dec789f7' }
end
binary = file('/opt/prometheus-2.10.0.linux-amd64/promtool').content
sha256sum = Digest::SHA256.hexdigest(binary)
describe file('/opt/prometheus-2.10.0.linux-amd64/promtool') do
its('sha256sum') { should eq 'db004c3c0d6a863929a51da5e1fc4a958668e80256ea2a14c5e461fa13656def' }
end
end