How to check if a class is being used?

2,029 views
Skip to first unread message

Guy Matz

unread,
Apr 1, 2015, 11:15:45 AM4/1/15
to puppet...@googlegroups.com
Hi!  I have a process that is controlled by daemontools on some servers, and by init on other (i know, i know) and I need a way to have a Service definition when daemontools is not in the run.  

I've tried this:
  unless defined(Class['daemontools']) {
    notify { 'I Do not see daemontools': }
    service { $dw_service:
      ensure    => 'running'
    }
  }

but that didn't work.  I've also tried Class['Daemontools'].

Daemontools is the module being used, but it's a actually a subclass, daemontools::service, that's being called . . . 
So I've also tried Class['daemontools::service'] as well as Class['Daemontools::Service'].  I've tried Class['::Daemontools::Service'], which didn't work either.

I added a notify in daemontools::service which gave me this:
Notice: /Stage[main]/Profiles::Dropwizard::Booker-integration/Daemontools::Service[booker-integration]/Notify[Here I am!!]/message: defined 'message' as 'Here I am!!'

So I also tried "unless defined(Class['Daemontools::Service[booker-integration]']), which, of course, did not work.

Anyone know what I need to get the "unless" to catch whether I am using daemontools::service?  And even better, doers anyone know how I could have debugged this to find this out for myself?

Thanks so much!
Guy

Guy Matz

unread,
Apr 1, 2015, 11:27:05 AM4/1/15
to puppet...@googlegroups.com

Guy Matz

unread,
Apr 1, 2015, 2:35:06 PM4/1/15
to puppet...@googlegroups.com
I've seen some folks say this should work:

if defined(::class::subclass)

but it doesn't seem to work for me.  Again, any thoughts would be greatly appreciated!

Regards,
Guy

paul.gomersbach

unread,
Apr 2, 2015, 7:52:45 AM4/2/15
to puppet...@googlegroups.com
This is working for me:

  if ! defined(Class['ops_logging']) {
    class { 'ops_logging':
      collectives   => $collectives,
      middleware    => $rabbithost,
      metrics       => $metrics,
      configuration => $configuration,
      monitor       => $monitor,
    }
  }


Op woensdag 1 april 2015 20:35:06 UTC+2 schreef guymatz:

Dan White

unread,
Apr 2, 2015, 8:03:17 AM4/2/15
to puppet...@googlegroups.com
There is something basically wrong with this, IMHO

If you need to do this, it means that there could be more than one place to instantiate the class, and we all know the problems caused by doing it more than once.

You may want to consider refactoring to separate the class instantiation from the multiple locations.“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)
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/99788530-31bd-455f-b2e4-8801982cd09b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Guy Matz

unread,
Apr 2, 2015, 8:28:43 AM4/2/15
to puppet...@googlegroups.com
Thanks!!  I'm not sure that solves my problem, though  .. . .    and maybe I'm using "defined" incorrectly . . .  what I really want is a way to check to see if a class has been included or assigned . . . .  then I would like to make a decision based on that info.  Does defined tell me if the class is being used in that run?  Or just if it is defined in puppet at all?  It's beginning to appear that it's the latter.  

So, is there any way to tell if a class/subclass is being applied to a server?

Thanks!
Guy

Guy Matz

unread,
Apr 2, 2015, 8:29:18 AM4/2/15
to puppet...@googlegroups.com
Thanks!!  I'm not sure that solves my problem, though  .. . .    and maybe I'm using "defined" incorrectly . . .  what I really want is a way to check to see if a class has been included or assigned . . . .  then I would like to make a decision based on that info.  Does defined tell me if the class is being used in that run?  Or just if it is defined in puppet at all?  It's beginning to appear that it's the latter.  

So, is there any way to tell if a class/subclass is being applied to a server?

Thanks!
On Thu, Apr 2, 2015 at 7:52 AM, paul.gomersbach <p.gome...@gmail.com> wrote:

jcbollinger

unread,
Apr 2, 2015, 9:22:35 AM4/2/15
to puppet...@googlegroups.com


On Wednesday, April 1, 2015 at 10:15:45 AM UTC-5, Guy Matz wrote:
Hi!  I have a process that is controlled by daemontools on some servers, and by init on other (i know, i know) and I need a way to have a Service definition when daemontools is not in the run.  

I've tried this:
  unless defined(Class['daemontools']) {
    notify { 'I Do not see daemontools': }
    service { $dw_service:
      ensure    => 'running'
    }
  }

but that didn't work.  I've also tried Class['Daemontools'].


You are approaching the problem the wrong way.  It is never a good idea to base decisions about which classes and resources to declare on which have already been declared at some particular point during catalog building.  Doing so introduces an evaluation-order dependency, which will then hide in a dark corner, waiting for an opportunity to leap out and clamp its jaws on your backside.

Ideally, you wouldn't even have to worry about this.  If on all machines you are using Puppet's idea of the default Service provider, then just declare your service resource somewhere, without regard to whether it will be handled by daemontools or not.  All nodes that need the service get it from the same declaration.

If you are using daemontools to handle the service of interest on some machines where another provider (e.g. init) is the default, then you still, at some level, want all nodes that need it to obtain it via the same declaration.  In that case, you must already have a means to know based on node identity and/or facts whether your machines rely on daemontools.  Use that mechanism to control details of the service declaration, and any ancillary declarations.  A simple implemention of that might go thusly:
 
class myservice::service ($usedaemontools = 'false') {
  include
'myservice::software'  
  service
{ 'myservice':
    enable  
=> true,
   
ensure   => running,
    provider
=> $usedaemontools ? {
     
'true'   => 'daemontools',
     
default  => undef
   
},
   
require  => Package['myservice']
 
}
}

That same class would be declared for every node that needs service "myservice"; a class parameter (preferably obtaining its value via automatic data binding) determines whether the provider is overridden to "daemontools".

Your needs may be more complex, but the same general idea can be applied.  It boils down to relying directly on data to determine what to declare, not on what has already been declared.


John

Reply all
Reply to author
Forward
0 new messages