From 36b3e6267860eb3a00fb4c88956bd8b0c29e94f2 Mon Sep 17 00:00:00 2001 From: N Date: Sun, 16 Jun 2019 01:33:24 +0100 Subject: [PATCH] feat(linux): alternatives support & updated unit tests --- pillar.example | 5 ++ prometheus/config/alternatives/clean.sls | 34 +++++++++ prometheus/config/alternatives/init.sls | 5 ++ prometheus/config/alternatives/install.sls | 70 +++++++++++++++++++ prometheus/config/clean.sls | 30 +++----- prometheus/config/environ.sls | 6 +- prometheus/config/init.sls | 70 ++----------------- prometheus/defaults.yaml | 14 ++-- prometheus/map.jinja | 9 +-- prometheus/osfamilymap.yaml | 20 ++++-- prometheus/package/clean.sls | 2 + prometheus/service/running.sls | 3 +- .../default/controls/config_spec.rb | 2 +- .../default/controls/environ_spec.rb | 2 +- .../default/todo/alternatives_spec.rb | 36 ++++++++++ .../integration/default/todo/archives_spec.rb | 28 ++++++++ 16 files changed, 232 insertions(+), 104 deletions(-) create mode 100644 prometheus/config/alternatives/clean.sls create mode 100644 prometheus/config/alternatives/init.sls create mode 100644 prometheus/config/alternatives/install.sls create mode 100644 test/integration/default/todo/alternatives_spec.rb create mode 100644 test/integration/default/todo/archives_spec.rb diff --git a/pillar.example b/pillar.example index fef22b0..3b01a01 100644 --- a/pillar.example +++ b/pillar.example @@ -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 @@ -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 diff --git a/prometheus/config/alternatives/clean.sls b/prometheus/config/alternatives/clean.sls new file mode 100644 index 0000000..315ca73 --- /dev/null +++ b/prometheus/config/alternatives/clean.sls @@ -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 %} diff --git a/prometheus/config/alternatives/init.sls b/prometheus/config/alternatives/init.sls new file mode 100644 index 0000000..d3e5518 --- /dev/null +++ b/prometheus/config/alternatives/init.sls @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +include: + - .install diff --git a/prometheus/config/alternatives/install.sls b/prometheus/config/alternatives/install.sls new file mode 100644 index 0000000..b349b2f --- /dev/null +++ b/prometheus/config/alternatives/install.sls @@ -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 %} diff --git a/prometheus/config/clean.sls b/prometheus/config/clean.sls index e178cbe..f20270a 100644 --- a/prometheus/config/clean.sls +++ b/prometheus/config/clean.sls @@ -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 +prometheus-config-clean-file-absent: + file.absent: + - names: + - {{ prometheus.config_file }} + - {{ prometheus.environ_file }} - require: - 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 %} diff --git a/prometheus/config/environ.sls b/prometheus/config/environ.sls index 76cec27..91d065e 100644 --- a/prometheus/config/environ.sls +++ b/prometheus/config/environ.sls @@ -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: @@ -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 }} + diff --git a/prometheus/config/init.sls b/prometheus/config/init.sls index c2c6196..6ce6124 100644 --- a/prometheus/config/init.sls +++ b/prometheus/config/init.sls @@ -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 diff --git a/prometheus/defaults.yaml b/prometheus/defaults.yaml index f6291fe..939a096 100644 --- a/prometheus/defaults.yaml +++ b/prometheus/defaults.yaml @@ -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 @@ -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 @@ -33,3 +33,7 @@ prometheus: pkg: name: prometheus-node-exporter service: prometheus-node-exporter + + linux: + #'Alternatives system' priority: zero disables (default) + altpriority: 0 diff --git a/prometheus/map.jinja b/prometheus/map.jinja index 04f386a..7955fa6 100644 --- a/prometheus/map.jinja +++ b/prometheus/map.jinja @@ -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 %} diff --git a/prometheus/osfamilymap.yaml b/prometheus/osfamilymap.yaml index 94ffa6e..8d49ad5 100644 --- a/prometheus/osfamilymap.yaml +++ b/prometheus/osfamilymap.yaml @@ -38,9 +38,24 @@ 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: {} @@ -48,13 +63,10 @@ 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('') }} diff --git a/prometheus/package/clean.sls b/prometheus/package/clean.sls index 30a2b26..04c2cfd 100644 --- a/prometheus/package/clean.sls +++ b/prometheus/package/clean.sls @@ -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 %} diff --git a/prometheus/service/running.sls b/prometheus/service/running.sls index 6f07ef1..a64f9fa 100644 --- a/prometheus/service/running.sls +++ b/prometheus/service/running.sls @@ -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: @@ -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 diff --git a/test/integration/default/controls/config_spec.rb b/test/integration/default/controls/config_spec.rb index 34d0f8f..1b93ca9 100644 --- a/test/integration/default/controls/config_spec.rb +++ b/test/integration/default/controls/config_spec.rb @@ -7,7 +7,7 @@ control 'Prometheus configuration' do 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 diff --git a/test/integration/default/controls/environ_spec.rb b/test/integration/default/controls/environ_spec.rb index 6b0ec5f..79bd781 100644 --- a/test/integration/default/controls/environ_spec.rb +++ b/test/integration/default/controls/environ_spec.rb @@ -1,4 +1,4 @@ -control 'template configuration environment' do +control 'prometheus configuration environment' do title 'should match desired lines' describe file('/etc/default/prometheus.sh') do diff --git a/test/integration/default/todo/alternatives_spec.rb b/test/integration/default/todo/alternatives_spec.rb new file mode 100644 index 0000000..03c340f --- /dev/null +++ b/test/integration/default/todo/alternatives_spec.rb @@ -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 diff --git a/test/integration/default/todo/archives_spec.rb b/test/integration/default/todo/archives_spec.rb new file mode 100644 index 0000000..8cd1e11 --- /dev/null +++ b/test/integration/default/todo/archives_spec.rb @@ -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