Skip to content

Commit

Permalink
Add package provider for improved plugin handling #178
Browse files Browse the repository at this point in the history
  • Loading branch information
TuningYourCode committed May 4, 2022
1 parent 6e31685 commit 378a0a2
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
72 changes: 72 additions & 0 deletions lib/puppet/provider/package/grafana.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 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
cmd = %w[plugins update]
cmd << install_options if resource[:install_options]
cmd << resource[:name]

grafana_cli(*cmd)
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
26 changes: 26 additions & 0 deletions spec/acceptance/grafana_plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,31 @@ class { 'grafana':
end
end
end

context 'grafana plugins' do
it 'installs' do
pp = <<-EOS
class { 'grafana':
version => 'latest',
}
package { 'grafana-image-renderer':
provider => '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-image-renderer') 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
end
end

0 comments on commit 378a0a2

Please sign in to comment.