class Puppet::Provider::Package::Targetable

But `query` in the provider depends upon whether a `command` attribute is defined for the resource. This is a Catch-22.

Instead …

Inspect any package to access the catalog (every package includes a reference to the catalog). Inspect the catalog to find all of the `command` attributes for all of the packages of this class. Find all of the package instances using each package `command`, including the default provider command. Assign each instance's `provider` by selecting it from the `packages` hash passed to `prefetch`, based upon `name` and `command`.

The original `command` parameter in the catalog is not populated by the default (`:default`) for the parameter in type/package.rb. Rather, the result of the `original_parameters` is `nil` when the `command` parameter is undefined in the catalog.

Public Class Methods

prefetch(packages) click to toggle source

Prefetch our package list, yo.

   # File lib/puppet/provider/package_targetable.rb
27 def self.prefetch(packages)
28   catalog_packages = packages.values.first.catalog.resources.select{ |p| p.provider.class == self }
29   package_commands = catalog_packages.map { |catalog_package| catalog_package::original_parameters[:command] }.uniq
30   package_commands.each do |command|
31     instances(command).each do |instance|
32       catalog_packages.each do |catalog_package|
33         if catalog_package[:name] == instance.name && catalog_package.original_parameters[:command] == command
34           catalog_package.provider = instance
35           self.debug "Prefetched instance: %{name} via command: %{cmd}" % { name: instance.name, cmd: (command || :default) }
36         end
37       end
38     end
39   end
40   package_commands
41 end
validate_command(cmd) click to toggle source

Targetable providers use has_command/is_optional to defer validation of provider suitability. Evaluate provider suitability here and now by validating that the command is defined and exists.

cmd: the full path to the package command.

   # File lib/puppet/provider/package_targetable.rb
54 def self.validate_command(cmd)
55   unless cmd
56     raise Puppet::Error, _("Provider %{name} package command is not functional on this host") % { name: name }
57   end
58   unless File.file?(cmd)
59     raise Puppet::Error, _("Provider %{name} package command '%{cmd}' does not exist on this host") % { name: name, cmd: cmd }
60   end
61 end

Public Instance Methods

resource_or_provider_command() click to toggle source

Returns the resource command or provider command.

   # File lib/puppet/provider/package_targetable.rb
45 def resource_or_provider_command
46   resource.original_parameters[:command] || self.class.provider_command
47 end
to_s() click to toggle source

Return information about the package, its provider, and its (optional) command.

   # File lib/puppet/provider/package_targetable.rb
65 def to_s
66   cmd = resource[:command] || :default
67   "#{@resource}(provider=#{self.class.name})(command=#{cmd})"
68 end