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 Jun 28, 2023
1 parent 145b3e7 commit e52eca8
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -870,6 +870,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
@@ -0,0 +1,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
26 changes: 26 additions & 0 deletions spec/acceptance/grafana_plugin_spec.rb
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 }

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - AlmaLinux 9

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - AlmaLinux 9

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - CentOS 9

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - CentOS 9

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - CentOS 8

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - CentOS 8

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Rocky 8

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Rocky 8

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - CentOS 7

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - CentOS 7

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Rocky 9

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Rocky 9

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - AlmaLinux 8

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - AlmaLinux 8

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - AlmaLinux 9

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - AlmaLinux 9

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - AlmaLinux 8

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - AlmaLinux 8

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - CentOS 9

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - CentOS 9

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Debian 11

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Debian 11

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Debian 10

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Debian 10

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Rocky 9

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Rocky 9

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Ubuntu 22.04

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Ubuntu 22.04

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - CentOS 7

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - CentOS 7

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Rocky 8

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Rocky 8

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - CentOS 8

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - CentOS 8

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Ubuntu 20.04

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Ubuntu 20.04

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Debian 11

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Debian 11

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Debian 10

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Debian 10

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Ubuntu 18.04

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Ubuntu 18.04

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Ubuntu 22.04

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Ubuntu 22.04

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Ubuntu 20.04

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Ubuntu 20.04

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Ubuntu 18.04

grafana_plugin with Grafana version 8.5.22 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" to be installed

Check failure on line 116 in spec/acceptance/grafana_plugin_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 - Ubuntu 18.04

grafana_plugin with Grafana version 9.2.15 grafana plugins Package "grafana-image-renderer" is expected to be installed Failure/Error: it { is_expected.to be_installed } expected Package "grafana-image-renderer" 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 e52eca8

Please sign in to comment.