Ilya Dmitrichenko
2013-Jan-11 15:59 UTC
[Puppet Users] Setting defaults on a type and in a class before defining a class resource
Hi, I''m have this in my nginx module: class nginx::package { package { ''nginx'': ensure => latest } ### there is more code here handling different ditro versions ... etc } class nginx::service { service { ''nginx'': ensure => running, enable => true, hasrestart => true, restart => ''/etc/init.d/nginx reload'', require => Class[''nginx::package'', ''nginx::config''], subscribe => Class[''nginx::package'', ''nginx::config''], } ### there is more code here ... } class nginx::config { File { require => Class[''nginx::package''], notify => Class[''nginx::service''], } define extra ($source=nil, $template=nil) { file { "/etc/nginx/sites-enabled/$name": source => $source } if $require { File["/etc/nginx/sites-enabled/$name"] { require +> $require } } if $notify { File["/etc/nginx/sites-enabled/$name"] { notify +> $notify } } } } When I originally wrote this, it was actually working and adding nginx::config::extra did notify nginx::service, but now it somewhat doesn''t (Puppet 2.7.11). Could it be a recent bug or it only seemed to work? Actually, I realise now that I may have had Puppet 2.7.17 when I was testing this originally ... could it be something that has been fixed after 2.7.11? Cheers, -- Ilya -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Ilya Dmitrichenko
2013-Jan-11 17:47 UTC
[Puppet Users] Re: Setting defaults on a type and in a class before defining a class resource
Ok, I tried this with 2.7.20 and it''s the same ... But copying `File { }` into the `define extra` block fixed it for me. That''s a bit of an unexpected kind of scoping, isn''t it? -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Erik Dalén
2013-Jan-11 18:01 UTC
Re: [Puppet Users] Re: Setting defaults on a type and in a class before defining a class resource
At least in puppet 3 resource defaults are local to defines and classes and doesn''t get inherited to any included defines or classes. So to make upgrading easier you shouldn''t make any such assumptions in your code. On Jan 11, 2013 6:47 PM, "Ilya Dmitrichenko" <errordeveloper@gmail.com> wrote:> Ok, I tried this with 2.7.20 and it''s the same ... > > But copying `File { }` into the `define extra` block fixed it for me. > That''s a bit of an unexpected kind of scoping, isn''t it? > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Dan White
2013-Jan-11 18:03 UTC
Re: [Puppet Users] Re: Setting defaults on a type and in a class before defining a class resource
Would you please post the code from the first posting with the correction that makes it work. I am confused and would like to see the difference. Thanks. “Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.” Bill Waterson (Calvin & Hobbes) ----- Original Message ----- From: "Ilya Dmitrichenko" <errordeveloper@gmail.com> To: puppet-users@googlegroups.com Sent: Friday, January 11, 2013 12:47:29 PM Subject: [Puppet Users] Re: Setting defaults on a type and in a class before defining a class resource Ok, I tried this with 2.7.20 and it''s the same ... But copying `File { }` into the `define extra` block fixed it for me. That''s a bit of an unexpected kind of scoping, isn''t it? -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
jcbollinger
2013-Jan-11 19:48 UTC
Re: [Puppet Users] Re: Setting defaults on a type and in a class before defining a class resource
On Friday, January 11, 2013 12:01:52 PM UTC-6, Erik Dalén wrote:> > At least in puppet 3 resource defaults are local to defines and classes > and doesn''t get inherited to any included defines or classes. So to make > upgrading easier you shouldn''t make any such assumptions in your code. >That is not correct (see http://docs.puppetlabs.com/puppet/3/reference/lang_defaults.html). The problem is that resource defaults are dynamically scoped (even in Puppet 3), not lexically scoped. Roughly speaking, that means defaults follow the chain of declarations, regardless of the location of the definitions of the classes and definitions in the chain. Thus, if the OP''s original code declared a File resource directly in class nginx::config, then File defaults declared in that class would apply to it. If it declared an nginx::config::extra instance directly in class nginx::config, then the File defaults of nginx::config and of nginx::config extra would apply to it. But if an instance of nginx::config::extra is declared by class mymodule::foo, then the File defaults that apply to that instance are those that apply in mymodule::foo, as modified by the default declarations appearing directly in nginx::config::extra. Moving the File defaults declaration from nginx::config into the definition is a perfectly valid way to achieve the desired result. Dynamic scoping of resource defaults can indeed produce surprises, but assuming local scope for defaults is not the solution (that incorrect assumption could also produce surprises). There would, however, be less likelihood of confusion in the present case if the definition were not nested inside a class. PuppetLabs style recommendations include to avoid nesting classes or definitions inside classes, and I am convinced that following that recommendation yields better code. John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/OZlLgLG_EW0J. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Erik Dalén
2013-Jan-12 10:36 UTC
Re: [Puppet Users] Re: Setting defaults on a type and in a class before defining a class resource
On Jan 11, 2013 8:49 PM, "jcbollinger" <John.Bollinger@stjude.org> wrote:> > > > On Friday, January 11, 2013 12:01:52 PM UTC-6, Erik Dalén wrote: >> >> At least in puppet 3 resource defaults are local to defines and classesand doesn''t get inherited to any included defines or classes. So to make upgrading easier you shouldn''t make any such assumptions in your code.> > > That is not correct (seehttp://docs.puppetlabs.com/puppet/3/reference/lang_defaults.html). The problem is that resource defaults are dynamically scoped (even in Puppet 3), not lexically scoped. Roughly speaking, that means defaults follow the chain of declarations, regardless of the location of the definitions of the classes and definitions in the chain. Hmm, okay I stand corrected. But I think the intention was at one point at least to remove dynamic scoping of resource defaults in puppet 3: http://docs.puppetlabs.com/guides/scope_and_puppet.html -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.