Data bag implementation for chef-solo


#1

We often use chef-solo for quick testing, etc. This is our extremely simple
implementation of data bags for chef-solo. Add “data_bag_path
/some/dir/data_bags” to your solo.rb and add this into your libraries:

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] = {}
        Dir.glob(File.join(Chef::Config[: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
end

I’m an old school C guy and I blame most of the code on a co-worker :wink: Use
at your own risk, will probably eat your hard drive, etc.

–Brian


#2

Heh, we were just talking about this in #vagrant the other day. This looks awesome; I’ll try this out. Thanks!


Ryan C. Creasey
Principal Systems Engineer
Gamespy Technology
IGN Entertainment

On Feb 1, 2011, at 7:42 AM, Brian Akins wrote:

We often use chef-solo for quick testing, etc. This is our extremely simple implementation of data bags for chef-solo. Add “data_bag_path /some/dir/data_bags” to your solo.rb and add this into your libraries:

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] = {}
        Dir.glob(File.join(Chef::Config[: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
end

I’m an old school C guy and I blame most of the code on a co-worker :wink: Use at your own risk, will probably eat your hard drive, etc.

–Brian


#3

thanks a lot! I needed this the other day.

– Thibaut


#4

Feel free to file a ticket and patch Chef if this is useful for you.

Adam

On Tue, Feb 1, 2011 at 7:42 AM, Brian Akins brian@akins.org wrote:

We often use chef-solo for quick testing, etc. This is our extremely simple
implementation of data bags for chef-solo. Add “data_bag_path
/some/dir/data_bags” to your solo.rb and add this into your libraries:

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] = {}
        Dir.glob(File.join(Chef::Config[: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
end

I’m an old school C guy and I blame most of the code on a co-worker :wink: Use
at your own risk, will probably eat your hard drive, etc.

–Brian


Opscode, Inc.
Adam Jacob, Chief Product Officer
T: (206) 619-7151 E: adam@opscode.com


#5

On Monday, February 7, 2011 at 10:26 AM, Thibaut Barrère wrote:
Did anyone file a ticket for that ? I’d like to vote for it.

I haven’t seen a ticket, but as long as there are tests and CLAs are taken care of, we’d happily merge this.

Dan DeLeo


#6

Yeah, it would be great to get this into Chef!

2011/2/1 Adam Jacob adam@opscode.com:

Feel free to file a ticket and patch Chef if this is useful for you.

Adam

On Tue, Feb 1, 2011 at 7:42 AM, Brian Akins brian@akins.org wrote:

We often use chef-solo for quick testing, etc. This is our extremely simple
implementation of data bags for chef-solo. Add “data_bag_path
/some/dir/data_bags” to your solo.rb and add this into your libraries:

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] = {}
        Dir.glob(File.join(Chef::Config[: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
end

I’m an old school C guy and I blame most of the code on a co-worker :wink: Use
at your own risk, will probably eat your hard drive, etc.

–Brian


Opscode, Inc.
Adam Jacob, Chief Product Officer
T: (206) 619-7151 E: adam@opscode.com


#7

Did anyone file a ticket for that ? I’d like to vote for it.

– Thibaut

On Wed, Feb 2, 2011 at 10:40 AM, Miquel Torres tobami@googlemail.comwrote:

Yeah, it would be great to get this into Chef!

2011/2/1 Adam Jacob adam@opscode.com:

Feel free to file a ticket and patch Chef if this is useful for you.

Adam

On Tue, Feb 1, 2011 at 7:42 AM, Brian Akins brian@akins.org wrote:

We often use chef-solo for quick testing, etc. This is our extremely
simple

implementation of data bags for chef-solo. Add “data_bag_path
/some/dir/data_bags” to your solo.rb and add this into your libraries:

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] = {}
        Dir.glob(File.join(Chef::Config[: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
end

I’m an old school C guy and I blame most of the code on a co-worker :wink:
Use

at your own risk, will probably eat your hard drive, etc.

–Brian


Opscode, Inc.
Adam Jacob, Chief Product Officer
T: (206) 619-7151 E: adam@opscode.com