Skip to content

Commit

Permalink
Merge pull request #5 from noelmcloughlin/archives
Browse files Browse the repository at this point in the history
feat(archives): introduce support for archive files
  • Loading branch information
noelmcloughlin authored Jun 23, 2019
2 parents bf6b17e + 4447f32 commit 9aa0f6e
Show file tree
Hide file tree
Showing 26 changed files with 457 additions and 32 deletions.
11 changes: 11 additions & 0 deletions docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ starts the associated grafana service.

This state will install the grafana package only.

``grafana.archive``
^^^^^^^^^^^^^^^^^^^

This state will install the grafana software from archive file only.

``grafana.config``
^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -91,3 +96,9 @@ dependency on ``grafana.service.clean`` via include list.

This state will remove the grafana package and has a depency on
``grafana.config.clean`` via include list.

``grafana.archive.clean``
^^^^^^^^^^^^^^^^^^^^^^^^^

This state will uninstall grafana software from archive extracted directory.

11 changes: 11 additions & 0 deletions grafana/archive/clean.sls
Original file line number Diff line number Diff line change
@@ -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 grafana with context %}
grafana-cli-package-archive-clean-file-absent:
file.absent:
- names:
- {{ grafana.pkg.archive.name }}
5 changes: 5 additions & 0 deletions grafana/archive/init.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#.-*- coding: utf-8 -*-
# vim: ft=sls

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

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import grafana with context %}
{%- from tplroot ~ "/jinja/macros.jinja" import format_kwargs with context %}
{%- if grafana.pkg.use_upstream_archive %}
grafana-package-archive-install-file-directory:
file.directory:
- name: {{ grafana.pkg.archive.name }}
- user: root
- group: root
- mode: 755
- makedirs: True
- require_in:
- archive: grafana-package-archive-install-archive-extracted
- recurse:
- user
- group
- mode
grafana-package-archive-install-archive-extracted:
archive.extracted:
{{- format_kwargs(grafana.pkg.archive) }}
- retry:
attempts: 3
until: True
interval: 60
splay: 10
- user: root
- group: root
- recurse:
- user
- group
{%- endif %}
1 change: 1 addition & 0 deletions grafana/clean.sls
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ include:
- .service.clean
- .config.clean
- .package.clean
- .archive.clean
34 changes: 34 additions & 0 deletions grafana/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 grafana with context %}
{%- set sls_archive_clean = tplroot ~ '.archive.clean' %}
{%- set sls_package_clean = tplroot ~ '.package.clean' %}
{%- if grains.kernel|lower == 'linux' and grafana.linux.altpriority|int > 0 %}
include:
- {{ sls_archive_clean if grafana.pkg.use_upstream_archive else sls_package_clean }}
grafana-package-archive-remove-home-alternative-remove:
alternatives.remove:
- name: grafana-home
- path: {{ grafana.pkg.archive.name }}
- onlyif: update-alternatives --get-selections |grep ^grafana-home
- require:
- sls: {{ sls_archive_clean if grafana.pkg.use_upstream_archive else sls_package_clean }}
{% for i in ['grafana-cli', 'grafana-server'] %}
grafana-package-archive-remove-{{ i }}-alternative-remove:
alternatives.remove:
- name: link-{{ i }}
- path: {{ grafana.pkg.archive.name }}/bin/{{ i }}
- onlyif: update-alternatives --get-selections |grep ^link-{{ i }}
- require:
- sls: {{ sls_archive_clean if grafana.pkg.use_upstream_archive else sls_package_clean }}
{% endfor %}
{%- endif %}
5 changes: 5 additions & 0 deletions grafana/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
71 changes: 71 additions & 0 deletions grafana/config/alternatives/install.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
# vim: ft=sls

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

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_service_clean = tplroot ~ '.service.clean' %}
{%- from tplroot ~ "/map.jinja" import grafana with context %}
{%- set sls_archive_clean = tplroot ~ '.archive.clean' %}
{%- set sls_package_clean = tplroot ~ '.package.clean' %}
{%- set sls_service_clean = tplroot ~ '.service.clean' %}
{%- set sls_alternatives_clean = tplroot ~ '.config.alternatives.clean' %}
{%- if grains.kernel|lower == 'linux' and grafana.linux.altpriority|int > 0 %}
include:
- {{ sls_service_clean }}
- {{ sls_archive_clean if grafana.pkg.use_upstream_archive else sls_package_clean }}
- {{ sls_alternatives_clean }}
grafana-config-clean-file-absent:
file.absent:
- name: {{ grafana.config_file }}
- names:
- {{ grafana.config_file }}
- {{ grafana.environ_file }}
- require:
- sls: {{ sls_service_clean }}
- sls: {{ sls_archive_clean if grafana.pkg.use_upstream_archive else sls_package_clean }}
- sls: {{ sls_alternatives_clean }}
{%- endif %}
29 changes: 29 additions & 0 deletions grafana/config/environ.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# vim: ft=sls

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_archive_install = tplroot ~ '.archive.install' %}
{%- set sls_package_install = tplroot ~ '.package.install' %}
{%- from tplroot ~ "/map.jinja" import grafana with context %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
include:
- {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }}
grafana-config-file-file-managed-environ_file:
file.managed:
- name: {{ grafana.environ_file }}
- source: {{ files_switch(['grafana.sh.jinja'],
lookup='grafana-config-file-file-managed-environ_file'
)
}}
- mode: 640
- user: root
- group: {{ grafana.rootgroup if grafana.pkg.use_upstream_archive else grafana.group }}
- makedirs: True
- template: jinja
- context:
config: {{ grafana.environ|json }}
- require:
- sls: {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }}
15 changes: 12 additions & 3 deletions grafana/config/file.sls
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_package_install = tplroot ~ '.package.install' %}
{%- from tplroot ~ "/map.jinja" import grafana with context %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
{%- if 'config' in grafana and grafana.config %}
{%- if grafana.pkg.use_upstream_archive %}
{%- set sls_package_install = tplroot ~ '.archive.install' %}
{%- else %}
{%- set sls_package_install = tplroot ~ '.package.install' %}
{%- endif %}
include:
- {{ sls_package_install }}
grafana-config-file-file-managed:
grafana-config-file-file-managed-config_file:
file.managed:
- name: {{ grafana.config_file }}
- source: {{ files_switch(['grafana.ini.jinja'],
lookup='grafana-config-file-file-managed'
lookup='grafana-config-file-file-managed-config_file'
)
}}
- mode: 640
- user: root
- group: {{ grafana.group }}
- group: {{ grafana.rootgroup if grafana.pkg.use_upstream_archive else grafana.group }}
- makedirs: True
- template: jinja
- context:
config: {{ grafana.config|json }}
- require:
- sls: {{ sls_package_install }}
{%- endif %}
2 changes: 2 additions & 0 deletions grafana/config/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

include:
- .file
- .environ
- .alternatives
23 changes: 23 additions & 0 deletions grafana/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
# vim: ft=yaml
---
grafana:
dir: /opt/grafana
version: '6.2.2'

pkg:
name: grafana
binary: grafana
use_upstream_archive: False
use_upstream_repo: False
repo:
humanname: grafana
Expand All @@ -12,11 +17,29 @@ grafana:
- installed by salt
enabled: 1
gpgcheck: 1
archive:
name: /opt
uri: https://dl.grafana.com/oss/release
source: None
source_hash: None
trim_output: True {# works in 2018.3.2. onwards #}
archive_suffix: tar.gz
archive_format: tar
enforce_toplevel: False # needed for grafana
options: '--strip-components=1' # needed for grafana

kernel: {{ grains.kernel | lower }}
rootgroup: root
group: grafana
config_file: /etc/grafana/grafana.ini
config: {}
environ_file: /etc/default/grafana.sh
environ: []
service:
name: grafana-server
user: grafana
group: grafana

linux:
#'Alternatives system' priority: zero disables (default)
altpriority: 0
8 changes: 8 additions & 0 deletions grafana/files/default/grafana.sh.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
########################################################################
# File managed by Salt at <{{ source }}>.
# Your changes will be overwritten.
########################################################################

{%- for item in config %}
{{ item }}
{%- endfor %}
6 changes: 5 additions & 1 deletion grafana/init.sls
Original file line number Diff line number Diff line change
@@ -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 grafana with context %}
include:
- .package
- {{ '.archive' if grafana.pkg.use_upstream_archive else '.package' }}
- .config
- .service
2 changes: 0 additions & 2 deletions grafana/jinja/macros.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
# Collection of common macros

{%- macro format_kwargs(kwarg) -%}

{%- filter indent(4) %}
{%- for k, v in kwarg|dictsort() %}
- {{ k }}: {{ v }}
{%- endfor %}
{%- endfilter %}

{%- endmacro %}
Loading

0 comments on commit 9aa0f6e

Please sign in to comment.