On Fri, Jun 24, 2011 at 03:00:20PM -0700, Luke Kanies wrote:
> On Jun 23, 2011, at 11:08 AM, Carl Caum wrote:
> 
> > 
> > On Jun 22, 2011, at 10:55 PM, Luke Kanies wrote:
> > 
> >> On Jun 22, 2011, at 6:05 PM, Carl Caum wrote:
> >> 
> >>> I'm not clear at all on how to do prefetching in custom providers.  Can 
> >>> someone shed some light? Perhaps point to very detailed documentation 
> >>> with example code?
> >>> 
> >>> A few questions right off the bat:
> >>> 
> >>> 1) Is @property_hash a convention or implementation?  If implementation, 
> >>> do I structure it a certain way to get certain behavior from puppet?
> >> 
> >> It's convention, and one I've always been a bit embarrassed by.
> >> 
> >>> 2) If I prefetch, do I still need to have getter methods for properties?
> >> 
> >> Um, I *think* you don't need them any more.  That is, I'm confident normal 
> >> puppet usage doesn't require them, but I'm not sure if things like ralsh 
> >> use requires them.  Maybe try 'ralsh <mytype>' and 'ralsh <mytype> <name>' 
> >> as tests?
> > 
> > Looks like I still need getter methods for ralsh.  Also, I need to 
> > implement self.instances.
> > 
> >> 
> >>> 3) Does my prefetch methods have to accept a hash of properties from the 
> >>> manifest?  What am I supposed to do with it?
> >> 
> >> Hash of properties?
> >> 
> >> I'm pretty sure they have to accept a hash of resources, and set the 
> >> provider instance on each resource.
> > 
> > Err,  I meant resources.  Digging through the code, it looks like prefetch 
> > doesn't have to take in a hash of resources.  I can instead call instances 
> > and iterate through that list, modifying the resource as necessary.  I 
> > haven't gotten far enough to see if that allows me to know if a provided 
> > resource is insync though. 
> 
> Hmm.  I wouldn't recommend that - normall, 'instances' calls prefetch, so if 
> prefetch also calls instances... bad things will likely result.
> 

Using instances inside prefetch looks like as basic approach to me.

Instances should return an array of providers for all resources puppet
can find on your system, (not necessarily in your manifest). This is
often achived by parsing a file, or running a command and parsing the
output.

The instances method is also used when you run puppet resource my_type.

Let's say you write an instances method of a package provider. You will
do something like

    def self.instances
      packages = [] ## Array of providers for each installed package
      output_of_a_list_command.each_line do |packagename|
        packages << new(:name => packagename, :ensure => :installed)
      end
      packages
    end

When you create a new instance of your provider class and pass a hash,
the hash is automatically stored as the @property_hash variable of your
new provider instance. So you should stick to that name.

The prefetch method is used to assign providers to all resources the
user specified in the manifest.  Puppet will pass a hash of resources to
the prefetch method.  This hash includes every resource in the catalog
and the namevar of each resource is used as a hashkey.  A simple
approach is to just call instances to collect providers for all possible
resources and iterate over them.  If a resource with a matching name
is also found in the catalog (= the user wants to manage this resource)
we can now simply assign the provider to this resource.  The assigned
providers alreay have the correct values stored in @property_hash

Sample prefetch method in package.rb

    # Prefetch our package list, yo.
    self.prefetch(packages)
      instances.each do |prov|
        if pkg = packages[prov.name]
          pkg.provider = prov
        end
      end
    end

More reasons to use @property_hash to store property values:
* you can use get(:property) and set(:property) inside the provider
  class
* you can use the classmethod mk_resource_methods to generate getter and
  setter methods for all your properties.

-Stefan

Attachment: pgpmZQ4QsXBoO.pgp
Description: PGP signature

Reply via email to