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>.
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?
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+unsubscr...@googlegroups.com
<mailto:puppet-users+unsubscr...@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/
--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-users/11c49969-0796-adbc-7d07-84b6b63a3b32%40puppet.com.
For more options, visit https://groups.google.com/d/optout.