diff --git a/data/common.yaml b/data/common.yaml new file mode 100644 index 0000000..a8516de --- /dev/null +++ b/data/common.yaml @@ -0,0 +1,27 @@ +--- +grafana::archive_source: ~ +grafana::cfg_location: '/etc/grafana/grafana.ini' +grafana::cfg: {} +grafana::ldap_cfg: ~ +grafana::container_cfg: false +grafana::container_params: {} +grafana::docker_image: 'grafana/grafana' +grafana::docker_ports: '3000:3000' +grafana::data_dir: '/var/lib/grafana' +grafana::install_dir: '/usr/share/grafana' +grafana::install_method: 'package' +grafana::manage_package_repo: true +grafana::package_name: 'grafana' +grafana::package_source: ~ +grafana::repo_name: 'stable' +grafana::rpm_iteration: '1' +grafana::service_name: 'grafana-server' +grafana::version: 'installed' +grafana::plugins: {} +grafana::provisioning_dashboards: {} +grafana::provisioning_datasources: {} +grafana::provisioning_dashboards_file: '/etc/grafana/provisioning/dashboards/puppetprovisioned.yaml' +grafana::provisioning_datasources_file: '/etc/grafana/provisioning/datasources/puppetprovisioned.yaml' +grafana::create_subdirs_provisioning: false +grafana::sysconfig_location: ~ +grafana::sysconfig: ~ diff --git a/data/family/Archlinux.yaml b/data/family/Archlinux.yaml new file mode 100644 index 0000000..927f4b6 --- /dev/null +++ b/data/family/Archlinux.yaml @@ -0,0 +1,5 @@ +--- +grafana::cfg_location: '/etc/grafana.ini' +grafana::install_method: 'repo' +grafana::manage_package_repo: false +grafana::service_name: 'grafana' diff --git a/data/family/Debian.yaml b/data/family/Debian.yaml new file mode 100644 index 0000000..61046c2 --- /dev/null +++ b/data/family/Debian.yaml @@ -0,0 +1,3 @@ +--- +grafana::install_method: 'repo' +grafana::sysconfig_location: '/etc/default/grafana-server' diff --git a/data/family/RedHat.yaml b/data/family/RedHat.yaml new file mode 100644 index 0000000..9e27d7d --- /dev/null +++ b/data/family/RedHat.yaml @@ -0,0 +1,3 @@ +--- +grafana::install_method: 'repo' +grafana::sysconfig_location: '/etc/sysconfig/grafana-server' diff --git a/hiera.yaml b/hiera.yaml new file mode 100644 index 0000000..e6158dd --- /dev/null +++ b/hiera.yaml @@ -0,0 +1,16 @@ +--- +version: 5 + +defaults: + datadir: 'data' + data_hash: 'yaml_data' + +hierarchy: + - name: 'Major Version' + path: "os/%{facts.os.name}-%{facts.os.release.major}.yaml" + - name: 'Distribution Name' + path: "os/%{facts.os.name}.yaml" + - name: 'Operating System Family' + path: "family/%{facts.os.family}.yaml" + - name: 'Common' + path: 'common.yaml' diff --git a/manifests/config.pp b/manifests/config.pp index a7190f6..94980a5 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -1,144 +1,146 @@ # == Class grafana::config # # This class is called from grafana # class grafana::config { case $grafana::install_method { 'docker': { if $grafana::container_cfg { $cfg = $grafana::cfg $myprovision = false - file { $grafana::cfg_location: + file { 'grafana.ini': ensure => file, + path => $grafana::cfg_location, content => template('grafana/config.ini.erb'), owner => 'grafana', group => 'grafana', } } } 'package','repo': { $cfg = $grafana::cfg $myprovision = true - file { $grafana::cfg_location: + file { 'grafana.ini': ensure => file, + path => $grafana::cfg_location, content => template('grafana/config.ini.erb'), owner => 'grafana', group => 'grafana', } $sysconfig = $grafana::sysconfig $sysconfig_location = $grafana::sysconfig_location if $sysconfig_location and $sysconfig { $changes = $sysconfig.map |$key, $value| { "set ${key} ${value}" } augeas{'sysconfig/grafana-server': context => "/files${$sysconfig_location}", changes => $changes, } } file { "${grafana::data_dir}/plugins": ensure => directory, owner => 'grafana', group => 'grafana', mode => '0750', } } 'archive': { $cfg = $grafana::cfg $myprovision = true file { "${grafana::install_dir}/conf/custom.ini": ensure => file, content => template('grafana/config.ini.erb'), owner => 'grafana', group => 'grafana', } file { [$grafana::data_dir, "${grafana::data_dir}/plugins"]: ensure => directory, owner => 'grafana', group => 'grafana', mode => '0750', } } default: { fail("Installation method ${grafana::install_method} not supported") } } if $grafana::ldap_cfg { $ldap_cfg = $grafana::ldap_cfg file { '/etc/grafana/ldap.toml': ensure => file, content => inline_template("<%= require 'toml'; TOML::Generator.new(@ldap_cfg).body %>\n"), owner => 'grafana', group => 'grafana', } } # If grafana version is > 5.0.0, and the install method is package, # repo, or archive, then use the provisioning feature. Dashboards # and datasources are placed in # /etc/grafana/provisioning/[dashboards|datasources] by default. # --dashboards-- if ((versioncmp($grafana::version, '5.0.0') >= 0) and ($myprovision)) { $pdashboards = $grafana::provisioning_dashboards if (length($pdashboards) >= 1 ) { $dashboardpaths = flatten(grafana::deep_find_and_remove('options', $pdashboards)) # template uses: # - pdashboards file { $grafana::provisioning_dashboards_file: ensure => file, owner => 'grafana', group => 'grafana', mode => '0640', content => epp('grafana/pdashboards.yaml.epp'), - notify => Service[$grafana::service_name], + notify => Service['grafana'], } # Loop over all providers, extract the paths and create # directories for each path of dashboards. $dashboardpaths.each | Integer $index, Hash $options | { if ('path' in $options) { # get sub paths of 'path' and create subdirs if necessary $subpaths = grafana::get_sub_paths($options['path']) if ($grafana::create_subdirs_provisioning and (length($subpaths) >= 1)) { file { $subpaths : ensure => directory, before => File[$options['path']], } } file { $options['path'] : ensure => directory, owner => 'grafana', group => 'grafana', mode => '0750', recurse => true, purge => true, source => $options['puppetsource'], } } } } # --datasources-- $pdatasources = $grafana::provisioning_datasources if (length($pdatasources) >= 1) { # template uses: # - pdatasources file { $grafana::provisioning_datasources_file: ensure => file, owner => 'grafana', group => 'grafana', mode => '0640', content => epp('grafana/pdatasources.yaml.epp'), - notify => Service[$grafana::service_name], + notify => Service['grafana'], } } } } diff --git a/manifests/init.pp b/manifests/init.pp index ade920f..e7ab1cc 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,148 +1,150 @@ # == Class: grafana # # Installs and configures Grafana. # # === Parameters # [*archive_source*] # Download location of tarball to be used with the 'archive' install method. # Defaults to the URL of the latest version of Grafana available at the time of module release. # # [*container_cfg*] # Boolean. Determines whether a configuration file should be generated when using the 'docker' install method. # If true, use the `cfg` and `cfg_location` parameters to control creation of the file. # Defaults to false. # # [*container_params*] # Hash of parameters to use when creating the Docker container. For use with the 'docker' install method. # Refer to documentation of the `docker::run` resource in the `garethr-docker` module for details of available parameters. # Defaults to: # # container_params => { # 'image' => 'grafana/grafana:latest', # 'ports' => '3000' # } # # [*data_dir*] # The directory Grafana will use for storing its data. # Defaults to '/var/lib/grafana'. # # [*install_dir*] # Installation directory to be used with the 'archive' install method. # Defaults to '/usr/share/grafana'. # # [*install_method*] # Set to 'archive' to install Grafana using the tar archive. # Set to 'docker' to install Grafana using the official Docker container. # Set to 'package' to install Grafana using .deb or .rpm packages. # Set to 'repo' to install Grafana using an apt or yum repository. # Defaults to 'package'. # # [*manage_package_repo*] # If true this will setup the official grafana repositories on your host. Defaults to true. # # [*package_name*] # The name of the package managed with the 'package' install method. # Defaults to 'grafana'. # # [*package_source*] # Download location of package to be used with the 'package' install method. # Defaults to the URL of the latest version of Grafana available at the time of module release. # # [*service_name*] # The name of the service managed with the 'archive' and 'package' install methods. # Defaults to 'grafana-server'. # # [*version*] # The version of Grafana to install and manage. # Defaults to 'installed' # # [*repo_name*] # When using 'repo' install_method, the repo to look for packages in. # Set to 'stable' to install only stable versions # Set to 'beta' to install beta versions # Defaults to stable. # # [*plugins*] # A hash of plugins to be passed to `create_resources`, wraps around the # `grafana_plugin` resource. # # [*provisioning_dashboards*] # Hash of dashboards to provision into grafana. grafana > v5.0.0 # required. Hash will be converted into YAML and used by grafana to # provision dashboards. # # [*provisioning_datasources*] # Hash of datasources to provision into grafana, grafana > v5.0.0 # required. Hash will be converted into YAML and used by granfana to # configure datasources. # # [*provisioning_dashboards_file*] # String with the fully qualified path to place the provisioning file # for dashboards, only used if provisioning_dashboards is specified. # Defaults to '/etc/grafana/provisioning/dashboards/puppetprovisioned.yaml' # # [*provisioning_datasources_file*] # String with the fully qualified path to place the provisioning file # for datasources, only used if provisioning_datasources is specified. # Default to '/etc/grafana/provisioning/datasources/puppetprovisioned.yaml' # # [*create_subdirs_provisioning*] # Boolean, defaults to false. If true puppet will create any # subdirectories in the given path when provisioning dashboards. # # [*sysconfig_location*] # Location of the sysconfig file for the environment of the grafana-server service. # This is only used when the install_method is 'package' or 'repo'. # # [*sysconfig*] # A hash of environment variables for the grafana-server service # # Example: # sysconfig => { 'http_proxy' => 'http://proxy.example.com/' } # # === Examples # # class { '::grafana': # install_method => 'docker', # } # class grafana ( - Optional[String] $archive_source = undef, - String $cfg_location = $grafana::params::cfg_location, - Hash $cfg = $grafana::params::cfg, - Optional[Hash] $ldap_cfg = undef, - Boolean $container_cfg = $grafana::params::container_cfg, - Hash $container_params = $grafana::params::container_params, - String $data_dir = $grafana::params::data_dir, - String $install_dir = $grafana::params::install_dir, - String $install_method = $grafana::params::install_method, - Boolean $manage_package_repo = $grafana::params::manage_package_repo, - String $package_name = $grafana::params::package_name, - Optional[String] $package_source = undef, - Enum['stable', 'beta'] $repo_name = $grafana::params::repo_name, - String $rpm_iteration = $grafana::params::rpm_iteration, - String $service_name = $grafana::params::service_name, - String $version = 'installed', - Hash $plugins = {}, - Hash $provisioning_dashboards = {}, - Hash $provisioning_datasources = {}, - String $provisioning_dashboards_file = $grafana::params::provisioning_dashboards_file, - String $provisioning_datasources_file = $grafana::params::provisioning_datasources_file, - Boolean $create_subdirs_provisioning = $grafana::params::create_subdirs_provisioning, - Optional[String] $sysconfig_location = $grafana::params::sysconfig_location, - Optional[Hash] $sysconfig = undef, -) inherits grafana::params { + Optional[String] $archive_source, + String $cfg_location, + Hash $cfg, + Optional[Hash] $ldap_cfg, + Boolean $container_cfg, + Hash $container_params, + String $docker_image, + String $docker_ports, + String $data_dir, + String $install_dir, + String $install_method, + Boolean $manage_package_repo, + String $package_name, + Optional[String] $package_source, + Enum['stable', 'beta'] $repo_name, + String $rpm_iteration, + String $service_name, + String $version, + Hash $plugins, + Hash $provisioning_dashboards, + Hash $provisioning_datasources, + String $provisioning_dashboards_file, + String $provisioning_datasources_file, + Boolean $create_subdirs_provisioning, + Optional[String] $sysconfig_location, + Optional[Hash] $sysconfig, +) { contain grafana::install contain grafana::config contain grafana::service Class['grafana::install'] -> Class['grafana::config'] ~> Class['grafana::service'] create_resources(grafana_plugin, $plugins) # Dependency added for Grafana_plugins to ensure it runs at the # correct time. Class['grafana::config'] -> Grafana_Plugin <| |> ~> Class['grafana::service'] } diff --git a/manifests/install.pp b/manifests/install.pp index 859742a..b07d96d 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -1,180 +1,185 @@ # == Class grafana::install # class grafana::install { $base_url = 'https://dl.grafana.com/oss/release' if $grafana::archive_source != undef { $real_archive_source = $grafana::archive_source } else { $real_archive_source = "${base_url}/grafana-${grafana::version}.linux-amd64.tar.gz" } if $grafana::package_source != undef { $real_package_source = $grafana::package_source } else { $real_package_source = $facts['os']['family'] ? { /(RedHat|Amazon)/ => "${base_url}/grafana-${grafana::version}-${grafana::rpm_iteration}.x86_64.rpm", 'Debian' => "${base_url}/grafana_${grafana::version}_amd64.deb", default => $real_archive_source, } } case $grafana::install_method { 'docker': { docker::image { 'grafana/grafana': image_tag => $grafana::version, require => Class['docker'], } } 'package': { case $facts['os']['family'] { 'Debian': { package { 'libfontconfig1': ensure => present, } archive { '/tmp/grafana.deb': source => $real_package_source, } - package { $grafana::package_name: + package { 'grafana': ensure => present, + name => $grafana::package_name, provider => 'dpkg', source => '/tmp/grafana.deb', require => [Archive['/tmp/grafana.deb'],Package['libfontconfig1']], } } 'RedHat': { package { 'fontconfig': ensure => present, } - package { $grafana::package_name: + package { 'grafana': ensure => present, + name => $grafana::package_name, provider => 'rpm', source => $real_package_source, require => Package['fontconfig'], } } default: { fail("${facts['os']['family']} not supported") } } } 'repo': { case $facts['os']['family'] { 'Debian': { package { 'libfontconfig1': ensure => present, } if ( $grafana::manage_package_repo ){ if !defined( Class['apt'] ) { include apt } apt::source { 'grafana': location => 'https://packages.grafana.com/oss/deb', release => $grafana::repo_name, architecture => 'amd64,arm64,armhf', repos => 'main', key => { 'id' => '4E40DDF6D76E284A4A6780E48C8C34C524098CB6', 'source' => 'https://packages.grafana.com/gpg.key', }, - before => Package[$grafana::package_name], + before => Package['grafana'], } - Class['apt::update'] -> Package[$grafana::package_name] + Class['apt::update'] -> Package['grafana'] } - package { $grafana::package_name: + package { 'grafana': ensure => $grafana::version, + name => $grafana::package_name, require => Package['libfontconfig1'], } } 'RedHat': { package { 'fontconfig': ensure => present, } if ( $grafana::manage_package_repo ){ # http://docs.grafana.org/installation/rpm/#install-via-yum-repository $baseurl = $grafana::repo_name ? { 'stable' => 'https://packages.grafana.com/oss/rpm', 'beta' => 'https://packages.grafana.com/oss/rpm-beta', } yumrepo { 'grafana': ensure => 'absent', - before => Package[$grafana::package_name], + before => Package['grafana'], } yumrepo { "grafana-${grafana::repo_name}": descr => "grafana-${grafana::repo_name} repo", baseurl => $baseurl, gpgcheck => 1, gpgkey => 'https://packages.grafana.com/gpg.key', enabled => 1, - before => Package[$grafana::package_name], + before => Package['grafana'], } } if $grafana::version =~ /(installed|latest|present)/ { $real_version = $grafana::version } else { $real_version = "${grafana::version}-${grafana::rpm_iteration}" } - package { $grafana::package_name: + package { 'grafana': ensure => $real_version, + name => $grafana::package_name, require => Package['fontconfig'], } } 'Archlinux': { if $grafana::manage_package_repo { fail('manage_package_repo is not supported on Archlinux') } - package { $grafana::package_name: - ensure => 'present', # pacman provider doesn't have feature versionable + package { 'grafana': + ensure => 'present', # pacman provider doesn't have feature versionable + name => $grafana::package_name, } } default: { fail("${facts['os']['name']} not supported") } } } 'archive': { # create log directory /var/log/grafana (or parameterize) if !defined(User['grafana']){ user { 'grafana': ensure => present, home => $grafana::install_dir, } } file { $grafana::install_dir: ensure => directory, group => 'grafana', owner => 'grafana', require => User['grafana'], } archive { '/tmp/grafana.tar.gz': ensure => present, extract => true, extract_command => 'tar xfz %s --strip-components=1', extract_path => $grafana::install_dir, source => $real_archive_source, user => 'grafana', group => 'grafana', cleanup => true, require => File[$grafana::install_dir], } } default: { fail("Installation method ${grafana::install_method} not supported") } } } diff --git a/manifests/params.pp b/manifests/params.pp deleted file mode 100644 index 3d4c0af..0000000 --- a/manifests/params.pp +++ /dev/null @@ -1,51 +0,0 @@ -# == Class grafana::params -# -# This class is meant to be called from grafana -# It sets variables according to platform -# -class grafana::params { - $cfg = {} - $container_cfg = false - $container_params = {} - $data_dir = '/var/lib/grafana' - $docker_image = 'grafana/grafana' - $docker_ports = '3000:3000' - $install_dir = '/usr/share/grafana' - $package_name = 'grafana' - $rpm_iteration = '1' - $repo_name = 'stable' - $create_subdirs_provisioning = false - $provisioning_dashboards_file = '/etc/grafana/provisioning/dashboards/puppetprovisioned.yaml' - $provisioning_datasources_file = '/etc/grafana/provisioning/datasources/puppetprovisioned.yaml' - - case $facts['os']['family'] { - 'Archlinux': { - $manage_package_repo = false - $install_method = 'repo' - $cfg_location = '/etc/grafana.ini' - $service_name = 'grafana' - $sysconfig_location = undef - } - 'Debian': { - $manage_package_repo = true - $install_method = 'repo' - $cfg_location = '/etc/grafana/grafana.ini' - $service_name = 'grafana-server' - $sysconfig_location = '/etc/default/grafana-server' - } - 'RedHat': { - $manage_package_repo = true - $install_method = 'repo' - $cfg_location = '/etc/grafana/grafana.ini' - $service_name = 'grafana-server' - $sysconfig_location = '/etc/sysconfig/grafana-server' - } - default: { - $manage_package_repo = true - $install_method = 'package' - $cfg_location = '/etc/grafana/grafana.ini' - $service_name = 'grafana-server' - $sysconfig_location = undef - } - } -} diff --git a/manifests/service.pp b/manifests/service.pp index bcc83f5..6cf9cf0 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -1,46 +1,48 @@ # == Class grafana::service # # This class is meant to be called from grafana # It ensure the service is running # class grafana::service { case $grafana::install_method { 'docker': { $container = { 'grafana' => $grafana::container_params, } $defaults = { image => "${grafana::params::docker_image}:${grafana::version}", ports => $grafana::params::docker_ports, } create_resources(docker::run, $container, $defaults) } 'package','repo': { - service { $grafana::service_name: + service { 'grafana': ensure => running, + name => $grafana::service_name, enable => true, - subscribe => Package[$grafana::package_name], + subscribe => Package['grafana'], } } 'archive': { $service_path = "${grafana::install_dir}/bin/${grafana::service_name}" $service_config = "${grafana::install_dir}/conf/custom.ini" - if !defined(Service[$grafana::service_name]){ - service { $grafana::service_name: + if !defined(Service['grafana']){ + service { 'grafana': ensure => running, + name => $grafana::service_name, provider => base, binary => "su - grafana -c '${service_path} -config=${service_config} -homepath=${grafana::install_dir} web &'", hasrestart => false, hasstatus => false, status => "ps -ef | grep ${grafana::service_name} | grep -v grep", } } } default: { fail("Installation method ${grafana::install_method} not supported") } } } diff --git a/spec/classes/grafana_spec.rb b/spec/classes/grafana_spec.rb index e311eee..064077b 100644 --- a/spec/classes/grafana_spec.rb +++ b/spec/classes/grafana_spec.rb @@ -1,347 +1,328 @@ require 'spec_helper' describe 'grafana' do on_supported_os.each do |os, facts| context "on #{os}" do let(:facts) do facts end - let :service_name do - case facts[:osfamily] - when 'Archlinux' - 'grafana' - else - 'grafana-server' - end - end - - let :config_path do - case facts[:osfamily] - when 'Archlinux' - '/etc/grafana.ini' - else - '/etc/grafana/grafana.ini' - end - end - context 'with default values' do it { is_expected.to compile.with_all_deps } it { is_expected.to contain_class('grafana') } - it { is_expected.to contain_class('grafana::params') } it { is_expected.to contain_class('grafana::install').that_comes_before('Class[grafana::config]') } it { is_expected.to contain_class('grafana::config').that_notifies('Class[grafana::service]') } it { is_expected.to contain_class('grafana::service') } end context 'with parameter install_method is set to package' do let(:params) do { install_method: 'package', version: '5.4.2' } end case facts[:osfamily] when 'Debian' download_location = '/tmp/grafana.deb' describe 'use archive to fetch the package to a temporary location' do it do is_expected.to contain_archive('/tmp/grafana.deb').with_source( 'https://dl.grafana.com/oss/release/grafana_5.4.2_amd64.deb' ) end it { is_expected.to contain_archive('/tmp/grafana.deb').that_comes_before('Package[grafana]') } end describe 'install dependencies first' do it { is_expected.to contain_package('libfontconfig1').with_ensure('present').that_comes_before('Package[grafana]') } end describe 'install the package' do it { is_expected.to contain_package('grafana').with_provider('dpkg') } it { is_expected.to contain_package('grafana').with_source(download_location) } end when 'RedHat' describe 'install dependencies first' do it { is_expected.to contain_package('fontconfig').with_ensure('present').that_comes_before('Package[grafana]') } end describe 'install the package' do it { is_expected.to contain_package('grafana').with_provider('rpm') } end end end context 'with some plugins passed in' do let(:params) do { plugins: { 'grafana-wizzle' => { 'ensure' => 'present' }, 'grafana-woozle' => { 'ensure' => 'absent' }, 'grafana-plugin' => { 'ensure' => 'present', 'repo' => 'https://nexus.company.com/grafana/plugins' } } } end it { is_expected.to contain_grafana_plugin('grafana-wizzle').with(ensure: 'present') } it { is_expected.to contain_grafana_plugin('grafana-woozle').with(ensure: 'absent').that_notifies('Class[grafana::service]') } describe 'install plugin with pluginurl' do it { is_expected.to contain_grafana_plugin('grafana-plugin').with(ensure: 'present', repo: 'https://nexus.company.com/grafana/plugins') } end end context 'with parameter install_method is set to repo' do let(:params) do { install_method: 'repo' } end case facts[:osfamily] when 'Debian' describe 'install apt repo dependencies first' do it { is_expected.to contain_class('apt') } it { is_expected.to contain_apt__source('grafana').with(release: 'stable', repos: 'main', location: 'https://packages.grafana.com/oss/deb') } it { is_expected.to contain_apt__source('grafana').that_comes_before('Package[grafana]') } end describe 'install dependencies first' do it { is_expected.to contain_package('libfontconfig1').with_ensure('present').that_comes_before('Package[grafana]') } end describe 'install the package' do it { is_expected.to contain_package('grafana').with_ensure('installed') } end when 'RedHat' describe 'yum repo dependencies first' do it { is_expected.to contain_yumrepo('grafana-stable').with(baseurl: 'https://packages.grafana.com/oss/rpm', gpgkey: 'https://packages.grafana.com/gpg.key', enabled: 1) } it { is_expected.to contain_yumrepo('grafana-stable').that_comes_before('Package[grafana]') } end describe 'install dependencies first' do it { is_expected.to contain_package('fontconfig').with_ensure('present').that_comes_before('Package[grafana]') } end describe 'install the package' do it { is_expected.to contain_package('grafana').with_ensure('installed') } end end end context 'with parameter install_method is set to repo and manage_package_repo is set to false' do let(:params) do { install_method: 'repo', manage_package_repo: false, version: 'present' } end case facts[:osfamily] when 'Debian' describe 'install dependencies first' do it { is_expected.to contain_package('libfontconfig1').with_ensure('present').that_comes_before('Package[grafana]') } end describe 'install the package' do it { is_expected.to contain_package('grafana').with_ensure('present') } end when 'RedHat' describe 'install dependencies first' do it { is_expected.to contain_package('fontconfig').with_ensure('present').that_comes_before('Package[grafana]') } end describe 'install the package' do it { is_expected.to contain_package('grafana').with_ensure('present') } end when 'Archlinux' describe 'install the package' do it { is_expected.to contain_package('grafana').with_ensure('present') } end end end context 'with parameter install_method is set to archive' do let(:params) do { install_method: 'archive', version: '5.4.2' } end install_dir = '/usr/share/grafana' service_config = '/usr/share/grafana/conf/custom.ini' archive_source = 'https://dl.grafana.com/oss/release/grafana-5.4.2.linux-amd64.tar.gz' describe 'extract archive to install_dir' do it { is_expected.to contain_archive('/tmp/grafana.tar.gz').with_ensure('present') } it { is_expected.to contain_archive('/tmp/grafana.tar.gz').with_source(archive_source) } it { is_expected.to contain_archive('/tmp/grafana.tar.gz').with_extract_path(install_dir) } end describe 'create grafana user' do it { is_expected.to contain_user('grafana').with_ensure('present').with_home(install_dir) } it { is_expected.to contain_user('grafana').that_comes_before('File[/usr/share/grafana]') } end describe 'create data_dir' do it { is_expected.to contain_file('/var/lib/grafana').with_ensure('directory') } end describe 'manage install_dir' do it { is_expected.to contain_file(install_dir).with_ensure('directory') } it { is_expected.to contain_file(install_dir).with_group('grafana').with_owner('grafana') } end describe 'configure grafana' do it { is_expected.to contain_file(service_config).with_ensure('file') } end describe 'run grafana as service' do - it { is_expected.to contain_service(service_name).with_ensure('running').with_provider('base') } - it { is_expected.to contain_service(service_name).with_hasrestart(false).with_hasstatus(false) } + it { is_expected.to contain_service('grafana').with_ensure('running').with_provider('base') } + it { is_expected.to contain_service('grafana').with_hasrestart(false).with_hasstatus(false) } end context 'when user already defined' do let(:pre_condition) do 'user{"grafana": ensure => present, }' end describe 'do NOT create grafana user' do it { is_expected.not_to contain_user('grafana').with_ensure('present').with_home(install_dir) } end end context 'when service already defined' do let(:pre_condition) do - 'service{"grafana-server": + 'service{"grafana": ensure => running, + name => "grafana-server", hasrestart => true, hasstatus => true, }' end - # let(:params) {{ :service_name => 'grafana-server'}} describe 'do NOT run service' do - it { is_expected.not_to contain_service('grafana-server').with_hasrestart(false).with_hasstatus(false) } + it { is_expected.not_to contain_service('grafana').with_hasrestart(false).with_hasstatus(false) } end end end context 'invalid parameters' do context 'cfg' do describe 'should not raise an error when cfg parameter is a hash' do let(:params) do { cfg: {} } end it { is_expected.to contain_package('grafana') } end end end context 'configuration file' do describe 'should not contain any configuration when cfg param is empty' do - it { is_expected.to contain_file(config_path).with_content("# This file is managed by Puppet, any changes will be overwritten\n\n") } + it { is_expected.to contain_file('grafana.ini').with_content("# This file is managed by Puppet, any changes will be overwritten\n\n") } end describe 'should correctly transform cfg param entries to Grafana configuration' do let(:params) do { cfg: { 'app_mode' => 'production', 'section' => { 'string' => 'production', 'number' => 8080, 'boolean' => false, 'empty' => '' } }, ldap_cfg: { 'servers' => [ { 'host' => 'server1', 'use_ssl' => true, 'search_filter' => '(sAMAccountName=%s)', 'search_base_dns' => ['dc=domain1,dc=com'] }, { 'host' => 'server2', 'use_ssl' => true, 'search_filter' => '(sAMAccountName=%s)', 'search_base_dns' => ['dc=domain2,dc=com'] } ], 'servers.attributes' => { 'name' => 'givenName', 'surname' => 'sn', 'username' => 'sAMAccountName', 'member_of' => 'memberOf', 'email' => 'email' } } } end expected = "# This file is managed by Puppet, any changes will be overwritten\n\n"\ "app_mode = production\n\n"\ "[section]\n"\ "boolean = false\n"\ "empty = \n"\ "number = 8080\n"\ "string = production\n" - it { is_expected.to contain_file(config_path).with_content(expected) } + it { is_expected.to contain_file('grafana.ini').with_content(expected) } ldap_expected = "\n[[servers]]\n"\ "host = \"server1\"\n"\ "search_base_dns = [\"dc=domain1,dc=com\"]\n"\ "search_filter = \"(sAMAccountName=%s)\"\n"\ "use_ssl = true\n"\ "\n"\ "[[servers]]\n"\ "host = \"server2\"\n"\ "search_base_dns = [\"dc=domain2,dc=com\"]\n"\ "search_filter = \"(sAMAccountName=%s)\"\n"\ "use_ssl = true\n"\ "\n"\ "[servers.attributes]\n"\ "email = \"email\"\n"\ "member_of = \"memberOf\"\n"\ "name = \"givenName\"\n"\ "surname = \"sn\"\n"\ "username = \"sAMAccountName\"\n"\ "\n" it { is_expected.to contain_file('/etc/grafana/ldap.toml').with_content(ldap_expected) } end end context 'sysconfig environment variables' do let(:params) do { install_method: 'repo', sysconfig: { http_proxy: 'http://proxy.example.com/' } } end case facts[:osfamily] when 'Debian' describe 'Add the environment variable to the config file' do it { is_expected.to contain_augeas('sysconfig/grafana-server').with_context('/files/etc/default/grafana-server') } it { is_expected.to contain_augeas('sysconfig/grafana-server').with_changes(['set http_proxy http://proxy.example.com/']) } end when 'RedHat' describe 'Add the environment variable to the config file' do it { is_expected.to contain_augeas('sysconfig/grafana-server').with_context('/files/etc/sysconfig/grafana-server') } it { is_expected.to contain_augeas('sysconfig/grafana-server').with_changes(['set http_proxy http://proxy.example.com/']) } end end end end end end