Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jsierles/422632 to your computer and use it in GitHub Desktop.
Save jsierles/422632 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
require 'rubygems'
require 'chef/data_bag'
require 'chef/data_bag_item'
Chef::Config.from_file(File.join(ENV['HOME'], '.chef', 'knife.rb'))
puts "Usage: bagger <bag> <source_file>" unless ARGV[0] && ARGV[1]
begin
bag = ARGV[0]
data = eval File.read(ARGV[1])
rescue Exception => e
STDERR.puts "Unable to load external data: #{e}"
end
print "Databag: #{bag} "
begin
@data_bag = Chef::DataBag.cdb_load(bag)
puts "loaded."
rescue Chef::Exceptions::CouchDBNotFound
@data_bag = Chef::DataBag.new
@data_bag.name(bag)
@data_bag.cdb_save
puts "created."
end
data.each do |name, item|
item["id"] = name.to_s.gsub(/\//, "_")
print " => #{item['id']} "
begin
@data_bag_item = Chef::DataBagItem.cdb_load(bag, item["id"])
@data_bag_item.raw_data = item
puts "updated. (#{@data_bag_item.cdb_save})"
rescue Chef::Exceptions::CouchDBNotFound
@data_bag_item = Chef::DataBagItem.new
@data_bag_item.data_bag(bag)
@data_bag_item.raw_data = item
puts "created. (#{@data_bag_item.cdb_save})"
end
end
@yevgeniyo-ps
Copy link

thx man

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