Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package provider for improved plugin handling #178 #332

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,24 @@ grafana_plugin { 'grafana-example-custom-plugin':
}
```

If you need to enforce a specific version of an plugin you can use the package provider.

```puppet
package { 'grafana-image-renderer':
ensure => '2.0.0',
provider => 'grafana',
}
```

The package provider also enables the possibility to use `latest` which will update plugins if there is a newer version.

```puppet
package { 'grafana-image-renderer':
ensure => 'latest',
provider => 'grafana',
}
```

##### `grafana_folder`

Creates and manages Grafana folders via the API.
Expand Down
77 changes: 77 additions & 0 deletions lib/puppet/provider/package/grafana.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

require 'puppet/provider/package'

Puppet::Type.type(:package).provide :grafana, parent: Puppet::Provider::Package do
TuningYourCode marked this conversation as resolved.
Show resolved Hide resolved
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
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').provider(:grafana) do
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anybody experienced enough to write this test correctly? Already hat it like describe package('grafana-image-renderer') do but was also failing (probably because it doesn't set provider to grafana?)

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
Loading