Forked from jsierles/Update a chef databag from a ruby hash
Created
March 10, 2021 20:49
-
-
Save indygwyn/57b24845993e56940ab9e5c47f2d2c40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment