Hi, I''d like to DRY up some of my Puppet lib code and wanted to put helper classes in other parts of the "lib/" folder in my plugin. What is the best way to access these files? I can think of various ways that may be considered hacks but wanted to know if there was a preferred way. Best, Mitchell -- 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.
On Fri, Sep 14, 2012 at 4:48 PM, Mitchell Hashimoto < mitchell.hashimoto@gmail.com> wrote:> Hi, > > I''d like to DRY up some of my Puppet lib code and wanted to put helper > classes in other parts of the "lib/" folder in my plugin. >There are a couple of issues that prevent require from simply working, but all of them have fairly clean work arounds that are forward-compatible, meaning as soon as we fix them you won''t need to change your code. I did a slight refactor of our registry module to employ these work arounds. Hopefully the change provides a sound example to work from: https://github.com/puppetlabs/puppetlabs-registry/pull/18/files#L1R2 The issues to work around are #4248, #7788 (Fixed in 3.0.0), and #14149. -Jeff -- 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.
Mitchell Hashimoto
2012-Sep-18 17:15 UTC
Re: [Puppet Users] Arbitrary Code in plugin "lib/"
Jeff, On Tue, Sep 18, 2012 at 10:02 AM, Jeff McCune <jeff@puppetlabs.com> wrote:> On Fri, Sep 14, 2012 at 4:48 PM, Mitchell Hashimoto < > mitchell.hashimoto@gmail.com> wrote: > >> Hi, >> >> I''d like to DRY up some of my Puppet lib code and wanted to put helper >> classes in other parts of the "lib/" folder in my plugin. >> > > There are a couple of issues that prevent require from simply working, but > all of them have fairly clean work arounds that are forward-compatible, > meaning as soon as we fix them you won''t need to change your code. > > I did a slight refactor of our registry module to employ these work > arounds. Hopefully the change provides a sound example to work from: > > https://github.com/puppetlabs/puppetlabs-registry/pull/18/files#L1R2 > > The issues to work around are #4248, #7788 (Fixed in 3.0.0), and #14149. >Thanks! This looks like basically what I was planning on doing, so it is good to see that this works. Can you expand on how #4248 is worked around by your pull? Best, Mitchell -- 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.
On Tue, Sep 18, 2012 at 10:15 AM, Mitchell Hashimoto < mitchell.hashimoto@gmail.com> wrote:> Thanks! This looks like basically what I was planning on doing, so it is > good to see that this works. >I''m glad.> Can you expand on how #4248 is worked around by your pull? >Sure, the issue is that the lib sub-directory of a puppet module isn''t added to Ruby''s $LOAD_PATH. This means a regular `require "foo/bar"` won''t find <modulepath>/<mymodule>/lib/foo/bar.rb and a LoadError will be raised. To work around this problem, my patch first tries the plain `require "foo/bar"` and catches any LoadErrors that are raised. I do this first because I expect #4248 to be fixed in the future which will make `require "foo/bar"` work as I expect it to. When I catch the load error, I try again, but this time I use the special __FILE__ variable to get the full path of the ruby file being executed, get the dirname, then construct an absolute path to the file I''m actually trying to load. If this second absolute path attempt fails then the exception will be raised normally. All of this is happening here: https://github.com/jeffmccune/puppetlabs-registry/blob/67b7ffd8d973d50b8baf987eee4324c220d936db/lib/puppet/provider/registry_key/registry.rb#L2-12 The comment, "# Work around #7788 (Rubygems support for modules)" really should say "Work around #4248 and #7788" In Telly (Puppet 3.0.0), we support plugins distributed as rubygems, so `require "foo/bar"` will work in this scenario, but only if the plugin is inside of a Gem. In this case, it''s not, it''s inside of a puppet module. -Jeff -- 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.
Mitchell Hashimoto
2012-Sep-19 00:00 UTC
Re: [Puppet Users] Arbitrary Code in plugin "lib/"
On Tue, Sep 18, 2012 at 1:46 PM, Jeff McCune <jeff@puppetlabs.com> wrote:> On Tue, Sep 18, 2012 at 10:15 AM, Mitchell Hashimoto < > mitchell.hashimoto@gmail.com> wrote: > >> Thanks! This looks like basically what I was planning on doing, so it is >> good to see that this works. >> > > I''m glad. > > >> Can you expand on how #4248 is worked around by your pull? >> > > Sure, the issue is that the lib sub-directory of a puppet module isn''t > added to Ruby''s $LOAD_PATH. This means a regular `require "foo/bar"` won''t > find <modulepath>/<mymodule>/lib/foo/bar.rb and a LoadError will be raised. > > To work around this problem, my patch first tries the plain `require > "foo/bar"` and catches any LoadErrors that are raised. I do this first > because I expect #4248 to be fixed in the future which will make `require > "foo/bar"` work as I expect it to. > > When I catch the load error, I try again, but this time I use the special > __FILE__ variable to get the full path of the ruby file being executed, get > the dirname, then construct an absolute path to the file I''m actually > trying to load. > > If this second absolute path attempt fails then the exception will be > raised normally. > > All of this is happening here: > > > https://github.com/jeffmccune/puppetlabs-registry/blob/67b7ffd8d973d50b8baf987eee4324c220d936db/lib/puppet/provider/registry_key/registry.rb#L2-12 > > The comment, "# Work around #7788 (Rubygems support for modules)" really > should say "Work around #4248 and #7788" > > In Telly (Puppet 3.0.0), we support plugins distributed as rubygems, so > `require "foo/bar"` will work in this scenario, but only if the plugin is > inside of a Gem. In this case, it''s not, it''s inside of a puppet module. > >Most of my modules are distributed using rsync onto a puppetmaster and not via gems. In this case, then, I still need to use __FILE__ and so on right? Or does Puppet 3.0.0 add those files to the load path as well?> -Jeff > > -- > 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.
On Tue, Sep 18, 2012 at 5:00 PM, Mitchell Hashimoto < mitchell.hashimoto@gmail.com> wrote:> On Tue, Sep 18, 2012 at 1:46 PM, Jeff McCune <jeff@puppetlabs.com> wrote: > >> >> In Telly (Puppet 3.0.0), we support plugins distributed as rubygems, so >> `require "foo/bar"` will work in this scenario, but only if the plugin is >> inside of a Gem. In this case, it''s not, it''s inside of a puppet module. >> >> > Most of my modules are distributed using rsync onto a puppetmaster and not > via gems. In this case, then, I still need to use __FILE__ and so on right? > Or does Puppet 3.0.0 add those files to the load path as well? >Puppet 3.0.0 does not add the <modulepath>/<modulename>/lib directories to the load path, so yes, you do need to use the __FILE__ work around with your rsync scenario. -Jeff -- 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.