Installing Gems for a LWRP


#1

Hello,

I am working on implementing COOK-847, and have encountered a
chicken-and-egg problem. I need to install the certificate_authority
gem; normally I would simply call chef_gem for something like this.
However, since the default recipe is likely to be executed after the
provider I am not able to do this. I also am unable to find a standard
Mixin for chef_gem in Chef 11.x.

What is the correct way to install a gem for Chef to use so I can
require it in my provider?

-Ryan H.
This electronic message contains information which may be confidential or privileged. The information is intended for the use of the individual or entity named above. If you are not the intended recipient, be aware that any disclosure, copying, distribution or use of the contents of this information is prohibited. If you have received this electronic transmission in error, please notify us by e-mail at (postmaster@rapid7.com) immediately.


#2

That’s difficult. I’ve found the easiest way is to put the onus of the dependency on the client of your lwrp

Plaese forgive typos – sent via phone

On May 1, 2013, at 20:55, Ryan Hass Ryan_Hass@rapid7.com wrote:

Hello,

I am working on implementing COOK-847, and have encountered a
chicken-and-egg problem. I need to install the certificate_authority
gem; normally I would simply call chef_gem for something like this.
However, since the default recipe is likely to be executed after the
provider I am not able to do this. I also am unable to find a standard
Mixin for chef_gem in Chef 11.x.

What is the correct way to install a gem for Chef to use so I can
require it in my provider?

-Ryan H.
This electronic message contains information which may be confidential or privileged. The information is intended for the use of the individual or entity named above. If you are not the intended recipient, be aware that any disclosure, copying, distribution or use of the contents of this information is prohibited. If you have received this electronic transmission in error, please notify us by e-mail at (postmaster@rapid7.com) immediately.


#3

Ditto what he said, but check out what we do in the AWS cookbook.

Granted this is a library, so adapt as needed.

  • Julian

Sent from my smartphone. Sorry about any typos.

On May 2, 2013, at 1:15 AM, Matthew Hooker mwhooker@gmail.com wrote:

That’s difficult. I’ve found the easiest way is to put the onus of the
dependency on the client of your lwrp

Plaese forgive typos – sent via phone

On May 1, 2013, at 20:55, Ryan Hass Ryan_Hass@rapid7.com wrote:

Hello,

I am working on implementing COOK-847, and have encountered a

chicken-and-egg problem. I need to install the certificate_authority

gem; normally I would simply call chef_gem for something like this.

However, since the default recipe is likely to be executed after the

provider I am not able to do this. I also am unable to find a standard

Mixin for chef_gem in Chef 11.x.

What is the correct way to install a gem for Chef to use so I can

require it in my provider?

-Ryan H.

This electronic message contains information which may be confidential or
privileged. The information is intended for the use of the individual or
entity named above. If you are not the intended recipient, be aware that
any disclosure, copying, distribution or use of the contents of this
information is prohibited. If you have received this electronic
transmission in error, please notify us by e-mail at (postmaster@rapid7.com)
immediately.


#4

I tried this for my LWRP, but the issue I have is the first execution
results in an error being thrown due to the gem not being installed
until after the node is converged. It seems like I may have to create a
cookbook with the sole purpose of installing the required gem, and force
the openssl cookbook to have a dependency on said cookbook; but doing so
is pretty ugly.

-Ryan H.

On 05/02/2013 02:19 AM, Julian Dunn wrote:

Ditto what he said, but check out what we do in the AWS cookbook.

https://github.com/opscode-cookbooks/aws/blob/master/libraries/ec2.rb#L44

Granted this is a library, so adapt as needed.

  • Julian

Sent from my smartphone. Sorry about any typos.

On May 2, 2013, at 1:15 AM, Matthew Hooker <mwhooker@gmail.com
mailto:mwhooker@gmail.com> wrote:

That’s difficult. I’ve found the easiest way is to put the onus of
the dependency on the client of your lwrp

Plaese forgive typos – sent via phone

On May 1, 2013, at 20:55, Ryan Hass <Ryan_Hass@rapid7.com
mailto:Ryan_Hass@rapid7.com> wrote:

Hello,

I am working on implementing COOK-847, and have encountered a
chicken-and-egg problem. I need to install the certificate_authority
gem; normally I would simply call chef_gem for something like this.
However, since the default recipe is likely to be executed after the
provider I am not able to do this. I also am unable to find a standard
Mixin for chef_gem in Chef 11.x.

What is the correct way to install a gem for Chef to use so I can
require it in my provider?

-Ryan H.
This electronic message contains information which may be
confidential or privileged. The information is intended for the use
of the individual or entity named above. If you are not the intended
recipient, be aware that any disclosure, copying, distribution or
use of the contents of this information is prohibited. If you have
received this electronic transmission in error, please notify us by
e-mail at (postmaster@rapid7.com mailto:postmaster@rapid7.com)
immediately.

