forked from bfraser/puppet-grafana
-
Notifications
You must be signed in to change notification settings - Fork 102
/
grafana.rb
77 lines (59 loc) · 1.71 KB
/
grafana.rb
1
2
3
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
# frozen_string_literal: true
require 'puppet/provider/package'
Puppet::Type.type(:package).provide :grafana, parent: Puppet::Provider::Package do
desc 'This provider only handles grafana plugins.'
has_feature :installable, :install_options, :uninstallable, :upgradeable, :versionable
has_command(:grafana_cli, 'grafana-cli') do
is_optional
end
def self.pluginslist
plugins = {}
grafana_cli('plugins', 'ls').split(%r{\n}).each do |line|
next unless line =~ %r{^(\S+)\s+@\s+((?:\d\.).+)\s*$}
name = Regexp.last_match(1)
version = Regexp.last_match(2)
plugins[name] = version
end
plugins
end
def self.instances
pluginslist.map do |k, v|
new(name: k, ensure: v, provider: 'grafana')
end
end
def query
plugins = self.class.pluginslist
if plugins.key?(resource[:name])
{ ensure: plugins[resource[:name]], name: resource[:name] }
else
{ ensure: :absent, name: resource[:name] }
end
end
def latest
grafana_cli('plugins', 'list-versions', resource[:name]).lines.first.strip
end
def update
plugins = self.class.pluginslist
if plugins.key?(resource[:name])
cmd = %w[plugins update]
cmd << install_options if resource[:install_options]
cmd << resource[:name]
grafana_cli(*cmd)
else
install
end
end
def install
cmd = %w[plugins install]
cmd << install_options if resource[:install_options]
cmd << resource[:name]
cmd << resource[:ensure] unless resource[:ensure].is_a? Symbol
grafana_cli(*cmd)
end
def install_options
join_options(resource[:install_options])
end
def uninstall
grafana_cli('plugins', 'uninstall', resource[:name])
end
end