Using standard ruby modules in LWRP


#1

Hello,

Maybe someone can tell me if there’s a better way to do this.

I wrote an LWRPhttp://wiki.opscode.com/display/chef/Light-weight+Resources+and+Providers+(LWRP) that, given a user account, creates the user account, gid, directory, creates user’s authorized_keys. And I often like to use File.directory? or File.exist? and others in my recipes.

If I call File.directory? in a provider, however, Chef interprets it as Chef::Providers::File:Class (or something like that).

So a silly workaround I did was to create libraries/file.rb, and in it:
Class Chef
Class Recipe
Class Plaxo
Require 'ftools’
end
end
end

Then I can call Chef::Recipe::Plaxo::File.directory? and whatnot.

Is there a more elegant way of doing this? :slight_smile:

For those who are not familiar with LWRP, I encourage you to check it out. It’s a bit of work to understand and set it, but it allows me to do stuff like:
plaxo_user “opsman” do
action :set
end
Which fetches user info from data bags, creates group, user, sets up user’s authorized_keys, etc. Pretty cool, stuff, this LWRP.

-Paul


#2

prepend ‘::’ to qualify the namespace

example:
::File.directory?

On Nov 9, 2010, at 10:41 AM, Paul Choi wrote:

Hello,

Maybe someone can tell me if there’s a better way to do this.

I wrote an LWRP that, given a user account, creates the user account, gid, directory, creates user’s authorized_keys. And I often like to use File.directory? or File.exist? and others in my recipes.

If I call File.directory? in a provider, however, Chef interprets it as Chef::Providers::File:Class (or something like that).

So a silly workaround I did was to create libraries/file.rb, and in it:
Class Chef
Class Recipe
Class Plaxo
Require ‘ftools’
end
end
end

Then I can call Chef::Recipe::Plaxo::File.directory? and whatnot.

Is there a more elegant way of doing this? J

For those who are not familiar with LWRP, I encourage you to check it out. It’s a bit of work to understand and set it, but it allows me to do stuff like:
plaxo_user “opsman” do
action :set
end
Which fetches user info from data bags, creates group, user, sets up user’s authorized_keys, etc. Pretty cool, stuff, this LWRP.

-Paul


#3

Great, thanks!
I do remember seeing that somewhere while trying to learn Ruby.
Maybe I can add this to the wiki to warn those who aren’t so proficient in Ruby. :slight_smile:

-Paul

From: Alex Soto [mailto:apsoto@gmail.com]
Sent: Tuesday, November 09, 2010 10:52 AM
To: chef@lists.opscode.com
Subject: [chef] Re: Using standard ruby modules in LWRP

prepend ‘::’ to qualify the namespace

example:
::File.directory?

On Nov 9, 2010, at 10:41 AM, Paul Choi wrote:

Hello,

Maybe someone can tell me if there’s a better way to do this.

I wrote an LWRPhttp://wiki.opscode.com/display/chef/Light-weight+Resources+and+Providers+(LWRP) that, given a user account, creates the user account, gid, directory, creates user’s authorized_keys. And I often like to use File.directory? or File.exist? and others in my recipes.

If I call File.directory? in a provider, however, Chef interprets it as Chef::Providers::File:Class (or something like that).

So a silly workaround I did was to create libraries/file.rb, and in it:
Class Chef
Class Recipe
Class Plaxo
Require 'ftools’
end
end
end

Then I can call Chef::Recipe::Plaxo::File.directory? and whatnot.

Is there a more elegant way of doing this? :slight_smile:

For those who are not familiar with LWRP, I encourage you to check it out. It’s a bit of work to understand and set it, but it allows me to do stuff like:
plaxo_user “opsman” do
action :set
end
Which fetches user info from data bags, creates group, user, sets up user’s authorized_keys, etc. Pretty cool, stuff, this LWRP.

-Paul