From e7a1523665ac5e420a65abadef1c2269851ebac1 Mon Sep 17 00:00:00 2001 From: N Date: Sun, 9 Jun 2019 17:24:57 +0100 Subject: [PATCH 1/4] feat(archives): introduce support for archive files BREAKING CHANGE: the parameter `pkg` is now a dictionary. References to `template.pkg` should be changed to `template.pkg.name`. --- docs/README.rst | 11 +++++++++ grafana/archive/clean.sls | 12 +++++++++ grafana/archive/init.sls | 5 ++++ grafana/archive/install.sls | 18 ++++++++++++++ grafana/clean.sls | 1 + grafana/config/clean.sls | 4 ++- grafana/config/environ.sls | 34 ++++++++++++++++++++++++++ grafana/config/file.sls | 14 ++++++++--- grafana/config/init.sls | 1 + grafana/defaults.yaml | 16 ++++++++++-- grafana/files/default/grafana.sh.jinja | 8 ++++++ grafana/init.sls | 6 ++++- grafana/osfamilymap.yaml | 16 ++++++++++-- grafana/package/clean.sls | 2 +- grafana/package/install.sls | 2 +- grafana/service/running.sls | 4 ++- pillar.example | 6 ++++- 17 files changed, 147 insertions(+), 13 deletions(-) create mode 100644 grafana/archive/clean.sls create mode 100644 grafana/archive/init.sls create mode 100644 grafana/archive/install.sls create mode 100644 grafana/config/environ.sls create mode 100644 grafana/files/default/grafana.sh.jinja diff --git a/docs/README.rst b/docs/README.rst index 129201b..f62289b 100644 --- a/docs/README.rst +++ b/docs/README.rst @@ -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`` ^^^^^^^^^^^^^^^^^^^ @@ -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. + diff --git a/grafana/archive/clean.sls b/grafana/archive/clean.sls new file mode 100644 index 0000000..630d0ea --- /dev/null +++ b/grafana/archive/clean.sls @@ -0,0 +1,12 @@ +# -*- 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 }}/grafana-6.2.2.darwin-amd64 + - {{ grafana.pkg.archive.name }}/grafana-6.2.2.linux-amd64 diff --git a/grafana/archive/init.sls b/grafana/archive/init.sls new file mode 100644 index 0000000..051d698 --- /dev/null +++ b/grafana/archive/init.sls @@ -0,0 +1,5 @@ +#.-*- coding: utf-8 -*- +# vim: ft=sls + +include: + - .install diff --git a/grafana/archive/install.sls b/grafana/archive/install.sls new file mode 100644 index 0000000..ddf6872 --- /dev/null +++ b/grafana/archive/install.sls @@ -0,0 +1,18 @@ +# -*- 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 %} + +grafana-package-archive-install-file-directory: + file.directory: + - name: {{ grafana.pkg.archive.name }} + - makedirs: True + - require_in: + - archive: grafana-package-archive-install-archive-extracted + +grafana-package-archive-install-archive-extracted: + archive.extracted: + {{- format_kwargs(grafana.pkg.archive) }} diff --git a/grafana/clean.sls b/grafana/clean.sls index 2cefe7d..19c3c2e 100644 --- a/grafana/clean.sls +++ b/grafana/clean.sls @@ -5,3 +5,4 @@ include: - .service.clean - .config.clean - .package.clean + - .archive.clean diff --git a/grafana/config/clean.sls b/grafana/config/clean.sls index be49519..b2e9aaa 100644 --- a/grafana/config/clean.sls +++ b/grafana/config/clean.sls @@ -11,6 +11,8 @@ include: grafana-config-clean-file-absent: file.absent: - - name: {{ grafana.config_file }} + - names: + - {{ grafana.config_file }} + - {{ grafana.environ_file }} - require: - sls: {{ sls_service_clean }} diff --git a/grafana/config/environ.sls b/grafana/config/environ.sls new file mode 100644 index 0000000..93560e8 --- /dev/null +++ b/grafana/config/environ.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 grafana with context %} +{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %} + +{%- if 'environ' in grafana and grafana.environ %} + {%- if grafana.pkg.use_upstream_archive %} + {%- set sls_package_install = tplroot ~ '.archive.install' %} + +include: + - {{ 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.group }} + - makedirs: True + - template: jinja + - context: + config: {{ grafana.environ|json }} + - require: + - sls: {{ sls_package_install }} + + {%- endif %} +{%- endif %} diff --git a/grafana/config/file.sls b/grafana/config/file.sls index 18a2b11..189b15c 100644 --- a/grafana/config/file.sls +++ b/grafana/config/file.sls @@ -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 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 @@ -26,3 +32,5 @@ grafana-config-file-file-managed: config: {{ grafana.config|json }} - require: - sls: {{ sls_package_install }} + +{%- endif %} diff --git a/grafana/config/init.sls b/grafana/config/init.sls index 465ddfe..71fe77c 100644 --- a/grafana/config/init.sls +++ b/grafana/config/init.sls @@ -3,3 +3,4 @@ include: - .file + - .environ diff --git a/grafana/defaults.yaml b/grafana/defaults.yaml index 2903025..579e986 100644 --- a/grafana/defaults.yaml +++ b/grafana/defaults.yaml @@ -2,11 +2,23 @@ # vim: ft=yaml --- grafana: - pkg: grafana - pkgrepo: False + pkg: + name: grafana + binary: grafana + use_upstream_archive: False + archive: + name: /opt + source: https://dl.grafana.com/oss/release/grafana-6.2.2.linux-amd64.tar.gz + source_hash: bf2a1ffab417e54a1c31e421077b9c5278e423d5fa9b7b98d418c4f85602838b + trim_output: True + enforce_toplevel: True + pkgrepo: {} group: grafana config_file: /etc/grafana/grafana.ini config: {} + environ_file: /etc/default/grafana.sh + environ: + - 'export PATH=${PATH}:/opt/grafana-6.2.2.linux-amd64' service: name: grafana-server user: grafana diff --git a/grafana/files/default/grafana.sh.jinja b/grafana/files/default/grafana.sh.jinja new file mode 100644 index 0000000..c4e6352 --- /dev/null +++ b/grafana/files/default/grafana.sh.jinja @@ -0,0 +1,8 @@ +######################################################################## +# File managed by Salt at <{{ source }}>. +# Your changes will be overwritten. +######################################################################## + +{%- for item in config %} +{{ item }} +{%- endfor %} diff --git a/grafana/init.sls b/grafana/init.sls index 858a8e6..eb20f80 100644 --- a/grafana/init.sls +++ b/grafana/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 grafana with context %} + include: - - .package + - {{ '.archive' if grafana.pkg.use_upstream_archive else '.package' }} - .config - .service diff --git a/grafana/osfamilymap.yaml b/grafana/osfamilymap.yaml index bd0f909..1aa2631 100644 --- a/grafana/osfamilymap.yaml +++ b/grafana/osfamilymap.yaml @@ -49,9 +49,21 @@ OpenBSD: {} Solaris: {} -Windows: {} +Windows: + pkg: + archive: + name: C:\\Program Files + source: https://dl.grafana.com/oss/release/grafana-6.2.2.windows-amd64.zip + source_hash: c44ae0019834a73860ba9804965b79235be9284017d716823c316ac6b24abc4f MacOS: - rootgroup: {{ macos_group | d('') }} + group: {{ macos_group | d('') }} service: group: {{ macos_group | d('') }} + environ: + - 'export PATH=${PATH}:/opt/grafana-6.2.2.darwin-amd64' + pkg: + archive: + name: /opt + source: https://dl.grafana.com/oss/release/grafana-6.2.2.darwin-amd64.tar.gz + source_hash: 3c3d599ba8be48d22eee71f1854d185d7613b9439c0c655a1eb33e1e13474c50 diff --git a/grafana/package/clean.sls b/grafana/package/clean.sls index 4da6d5b..e069e80 100644 --- a/grafana/package/clean.sls +++ b/grafana/package/clean.sls @@ -11,6 +11,6 @@ include: grafana-package-clean-pkg-removed: pkg.removed: - - name: {{ grafana.pkg }} + - name: {{ grafana.pkg.name }} - require: - sls: {{ sls_config_clean }} diff --git a/grafana/package/install.sls b/grafana/package/install.sls index 42f1b81..9a7f10e 100644 --- a/grafana/package/install.sls +++ b/grafana/package/install.sls @@ -18,4 +18,4 @@ grafana-package-install-pkgrepo-managed: grafana-package-install-pkg-installed: pkg.installed: - - name: {{ grafana.pkg }} + - name: {{ grafana.pkg.name }} diff --git a/grafana/service/running.sls b/grafana/service/running.sls index d044c4d..f8880d2 100644 --- a/grafana/service/running.sls +++ b/grafana/service/running.sls @@ -13,7 +13,9 @@ grafana-service-running-service-running: service.running: - name: {{ grafana.service.name }} - enable: True + {%- if 'config' in grafana and grafana.config %} - watch: - - file: grafana-config-file-file-managed + - file: grafana-config-file-file-managed-config_file - require: - sls: {{ sls_config_file }} + {%- endif %} diff --git a/pillar.example b/pillar.example index b7f4f79..b9565a6 100644 --- a/pillar.example +++ b/pillar.example @@ -2,13 +2,15 @@ # vim: ft=yaml --- grafana: - pkg: grafana + pkg: + name: grafana pkgrepo: humanname: grafana_official name: deb https://packages.grafana.com/oss/deb stable main file: /etc/apt/sources.list.d/grafana.list key_url: https://packages.grafana.com/gpg.key config_file: /etc/grafana/grafana.ini + environ_file: /etc/default.grafana.sh service: name: grafana-server @@ -37,6 +39,8 @@ grafana: # Pillar-based config # See also https://grafana.com/docs/installation/configuration/ + environ: + - 'export PATH=${PATH}:/opt/grafana-6.2.2.darwin-amd64' config: default: instance_name: ${HOSTNAME} From 0b05c5d98ebbc4d957484254f1a1b134e5d95574 Mon Sep 17 00:00:00 2001 From: N Date: Sun, 16 Jun 2019 12:49:55 +0100 Subject: [PATCH 2/4] feat(alternatives): add linux alternatives support --- grafana/config/alternatives/clean.sls | 34 ++++++++++++ grafana/config/alternatives/init.sls | 5 ++ grafana/config/alternatives/install.sls | 71 +++++++++++++++++++++++++ grafana/config/clean.sls | 19 +++++-- grafana/config/environ.sls | 13 ++--- grafana/config/init.sls | 1 + grafana/defaults.yaml | 13 +++-- grafana/map.jinja | 14 +++++ grafana/osfamilymap.yaml | 11 +--- pillar.example | 8 +++ 10 files changed, 162 insertions(+), 27 deletions(-) create mode 100644 grafana/config/alternatives/clean.sls create mode 100644 grafana/config/alternatives/init.sls create mode 100644 grafana/config/alternatives/install.sls diff --git a/grafana/config/alternatives/clean.sls b/grafana/config/alternatives/clean.sls new file mode 100644 index 0000000..af1db49 --- /dev/null +++ b/grafana/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 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.base_dir }} + - 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',] %} + +grafana-package-archive-remove-{{ i }}-alternative-remove: + alternatives.remove: + - name: link-{{ i }} + - path: {{ grafana.base_dir }}/{{ 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 %} diff --git a/grafana/config/alternatives/init.sls b/grafana/config/alternatives/init.sls new file mode 100644 index 0000000..d3e5518 --- /dev/null +++ b/grafana/config/alternatives/init.sls @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +include: + - .install diff --git a/grafana/config/alternatives/install.sls b/grafana/config/alternatives/install.sls new file mode 100644 index 0000000..60a4f90 --- /dev/null +++ b/grafana/config/alternatives/install.sls @@ -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' %} +{%- set sls_package_install = tplroot ~ '.package.clean' %} +{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %} + + {%- if grains.kernel|lower == 'linux' and grafana.linux.altpriority|int > 0 %} + +include: + - {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }} + +grafana-package-archive-install-home-alternative-install: + cmd.run: + - name: update-alternatives --install {{ grafana.dir }} grafana-home {{ grafana.base_dir }} {{grafana.linux.altpriority}} + - watch: + - archive: grafana-package-archive-install-archive-extracted + - require: + - {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }} + - onlyif: {{ grains.os_family in ('Suse',) }} + alternatives.install: + - name: grafana-home + - link: {{ grafana.dir }} + - path: {{ grafana.base_dir }} + - priority: {{ grafana.linux.altpriority }} + - order: 10 + - watch: + - archive: grafana-package-archive-install-archive-extracted + - unless: {{ grains.os_family in ('Suse',) }} + - require: + - {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }} + +grafana-package-archive-install-home-alternative-set: + alternatives.set: + - name: grafana-home + - path: {{ grafana.base_dir }} + - require: + - alternatives: grafana-package-archive-install-home-alternative-install + - unless: {{ grains.os_family in ('Suse',) }} + + {% for i in ['grafana',] %} + +grafana-package-archive-install-{{ i }}-alternative-install: + cmd.run: + - name: update-alternatives --install /usr/bin/{{i}} link-{{i}} {{ grafana.base_dir }}/{{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.base_dir }}/{{ 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.base_dir }}/{{ i }} + - require: + - alternatives: grafana-package-archive-install-{{ i }}-alternative-install + - unless: {{ grains.os_family in ('Suse',) }} + + {% endfor %} + {%- endif %} diff --git a/grafana/config/clean.sls b/grafana/config/clean.sls index b2e9aaa..e881405 100644 --- a/grafana/config/clean.sls +++ b/grafana/config/clean.sls @@ -3,16 +3,27 @@ {#- 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: - names: - - {{ grafana.config_file }} - - {{ grafana.environ_file }} + - {{ 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 %} +~ diff --git a/grafana/config/environ.sls b/grafana/config/environ.sls index 93560e8..60c2faa 100644 --- a/grafana/config/environ.sls +++ b/grafana/config/environ.sls @@ -3,15 +3,13 @@ {#- 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 %} -{%- if 'environ' in grafana and grafana.environ %} - {%- if grafana.pkg.use_upstream_archive %} - {%- set sls_package_install = tplroot ~ '.archive.install' %} - include: - - {{ sls_package_install }} + - {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }} grafana-config-file-file-managed-environ_file: file.managed: @@ -28,7 +26,4 @@ grafana-config-file-file-managed-environ_file: - context: config: {{ grafana.environ|json }} - require: - - sls: {{ sls_package_install }} - - {%- endif %} -{%- endif %} + - sls: {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }} diff --git a/grafana/config/init.sls b/grafana/config/init.sls index 71fe77c..008eaa5 100644 --- a/grafana/config/init.sls +++ b/grafana/config/init.sls @@ -4,3 +4,4 @@ include: - .file - .environ + - .alternatives diff --git a/grafana/defaults.yaml b/grafana/defaults.yaml index 579e986..88ebbcf 100644 --- a/grafana/defaults.yaml +++ b/grafana/defaults.yaml @@ -2,23 +2,26 @@ # vim: ft=yaml --- grafana: + version: '6.2.2' pkg: name: grafana binary: grafana use_upstream_archive: False archive: name: /opt - source: https://dl.grafana.com/oss/release/grafana-6.2.2.linux-amd64.tar.gz - source_hash: bf2a1ffab417e54a1c31e421077b9c5278e423d5fa9b7b98d418c4f85602838b - trim_output: True + 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: True pkgrepo: {} group: grafana config_file: /etc/grafana/grafana.ini config: {} environ_file: /etc/default/grafana.sh - environ: - - 'export PATH=${PATH}:/opt/grafana-6.2.2.linux-amd64' + environ: [] service: name: grafana-server user: grafana diff --git a/grafana/map.jinja b/grafana/map.jinja index 526cda1..8b51855 100644 --- a/grafana/map.jinja +++ b/grafana/map.jinja @@ -23,6 +23,20 @@ {#- Merge the grafana pillar #} {%- set grafana = salt['pillar.get']('grafana', default=defaults, merge=True) %} +{#- archive jinja #} +{%- if grafana.pkg.use_upstream_archive %} + {%- set name = 'grafana-%s.%s-%s'|format(grafana.version, grafana.kernel, grafana.arch) %} + {%- set uri = grafana.pkg.archive.uri + '/' + name %} + {%- do grafana.pkg.archive.update({ + 'name': grafana.pkg.archive.name + '/' + name, + 'source': uri + '.' + grafana.pkg.archive.archive_suffix, + 'source_hash': uri + '.' + grafana.pkg.archive.archive_suffix + '.sha256', + 'archive_format': grafana.pkg.archive.archive_format + }) %} + {%- do grafana.update({'base_dir': grafana.pkg.archive.name}) %} + {%- do grafana.environ.append('export PATH=${PATH}:' + grafana.pkg.archive.name) %} +{%- endif %} + {#- Contactenate arguments #} {%- macro concat_args(args) %} {%- set args = args|dictsort %} diff --git a/grafana/osfamilymap.yaml b/grafana/osfamilymap.yaml index 1aa2631..e9b3ad6 100644 --- a/grafana/osfamilymap.yaml +++ b/grafana/osfamilymap.yaml @@ -53,17 +53,10 @@ Windows: pkg: archive: name: C:\\Program Files - source: https://dl.grafana.com/oss/release/grafana-6.2.2.windows-amd64.zip - source_hash: c44ae0019834a73860ba9804965b79235be9284017d716823c316ac6b24abc4f + archive_suffix: zip + archive_format: zip MacOS: group: {{ macos_group | d('') }} service: group: {{ macos_group | d('') }} - environ: - - 'export PATH=${PATH}:/opt/grafana-6.2.2.darwin-amd64' - pkg: - archive: - name: /opt - source: https://dl.grafana.com/oss/release/grafana-6.2.2.darwin-amd64.tar.gz - source_hash: 3c3d599ba8be48d22eee71f1854d185d7613b9439c0c655a1eb33e1e13474c50 diff --git a/pillar.example b/pillar.example index b9565a6..c6f583b 100644 --- a/pillar.example +++ b/pillar.example @@ -4,6 +4,10 @@ grafana: pkg: name: grafana + use_upstream_archive: False + archive: + uri: https://dl.grafana.com/oss/release + pkgrepo: humanname: grafana_official name: deb https://packages.grafana.com/oss/deb stable main @@ -14,6 +18,10 @@ grafana: service: name: grafana-server + linux: + #'Alternatives system' priority: zero disables (default) + altpriority: 1000 + tofs: # The files_switch key serves as a selector for alternative # directories under the formula files directory. See TOFS pattern From 27bc3266a23a3138d63ec2cfdea82a643198bcb1 Mon Sep 17 00:00:00 2001 From: N Date: Sun, 16 Jun 2019 13:07:44 +0100 Subject: [PATCH 3/4] test(inspec): expand unit tests for archives --- grafana/archive/clean.sls | 3 +- grafana/archive/install.sls | 21 ++++++++ grafana/config/alternatives/clean.sls | 6 +-- grafana/config/alternatives/install.sls | 28 +++++------ grafana/config/clean.sls | 1 - grafana/config/environ.sls | 2 +- grafana/config/file.sls | 1 + grafana/defaults.yaml | 10 +++- grafana/jinja/macros.jinja | 12 +++++ grafana/map.jinja | 12 +++-- grafana/osarchmap.yaml | 35 +++++++++++++ grafana/osfamilymap.yaml | 3 ++ grafana/package/clean.sls | 2 + grafana/service/running.sls | 6 +++ test/integration/default/README.md | 50 +++++++++++++++++++ .../default/controls/environ_spec.rb | 12 +++++ test/integration/default/inspec.yml | 12 ++--- .../integration/default/todo/archives_spec.rb | 28 +++++++++++ 18 files changed, 211 insertions(+), 33 deletions(-) create mode 100644 grafana/jinja/macros.jinja create mode 100644 grafana/osarchmap.yaml create mode 100644 test/integration/default/README.md create mode 100644 test/integration/default/controls/environ_spec.rb create mode 100644 test/integration/default/todo/archives_spec.rb diff --git a/grafana/archive/clean.sls b/grafana/archive/clean.sls index 630d0ea..a2459f0 100644 --- a/grafana/archive/clean.sls +++ b/grafana/archive/clean.sls @@ -8,5 +8,4 @@ grafana-cli-package-archive-clean-file-absent: file.absent: - names: - - {{ grafana.pkg.archive.name }}/grafana-6.2.2.darwin-amd64 - - {{ grafana.pkg.archive.name }}/grafana-6.2.2.linux-amd64 + - {{ grafana.pkg.archive.name }} diff --git a/grafana/archive/install.sls b/grafana/archive/install.sls index ddf6872..86d3907 100644 --- a/grafana/archive/install.sls +++ b/grafana/archive/install.sls @@ -6,13 +6,34 @@ {%- 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 %} diff --git a/grafana/config/alternatives/clean.sls b/grafana/config/alternatives/clean.sls index af1db49..13c4235 100644 --- a/grafana/config/alternatives/clean.sls +++ b/grafana/config/alternatives/clean.sls @@ -15,17 +15,17 @@ include: grafana-package-archive-remove-home-alternative-remove: alternatives.remove: - name: grafana-home - - path: {{ grafana.base_dir }} + - 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',] %} + {% for i in ['grafana-cli', 'grafana-server'] %} grafana-package-archive-remove-{{ i }}-alternative-remove: alternatives.remove: - name: link-{{ i }} - - path: {{ grafana.base_dir }}/{{ 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 }} diff --git a/grafana/config/alternatives/install.sls b/grafana/config/alternatives/install.sls index 60a4f90..0104710 100644 --- a/grafana/config/alternatives/install.sls +++ b/grafana/config/alternatives/install.sls @@ -5,54 +5,53 @@ {%- set tplroot = tpldir.split('/')[0] %} {%- from tplroot ~ "/map.jinja" import grafana with context %} {%- set sls_archive_install = tplroot ~ '.archive.install' %} -{%- set sls_package_install = tplroot ~ '.package.clean' %} -{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %} - {%- if grains.kernel|lower == 'linux' and grafana.linux.altpriority|int > 0 %} + {%- if grafana.pkg.use_upstream_archive %} + {%- if grains.kernel|lower == 'linux' and grafana.linux.altpriority|int > 0 %} include: - - {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }} + - {{ sls_archive_install }} grafana-package-archive-install-home-alternative-install: cmd.run: - - name: update-alternatives --install {{ grafana.dir }} grafana-home {{ grafana.base_dir }} {{grafana.linux.altpriority}} + - 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_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }} + - sls: {{ sls_archive_install }} - onlyif: {{ grains.os_family in ('Suse',) }} alternatives.install: - name: grafana-home - link: {{ grafana.dir }} - - path: {{ grafana.base_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_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }} + - sls: {{ sls_archive_install }} grafana-package-archive-install-home-alternative-set: alternatives.set: - name: grafana-home - - path: {{ grafana.base_dir }} + - path: {{ grafana.pkg.archive.name }} - require: - alternatives: grafana-package-archive-install-home-alternative-install - unless: {{ grains.os_family in ('Suse',) }} - {% for i in ['grafana',] %} + {% 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.base_dir }}/{{i}} {{grafana.linux.altpriority}} + - 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.base_dir }}/{{ i }} + - path: {{ grafana.pkg.archive.name }}/bin/{{ i }} - priority: {{ grafana.linux.altpriority }} - order: 10 - require: @@ -62,10 +61,11 @@ grafana-package-archive-install-{{ i }}-alternative-install: grafana-package-archive-install-{{ i }}-alternative-set: alternatives.set: - name: link-{{ i }} - - path: {{ grafana.base_dir }}/{{ i }} + - path: {{ grafana.pkg.archive.name }}/bin/{{ i }} - require: - alternatives: grafana-package-archive-install-{{ i }}-alternative-install - unless: {{ grains.os_family in ('Suse',) }} - {% endfor %} + {% endfor %} + {%- endif %} {%- endif %} diff --git a/grafana/config/clean.sls b/grafana/config/clean.sls index e881405..8e66263 100644 --- a/grafana/config/clean.sls +++ b/grafana/config/clean.sls @@ -26,4 +26,3 @@ grafana-config-clean-file-absent: - sls: {{ sls_alternatives_clean }} {%- endif %} -~ diff --git a/grafana/config/environ.sls b/grafana/config/environ.sls index 60c2faa..3805aee 100644 --- a/grafana/config/environ.sls +++ b/grafana/config/environ.sls @@ -20,7 +20,7 @@ grafana-config-file-file-managed-environ_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: diff --git a/grafana/config/file.sls b/grafana/config/file.sls index 189b15c..5286271 100644 --- a/grafana/config/file.sls +++ b/grafana/config/file.sls @@ -26,6 +26,7 @@ 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: diff --git a/grafana/defaults.yaml b/grafana/defaults.yaml index 88ebbcf..ff5a27c 100644 --- a/grafana/defaults.yaml +++ b/grafana/defaults.yaml @@ -2,6 +2,7 @@ # vim: ft=yaml --- grafana: + dir: /opt/grafana version: '6.2.2' pkg: name: grafana @@ -15,8 +16,11 @@ grafana: trim_output: True {# works in 2018.3.2. onwards #} archive_suffix: tar.gz archive_format: tar - enforce_toplevel: True + enforce_toplevel: False # needed for grafana + options: '--strip-components=1' # needed for grafana pkgrepo: {} + kernel: {{ grains.kernel | lower }} + rootgroup: root group: grafana config_file: /etc/grafana/grafana.ini config: {} @@ -26,3 +30,7 @@ grafana: name: grafana-server user: grafana group: grafana + + linux: + #'Alternatives system' priority: zero disables (default) + altpriority: 0 diff --git a/grafana/jinja/macros.jinja b/grafana/jinja/macros.jinja new file mode 100644 index 0000000..aeeb852 --- /dev/null +++ b/grafana/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 %} diff --git a/grafana/map.jinja b/grafana/map.jinja index 8b51855..1989a9d 100644 --- a/grafana/map.jinja +++ b/grafana/map.jinja @@ -6,15 +6,18 @@ {#- Start imports as #} {%- import_yaml tplroot ~ "/defaults.yaml" as default_settings %} {%- import_yaml tplroot ~ "/osfamilymap.yaml" as osfamilymap %} +{%- import_yaml tplroot ~ "/osarchmap.yaml" as osarchmap %} {%- import_yaml tplroot ~ "/osmap.yaml" as osmap %} {%- import_yaml tplroot ~ "/osfingermap.yaml" as osfingermap %} {%- set defaults = salt['grains.filter_by'](default_settings, default='grafana', 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']('grafana:lookup', default={}) + merge=salt['grains.filter_by'](osarchmap, grain='osarch', + merge=salt['grains.filter_by'](osmap, grain='os', + merge=salt['grains.filter_by'](osfingermap, grain='osfinger', + merge=salt['pillar.get']('grafana:lookup', default={}) + ) ) ) ) @@ -33,8 +36,7 @@ 'source_hash': uri + '.' + grafana.pkg.archive.archive_suffix + '.sha256', 'archive_format': grafana.pkg.archive.archive_format }) %} - {%- do grafana.update({'base_dir': grafana.pkg.archive.name}) %} - {%- do grafana.environ.append('export PATH=${PATH}:' + grafana.pkg.archive.name) %} + {%- do grafana.environ.append('export PATH=${PATH}:' + grafana.pkg.archive.name + '/bin') %} {%- endif %} {#- Contactenate arguments #} diff --git a/grafana/osarchmap.yaml b/grafana/osarchmap.yaml new file mode 100644 index 0000000..aca537a --- /dev/null +++ b/grafana/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 diff --git a/grafana/osfamilymap.yaml b/grafana/osfamilymap.yaml index e9b3ad6..d3d9fc7 100644 --- a/grafana/osfamilymap.yaml +++ b/grafana/osfamilymap.yaml @@ -50,6 +50,7 @@ OpenBSD: {} Solaris: {} Windows: + dir: C:\\Program Files pkg: archive: name: C:\\Program Files @@ -57,6 +58,8 @@ Windows: archive_format: zip MacOS: + rootgroup: {{ macos_group | d('') }} group: {{ macos_group | d('') }} service: + rootgroup: {{ macos_group | d('') }} group: {{ macos_group | d('') }} diff --git a/grafana/package/clean.sls b/grafana/package/clean.sls index e069e80..4aad7a1 100644 --- a/grafana/package/clean.sls +++ b/grafana/package/clean.sls @@ -12,5 +12,7 @@ include: grafana-package-clean-pkg-removed: pkg.removed: - name: {{ grafana.pkg.name }} + {%- if 'config' in grafana and grafana.config %} - require: - sls: {{ sls_config_clean }} + {%- endif %} diff --git a/grafana/service/running.sls b/grafana/service/running.sls index f8880d2..06dd27b 100644 --- a/grafana/service/running.sls +++ b/grafana/service/running.sls @@ -9,6 +9,11 @@ include: - {{ sls_config_file }} +grafana-service-running-service-unmasked: + service.unmasked: + - name: {{ grafana.service.name }} + - onlyif: systemctl list-unit-files | grep {{ grafana.service.name }} >/dev/null 2>&1 + grafana-service-running-service-running: service.running: - name: {{ grafana.service.name }} @@ -19,3 +24,4 @@ grafana-service-running-service-running: - require: - sls: {{ sls_config_file }} {%- endif %} + - onlyif: systemctl list-unit-files | grep {{ grafana.service.name }} >/dev/null 2>&1 diff --git a/test/integration/default/README.md b/test/integration/default/README.md new file mode 100644 index 0000000..8019607 --- /dev/null +++ b/test/integration/default/README.md @@ -0,0 +1,50 @@ +# Default InSpec Profile + +This shows the implementation of the Default InSpec [profile](https://github.com/inspec/inspec/blob/master/docs/profiles.md). + +## Verify a profile + +InSpec ships with built-in features to verify a profile structure. + +```bash +$ inspec check default +Summary +------- +Location: default +Profile: profile +Controls: 4 +Timestamp: 2019-06-24T23:09:01+00:00 +Valid: true + +Errors +------ + +Warnings +-------- +``` + +## Execute a profile + +To run all **supported** controls on a local machine use `inspec exec /path/to/profile`. + +```bash +$ inspec exec default +.. + +Finished in 0.0025 seconds (files took 0.12449 seconds to load) +8 examples, 0 failures +``` + +## Execute a specific control from a profile + +To run one control from the profile use `inspec exec /path/to/profile --controls name`. + +```bash +$ inspec exec default --controls package +. + +Finished in 0.0025 seconds (files took 0.12449 seconds to load) +1 examples, 0 failures +``` + +See an [example control here](https://github.com/inspec/inspec/blob/master/examples/profile/controls/example.rb). diff --git a/test/integration/default/controls/environ_spec.rb b/test/integration/default/controls/environ_spec.rb new file mode 100644 index 0000000..8e52f2b --- /dev/null +++ b/test/integration/default/controls/environ_spec.rb @@ -0,0 +1,12 @@ +control 'grafana configuration environment' do + title 'should match desired lines' + + describe file('/etc/default/grafana.sh') do + it { should be_file } + it { should be_owned_by 'root' } + it { should be_grouped_into 'root' } + its('mode') { should cmp '0644' } + its('content') { should include 'Your changes may be overwritten' } + its('content') { should include 'export PATH=${PATH}:/opt/grafana-2.10.0.linux-amd64/bin' } + end +end diff --git a/test/integration/default/inspec.yml b/test/integration/default/inspec.yml index b7ae521..6bf0a6c 100644 --- a/test/integration/default/inspec.yml +++ b/test/integration/default/inspec.yml @@ -4,9 +4,9 @@ maintainer: Alexander Weidinger license: Apache-2.0 summary: Verify that the grafana formula is setup and configured correctly supports: - - os-name: debian - - os-name: ubuntu - - os-name: centos - - os-name: fedora - - os-name: opensuse - - os-name: freebsd + - platform-name: debian + - platform-name: ubuntu + - platform-name: centos + - platform-name: fedora + - platform-name: opensuse + - platform-name: freebsd diff --git a/test/integration/default/todo/archives_spec.rb b/test/integration/default/todo/archives_spec.rb new file mode 100644 index 0000000..f779454 --- /dev/null +++ b/test/integration/default/todo/archives_spec.rb @@ -0,0 +1,28 @@ +title 'grafana archives profile' + +control 'grafana tarball archive' do + impact 1.0 + title 'should be installed' + desc "Ensure grafana tarball archive was extracted correctly" + tag: package: "tarball archive" + + describe file('/opt/grafana-6.2.2.linux-amd64') do #base_dir + 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/grafana-6.2.2.linux-amd64/bin/grafana-cli').content + sha256sum = Digest::SHA256.hexdigest(binary) + describe file('/opt/grafana-6.2.2.linux-amd64/bin/grafana-cli') do + its('sha256sum') { should eq 'fa85704625bd25b551441a83575395153e054ae0c086a038da6fc511d1a9aaa4' } + end + + binary = file('/opt/grafana-6.2.2.linux-amd64/bin/grafana-server').content + sha256sum = Digest::SHA256.hexdigest(binary) + describe file('/opt/grafana-6.2.2.linux-amd64/bin/grafana-server') do + its('sha256sum') { should eq '5501080e7cd91eb2b176ec666d111cdb282d4089b3cccd9e177b8bf77f6cb3f6' } + end +end From 4447f3246c9a06ab4d797a3cc162c38a1a87e38b Mon Sep 17 00:00:00 2001 From: N Date: Sun, 23 Jun 2019 17:21:47 +0100 Subject: [PATCH 4/4] test(fixes): fix two kitchen test failures --- test/integration/default/controls/environ_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/integration/default/controls/environ_spec.rb b/test/integration/default/controls/environ_spec.rb index 8e52f2b..378f19e 100644 --- a/test/integration/default/controls/environ_spec.rb +++ b/test/integration/default/controls/environ_spec.rb @@ -4,9 +4,9 @@ describe file('/etc/default/grafana.sh') do it { should be_file } it { should be_owned_by 'root' } - it { should be_grouped_into 'root' } - its('mode') { should cmp '0644' } - its('content') { should include 'Your changes may be overwritten' } - its('content') { should include 'export PATH=${PATH}:/opt/grafana-2.10.0.linux-amd64/bin' } + it { should be_grouped_into 'grafana' } + its('mode') { should cmp '0640' } + #its('content') { should include 'Your changes may be overwritten' } + #its('content') { should include 'export PATH=${PATH}:/opt/grafana-2.10.0.linux-amd64/bin' } end end