Ryan Uber
2013-Apr-30 22:37 UTC
[Puppet Users] Retrieve scoped resource defaults from class method in custom type
[accidently sent this to puppet-dev, re-posting to puppet-users] Hello puppet-users, I am working on a module that provides a custom type. The type when called will create new resources in the catalog using syntax like: Puppet::Type.type(:file).new(:title => ''blah'') This works fine and the resources are added as expected. However, I am unable to apply any scoped defaults to the generated resources. So something like: File { mode => 0750 } would not be applied to the generated resources. I understand that this is most likely because I am altering the catalog directly. The only thing I can think of doing would be to retrieve the scoped attributes first, and then apply them to the generated resource. I have been trying to do this with code like: f = Puppet::Type.type(:file) f.allattrs.each do |attrname| attrvalue = f.value(attrname) puts "#{attrname} = #{attrvalue}" end I know this doesn''t work, but wanted to throw this out there to see if I am taking the wrong approach at fetching resource defaults from the scope in which my custom type is called. Thanks for any help or insight. - Ryan -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Stefan Schulte
2013-May-01 11:54 UTC
Re: [Puppet Users] Retrieve scoped resource defaults from class method in custom type
On Tue, 30 Apr 2013 15:37:11 -0700 Ryan Uber <ru@ryanuber.com> wrote:> [accidently sent this to puppet-dev, re-posting to puppet-users] > > Hello puppet-users, > > I am working on a module that provides a custom type. The type when > called will create new resources in the catalog using syntax like: > > Puppet::Type.type(:file).new(:title => ''blah'') > > This works fine and the resources are added as expected. However, I am > unable to apply any scoped defaults to the generated resources. So > something like: > > File { > mode => 0750 > } >were do you generate the resources? Puppet already has a method "eval_generate" that every type can implement and which has to return an array of generated resources (that''s how puppet generates implicit file resource when you use `recurse`). These resources are automatically added to the catalog. So e.g. Puppet::Type.newtype(:foo) do newparam(:name) [...] def eval_generate resources = [] resources << Puppet::Type.type(:file).new(:title => ''blah'') resources end end I played with it a little bit (wanted to make a proof of concept for a `dirtree` type that simulates the behaviour of `mkdir -p`) but I hit problems because autorequirements of generated resources do not seem to work (e.g. You have File[''/foo''] in your puppet manifest and your custom type generates a resource File[''/foo/bar''] there a no automatic dependencies so File[''/foo/bar''] may be applied before File[''/foo''] but maybe I am wrong about that one. -Stefan -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Ryan Uber
2013-May-05 19:23 UTC
Re: [Puppet Users] Retrieve scoped resource defaults from class method in custom type
Hi Stefan, My apologies that I did not get back sooner. What you are suggesting yields the same results I was seeing before. Let me try to explain better: Using no modules (just standard puppet types), I can do this: --- Package { notify => Exec["package-changed"] } exec { "package-changed": command => "/bin/true", refreshonly => true; } package { "vim-enhanced": ensure => installed; } --- If the above code happens to install vim-enhanced, then I will see output like this: --- Notice: /Stage[main]//Package[vim-enhanced]/ensure: created Notice: /Stage[main]//Exec[package-changed]: Triggered ''refresh'' from 1 events Notice: Finished catalog run in 4.75 seconds --- The ''Package {}'' defaults are applied and the Exec gets notified. Now, I write a simple module with a type like this: --- Puppet::Type.newtype(:foo) do newparam(:title) do isnamevar end def eval_generate resources = [] resources << Puppet::Type.type(:package).new(:title => self.title) resources end end --- All it does is add a new package resource via eval_generate. I execute it using a manifest that looks like this: --- Package { notify => Exec["package-changed"] } exec { "package-changed": command => "/bin/true", refreshonly => true; } foo { "vim-enhanced": } --- It yields the following: --- Notice: /Package[vim-enhanced]/ensure: created Notice: Finished catalog run in 5.10 seconds --- The Exec from the resource defaults in my current scope is not notified. If they were, the Exec resource should have run, just like in the first example. I am looking for a way to generate resources from a type, and have the resource defaults from the scope they are defined applied to them. Any other thoughts or suggestions are most appreciated, as always. Thanks! - Ryan> From: "Stefan Schulte" <stefan.schulte@taunusstein.net> > To: puppet-users@googlegroups.com > Sent: Wednesday, May 1, 2013 4:54:38 AM > Subject: Re: [Puppet Users] Retrieve scoped resource defaults from class > method in custom type > > On Tue, 30 Apr 2013 15:37:11 -0700 > Ryan Uber <ru@ryanuber.com> wrote: > > > [accidently sent this to puppet-dev, re-posting to puppet-users] > > > > Hello puppet-users, > > > > I am working on a module that provides a custom type. The type when > > called will create new resources in the catalog using syntax like: > > > > Puppet::Type.type(:file).new(:title => ''blah'') > > > > This works fine and the resources are added as expected. However, I am > > unable to apply any scoped defaults to the generated resources. So > > something like: > > > > File { > > mode => 0750 > > } > > > > were do you generate the resources? Puppet already has a method > "eval_generate" that every type can implement and which has to return > an array of generated resources (that''s how puppet generates implicit > file resource when you use `recurse`). These resources are > automatically added to the catalog. > > So e.g. > > Puppet::Type.newtype(:foo) do > newparam(:name) > [...] > def eval_generate > resources = [] > resources << Puppet::Type.type(:file).new(:title => ''blah'') > resources > end > end > > I played with it a little bit (wanted to make a proof of concept for > a `dirtree` type that simulates the behaviour of `mkdir -p`) but I hit > problems because autorequirements of generated resources do not seem to > work (e.g. You have File[''/foo''] in your puppet manifest and your > custom type generates a resource File[''/foo/bar''] there a no automatic > dependencies so File[''/foo/bar''] may be applied before File[''/foo''] but > maybe I am wrong about that one. > > -Stefan > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to puppet-users+unsubscribe@googlegroups.com. > To post to this group, send email to puppet-users@googlegroups.com. > Visit this group at http://groups.google.com/group/puppet-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-May-06 16:55 UTC
Re: [Puppet Users] Retrieve scoped resource defaults from class method in custom type
On Sunday, May 5, 2013 2:23:25 PM UTC-5, Ryan R. Uber wrote:> > Hi Stefan, > > My apologies that I did not get back sooner. What you are suggesting > yields the same results I was seeing before. Let me try to explain > better: > >I think Stefan understood the question. He advised you on the appropriate interface for a resource type to use for adding generated resources to the catalog, but he was clearly uncertain about whether using that approach (which you should do regardless) would be sufficient to resolve your issue. I think the final answer is probably that the Puppet API does not provide a mechanism for what you''re asking. It''s certainly good that Puppet doesn''t automatically exhibit the behavior you describe, but I don''t think it provides even an optional way to obtain the defaults. If it did, however, then probably it would be associated with a scope object, since resource defaults are (dynamic) scope-dependent. Can you implement your resource type as a defined type? That would give you resource defaults automatically. John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.