Skip to content

Commit

Permalink
feat(archives): support for archives file format
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the parameter `pkg` is now a dictionary. References
 to `prometheus.pkg` should be changed to `prometheus.pkg.name`.
  • Loading branch information
noelmcloughlin committed Jun 15, 2019
1 parent ea84c92 commit 1f86f4a
Show file tree
Hide file tree
Showing 23 changed files with 236 additions and 17 deletions.
20 changes: 20 additions & 0 deletions docs/README.rst
Expand Up @@ -48,11 +48,21 @@ This installs the prometheus package,
manages the prometheus configuration file and then
starts the associated prometheus service.

``prometheus.archive``
^^^^^^^^^^^^^^^^^^^^

This state will install the prometheus from archive file only.

``prometheus.package``
^^^^^^^^^^^^^^^^^^^^

This state will install the prometheus package only.

``prometheus.package.repo``
^^^^^^^^^^^^^^^^^^^^^^^^^

This state will install the prometheus package only.

``prometheus.config``
^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -92,6 +102,16 @@ dependency on ``prometheus.service.clean`` via include list.
This state will remove the prometheus package and has a depency on
``prometheus.config.clean`` via include list.

``prometheus.package.archive.clean``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This state will uninstall the prometheus archive-extracted directory only.

``prometheus.package.repo.clean``
^^^^^^^^^^^^^^^^^^^^^^^^^

This state will uninstall the prometheus upstream package repository only.

``prometheus.exporters``
^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
10 changes: 9 additions & 1 deletion pillar.example
Expand Up @@ -2,8 +2,14 @@
# vim: ft=yaml
---
prometheus:
pkg: prometheus
pkg:
name: prometheus
archive:
source: https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheus-2.10.0.linux-amd64.tar.gz
source_hash: f4233783826f18606b79e5cef0686e4a9c2030146a3c7ce134f0add09f5adcb7
enforce_toplevel: False
config_file: /etc/prometheus/prometheus.yml
environ_file: /etc/default/prometheus.sh
service:
name: prometheus
args:
Expand Down Expand Up @@ -33,6 +39,8 @@ prometheus:
# - 'example_alt.tmpl.jinja'

# Pillar-based config
environ:
- 'export PATH=${PATH}:/opt/prometheus-2.10.0.linux-amd64'
config:
# my global config
global:
Expand Down
11 changes: 11 additions & 0 deletions prometheus/archive/clean.sls
@@ -0,0 +1,11 @@
# -*- 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-cli-package-archive-clean-file-absent:
file.absent:
- names:
- {{ prometheus.base_dir }}
5 changes: 5 additions & 0 deletions prometheus/archive/init.sls
@@ -0,0 +1,5 @@
#.-*- coding: utf-8 -*-
# vim: ft=sls

