Style (syntax?) question

37 views
Skip to first unread message

Peter Berghold

unread,
Jun 4, 2018, 11:25:39 AM6/4/18
to puppet-users
I was looking at someone else's code one day last week and saw a pattern I've not seen before. Maybe that's what I get for developing Puppet code in a vacuum. :-) 

class someclass (
    $parm1 = $::someclass::params::parm1,
    $parm2 = $::someclass::params::parm2       # so far I get it. 
) inherits someclass::params {             # ok, I get it

     class{'someclass::package': }        # OK
     -> class('someclass::configure':}    # right...
     -> Class{'someclass':}                    #  HUH?  What does that do? 
}

Is that last step necessary and why? 

Ramin K

unread,
Jun 4, 2018, 2:06:52 PM6/4/18
to puppet...@googlegroups.com
The last step was fairly common in Puppet 2.7 code before Anchors. It is
necessary if you want to do something like this.

class profile::mystack {

include ::otherclass
include ::someclass

Class['someclass'] -> Class['otherclass']
}

By adding that -> Class{'someclass':} to the end you create a chain that
requires all classes to completed before 'someclass' is completed.
Allows you to do the ordering I've shown.

In Puppet 3.4 the keyword 'contain' was introduced. The modern
implementation might look like

class someclass (
$parm1 = $::someclass::params::parm1,
$parm2 = $::someclass::params::parm2,
) inherits someclass::params {

contain someclass::package
contain someclass::configure

Class['someclass::package']
-> Class['someclass::configure']
}

fwiw a lot of my modules internally will mix and match include with
contain. In the case below I do not want containment around the yum repo
because in our system that's guaranteed to cause dependency cycles. Also
service usually encapsulate some systemd reloading which can cause the
same problem. Containing install and config is usually enough to allow
other modules to depend on my somedaemon module though not always.

class somedaemon {

include ::somedaemon::repo
contain ::somedaemon::install
contain ::somedaemon::config
include ::somedaemon::service

Class['::yum']
-> class['::somedaemon::install']
-> Class['::somedaemon::config']
~> Class['::somedaemon::service']
}

Ramin

jcbollinger

unread,
Jun 5, 2018, 9:01:21 AM6/5/18
to Puppet Users


On Monday, June 4, 2018 at 1:06:52 PM UTC-5, Ramin K wrote:
On 6/4/18 8:25 AM, Peter Berghold wrote:
> I was looking at someone else's code one day last week and saw a pattern
> I've not seen before. Maybe that's what I get for developing Puppet code
> in a vacuum. :-)
>
> class someclass (
>      $parm1 = $::someclass::params::parm1,
>      $parm2 = $::someclass::params::parm2       # so far I get it.
> ) inherits someclass::params {             # ok, I get it
>
>       class{'someclass::package': }        # OK
>       -> class('someclass::configure':}    # right...
>       -> Class{'someclass':}                    #  HUH?  What does that do?
> }
>
> Is that last step necessary and why?


The last step was fairly common in Puppet 2.7 code before Anchors.


Are you sure about that, Ramin?  I've been around Puppet since well before v2.7, and to the best of my knowledge, Class{'someclass':} (with capital 'C') is and always has been syntactically invalid.  I'm prepared to learn something new today, but you'll need to point me to some docs to support your assertion.

Myself, I'm inclined to guess that it's a simple typo, that an ordinary resource-style class declaration (with lowercase 'c') is what was intended.

 
[...]

 
By adding that -> Class{'someclass':} to the end you create a chain that
requires all classes to completed before 'someclass' is completed.


I don't think it's the chain operators that have Salty confused.  He says he's ok with a previous line that also uses one.  I'd be confused, too, if someone demonstrated that example code to compile successfully.


John

R.I.Pienaar

unread,
Jun 5, 2018, 9:10:31 AM6/5/18
to puppet...@googlegroups.com