This electronic message contains information which may be confidential or privileged. The information is intended for the use of the individual or entity named above. If you are not the intended recipient, be aware that any disclosure, copying, distribution or use of the contents of this information is prohibited. If you have received this electronic transmission in error, please notify us by e-mail at (postmaster@rapid7.com) immediately.


#5

I wouldn’t call it ‘proper’ or even arguably less ugly than the cb
dependency route, but it works. This assumes the gem is already available
in your current sources, otherwise you’ll need to add a known good source
to Gem.sources as well or it will bail… You would also need to modify
this if you had dependencies to said ‘miraclegem’ with other sources.

DISCLAIMER:: PseudoRuby™

**may not actually work as-is

begin
Gem::Specification.find_by_name(‘miraclegem’, ‘>= 1.0.0’)
rescue Gem::LoadError
::Chef::Log.info(“Missing the miraclegem Gem. Installing…”)
require ‘rubygems/dependency_installer’

Gem::DependencyInstaller.new(Gem::DependencyInstaller::DEFAULT_OPTIONS).install(‘miraclegem’)
end

require ‘miraclegem’

hope that helps a bit…

Brandon

On Thu, May 2, 2013 at 3:15 PM, Ryan Hass Ryan_Hass@rapid7.com wrote:

I tried this for my LWRP, but the issue I have is the first execution
results in an error being thrown due to the gem not being installed until
after the node is converged. It seems like I may have to create a cookbook
with the sole purpose of installing the required gem, and force the openssl
cookbook to have a dependency on said cookbook; but doing so is pretty ugly.

-Ryan H.

On 05/02/2013 02:19 AM, Julian Dunn wrote:

Ditto what he said, but check out what we do in the AWS cookbook.

https://github.com/opscode-cookbooks/aws/blob/master/libraries/ec2.rb#L44

Granted this is a library, so adapt as needed.

  • Julian

Sent from my smartphone. Sorry about any typos.

On May 2, 2013, at 1:15 AM, Matthew Hooker mwhooker@gmail.com wrote:

That’s difficult. I’ve found the easiest way is to put the onus of the
dependency on the client of your lwrp

Plaese forgive typos – sent via phone

On May 1, 2013, at 20:55, Ryan Hass Ryan_Hass@rapid7.com wrote:

Hello,

I am working on implementing COOK-847, and have encountered a

chicken-and-egg problem. I need to install the certificate_authority

gem; normally I would simply call chef_gem for something like this.

However, since the default recipe is likely to be executed after the

provider I am not able to do this. I also am unable to find a standard

Mixin for chef_gem in Chef 11.x.

What is the correct way to install a gem for Chef to use so I can

require it in my provider?

-Ryan H.

This electronic message contains information which may be confidential
or privileged. The information is intended for the use of the individual or
entity named above. If you are not the intended recipient, be aware that
any disclosure, copying, distribution or use of the contents of this
information is prohibited. If you have received this electronic
transmission in error, please notify us by e-mail at (
postmaster@rapid7.com) immediately.

This electronic message contains information which may be confidential or
privileged. The information is intended for the use of the individual or
entity named above. If you are not the intended recipient, be aware that
any disclosure, copying, distribution or use of the contents of this
information is prohibited. If you have received this electronic
transmission in error, please notify us by e-mail at (
postmaster@rapid7.com) immediately.


#6

I am doing something similar for a a LWRP I am writing and in a helper
library I define the following method:

def install_chef_vault(source=“http://rubygems.org”, version=“1.2.0”)
installed ||= begin
gem_installer = Chef::Resource::ChefGem.new(“chef-vault”, run_context)
gem_installer.version version
gem_installer.options "–clear-sources --source #{source}"
gem_installer.action :install
gem_installer.after_created

require 'chef-vault'

end
end

Then where I need to know that the gem is installed for use (in other
functions in the library) I just call this method.

It’s not perfect yet because you would probably want to parameterize the
source and version but that could be easy enough to do.

Hope that helps!

Kevin

On 5/1/13 10:15 PM, “Matthew Hooker” mwhooker@gmail.com wrote:

That’s difficult. I’ve found the easiest way is to put the onus of the
dependency on the client of your lwrp

Plaese forgive typos – sent via phone

On May 1, 2013, at 20:55, Ryan Hass Ryan_Hass@rapid7.com wrote:

Hello,

I am working on implementing COOK-847, and have encountered a
chicken-and-egg problem. I need to install the certificate_authority
gem; normally I would simply call chef_gem for something like this.
However, since the default recipe is likely to be executed after the
provider I am not able to do this. I also am unable to find a standard
Mixin for chef_gem in Chef 11.x.

What is the correct way to install a gem for Chef to use so I can
require it in my provider?

-Ryan H.
This electronic message contains information which may be confidential
or privileged. The information is intended for the use of the individual
or entity named above. If you are not the intended recipient, be aware
that any disclosure, copying, distribution or use of the contents of
this information is prohibited. If you have received this electronic
transmission in error, please notify us by e-mail at
(postmaster@rapid7.com) immediately.