diff --git a/manifests/init.pp b/manifests/init.pp index 302b589..a9e5cf7 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,148 +1,148 @@ # == 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 'testing' to install beta 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', 'testing'] $repo_name = $::grafana::params::repo_name, + 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 { 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 a600065..74929dd 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -1,169 +1,175 @@ # == Class grafana::install # class grafana::install { $base_url = 'https://s3-us-west-2.amazonaws.com/grafana-releases/release' if $::grafana::archive_source != undef { $real_archive_source = $::grafana::archive_source } else { $real_archive_source = "${base_url}/grafana-${::grafana::version}.linux-x64.tar.gz" } if $::grafana::package_source != undef { $real_package_source = $::grafana::package_source } else { $real_package_source = $::osfamily ? { /(RedHat|Amazon)/ => "${base_url}/grafana-${::grafana::version}-${::grafana::rpm_iteration}.x86_64.rpm", 'Debian' => "${base_url}/builds/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: ensure => present, provider => 'dpkg', source => '/tmp/grafana.deb', require => [Archive['/tmp/grafana.deb'],Package['libfontconfig1']], } } 'RedHat': { package { 'fontconfig': ensure => present, } package { $::grafana::package_name: ensure => present, 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'] ) { class { '::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], } Class['apt::update'] -> Package[$::grafana::package_name] } package { $::grafana::package_name: ensure => $::grafana::version, 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': descr => 'grafana repo', - baseurl => 'https://packages.grafana.com/oss/rpm', + baseurl => $baseurl, gpgcheck => 1, gpgkey => 'https://packages.grafana.com/gpg.key', enabled => 1, before => Package[$::grafana::package_name], } } if $::grafana::version =~ /(installed|latest|present)/ { $real_version = $::grafana::version } else { $real_version = "${::grafana::version}-${::grafana::rpm_iteration}" } package { $::grafana::package_name: ensure => $real_version, 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 } } default: { fail("${::operatingsystem} 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/spec/acceptance/class_spec.rb b/spec/acceptance/class_spec.rb index 0f940f3..1aaa6ec 100644 --- a/spec/acceptance/class_spec.rb +++ b/spec/acceptance/class_spec.rb @@ -1,112 +1,142 @@ require 'spec_helper_acceptance' describe 'grafana class' do # Create dummy module directorty shell('mkdir -p /etc/puppetlabs/code/environments/production/modules/my_custom_module/files/dashboards') context 'default parameters' do # Using puppet_apply as a helper it 'works idempotently with no errors' do pp = <<-EOS class { 'grafana': } EOS # Run it twice and test for idempotency apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end describe package('grafana') do it { is_expected.to be_installed } end describe service('grafana-server') do it { is_expected.to be_enabled } it { is_expected.to be_running } end end context 'with fancy dashboard config' do it 'works idempotently with no errors' do pp = <<-EOS class { 'grafana': provisioning_datasources => { apiVersion => 1, datasources => [ { name => 'Prometheus', type => 'prometheus', access => 'proxy', url => 'http://localhost:9090/prometheus', isDefault => false, }, ], }, provisioning_dashboards => { apiVersion => 1, providers => [ { name => 'default', orgId => 1, folder => '', type => 'file', disableDeletion => true, options => { path => '/var/lib/grafana/dashboards', puppetsource => 'puppet:///modules/my_custom_module/dashboards', }, }, ], } } EOS # Run it twice and test for idempotency apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end end context 'with fancy dashboard config and custom target file' do it 'works idempotently with no errors' do pp = <<-EOS class { 'grafana': provisioning_datasources => { apiVersion => 1, datasources => [ { name => 'Prometheus', type => 'prometheus', access => 'proxy', url => 'http://localhost:9090/prometheus', isDefault => false, }, ], }, provisioning_dashboards => { apiVersion => 1, providers => [ { name => 'default', orgId => 1, folder => '', type => 'file', disableDeletion => true, options => { path => '/var/lib/grafana/dashboards', puppetsource => 'puppet:///modules/my_custom_module/dashboards', }, }, ], }, provisioning_dashboards_file => '/etc/grafana/provisioning/dashboards/dashboard.yaml', provisioning_datasources_file => '/etc/grafana/provisioning/datasources/datasources.yaml' } EOS # Run it twice and test for idempotency apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end end + + context 'beta release' do + it 'works idempotently with no errors' do + case fact('os.family') + when 'Debian' + pp = <<-EOS + class { 'grafana': + version => '6.0.0-beta1', + repo_name => 'beta', + } + EOS + when 'RedHat' + pp = <<-EOS + class { 'grafana': + version => '6.0.0', + rpm_iteration => 'beta1', + repo_name => 'beta', + } + EOS + end + + # Run it twice and test for idempotency + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe package('grafana') do + it { is_expected.to be_installed } + end + end end