On Tue, 5 Jun 2018, at 15:01, jcbollinger wrote:
>
>
> On Monday, June 4, 2018 at 1:06:52 PM UTC-5, Ramin K wrote:
> >
> > On 6/4/18 8:25 AM, Peter Berghold wrote:
> > > I was looking at someone else's code one day last week and saw a pattern
> > > I've not seen before. Maybe that's what I get for developing Puppet code
> > > in a vacuum. :-)
> > >
> > > class someclass (
> > > $parm1 = $::someclass::params::parm1,
> > > $parm2 = $::someclass::params::parm2 # so far I get it.
> > > ) inherits someclass::params { # ok, I get it
> > >
> > > class{'someclass::package': } # OK
> > > -> class('someclass::configure':} # right...
> > > -> Class{'someclass':} # HUH? What does that
> > do?
> > > }
> > >
> > > Is that last step necessary and why?
> >
> >
> > The last step was fairly common in Puppet 2.7 code before Anchors.
>
>
>
> Are you sure about that, Ramin? I've been around Puppet since well before
> v2.7, and to the best of my knowledge, Class{'someclass':} (with capital
> 'C') is and always has been syntactically invalid. I'm prepared to learn
> something new today, but you'll need to point me to some docs to support
> your assertion.
>
> Myself, I'm inclined to guess that it's a simple typo, that an ordinary
> resource-style class declaration (with lowercase 'c') is what was intended.
>

class{'someclass::package': }
-> class('someclass::configure':}
-> Class["someclass"] # or -> Class[$name]

Is the correct version of this. It's a "soft" version of the anchor pattern, because:

package{"foo": require => Class["someclass"]}

will have the inner classes completed before it without any crazy extra resources and stuff and it handles the case most people actually care for - but this is not a full contained class like contain() makes, you should probably use contain() today.

Arnau

unread,
Jun 5, 2018, 10:00:47 AM6/5/18
to puppet...@googlegroups.com
2018-06-05 15:01 GMT+02:00 jcbollinger <John.Bo...@stjude.org>:


On Monday, June 4, 2018 at 1:06:52 PM UTC-5, Ramin K wrote:
On 6/4/18 8:25 AM, Peter Berghold wrote:
> I was looking at someone else's code one day last week and saw a pattern
> I've not seen before. Maybe that's what I get for developing Puppet code
> in a vacuum. :-)
>
> class someclass (
>      $parm1 = $::someclass::params::parm1,
>      $parm2 = $::someclass::params::parm2       # so far I get it.
> ) inherits someclass::params {             # ok, I get it
>
>       class{'someclass::package': }        # OK
>       -> class('someclass::configure':}    # right...
>       -> Class{'someclass':}                    #  HUH?  What does that do?
> }
>
> Is that last step necessary and why?


The last step was fairly common in Puppet 2.7 code before Anchors.


Are you sure about that, Ramin?  I've been around Puppet since well before v2.7, and to the best of my knowledge, Class{'someclass':} (with capital 'C') is and always has been syntactically invalid.  I'm prepared to learn something new today, but you'll need to point me to some docs to support your assertion.

I've been using that syntax in 3.8 for a long time:

  contain ::soge::install
  contain ::soge::configure
  contain ::soge::service

  Class ['::soge::install'] ->
  Class ['::soge::configure'] ~>
  Class ['::soge::service']


I remeber start using it when the contain function was released...

( fun fact: https://groups.google.com/forum/#!topic/puppet-users/T1cIUuKBU0E isn't this you using "Class"? or am I missing something?

Best,
Arnau


Ramin K

unread,
Jun 5, 2018, 11:59:35 AM6/5/18
to puppet...@googlegroups.com
pattern I've not seen before." seems clear enough to me. Was pseudo code
and I didn't think to look at the syntax. Jumped straight to the
confusing part, a resource chain that ends on the class. I've answered
the same question several times in support of our Puppet system at work.
And then moved the module to contain... which generated more questions
about include vs require vs contain. No winning some days.

Ramin

jcbollinger

unread,
Jun 6, 2018, 8:30:29 AM6/6/18
to Puppet Users


On Tuesday, June 5, 2018 at 9:00:47 AM UTC-5, Arnau wrote:
 
( fun fact: https://groups.google.com/forum/#!topic/puppet-users/T1cIUuKBU0E isn't this you using "Class"? or am I missing something?

Sure is.  But those are (valid) resource / class references (Class['someclass']),  not the invalid mutant form appearing in the OP's example (Class { 'somclass': }).  Note differences in bracketing and presence vs absence of a colon.


John

Arnau

unread,
Jun 6, 2018, 8:42:59 AM6/6/18
to puppet...@googlegroups.com
You're totally right... I think my brain did reformat the OP message :-)
Now I see that even the second line is totally wrong:

     -> class('someclass::configure':}    # right... <- Opening with a parenthesis closing with a bracket...
     -> Class{'someclass':}    
 

John


Sorry for the noise,
Arnau 
Reply all
Reply to author
Forward
0 new messages