Ask Your Question

How to access the value of new parameter inside a custom package-provider?

asked 2016-07-07 16:45:06 -0600

UnitedMarsupials gravatar image

updated 2016-07-07 18:36:18 -0600

Trying to work around being unable to specify the alternative path to gem, I decided to implement my own package-provider, which would allow users to specify the path to the command as a separate parameter. My usage would be something like:

  package {'sensu':
    ensure   => installed,
    provider => 'mygem',
    command  => '/usr/local/bin/gem22'

My plan was to inherit my new provider-class from the existing gem-class and overwrite the :gemcmd in it with the value supplied by the manifest. I declare the new provider thus:

Puppet::Type.type(:package).provide :mygem, :parent => :gem, :source => :gem do
    desc "Ruby Gem support with the ability to specify an alternative
        gem-command. For example: command => '/usr/local/bin/gem22'"

I then successfully (?) added the new parameter to all package-providers:

Puppet::Type.type(:package).newparam(:command) do
    desc "Specify alternative package-command such as

and the sample manifest quoted above is no longer triggering Puppet errors. However, the methods inside my new provider do not get the value of the command-attribute, whatever I try. The @resource is not set, for example, and I can't figure out, how to access the new value. For example, trying to overwrite the gem's self.instances method:

def self.instances(justme = false)
    puts "==> In instances"
    commands :gemcmd => @resource[:command]

I get an error about @resource being nil:

==> In instances
Error: Could not prefetch package provider 'mygem': undefined method `[]' for nil:NilClass

How can the self.prefetch method, for example, get access to the attributes specified in the manifest? Thanks!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-07-07 20:48:19 -0600

UnitedMarsupials gravatar image

Ok, the reason @resource is not available in the self.prefetch and self.instances methods is that those self-methods are methods of the class in Ruby, rather than instance methods. (In Java world they'd be called static.)

Many thanks to the participants of this conversation for the clue...

The @resource hash is available in instance-methods (such as install), but by then it is too late because a number of things have already been done with the "wrong" gem-command. The entire gem-provider is written assuming, :gemcmd is the same across all instances. It can be rewritten completely, but I chose the simpler route of hard-coding the alternative gem-command.

See my answer here for a working implementation. Its drawback is that a separate provider is necessary for each possible gem (from Ruby-1.8, 1.9, 2.0, 2.1, 2.2., 2.3)...

edit flag offensive delete link more

answered 2016-07-07 18:03:29 -0600

lupin gravatar image

Why do you need to access additional parameters? You can use your custom gem provider once you overridden the :gemcmd value. If you wanted to access the parameter you can access it as resource[:command] within your custom provider definition.

edit flag offensive delete link more


I need the manifest-specified value in order to overwrite the `:gemcmd`. And accessing it through `@resource[:command]` does not work. (Just plain `resource` is an undefined local variable.)

UnitedMarsupials gravatar imageUnitedMarsupials ( 2016-07-07 18:35:08 -0600 )edit

no you don't. what you need to specify is the name of your custom provided. e.g package { 'pix': ensure => 'installed', provider => 'custom_gem', }

lupin gravatar imagelupin ( 2016-07-07 18:38:28 -0600 )edit

In this case I'll need a custom-provider for each possible gem-command. I was hoping -- as my example manifest shows -- to be able to specify the desired gem-command as one of the parameters.

UnitedMarsupials gravatar imageUnitedMarsupials ( 2016-07-07 19:01:14 -0600 )edit

No. the parent :gem provider has already defined that for you for you. What doing by overriding the :gemcmd is pointing it to a different executable(/opt/rhn22/bin/gem).

lupin gravatar imagelupin ( 2016-07-07 20:15:24 -0600 )edit

Lupin, you aren't even wrong...

UnitedMarsupials gravatar imageUnitedMarsupials ( 2016-07-07 20:50:01 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-07-07 16:45:06 -0600

Seen: 395 times

Last updated: Jul 07 '16