Class: Spiceweasel::DataBags
- Inherits:
-
Object
- Object
- Spiceweasel::DataBags
- Includes:
- CommandHelper
- Defined in:
- lib/spiceweasel/data_bags.rb
Overview
manages parsing of Data Bags
Instance Attribute Summary collapse
-
#create ⇒ Object
readonly
Returns the value of attribute create.
-
#delete ⇒ Object
readonly
Returns the value of attribute delete.
Instance Method Summary collapse
- #data_bag_from_file(db, items, secret) ⇒ Object
- #identify_items_secret(data_bag, db) ⇒ Object
-
#initialize(data_bags = []) ⇒ DataBags
constructor
rubocop:disable CyclomaticComplexity.
- #invoke_validate_item(db, items, secret) ⇒ Object
- #knife_data_bag_create_delete(data_bag) ⇒ Object
-
#validate_item(db, item) ⇒ Object
validate the item to be loaded.
Methods included from CommandHelper
#bundler?, #create_command, #delete_command
Constructor Details
#initialize(data_bags = []) ⇒ DataBags
rubocop:disable CyclomaticComplexity
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/spiceweasel/data_bags.rb', line 31 def initialize(data_bags = []) # rubocop:disable CyclomaticComplexity @create = [] @delete = [] return unless data_bags Spiceweasel::Log.debug("data bags: #{data_bags}") data_bags.each do |data_bag| db, items, secret = knife_data_bag_create_delete(data_bag) items = invoke_validate_item(db, items, secret) items.delete_if { |x| x.include?('*') } # remove wildcards items.sort!.uniq! data_bag_from_file(db, items, secret) end end |
Instance Attribute Details
#create ⇒ Object (readonly)
Returns the value of attribute create.
29 30 31 |
# File 'lib/spiceweasel/data_bags.rb', line 29 def create @create end |
#delete ⇒ Object (readonly)
Returns the value of attribute delete.
29 30 31 |
# File 'lib/spiceweasel/data_bags.rb', line 29 def delete @delete end |
Instance Method Details
#data_bag_from_file(db, items, secret) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/spiceweasel/data_bags.rb', line 51 def data_bag_from_file(db, items, secret) return if items.empty? if secret create_command("knife data bag#{Spiceweasel::Config[:knife_options]} from file #{db} #{items.join('.json ')}.json --secret-file #{secret}") else create_command("knife data bag#{Spiceweasel::Config[:knife_options]} from file #{db} #{items.join('.json ')}.json") end end |
#identify_items_secret(data_bag, db) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/spiceweasel/data_bags.rb', line 99 def identify_items_secret(data_bag, db) items = nil secret = nil if data_bag[db] items = data_bag[db]['items'] secret = data_bag[db]['secret'] if secret && !File.exist?(File.(secret)) && !Spiceweasel::Config[:novalidation] STDERR.puts "ERROR: secret key #{secret} not found, unable to load encrypted data bags for data bag #{db}." exit(-1) end end return items, secret end |
#invoke_validate_item(db, items, secret) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/spiceweasel/data_bags.rb', line 61 def invoke_validate_item(db, items, secret) items = [] if items.nil? Spiceweasel::Log.debug("data bag: #{db} #{secret} #{items}") items.each do |item| Spiceweasel::Log.debug("data bag #{db} item: #{item}") if item =~ /\*/ # wildcard support, will fail if directory not present files = Dir.glob("data_bags/#{db}/#{item}") # remove anything not ending in .json files.delete_if { |x| !x.end_with?('.json') } items.concat(files.map { |x| x["data_bags/#{db}/".length..-6] }) Spiceweasel::Log.debug("found files '#{files}' for data bag: #{db} with wildcard #{item}") next end validate_item(db, item) unless Spiceweasel::Config[:novalidation] end items end |
#knife_data_bag_create_delete(data_bag) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/spiceweasel/data_bags.rb', line 79 def knife_data_bag_create_delete(data_bag) db = data_bag.keys.first # check directories if !File.directory?('data_bags') && !Spiceweasel::Config[:novalidation] STDERR.puts "ERROR: 'data_bags' directory not found, unable to validate or load data bag items" exit(-1) end if !File.directory?("data_bags/#{db}") && !Spiceweasel::Config[:novalidation] STDERR.puts "ERROR: 'data_bags/#{db}' directory not found, unable to validate or load data bag items" exit(-1) end create_command("knife data bag#{Spiceweasel::Config[:knife_options]} create #{db}") delete_command("knife data bag#{Spiceweasel::Config[:knife_options]} delete #{db} -y") items, secret = identify_items_secret(data_bag, db) return db, items, secret end |
#validate_item(db, item) ⇒ Object
validate the item to be loaded
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/spiceweasel/data_bags.rb', line 114 def validate_item(db, item) unless File.exist?("data_bags/#{db}/#{item}.json") STDERR.puts "ERROR: data bag '#{db}' item '#{item}' file 'data_bags/#{db}/#{item}.json' does not exist" exit(-1) end f = File.read("data_bags/#{db}/#{item}.json") begin itemfile = JSON.parse(f) rescue JSON::ParserError => e # invalid JSON STDERR.puts "ERROR: data bag '#{db} item '#{item}' has JSON errors." STDERR.puts e. exit(-1) end # validate the id matches the file name item = item.split('/').last if item =~ /\// # pull out directories return if item.eql?(itemfile['id']) STDERR.puts "ERROR: data bag '#{db}' item '#{item}' listed in the manifest does not match the id '#{itemfile['id']}' within the 'data_bags/#{db}/#{item}.json' file." exit(-1) end |