include:
- .install
23 changes: 23 additions & 0 deletions prometheus/archive/install.sls
@@ -0,0 +1,23 @@
# -*- 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 ~ "/jinja/macros.jinja" import format_kwargs with context %}
prometheus-package-archive-install-file-directory:
file.directory:
- name: {{ prometheus.pkg.archive.name }}
- makedirs: True
- require_in:
- archive: prometheus-package-archive-install-archive-extracted
prometheus-package-archive-install-archive-extracted:
archive.extracted:
{{- format_kwargs(prometheus.pkg.archive) }}
- retry:
attempts: 3
until: True
interval: 60
splay: 10
1 change: 1 addition & 0 deletions prometheus/clean.sls
Expand Up @@ -4,4 +4,5 @@
include:
- .service.clean
- .config.clean
- .archive.clean
- .package.clean
4 changes: 3 additions & 1 deletion prometheus/config/clean.sls
Expand Up @@ -11,7 +11,9 @@ include:
prometheus-config-clean-file-absent:
file.absent:
- name: {{ prometheus.config_file }}
- names:
- {{ prometheus.config_file }}
- {{ prometheus.environ_file }}
- require:
- sls: {{ sls_service_clean }}
Expand Down
28 changes: 28 additions & 0 deletions prometheus/config/environ.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 %}
{%- set sls_archive_install = tplroot ~ '.archive.install' %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
include:
- {{ sls_archive_install }}
prometheus-config-file-file-managed-environ_file:
file.managed:
- name: {{ prometheus.environ_file }}
- source: {{ files_switch(['prometheus.sh.jinja'],
lookup='prometheus-config-file-file-managed-environ_file'
)
}}
- mode: 644
- user: root
- group: {{ prometheus.rootgroup }}
- makedirs: True
- template: jinja
- context:
prometheus: {{ prometheus|json }}
- require:
- sls: {{ sls_archive_install }}
14 changes: 11 additions & 3 deletions prometheus/config/file.sls
Expand Up @@ -3,18 +3,24 @@

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_package_install = tplroot ~ '.package.install' %}
{%- from tplroot ~ "/map.jinja" import prometheus with context %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
{%- if 'config' in prometheus and prometheus.config %}
{%- if prometheus.pkg.use_upstream_archive %}
{%- set sls_package_install = tplroot ~ '.archive.install' %}
{%- else %}
{%- set sls_package_install = tplroot ~ '.package.install' %}
{%- endif %}
include:
- {{ sls_package_install }}
prometheus-config-file-file-managed:
prometheus-config-file-file-managed-config_file:
file.managed:
- name: {{ prometheus.config_file }}
- source: {{ files_switch(['prometheus.yml.jinja'],
lookup='prometheus-config-file-file-managed'
lookup='prometheus-config-file-file-managed-config_file'
)
}}
- mode: 644
Expand All @@ -26,3 +32,5 @@ prometheus-config-file-file-managed:
config: {{ prometheus.config|json }}
- require:
- sls: {{ sls_package_install }}
{%- endif %}
1 change: 1 addition & 0 deletions prometheus/config/init.sls
Expand Up @@ -4,3 +4,4 @@
include:
- .args
- .file
- .environ
21 changes: 19 additions & 2 deletions prometheus/defaults.yaml
Expand Up @@ -2,15 +2,32 @@
# vim: ft=yaml
---
prometheus:
pkg: prometheus
version: '2.10.0'
pkg:
name: prometheus
binary: prometheus
use_upstream_archive: False
archive:
name: /opt
uri: https://github.com/prometheus/prometheus/releases/download/
source: None
# linux amd64 source hash
source_hash: f4233783826f18606b79e5cef0686e4a9c2030146a3c7ce134f0add09f5adcb7
trim_output: True
enforce_toplevel: True
archive_format: tar.gz
rootgroup: root
kernel: {{ grains.kernel | lower }}
config_file: /etc/prometheus/prometheus.yml
config: {}
environ_file: /etc/default/prometheus.sh
environ: []
service:
name: prometheus
user: prometheus
group: prometheus
exporters:
node:
pkg: prometheus-node-exporter
pkg:
name: prometheus-node-exporter
service: prometheus-node-exporter
2 changes: 1 addition & 1 deletion prometheus/exporters/node/clean.sls
Expand Up @@ -12,7 +12,7 @@ prometheus-exporters-node-service-dead:
prometheus-exporters-node-pkg-removed:
pkg.removed:
- name: {{ prometheus.exporters.node.pkg }}
- name: {{ prometheus.exporters.node.pkg.name }}
- require:
- service: prometheus-exporters-node-service-dead
Expand Down
2 changes: 1 addition & 1 deletion prometheus/exporters/node/init.sls
Expand Up @@ -8,7 +8,7 @@
prometheus-exporters-node-pkg-installed:
pkg.installed:
- name: {{ prometheus.exporters.node.pkg }}
- name: {{ prometheus.exporters.node.pkg.name }}
{%- if 'args' in prometheus.exporters.node %}
{%- set args = prometheus.exporters.node.get('args', {}) -%}
Expand Down
10 changes: 10 additions & 0 deletions prometheus/files/default/prometheus.sh.jinja
@@ -0,0 +1,10 @@
########################################################################
# File managed by Salt at <{{ source }}>.
# Your changes may be overwritten.
########################################################################

{%- if prometheus.environ %}
{%- for item in prometheus.environ %}
{{ item }}
{%- endfor %}
{%- endif %}
2 changes: 1 addition & 1 deletion prometheus/files/default/prometheus.yml.jinja
@@ -1,6 +1,6 @@
########################################################################
# File managed by Salt at <{{ source }}>.
# Your changes will be overwritten.
# Your changes may be overwritten.
########################################################################

{{ config|yaml(False) }}
6 changes: 5 additions & 1 deletion prometheus/init.sls
@@ -1,7 +1,11 @@
# -*- coding: utf-8 -*-
# vim: ft=sls

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import prometheus with context %}
include:
- .package
- {{ '.archive' if prometheus.pkg.use_upstream_archive else '.package' }}
- .config
- .service
12 changes: 12 additions & 0 deletions prometheus/jinja/macros.jinja
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
# vim: ft=jinja
#
# Collection of common macros

