diff --git a/manifests/config.pp b/manifests/config.pp index c146000..580bebf 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -1,164 +1,163 @@ # == 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.ini': + file { 'grafana.ini': ensure => file, path => $grafana::cfg_location, content => template('grafana/config.ini.erb'), owner => 'grafana', group => 'grafana', notify => Class['grafana::service'], } } } 'package','repo': { $cfg = $grafana::cfg $myprovision = true - file { 'grafana.ini': + file { 'grafana.ini': ensure => file, path => $grafana::cfg_location, content => template('grafana/config.ini.erb'), owner => 'grafana', group => 'grafana', notify => Class['grafana::service'], } $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': + augeas { 'sysconfig/grafana-server': context => "/files${$sysconfig_location}", changes => $changes, notify => Class['grafana::service'], } } 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', notify => Class['grafana::service'], } 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 { if $grafana::ldap_cfg =~ Array { $ldap_cfg_ary = $grafana::ldap_cfg } else { $ldap_cfg_ary = [$grafana::ldap_cfg] } $template_body = [ "<% scope['ldap_cfg_ary'].each do |v| %>", "<%= require 'toml'; TOML::Generator.new(v).body %>\n", '<% end %>', ] $ldap_cfg_toml = inline_template($template_body.join('')) file { '/etc/grafana/ldap.toml': ensure => file, content => $ldap_cfg_toml, owner => 'grafana', group => 'grafana', notify => Class['grafana::service'], } } # 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 => Class['grafana::service'], } # 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 => Class['grafana::service'], } } - } } diff --git a/manifests/init.pp b/manifests/init.pp index 5e361f6..69a4552 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,186 +1,185 @@ # == 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/' } # # [*ldap_servers*] # A hash of ldap_servers to be passed to `create_resources`, wraps around the # `grafana_ldap_server` resource. # # [*ldap_group_mappings*] # A hash of ldap_servers to be passed to `create_resources`, wraps around the # `grafana_ldap_group_mapping` resource. # # [*toml_manage_package*] # ruby-toml is required to generate the TOML-based LDAP config for Grafana. # Defaults to true. Set to false if you manage package- or gem-install # somewhere else. # # [*toml_package_name*] # Name of the software-package providing the TOML parser library. # Defaults to ruby-toml. # # [*toml_package_ensure*] # Ensure the package-resource - e.g. installed, absent, etc. # https://puppet.com/docs/puppet/latest/types/package.html#package-attribute-ensure # Defaults to present # # [*toml_package_provider*] # The package-provider used to install the TOML parser library. # Defaults to undef, to let Puppet decide. See # https://puppet.com/docs/puppet/latest/types/package.html#package-attribute-provider # # === Examples # # class { '::grafana': # install_method => 'docker', # } # class grafana ( Optional[String] $archive_source, String $cfg_location, Hash $cfg, Optional[Variant[Hash,Array]] $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, Hash[String[1], Hash] $ldap_servers, Hash[String[1], Hash] $ldap_group_mappings, Boolean $toml_manage_package, String[1] $toml_package_name, String[1] $toml_package_ensure, Optional[String[1]] $toml_package_provider, ) { - 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'] create_resources('grafana_ldap_server', $ldap_servers) create_resources('grafana_ldap_group_mapping', $ldap_group_mappings) } diff --git a/manifests/install.pp b/manifests/install.pp index 0cd3cd9..e13b848 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -1,208 +1,207 @@ # == 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': 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': ensure => present, name => $grafana::package_name, provider => 'rpm', source => $real_package_source, require => Package['fontconfig'], } } 'FreeBSD': { package { 'grafana': ensure => present, name => $grafana::package_name, provider => 'pkgng', } } 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'] ) { + 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 => { + key => { 'id' => '4E40DDF6D76E284A4A6780E48C8C34C524098CB6', 'source' => 'https://packages.grafana.com/gpg.key', }, before => Package['grafana'], } Class['apt::update'] -> Package['grafana'] } package { 'grafana': ensure => $grafana::version, name => $grafana::package_name, require => Package['libfontconfig1'], } } 'RedHat': { package { 'fontconfig': ensure => present, } - if ( $grafana::manage_package_repo ){ + 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'], } 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'], } } if $grafana::version =~ /(installed|latest|present)/ { $real_version = $grafana::version } else { $real_version = "${grafana::version}-${grafana::rpm_iteration}" } 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': ensure => 'present', # pacman provider doesn't have feature versionable name => $grafana::package_name, } } 'FreeBSD': { package { 'grafana': ensure => 'present', # pkgng 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']){ + 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") } } if $grafana::toml_manage_package and !empty($grafana::ldap_servers) { ensure_packages(['toml-pkg'], { - ensure => $grafana::toml_package_ensure, - name => $grafana::toml_package_name, - provider => $grafana::toml_package_provider, + ensure => $grafana::toml_package_ensure, + name => $grafana::toml_package_name, + provider => $grafana::toml_package_provider, }) Package['toml-pkg'] -> Grafana_ldap_config <||> } } diff --git a/manifests/service.pp b/manifests/service.pp index 03d3456..c609c8f 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -1,48 +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::docker_image}:${grafana::version}", ports => $grafana::docker_ports, } create_resources(docker::run, $container, $defaults) } 'package','repo': { service { 'grafana': ensure => running, name => $grafana::service_name, enable => true, 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']){ + 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/manifests/validator.pp b/manifests/validator.pp index 399595d..3e9f04a 100644 --- a/manifests/validator.pp +++ b/manifests/validator.pp @@ -1,17 +1,16 @@ # @summary Manage grafana_conn_validator resource # # @param grafana_url # Grafana URL. # @param grafana_api_path # API path to validate with. # class grafana::validator ( Stdlib::HTTPUrl $grafana_url = 'http://localhost:3000', Stdlib::Absolutepath $grafana_api_path = '/api/health', ) { - grafana_conn_validator { 'grafana': grafana_url => $grafana_url, grafana_api_path => $grafana_api_path, } }