Puppet counter part for ruby caller function/object

36 views
Skip to first unread message

Michel Verbraak

unread,
Dec 12, 2016, 10:37:58 AM12/12/16
to Puppet Users
I have been googling around to find an answer but was not able to find it.

I have created my own custom type in puppet. According to the documentation here Puppet Resource Types manual. This type creates a config file and in it I would like to add as comment lines information from where the resource is called/created.

I want to get the filename, classname, linenumber of the puppet class and pp file calling this custom type (resource)?

I know in Ruby I can use the "caller" function/variable/object to get a stack trace. But when I do this in my custom type I get a stack trace from Ruby and not Puppet.

For example I have the following code:

# File my_module/manifests/my_module.pp
class my_module{

  my_module_type( {'aname':;}
}

# File my_module/lib/puppet/type/my_module_type.pp
Puppet::Type.newtype(:my_module_type) do
  newparam(:trace) do
    defaultto caller.inspect
  end
end

So "caller.inspect" produces:

["/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet/util/classgen.rb:136:in `class_eval'", "/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet/util/classgen.rb:136:in `genthing'", "/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet/util/classgen.rb:36:in `genclass'", "/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet/type.rb:459:in `newparam'", "/vagrant/site/my_module/lib/puppet/type/my_module_type.rb:69:in `block in <top (required)>'", "/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet/util/classgen.rb:136:in `c...]

It nicely shows a stack trace of the different puppet ruby files used. But what I would like is something like:

[ my_module/manifests/my_module.pp:3:in "class my_module", .....]

Is this possible? If so how?

Regards,

Michel.


Henrik Lindberg

unread,
Dec 13, 2016, 5:54:29 AM12/13/16
to puppet...@googlegroups.com
On 12/12/16 11:11, Michel Verbraak wrote:
> I have been googling around to find an answer but was not able to find it.
>
> I have created my own custom type in puppet. According to the
> documentation here Puppet Resource Types manual
> <https://github.com/puppetlabs/puppet-specifications/blob/master/language/resource_types.md>.
The two locations you are trying to tie together are very far apart.
The creation of the resource takes place in a .pp file parsed and
evaluated by the compiler (typically on the master side). Much later the
agent side is acting on a instance of your resource type. That instance
was recreated from the information in the catalog that the compiler
produced.

The catalog contains file and line information for resources.
(In some versions of puppet this information is missing if the resources
were created with the create_resources function).

IIRC, you can get them via methods file() and line() on the resource.

As you already found out, the caller of the actual logic on the agent
side is something completely different.

- henrik

> Regards,
>
> Michel.
>
>
> --
> 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
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/cef67564-c584-4014-b21b-df28308418a5%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/cef67564-c584-4014-b21b-df28308418a5%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Michel Verbraak

unread,
Dec 13, 2016, 8:52:18 AM12/13/16
to Puppet Users


Op dinsdag 13 december 2016 11:54:29 UTC+1 schreef Henrik Lindberg:

Op dinsdag 13 december 2016 11:54:29 UTC+1 schreef Henrik Lindberg:

Thank you very much. Exactly what I was looking for.
I also experimented with a horrible:

    begin
      fail Puppet::ParseError, "dummy"
    rescue => detail
      self[:trace] = detail
    end

construction. It worked but the methods you mentioned are much nicer.
Reply all
Reply to author
Forward
0 new messages