Puppet Class: grafana::config

Defined in:
manifests/config.pp

Summary

Manage grafana configuration

Overview



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'manifests/config.pp', line 4

class grafana::config {
  if $grafana::cfg =~ Sensitive {
    $cfg = $grafana::cfg.unwrap
    $cfg_content = Sensitive(template('grafana/config.ini.erb'))
  } else {
    $cfg = $grafana::cfg
    $cfg_content = template('grafana/config.ini.erb')
  }

  case $grafana::install_method {
    'docker': {
      if $grafana::container_cfg {
        $myprovision = false

        file { 'grafana.ini':
          ensure  => file,
          path    => $grafana::cfg_location,
          content => $cfg_content,
          owner   => 'grafana',
          group   => 'grafana',
          notify  => Class['grafana::service'],
        }
      }
    }
    'package','repo': {
      $myprovision = true

      file { 'grafana.ini':
        ensure  => file,
        path    => $grafana::cfg_location,
        content => $cfg_content,
        owner   => 'root',
        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':
          context => "/files${sysconfig_location}",
          changes => $changes,
          notify  => Class['grafana::service'],
        }
      }

      file { "${grafana::data_dir}/plugins":
        ensure => directory,
        owner  => 'grafana',
        group  => 'grafana',
        mode   => '0750',
      }
    }
    'archive': {
      $myprovision = true

      file { "${grafana::install_dir}/conf/custom.ini":
        ensure  => file,
        content => $cfg_content,
        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 =~ Sensitive {
      $ldap_cfg = $grafana::ldap_cfg.unwrap
    } else {
      $ldap_cfg = $grafana::ldap_cfg
    }

    $template_body = [
      "<% [scope['ldap_cfg']].flatten(1).each do |v| %>",
      "<%= require 'toml'; TOML::Generator.new(v).body %>\n",
      '<% end %>',
    ]

    if $grafana::ldap_cfg =~ Sensitive {
      $ldap_cfg_toml = Sensitive(inline_template($template_body.join('')))
    } else {
      $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']],
            }
          }

          if $options['puppetsource'] {
            file { $options['path'] :
              ensure       => directory,
              owner        => 'grafana',
              group        => 'grafana',
              mode         => '0750',
              recurse      => true,
              purge        => true,
              source       => $options['puppetsource'],
              sourceselect => 'all',
            }
          }
        }
      }
    }

    # --datasources--
    $pdatasources = $grafana::provisioning_datasources
    if (length($pdatasources) >= 1) {
      # template uses:
      #   - pdatasources

      $datasources_provisioning_dir = "${grafana::provisioning_dir}/datasources"

      file { $datasources_provisioning_dir:
        ensure => directory,
        owner  => 'grafana',
        group  => 'grafana',
        mode   => '0750',
        before => File[$grafana::provisioning_datasources_file],
      }

      file { $grafana::provisioning_datasources_file:
        ensure    => file,
        owner     => 'grafana',
        group     => 'grafana',
        mode      => '0640',
        show_diff => false,
        content   => epp('grafana/pdatasources.yaml.epp'),
        notify    => Class['grafana::service'],
      }
    }
  }
}