Question

I'm writing an LWRP to seed a redis database with API keys to allow for authentication. My trouble is using the redis library for ruby. I've searched around and found a few examples online and nothing has worked for me.

I am running this on AWS OpsWorks so it is using chef-solo

I've tried including a recipe in my run list that installs the redis gem (https://github.com/brianbianco/redisio/blob/master/recipes/redis_gem.rb)

I've also tried installing them gem inside of the cookbook.

    r = gem_package "redis" do
      action :install
    end

    r.run_action(:install)

or

    r = chef_gem "redis" do
      action :install
    end

    r.run_action(:install)

This is the error that I'm am getting on my chef run

[2013-10-03T16:11:41+00:00] DEBUG: filtered backtrace of compile error: 
[2013-10-03T16:11:41+00:00] DEBUG: filtered backtrace of compile error: 
[2013-10-03T16:11:41+00:00] DEBUG: backtrace entry for compile error: '/opt/aws/opsworks/releases/20130926123105_208/site-cookbooks/ilnkmx/providers/add_app.rb:1:in `require''
[2013-10-03T16:11:41+00:00] DEBUG: Line number of compile error: '1'
[2013-10-03T16:11:42+00:00] ERROR: Caught exception while compiling OpsWorks custom run list: LoadError - no such file to load -- redis - /opt/aws/opsworks/releases/20130926123105_208/site-cookbooks/ilnkmx/providers/add_app.rb:1:in `require'

I'm new to ruby so any and all help is appreciate, thank you.

Was it helpful?

Solution

So it seems I was missing a small piece and I had a few things in the wrong place.

First I needed to refresh the gems after installing the redis gem in my recipe which looks like this.

r = chef_gem "redis" do
  action :nothing
end

r.run_action(:install)
Gem.clear_paths

I was also requiring the library in my Provider which was incorrect. I needed to require it in my recipe after Gem.clear_paths then in my provider I would open the connection and preform add, delete, or update records which looks like this.

action :create do
    if @current_resource.exists
        Chef::Log.info "#{ @new_resource } already exist - nothing to do."
    else
        converge_by("Create #{ @new_resource }") do
            create_app_key
        end
    end
end

def create_app_key
  redis = ::Redis.new
  redis.set "#{@new_resource.app_name}", "#{@new_resource.api_key}"
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top