{%- macro format_kwargs(kwarg) -%}
{%- filter indent(4) %}
{%- for k, v in kwarg|dictsort() %}
- {{ k }}: {{ v }}
{%- endfor %}
{%- endfilter %}
{%- endmacro %}
17 changes: 16 additions & 1 deletion prometheus/map.jinja
Expand Up @@ -7,14 +7,17 @@
{%- import_yaml tplroot ~ "/defaults.yaml" as default_settings %}
{%- import_yaml tplroot ~ "/osfamilymap.yaml" as osfamilymap %}
{%- import_yaml tplroot ~ "/osmap.yaml" as osmap %}
{%- import_yaml tplroot ~ "/osarchmap.yaml" as osarchmap %}
{%- import_yaml tplroot ~ "/osfingermap.yaml" as osfingermap %}

{%- set defaults = salt['grains.filter_by'](default_settings,
default='prometheus',
merge=salt['grains.filter_by'](osfamilymap, grain='os_family',
merge=salt['grains.filter_by'](osmap, grain='os',
merge=salt['grains.filter_by'](osfingermap, grain='osfinger',
merge=salt['pillar.get']('prometheus:lookup', default={})
merge=salt['grains.filter_by'](osarchmap, grain='osarch',
merge=salt['pillar.get']('prometheus:lookup', default={})
)
)
)
)
Expand All @@ -23,6 +26,18 @@
{#- Merge the prometheus pillar #}
{%- set prometheus = salt['pillar.get']('prometheus', default=defaults, merge=True) %}

{#- Update archive details #}
{%- if prometheus.pkg.use_upstream_archive %}
{%- set name = 'prometheus-%s.%s-%s'|format(prometheus.version, prometheus.kernel, prometheus.arch) %}
{%- 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]
}) %}
{%- 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
35 changes: 35 additions & 0 deletions prometheus/osarchmap.yaml
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# vim: ft=yaml
#
# Setup variables using grains['osarch'] based logic.
# You just need to add the key:values for an `osarch` that differ
# from `defaults.yaml` + `os_family.yaml`.
# Only add an `osarch` which is/will be supported by the formula
#
# If you do not need to provide defaults via the `osarch` grain,
# you will need to provide at least an empty dict in this file, e.g.
# osarch: {}
---
amd64:
arch: amd64

x86_64:
arch: amd64

386:
arch: 386

arm64:
arch: arm64

armv6l:
arch: armv6l

armv7l:
arch: armv6l

ppc64le:
arch: ppc64le

s390x:
arch: s390x
15 changes: 13 additions & 2 deletions prometheus/osfamilymap.yaml
Expand Up @@ -35,15 +35,26 @@ FreeBSD:
config_file: /usr/local/etc/prometheus.yml
exporters:
node:
pkg: node_exporter
pkg:
name: node_exporter
service: node_exporter

OpenBSD:
rootgroup: wheel

Solaris: {}

Windows: {}
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:
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: 1 addition & 1 deletion prometheus/package/clean.sls
Expand Up @@ -11,6 +11,6 @@ include:
prometheus-package-clean-pkg-removed:
pkg.removed:
- name: {{ prometheus.pkg }}
- name: {{ prometheus.pkg.name }}
- require:
- sls: {{ sls_config_clean }}

0 comments on commit 1f86f4a

Please sign in to comment.