Created
November 22, 2010 21:42
-
-
Save lusis/710759 to your computer and use it in GitHub Desktop.
Splits a single JSON array file into distinct files for data bags in chef
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
require 'json' | |
require 'optparse' | |
all_json = [] | |
options = {} | |
options[:overwrite] = false | |
optparse = OptionParser.new do |opts| | |
opts.banner = "Usage: split-em -f FILE -d DATABAG [-o]" | |
opts.on( '-f' ,'--file FILE', 'JSON file to split') do |file| | |
options[:file] = file | |
end | |
opts.on( '-d' ,'--databag DATABAGNAME', 'Data Bag to use for knife output') do |databag| | |
options[:databag] = databag | |
end | |
opts.on( '-o','--overwrite', 'Overwrite existing JSON') do | |
options[:overwrite] = true | |
end | |
end | |
optparse.parse! | |
json_data = JSON.load(File.open(options[:file], "r")) || exit(1) | |
basedir = File.dirname(options[:file]) | |
json_data.each do |item| | |
all_json << item['id'] | |
outfile = File.join(basedir, "#{item['id']}.json") | |
puts "Parsing data for #{item['id']} into file #{outfile}" | |
if File.exists?(outfile) && options[:overwrite] == false | |
puts "Found existing file for #{item['id']}. Not overwriting" | |
else | |
datafile = File.new(outfile, "w+") | |
datafile.write(item.to_json) | |
datafile.close | |
end | |
end | |
puts "#Run the following command to load the split bags into the #{options[:databag]} in chef" | |
puts "for i in #{all_json.join(' ')}; do knife data bag from file #{options[:databag]} $i.json; done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment