Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Monkeypatch for chef solo to do basic data_bag manipulations
# save this in the library folder of a cookbook
# (e.g. ./coookbooks/vagrant/library/chef_solo_patch.rb)
# see also https://gist.github.com/867958 for vagrant patch
# based on http://lists.opscode.com/sympa/arc/chef/2011-02/msg00000.html
if Chef::Config[:solo]
class Chef
module Mixin
module Language
def data_bag(bag)
@solo_data_bags = {} if @solo_data_bags.nil?
unless @solo_data_bags[bag]
@solo_data_bags[bag] = {}
data_bag_path = Chef::Config[:data_bag_path]
Dir.glob(File.join(data_bag_path, bag, "*.json")).each do |f|
item = JSON.parse(IO.read(f))
@solo_data_bags[bag][item['id']] = item
end
end
@solo_data_bags[bag].keys
end
def data_bag_item(bag, item)
data_bag(bag) unless ( !@solo_data_bags.nil? && @solo_data_bags[bag])
@solo_data_bags[bag][item]
end
end
end
end
class Chef
class Recipe
def search(bag_name, query=nil)
Chef::Log.warn("Simplistic search patch, ignoring query of %s" % [query]) unless query.nil?
data_bag(bag_name.to_s).each do |bag_item_id|
bag_item = data_bag_item(bag_name.to_s, bag_item_id)
yield bag_item
end
end
end
end
end

Thanks for this snippet. A few questions if you don't mind:
Is this chef 0.10.0 compatible?
Must this go in the library subfolder of every cookbook? Anyone, or the first or last cookbook loaded?

Owner

timdiggins commented Jun 29, 2011

Haven't tried this out with chef 0.10.0 (developed against 0.9.14) - but can't see why it wouldn't work.

Only needs to be in one library subfolder. I'm not 100% sure about whether the order of cookbook is important, but I tend to put it in the library subfolder of my "vagrant" cookbook, which I tend to put first (and only in the vagrant node obviously).

BTW Need to read the rest of the config at http://red56.wordpress.com/2011/03/13/chef-and-vagrant-workflow/ to make it work with vagrant.

Brilliant thank. Will let you kow how I go on chef 0.10.0

I'm not familiar with the behavior of the library folder in chef. Is it like rspec's and cucumber's support/ where any ruby file in there is automagically required?
I suppose I'm a little puzzled about where these patch files should be required/included.

Appreciate any insight you can offer.

Owner

timdiggins commented Jun 30, 2011

I don't know exactly how it works, but know from experience that library files are evaluated on any node which is using a recipe from their cookbook (so probably required by chef during recipe discovery/loading).

Because the recipe files are evaluated in Chef::Recipe context (like an extend/include I suppose) which itself includes various mixins, you can see how the monkey patching above overrides the existing methods.

Might be worth asking the question on the chef-list.

hedgehog commented Jul 1, 2011

Hmm, it looks like data_bags_path is now in Vagrant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment